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

Source for file TestCase.php

Documentation is available at TestCase.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit2                                                       |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 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.7.2.3 2004/08/24 07:59:53 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit2/Framework/Assert.php';
  19. require_once 'PHPUnit2/Framework/Test.php';
  20. require_once 'PHPUnit2/Framework/TestResult.php';
  21. require_once 'PHPUnit2/Util/Filter.php';
  22.  
  23. /**
  24.  * A TestCase defines the fixture to run multiple tests.
  25.  *
  26.  * To define a TestCase
  27.  *
  28.  *   1) Implement a subclass of PHPUnit2_Framework_TestCase.
  29.  *   2) Define instance variables that store the state of the fixture.
  30.  *   3) Initialize the fixture state by overriding setUp().
  31.  *   4) Clean-up after a test by overriding tearDown().
  32.  *
  33.  * Each test runs in its own fixture so there can be no side effects
  34.  * among test runs.
  35.  *
  36.  * Here is an example:
  37.  *
  38.  * <code>
  39.  * <?php
  40.  * require_once 'PHPUnit2/Framework/TestCase.php';
  41.  *
  42.  * class MathTest extends PHPUnit2_Framework_TestCase {
  43.  *     public $value1;
  44.  *     public $value2;
  45.  *
  46.  *     public function __construct($name) {
  47.  *         parent::__construct($name);
  48.  *     }
  49.  *
  50.  *     public function setUp() {
  51.  *         $this->value1 = 2;
  52.  *         $this->value2 = 3;
  53.  *     }
  54.  * }
  55.  * ?>
  56.  * </code>
  57.  *
  58.  * For each test implement a method which interacts with the fixture.
  59.  * Verify the expected results with assertions specified by calling
  60.  * assert with a boolean.
  61.  *
  62.  * <code>
  63.  * <?php
  64.  * public function testPass() {
  65.  *     $this->assertTrue($this->value1 + $this->value2 == 5);
  66.  * }
  67.  * ?>
  68.  * </code>
  69.  *
  70.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  71.  * @copyright   Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  72.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  73.  * @category    PHP
  74.  * @package     PHPUnit2
  75.  * @subpackage  Framework
  76.  * @abstract
  77.  */
  78.     // {{{ Members
  79.  
  80.     /**
  81.     * The name of the test case.
  82.     *
  83.     * @var    string 
  84.     * @access private
  85.     */
  86.     private $name = null;
  87.  
  88.     // }}}
  89.     // {{{ public function __construct($name = false)
  90.  
  91.     /**
  92.     * Constructs a test case with the given name.
  93.     *
  94.     * @param  string 
  95.     * @access public
  96.     */
  97.     public function __construct($name = null{
  98.         if ($name != null{
  99.             $this->setName($name);
  100.         }
  101.     }
  102.  
  103.     // }}}
  104.     // {{{ public function toString()
  105.  
  106.     /**
  107.     * Returns a string representation of the test case.
  108.     *
  109.     * @return string 
  110.     * @access public
  111.     */
  112.     public function toString({
  113.         $class = new ReflectionClass($this);
  114.  
  115.         return sprintf(
  116.           '%s(%s)',
  117.  
  118.           $this->getName(),
  119.           $class->name
  120.         );
  121.     }
  122.  
  123.     // }}}
  124.     // {{{ public function countTestCases()
  125.  
  126.     /**
  127.     * Counts the number of test cases executed by run(TestResult result).
  128.     *
  129.     * @return integer 
  130.     * @access public
  131.     */
  132.     public function countTestCases({
  133.         return 1;
  134.     }
  135.  
  136.     // }}}
  137.     // {{{ public function getName()
  138.  
  139.     /**
  140.     * Gets the name of a TestCase.
  141.     *
  142.     * @return string 
  143.     * @access public
  144.     */
  145.     public function getName({
  146.         return $this->name;
  147.     }
  148.  
  149.     // }}}
  150.     // {{{ public function run(PHPUnit2_Framework_TestResult)
  151.  
  152.     /**
  153.     * Runs the test case and collects the results in a TestResult object.
  154.     * If no TestResult object is passed a new one will be created.
  155.     *
  156.     * @param  PHPUnit2_Framework_TestResult $result 
  157.     * @return PHPUnit2_Framework_TestResult 
  158.     * @access public
  159.     */
  160.     public function run($result = null{
  161.         if ($result === null{
  162.             $result $this->createResult();
  163.         }
  164.  
  165.         // XXX: Workaround for missing optional class type hints.
  166.         else if (!($result instanceof PHPUnit2_Framework_TestResult)) {
  167.             throw new Exception(
  168.               'Argument 1 must be an instance of PHPUnit2_Framework_TestResult.'
  169.             );
  170.         }
  171.  
  172.         $result->run($this);
  173.  
  174.         return $result;
  175.     }
  176.  
  177.     // }}}
  178.     // {{{ public function runBare()
  179.  
  180.     /**
  181.     * Runs the bare test sequence.
  182.     *
  183.     * @access public
  184.     */
  185.     public function runBare({
  186.         $catchedException = null;
  187.  
  188.         $this->setUp();
  189.  
  190.         try {
  191.             $this->runTest();
  192.         }
  193.  
  194.         catch (Exception $e{
  195.             $catchedException $e;
  196.         }
  197.  
  198.         $this->tearDown();
  199.  
  200.         // Workaround for missing "finally".
  201.         if ($catchedException !== null{
  202.             throw $catchedException;
  203.         }
  204.     }
  205.  
  206.     // }}}
  207.     // {{{ protected function runTest()
  208.  
  209.     /**
  210.     * Override to run the test and assert its state.
  211.     *
  212.     * @access protected
  213.     */
  214.     protected function runTest({
  215.         self::assertNotNull($this->name);
  216.  
  217.         try {
  218.             $class  = new ReflectionClass($this);
  219.             $method $class->getMethod($this->name);
  220.         }
  221.  
  222.         catch (ReflectionException $e{
  223.             $this->fail($e->getMessage());
  224.         }
  225.  
  226.         $method->invoke($this);
  227.     }
  228.  
  229.     // }}}
  230.     // {{{ public function setName($name)
  231.  
  232.     /**
  233.     * Sets the name of a TestCase.
  234.     *
  235.     * @param  string 
  236.     * @access public
  237.     */
  238.     public function setName($name{
  239.         $this->name $name;
  240.     }
  241.  
  242.     // }}}
  243.     // {{{ protected function createResult()
  244.  
  245.     /**
  246.     * Creates a default TestResult object.
  247.     *
  248.     * @return PHPUnit2_Framework_TestResult 
  249.     * @access protected
  250.     */
  251.     protected function createResult({
  252.         return new PHPUnit2_Framework_TestResult;
  253.     }
  254.  
  255.     // }}}
  256.     // {{{ protected function setUp()
  257.  
  258.     /**
  259.     * Sets up the fixture, for example, open a network connection.
  260.     * This method is called before a test is executed.
  261.     *
  262.     * @access protected
  263.     */
  264.     protected function setUp({}
  265.  
  266.     // }}}
  267.     // {{{ protected function tearDown()
  268.  
  269.     /**
  270.     * Tears down the fixture, for example, close a network connection.
  271.     * This method is called after a test is executed.
  272.     *
  273.     * @access protected
  274.     */
  275.     protected function tearDown({}
  276.  
  277.     // }}}
  278. }
  279.  
  280. /*
  281.  * vim600:  et sw=2 ts=2 fdm=marker
  282.  * vim<600: et sw=2 ts=2
  283.  */
  284. ?>

Documentation generated on Mon, 11 Mar 2019 13:55:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.