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

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