home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / TestCase.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.2 KB  |  228 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 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.9 2003/04/24 05:43:42 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.  *   class MathTest extends PHPUnit_TestCase {
  37.  *     var $fValue1;
  38.  *     var $fValue2;
  39.  *
  40.  *     function MathTest($name) {
  41.  *       $this->PHPUnit_TestCase($name);
  42.  *     }
  43.  *    
  44.  *     function setUp() {
  45.  *       $this->fValue1 = 2;
  46.  *       $this->fValue2 = 3;
  47.  *     }
  48.  *   }
  49.  *
  50.  * For each test implement a method which interacts with the fixture.
  51.  * Verify the expected results with assertions specified by calling
  52.  * assert with a boolean. 
  53.  *
  54.  *   function testPass() {
  55.  *     $this->assertTrue($this->fValue1 + $this->fValue2 == 5);
  56.  *   }
  57.  *
  58.  * @package PHPUnit
  59.  * @author  Sebastian Bergmann <sb@sebastian-bergmann.de>
  60.  *          Based upon JUnit, see http://www.junit.org/ for details.
  61.  */
  62. class PHPUnit_TestCase extends PHPUnit_Assert {
  63.     /**
  64.     * @var    boolean
  65.     * @access private
  66.     */
  67.     var $_failed = false;
  68.  
  69.     /**
  70.     * The name of the test case.
  71.     *
  72.     * @var    string
  73.     * @access private
  74.     */
  75.     var $_name = '';
  76.  
  77.     /**
  78.     * PHPUnit_TestResult object
  79.     *
  80.     * @var    object
  81.     * @access private
  82.     */
  83.     var $_result;
  84.  
  85.     /**
  86.     * Constructs a test case with the given name.
  87.     *
  88.     * @param  string  
  89.     * @access public
  90.     */
  91.     function PHPUnit_TestCase($name = false) {
  92.         if ($name != false) {
  93.             $this->setName($name);
  94.         }
  95.     }
  96.  
  97.     /**
  98.     * Counts the number of test cases executed by run(TestResult result).
  99.     *
  100.     * @return integer
  101.     * @access public
  102.     */
  103.     function countTestCases() {
  104.         return 1;
  105.     }
  106.  
  107.     /**
  108.     * Gets the name of a TestCase.
  109.     *
  110.     * @return string
  111.     * @access public
  112.     */
  113.     function getName() {
  114.         return $this->_name;
  115.     }
  116.  
  117.     /**
  118.     * Runs the test case and collects the results in a given TestResult object.
  119.     *
  120.     * @param  object
  121.     * @return object
  122.     * @access public
  123.     */
  124.     function run(&$result) {
  125.         $this->_result = &$result;
  126.         $this->_result->run($this);
  127.  
  128.         return $this->_result;
  129.     }
  130.  
  131.     /**
  132.     * Runs the bare test sequence.
  133.     *
  134.     * @access public
  135.     */
  136.     function runBare() {
  137.         $this->setUp();
  138.         $this->runTest();
  139.         $this->tearDown();
  140.         $this->pass();
  141.     }
  142.  
  143.     /**
  144.     * Override to run the test and assert its state.
  145.     *
  146.     * @access protected
  147.     */
  148.     function runTest() {
  149.         call_user_func(
  150.           array(
  151.             &$this,
  152.             $this->_name
  153.           )
  154.         );
  155.     }
  156.  
  157.     /**
  158.     * Sets the name of a TestCase.
  159.     *
  160.     * @param  string
  161.     * @access public
  162.     */
  163.     function setName($name) {
  164.         $this->_name = $name;
  165.     }
  166.  
  167.     /**
  168.     * Returns a string representation of the test case.
  169.     *
  170.     * @return string
  171.     * @access public
  172.     */
  173.     function toString() {
  174.         return '';
  175.     }
  176.  
  177.     /**
  178.     * Creates a default TestResult object.
  179.     *
  180.     * @return object
  181.     * @access protected
  182.     */
  183.     function &createResult() {
  184.         return new PHPUnit_TestResult;
  185.     }
  186.  
  187.     /**
  188.     * Fails a test with the given message.
  189.     *
  190.     * @param  string
  191.     * @access protected
  192.     */
  193.     function fail($message = '') {
  194.         $this->_result->addFailure($this, $message);
  195.         $this->_failed = true;
  196.     }
  197.  
  198.     /**
  199.     * Passes a test.
  200.     *
  201.     * @access protected
  202.     */
  203.     function pass() {
  204.         if (!$this->_failed) {
  205.             $this->_result->addPassedTest($this);
  206.         }
  207.     }
  208.  
  209.     /**
  210.     * Sets up the fixture, for example, open a network connection.
  211.     * This method is called before a test is executed.
  212.     *
  213.     * @access protected
  214.     * @abstract
  215.     */
  216.     function setUp() { /* abstract */ }
  217.  
  218.     /**
  219.     * Tears down the fixture, for example, close a network connection.
  220.     * This method is called after a test is executed.
  221.     *
  222.     * @access protected
  223.     * @abstract
  224.     */
  225.     function tearDown() { /* abstract */ }
  226. }
  227. ?>
  228.