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

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