PHPUnit
[ class tree: PHPUnit ] [ index: PHPUnit ] [ all elements ]

Source for file TestCase.php

Documentation is available at TestCase.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: TestCase.php,v 1.16 2004/12/22 08:06:11 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit/Assert.php';
  19. require_once 'PHPUnit/TestResult.php';
  20.  
  21. /**
  22.  * A TestCase defines the fixture to run multiple tests.
  23.  *
  24.  * To define a TestCase
  25.  *
  26.  *   1) Implement a subclass of PHPUnit_TestCase.
  27.  *   2) Define instance variables that store the state of the fixture.
  28.  *   3) Initialize the fixture state by overriding setUp().
  29.  *   4) Clean-up after a test by overriding tearDown().
  30.  *
  31.  * Each test runs in its own fixture so there can be no side effects
  32.  * among test runs.
  33.  *
  34.  * Here is an example:
  35.  *
  36.  * <code>
  37.  * <?php
  38.  * class MathTest extends PHPUnit_TestCase {
  39.  *     var $fValue1;
  40.  *     var $fValue2;
  41.  *
  42.  *     function MathTest($name) {
  43.  *         $this->PHPUnit_TestCase($name);
  44.  *     }
  45.  *
  46.  *     function setUp() {
  47.  *         $this->fValue1 = 2;
  48.  *         $this->fValue2 = 3;
  49.  *     }
  50.  * }
  51.  * ?>
  52.  * </code>
  53.  *
  54.  * For each test implement a method which interacts with the fixture.
  55.  * Verify the expected results with assertions specified by calling
  56.  * assert with a boolean.
  57.  *
  58.  * <code>
  59.  * <?php
  60.  * function testPass() {
  61.  *     $this->assertTrue($this->fValue1 + $this->fValue2 == 5);
  62.  * }
  63.  * ?>
  64.  * </code>
  65.  *
  66.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  67.  * @copyright   Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  68.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  69.  * @category    Testing
  70.  * @package     PHPUnit
  71.  */
  72. class PHPUnit_TestCase extends PHPUnit_Assert {
  73.     /**
  74.     * @var    boolean 
  75.     * @access private
  76.     */
  77.     var $_failed = FALSE;
  78.  
  79.     /**
  80.     * The name of the test case.
  81.     *
  82.     * @var    string 
  83.     * @access private
  84.     */
  85.     var $_name '';
  86.  
  87.     /**
  88.     * PHPUnit_TestResult object
  89.     *
  90.     * @var    object 
  91.     * @access private
  92.     */
  93.     var $_result;
  94.  
  95.     /**
  96.     * Constructs a test case with the given name.
  97.     *
  98.     * @param  string 
  99.     * @access public
  100.     */
  101.     function PHPUnit_TestCase($name = FALSE{
  102.         if ($name !== FALSE{
  103.             $this->setName($name);
  104.         }
  105.     }
  106.  
  107.     /**
  108.     * Counts the number of test cases executed by run(TestResult result).
  109.     *
  110.     * @return integer 
  111.     * @access public
  112.     */
  113.     function countTestCases({
  114.         return 1;
  115.     }
  116.  
  117.     /**
  118.     * Gets the name of a TestCase.
  119.     *
  120.     * @return string 
  121.     * @access public
  122.     */
  123.     function getName({
  124.         return $this->_name;
  125.     }
  126.  
  127.     /**
  128.     * Runs the test case and collects the results in a given TestResult object.
  129.     *
  130.     * @param  object 
  131.     * @return object 
  132.     * @access public
  133.     */
  134.     function run(&$result{
  135.         $this->_result &$result;
  136.         $this->_result->run($this);
  137.  
  138.         return $this->_result;
  139.     }
  140.  
  141.     /**
  142.     * Runs the bare test sequence.
  143.     *
  144.     * @access public
  145.     */
  146.     function runBare({
  147.         $this->setUp();
  148.         $this->runTest();
  149.         $this->tearDown();
  150.         $this->pass();
  151.     }
  152.  
  153.     /**
  154.     * Override to run the test and assert its state.
  155.     *
  156.     * @access protected
  157.     */
  158.     function runTest({
  159.         call_user_func(
  160.           array(
  161.             &$this,
  162.             $this->_name
  163.           )
  164.         );
  165.     }
  166.  
  167.     /**
  168.     * Sets the name of a TestCase.
  169.     *
  170.     * @param  string 
  171.     * @access public
  172.     */
  173.     function setName($name{
  174.         $this->_name $name;
  175.     }
  176.  
  177.     /**
  178.     * Returns a string representation of the test case.
  179.     *
  180.     * @return string 
  181.     * @access public
  182.     */
  183.     function toString({
  184.         return '';
  185.     }
  186.  
  187.     /**
  188.     * Creates a default TestResult object.
  189.     *
  190.     * @return object 
  191.     * @access protected
  192.     */
  193.     function &createResult({
  194.         return new PHPUnit_TestResult;
  195.     }
  196.  
  197.     /**
  198.     * Fails a test with the given message.
  199.     *
  200.     * @param  string 
  201.     * @access protected
  202.     */
  203.     function fail($message ''{
  204.         $this->_result->addFailure($this$message);
  205.         $this->_failed = TRUE;
  206.     }
  207.  
  208.     /**
  209.     * Passes a test.
  210.     *
  211.     * @access protected
  212.     */
  213.     function pass({
  214.         if (!$this->_failed{
  215.             $this->_result->addPassedTest($this);
  216.         }
  217.     }
  218.  
  219.     /**
  220.     * Sets up the fixture, for example, open a network connection.
  221.     * This method is called before a test is executed.
  222.     *
  223.     * @access protected
  224.     * @abstract
  225.     */
  226.     function setUp(/* abstract */ }
  227.  
  228.     /**
  229.     * Tears down the fixture, for example, close a network connection.
  230.     * This method is called after a test is executed.
  231.     *
  232.     * @access protected
  233.     * @abstract
  234.     */
  235.     function tearDown(/* abstract */ }
  236. }
  237. ?>

Documentation generated on Mon, 11 Mar 2019 14:22:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.