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.14.2.2 2004/10/24 19:50:25 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    Testing
  74.  * @package     PHPUnit2
  75.  * @subpackage  Framework
  76.  * @abstract
  77.  */
  78.     // {{{ Members
  79.  
  80.     /**
  81.     * Code Coverage information provided by Xdebug.
  82.     *
  83.     * @var    array 
  84.     * @access private
  85.     */
  86.     private $codeCoverageInformation = array();
  87.  
  88.     /**
  89.     * The name of the test case.
  90.     *
  91.     * @var    string 
  92.     * @access private
  93.     */
  94.     private $name = NULL;
  95.  
  96.     // }}}
  97.     // {{{ public function __construct($name = NULL)
  98.  
  99.     /**
  100.     * Constructs a test case with the given name.
  101.     *
  102.     * @param  string 
  103.     * @access public
  104.     */
  105.     public function __construct($name = NULL{
  106.         if ($name !== NULL{
  107.             $this->setName($name);
  108.         }
  109.     }
  110.  
  111.     // }}}
  112.     // {{{ public function toString()
  113.  
  114.     /**
  115.     * Returns a string representation of the test case.
  116.     *
  117.     * @return string 
  118.     * @access public
  119.     */
  120.     public function toString({
  121.         $class = new ReflectionClass($this);
  122.  
  123.         return sprintf(
  124.           '%s(%s)',
  125.  
  126.           $this->getName(),
  127.           $class->name
  128.         );
  129.     }
  130.  
  131.     // }}}
  132.     // {{{ public function countTestCases()
  133.  
  134.     /**
  135.     * Counts the number of test cases executed by run(TestResult result).
  136.     *
  137.     * @return integer 
  138.     * @access public
  139.     */
  140.     public function countTestCases({
  141.         return 1;
  142.     }
  143.  
  144.     // }}}
  145.     // {{{ public function getCodeCoverageInformation()
  146.  
  147.     /**
  148.     * Returns the Code Coverage information provided by Xdebug.
  149.     *
  150.     * @return array 
  151.     * @access public
  152.     */
  153.     public function getCodeCoverageInformation({
  154.         return $this->codeCoverageInformation;
  155.     }
  156.  
  157.     // }}}
  158.     // {{{ public function getName()
  159.  
  160.     /**
  161.     * Gets the name of a TestCase.
  162.     *
  163.     * @return string 
  164.     * @access public
  165.     */
  166.     public function getName({
  167.         return $this->name;
  168.     }
  169.  
  170.     // }}}
  171.     // {{{ public function run(PHPUnit2_Framework_TestResult)
  172.  
  173.     /**
  174.     * Runs the test case and collects the results in a TestResult object.
  175.     * If no TestResult object is passed a new one will be created.
  176.     *
  177.     * @param  PHPUnit2_Framework_TestResult $result 
  178.     * @return PHPUnit2_Framework_TestResult 
  179.     * @access public
  180.     */
  181.     public function run($result = NULL{
  182.         if ($result === NULL{
  183.             $result $this->createResult();
  184.         }
  185.  
  186.         // XXX: Workaround for missing ability to declare type-hinted parameters as optional.
  187.         else if (!($result instanceof PHPUnit2_Framework_TestResult)) {
  188.             throw new Exception(
  189.               'Argument 1 must be an instance of PHPUnit2_Framework_TestResult.'
  190.             );
  191.         }
  192.  
  193.         $result->run($this);
  194.  
  195.         return $result;
  196.     }
  197.  
  198.     // }}}
  199.     // {{{ public function runBare()
  200.  
  201.     /**
  202.     * Runs the bare test sequence.
  203.     *
  204.     * @access public
  205.     */
  206.     public function runBare({
  207.         $catchedException = NULL;
  208.  
  209.         $this->setUp();
  210.  
  211.         try {
  212.             $this->runTest();
  213.         }
  214.  
  215.         catch (Exception $e{
  216.             $catchedException $e;
  217.         }
  218.  
  219.         $this->tearDown();
  220.  
  221.         // Workaround for missing "finally".
  222.         if ($catchedException !== NULL{
  223.             throw $catchedException;
  224.         }
  225.     }
  226.  
  227.     // }}}
  228.     // {{{ protected function runTest()
  229.  
  230.     /**
  231.     * Override to run the test and assert its state.
  232.     *
  233.     * @access protected
  234.     */
  235.     protected function runTest({
  236.         self::assertNotNull($this->name);
  237.  
  238.         try {
  239.             $class  = new ReflectionClass($this);
  240.             $method $class->getMethod($this->name);
  241.         }
  242.  
  243.         catch (ReflectionException $e{
  244.             $this->fail($e->getMessage());
  245.         }
  246.  
  247.         PHPUnit2_Util_Filter::addFileToFilter($class->getFileName());
  248.  
  249.         if (extension_loaded('xdebug')) {
  250.             xdebug_start_code_coverage();
  251.         }
  252.  
  253.         $method->invoke($this);
  254.  
  255.         if (extension_loaded('xdebug')) {
  256.             $this->codeCoverageInformation PHPUnit2_Util_Filter::getFilteredCodeCoverage(
  257.               xdebug_get_code_coverage()
  258.             );
  259.  
  260.             xdebug_stop_code_coverage();
  261.         }
  262.     }
  263.  
  264.     // }}}
  265.     // {{{ public function setName($name)
  266.  
  267.     /**
  268.     * Sets the name of a TestCase.
  269.     *
  270.     * @param  string 
  271.     * @access public
  272.     */
  273.     public function setName($name{
  274.         $this->name $name;
  275.     }
  276.  
  277.     // }}}
  278.     // {{{ protected function createResult()
  279.  
  280.     /**
  281.     * Creates a default TestResult object.
  282.     *
  283.     * @return PHPUnit2_Framework_TestResult 
  284.     * @access protected
  285.     */
  286.     protected function createResult({
  287.         return new PHPUnit2_Framework_TestResult;
  288.     }
  289.  
  290.     // }}}
  291.     // {{{ protected function setUp()
  292.  
  293.     /**
  294.     * Sets up the fixture, for example, open a network connection.
  295.     * This method is called before a test is executed.
  296.     *
  297.     * @access protected
  298.     */
  299.     protected function setUp({}
  300.  
  301.     // }}}
  302.     // {{{ protected function tearDown()
  303.  
  304.     /**
  305.     * Tears down the fixture, for example, close a network connection.
  306.     * This method is called after a test is executed.
  307.     *
  308.     * @access protected
  309.     */
  310.     protected function tearDown({}
  311.  
  312.     // }}}
  313. }
  314.  
  315. /*
  316.  * vim600:  et sw=2 ts=2 fdm=marker
  317.  * vim<600: et sw=2 ts=2
  318.  */
  319. ?>

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