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

Source for file TestResult.php

Documentation is available at TestResult.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: TestResult.php,v 1.6.2.4 2004/11/02 15:40:17 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit2/Framework/AssertionFailedError.php';
  19. require_once 'PHPUnit2/Framework/IncompleteTest.php';
  20. require_once 'PHPUnit2/Framework/Test.php';
  21. require_once 'PHPUnit2/Framework/TestFailure.php';
  22. require_once 'PHPUnit2/Framework/TestListener.php';
  23.  
  24. /**
  25.  * A TestResult collects the results of executing a test case.
  26.  *
  27.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  28.  * @copyright   Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  29.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  30.  * @category    Testing
  31.  * @package     PHPUnit2
  32.  * @subpackage  Framework
  33.  */
  34.     // {{{ Members
  35.  
  36.     /**
  37.     * @var    array 
  38.     * @access protected
  39.     */
  40.     protected $errors = array();
  41.  
  42.     /**
  43.     * @var    array 
  44.     * @access protected
  45.     */
  46.     protected $failures = array();
  47.  
  48.     /**
  49.     * @var    array 
  50.     * @access protected
  51.     */
  52.     protected $notImplemented = array();
  53.  
  54.     /**
  55.     * @var    array 
  56.     * @access protected
  57.     */
  58.     protected $listeners = array();
  59.  
  60.     /**
  61.     * @var    integer 
  62.     * @access protected
  63.     */
  64.     protected $runTests = 0;
  65.  
  66.     /**
  67.     * @var    boolean 
  68.     * @access private
  69.     */
  70.     private $stop = FALSE;
  71.  
  72.     /**
  73.     * Code Coverage information provided by Xdebug.
  74.     *
  75.     * @var    array 
  76.     * @access private
  77.     */
  78.     private $codeCoverageInformation = array();
  79.  
  80.     // }}}
  81.     // {{{ public function addError(PHPUnit2_Framework_Test $test, Exception $e)
  82.  
  83.     /**
  84.     * Adds an error to the list of errors.
  85.     * The passed in exception caused the error.
  86.     *
  87.     * @param  PHPUnit2_Framework_Test $test 
  88.     * @param  Exception               $e 
  89.     * @access public
  90.     */
  91.     public function addError(PHPUnit2_Framework_Test $testException $e{
  92.         if ($e instanceof PHPUnit2_Framework_IncompleteTest{
  93.             $this->notImplemented[= new PHPUnit2_Framework_TestFailure($test$e);
  94.  
  95.             foreach ($this->listeners as $listener{
  96.                 $listener->addIncompleteTest($test$e);
  97.             }
  98.         else {
  99.             $this->errors[= new PHPUnit2_Framework_TestFailure($test$e);
  100.  
  101.             foreach ($this->listeners as $listener{
  102.                 $listener->addError($test$e);
  103.             }
  104.         }
  105.     }
  106.  
  107.     // }}}
  108.     // {{{ public function addFailure(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e)
  109.  
  110.     /**
  111.     * Adds a failure to the list of failures.
  112.     * The passed in exception caused the failure.
  113.     *
  114.     * @param  PHPUnit2_Framework_Test                  $test 
  115.     * @param  PHPUnit2_Framework_AssertionFailedError  $e 
  116.     * @access public
  117.     */
  118.     public function addFailure(PHPUnit2_Framework_Test $testPHPUnit2_Framework_AssertionFailedError $e{
  119.         if ($e instanceof PHPUnit2_Framework_IncompleteTest{
  120.             $this->notImplemented[= new PHPUnit2_Framework_TestFailure($test$e);
  121.  
  122.             foreach ($this->listeners as $listener{
  123.                 $listener->addIncompleteTest($test$e);
  124.             }
  125.         else {
  126.             $this->failures[= new PHPUnit2_Framework_TestFailure($test$e);
  127.  
  128.             foreach ($this->listeners as $listener{
  129.                 $listener->addFailure($test$e);
  130.             }
  131.         }
  132.     }
  133.  
  134.     // }}}
  135.     // {{{ public function addListener(PHPUnit2_Framework_TestListener $listener)
  136.  
  137.     /**
  138.     * Registers a TestListener.
  139.     *
  140.     * @param  PHPUnit2_Framework_TestListener 
  141.     * @access public
  142.     */
  143.     public function addListener(PHPUnit2_Framework_TestListener $listener{
  144.         $this->listeners[$listener;
  145.     }
  146.  
  147.     // }}}
  148.     // {{{ public function endTest(PHPUnit2_Framework_Test $test)
  149.  
  150.     /**
  151.     * Informs the result that a test was completed.
  152.     *
  153.     * @param  PHPUnit2_Framework_Test 
  154.     * @access public
  155.     */
  156.     public function endTest(PHPUnit2_Framework_Test $test{
  157.         if ($test instanceof PHPUnit2_Framework_TestCase{
  158.             $this->codeCoverageInformation[$test->getName()$test->getCodeCoverageInformation();
  159.         }
  160.  
  161.         foreach ($this->listeners as $listener{
  162.             $listener->endTest($test);
  163.         }
  164.     }
  165.  
  166.     // }}}
  167.     // {{{ public function allCompletlyImplemented()
  168.  
  169.     /**
  170.     * Returns TRUE if no incomplete test occured.
  171.     *
  172.     * @return boolean 
  173.     * @access public
  174.     */
  175.     public function allCompletlyImplemented({
  176.         return $this->notImplementedCount(== 0;
  177.     }
  178.  
  179.     // }}}
  180.     // {{{ public function notImplementedCount()
  181.  
  182.     /**
  183.     * Gets the number of incomplete tests.
  184.     *
  185.     * @return integer 
  186.     * @access public
  187.     */
  188.     public function notImplementedCount({
  189.         return sizeof($this->notImplemented);
  190.     }
  191.  
  192.     // }}}
  193.     // {{{ public function notImplemented)
  194.  
  195.     /**
  196.     * Returns an Enumeration for the incomplete tests.
  197.     *
  198.     * @return array 
  199.     * @access public
  200.     */
  201.     public function notImplemented({
  202.         return $this->notImplemented;
  203.     }
  204.  
  205.     // }}}
  206.     // {{{ public function errorCount()
  207.  
  208.     /**
  209.     * Gets the number of detected errors.
  210.     *
  211.     * @return integer 
  212.     * @access public
  213.     */
  214.     public function errorCount({
  215.         return sizeof($this->errors);
  216.     }
  217.  
  218.     // }}}
  219.     // {{{ public function errors()
  220.  
  221.     /**
  222.     * Returns an Enumeration for the errors.
  223.     *
  224.     * @return array 
  225.     * @access public
  226.     */
  227.     public function errors({
  228.         return $this->errors;
  229.     }
  230.  
  231.     // }}}
  232.     // {{{ public function failureCount()
  233.  
  234.     /**
  235.     * Gets the number of detected failures.
  236.     *
  237.     * @return integer 
  238.     * @access public
  239.     */
  240.     public function failureCount({
  241.         return sizeof($this->failures);
  242.     }
  243.  
  244.     // }}}
  245.     // {{{ public function failures()
  246.  
  247.     /**
  248.     * Returns an Enumeration for the failures.
  249.     *
  250.     * @return array 
  251.     * @access public
  252.     */
  253.     public function failures({
  254.         return $this->failures;
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ public function getCodeCoverageInformation()
  259.  
  260.     /**
  261.     * Returns Code Coverage data per test case.
  262.     *
  263.     * Format of the result array:
  264.     *
  265.     * <code>
  266.     * array(
  267.     *   "testCase" => array(
  268.     *     "/tested/code.php" => array(
  269.     *       linenumber => numberOfExecutions
  270.     *     )
  271.     *   )
  272.     * )
  273.     * </code>
  274.     *
  275.     * @return array 
  276.     * @access public
  277.     */
  278.     public function getCodeCoverageInformation({
  279.         return $this->codeCoverageInformation;
  280.     }
  281.  
  282.     // }}}
  283.     // {{{ public function removeListener(PHPUnit2_Framework_TestListener $listener)
  284.  
  285.     /**
  286.     * Unregisters a TestListener.
  287.     *
  288.     * @param  PHPUnit2_Framework_TestListener $listener 
  289.     * @access public
  290.     */
  291.     public function removeListener(PHPUnit2_Framework_TestListener $listener{
  292.         for ($i = 0; $i sizeof($this->listeners)$i++{
  293.             if ($this->listeners[$i=== $listener{
  294.                 unset($this->listeners[$i]);
  295.             }
  296.         }
  297.     }
  298.  
  299.     // }}}
  300.     // {{{ public function run(PHPUnit2_Framework_Test $test)
  301.  
  302.     /**
  303.     * Runs a TestCase.
  304.     *
  305.     * @param  PHPUnit2_Framework_Test $test 
  306.     * @access public
  307.     */
  308.     public function run(PHPUnit2_Framework_Test $test{
  309.         $this->startTest($test);
  310.  
  311.         try {
  312.             $test->runBare();
  313.         }
  314.  
  315.         catch (PHPUnit2_Framework_AssertionFailedError $e{
  316.             $this->addFailure($test$e);
  317.         }
  318.  
  319.         catch (Exception $e{
  320.             $this->addError($test$e);
  321.         }
  322.  
  323.         $this->endTest($test);
  324.     }
  325.  
  326.     // }}}
  327.     // {{{ public function runCount()
  328.  
  329.     /**
  330.     * Gets the number of run tests.
  331.     *
  332.     * @return integer 
  333.     * @access public
  334.     */
  335.     public function runCount({
  336.         return $this->runTests;
  337.     }
  338.  
  339.     // }}}
  340.     // {{{ public function shouldStop()
  341.  
  342.     /**
  343.     * Checks whether the test run should stop.
  344.     *
  345.     * @return boolean 
  346.     * @access public
  347.     */
  348.     public function shouldStop({
  349.         return $this->stop;
  350.     }
  351.  
  352.     // }}}
  353.     // {{{ public function startTest(PHPUnit2_Framework_Test $test)
  354.  
  355.     /**
  356.     * Informs the result that a test will be started.
  357.     *
  358.     * @param  PHPUnit2_Framework_Test $test 
  359.     * @access public
  360.     */
  361.     public function startTest(PHPUnit2_Framework_Test $test{
  362.         $this->runTests += $test->countTestCases();
  363.  
  364.         foreach ($this->listeners as $listener{
  365.             $listener->startTest($test);
  366.         }
  367.     }
  368.  
  369.     // }}}
  370.     // {{{ public function stop()
  371.  
  372.     /**
  373.     * Marks that the test run should stop.
  374.     *
  375.     * @access public
  376.     */
  377.     public function stop({
  378.         $this->stop = TRUE;
  379.     }
  380.  
  381.     // }}}
  382.     // {{{ public function wasSuccessful()
  383.  
  384.     /**
  385.     * Returns whether the entire test was successful or not.
  386.     *
  387.     * @return boolean 
  388.     * @access public
  389.     */
  390.     public function wasSuccessful({
  391.         if (empty($this->errors&& empty($this->failures)) {
  392.             return TRUE;
  393.         else {
  394.             return FALSE;
  395.         }
  396.     }
  397.  
  398.     // }}}
  399. }
  400.  
  401. /*
  402.  * vim600:  et sw=2 ts=2 fdm=marker
  403.  * vim<600: et sw=2 ts=2
  404.  */
  405. ?>

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