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

Source for file TestResult.php

Documentation is available at TestResult.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  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.12 2004/12/22 08:06:11 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit/TestFailure.php';
  19. require_once 'PHPUnit/TestListener.php';
  20.  
  21. /**
  22.  * A TestResult collects the results of executing a test case.
  23.  *
  24.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  25.  * @copyright   Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  26.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  27.  * @category    Testing
  28.  * @package     PHPUnit
  29.  */
  30.     /**
  31.     * @var    array 
  32.     * @access protected
  33.     */
  34.     var $_errors = array();
  35.  
  36.     /**
  37.     * @var    array 
  38.     * @access protected
  39.     */
  40.     var $_failures = array();
  41.  
  42.     /**
  43.     * @var    array 
  44.     * @access protected
  45.     */
  46.     var $_listeners = array();
  47.  
  48.     /**
  49.     * @var    array 
  50.     * @access protected
  51.     */
  52.     var $_passedTests = array();
  53.  
  54.     /**
  55.     * @var    integer 
  56.     * @access protected
  57.     */
  58.     var $_runTests = 0;
  59.  
  60.     /**
  61.     * @var    boolean 
  62.     * @access private
  63.     */
  64.     var $_stop = FALSE;
  65.  
  66.     /**
  67.     * Adds an error to the list of errors.
  68.     * The passed in exception caused the error.
  69.     *
  70.     * @param  object 
  71.     * @param  object 
  72.     * @access public
  73.     */
  74.     function addError(&$test&$t{
  75.         $this->_errors[= new PHPUnit_TestFailure($test$t);
  76.  
  77.         for ($i = 0; $i sizeof($this->_listeners)$i++{
  78.             $this->_listeners[$i]->addError($test$t);
  79.         }
  80.     }
  81.  
  82.     /**
  83.     * Adds a failure to the list of failures.
  84.     * The passed in exception caused the failure.
  85.     *
  86.     * @param  object 
  87.     * @param  object 
  88.     * @access public
  89.     */
  90.     function addFailure(&$test&$t{
  91.         $this->_failures[= new PHPUnit_TestFailure($test$t);
  92.  
  93.         for ($i = 0; $i sizeof($this->_listeners)$i++{
  94.             $this->_listeners[$i]->addFailure($test$t);
  95.         }
  96.     }
  97.  
  98.     /**
  99.     * Registers a TestListener.
  100.     *
  101.     * @param  object 
  102.     * @access public
  103.     */
  104.     function addListener(&$listener{
  105.         if (is_object($listener&&
  106.             is_a($listener'PHPUnit_TestListener')) {
  107.             $this->_listeners[$listener;
  108.         }
  109.     }
  110.  
  111.     /**
  112.     * Adds a passed test to the list of passed tests.
  113.     *
  114.     * @param  object 
  115.     * @access public
  116.     */
  117.     function addPassedTest(&$test{
  118.         $this->_passedTests[$test;
  119.     }
  120.  
  121.     /**
  122.     * Informs the result that a test was completed.
  123.     *
  124.     * @param  object 
  125.     * @access public
  126.     */
  127.     function endTest(&$test{
  128.         for ($i = 0; $i sizeof($this->_listeners)$i++{
  129.             $this->_listeners[$i]->endTest($test);
  130.         }
  131.     }
  132.  
  133.     /**
  134.     * Gets the number of detected errors.
  135.     *
  136.     * @return integer 
  137.     * @access public
  138.     */
  139.     function errorCount({
  140.         return sizeof($this->_errors);
  141.     }
  142.  
  143.     /**
  144.     * Returns an Enumeration for the errors.
  145.     *
  146.     * @return array 
  147.     * @access public
  148.     */
  149.     function &errors({
  150.         return $this->_errors;
  151.     }
  152.  
  153.     /**
  154.     * Gets the number of detected failures.
  155.     *
  156.     * @return integer 
  157.     * @access public
  158.     */
  159.     function failureCount({
  160.         return sizeof($this->_failures);
  161.     }
  162.  
  163.     /**
  164.     * Returns an Enumeration for the failures.
  165.     *
  166.     * @return array 
  167.     * @access public
  168.     */
  169.     function &failures({
  170.         return $this->_failures;
  171.     }
  172.  
  173.     /**
  174.     * Returns an Enumeration for the passed tests.
  175.     *
  176.     * @return array 
  177.     * @access public
  178.     */
  179.     function &passedTests({
  180.         return $this->_passedTests;
  181.     }
  182.  
  183.     /**
  184.     * Unregisters a TestListener.
  185.     * This requires the Zend Engine 2 (to work properly).
  186.     *
  187.     * @param  object 
  188.     * @access public
  189.     */
  190.     function removeListener(&$listener{
  191.         for ($i = 0; $i sizeof($this->_listeners)$i++{
  192.             if ($this->_listeners[$i=== $listener{
  193.                 unset($this->_listeners[$i]);
  194.             }
  195.         }
  196.     }
  197.  
  198.     /**
  199.     * Runs a TestCase.
  200.     *
  201.     * @param  object 
  202.     * @access public
  203.     */
  204.     function run(&$test{
  205.         $this->startTest($test);
  206.         $this->_runTests++;
  207.         $test->runBare();
  208.         $this->endTest($test);
  209.     }
  210.  
  211.     /**
  212.     * Gets the number of run tests.
  213.     *
  214.     * @return integer 
  215.     * @access public
  216.     */
  217.     function runCount({
  218.         return $this->_runTests;
  219.     }
  220.  
  221.     /**
  222.     * Checks whether the test run should stop.
  223.     *
  224.     * @access public
  225.     */
  226.     function shouldStop({
  227.         return $this->_stop;
  228.     }
  229.  
  230.     /**
  231.     * Informs the result that a test will be started.
  232.     *
  233.     * @param  object 
  234.     * @access public
  235.     */
  236.     function startTest(&$test{
  237.         for ($i = 0; $i sizeof($this->_listeners)$i++{
  238.             $this->_listeners[$i]->startTest($test);
  239.         }
  240.     }
  241.  
  242.     /**
  243.     * Marks that the test run should stop.
  244.     *
  245.     * @access public
  246.     */
  247.     function stop({
  248.         $this->_stop = TRUE;
  249.     }
  250.  
  251.     /**
  252.     * Returns a HTML representation of the test result.
  253.     *
  254.     * @return string 
  255.     * @access public
  256.     */
  257.     function toHTML({
  258.         return '<pre>' htmlspecialchars($this->toString()) '</pre>';
  259.     }
  260.  
  261.     /**
  262.     * Returns a text representation of the test result.
  263.     *
  264.     * @return string 
  265.     * @access public
  266.     */
  267.     function toString({
  268.         $result '';
  269.  
  270.         foreach ($this->_passedTests as $passedTest{
  271.             $result .= sprintf(
  272.               "TestCase %s->%s() passed\n",
  273.  
  274.               get_class($passedTest),
  275.               $passedTest->getName()
  276.             );
  277.         }
  278.  
  279.         foreach ($this->_failures as $failedTest{
  280.             $result .= $failedTest->toString();
  281.         }
  282.  
  283.         return $result;
  284.     }
  285.     /**
  286.     * Returns whether the entire test was successful or not.
  287.     *
  288.     * @return boolean 
  289.     * @access public
  290.     */
  291.     function wasSuccessful({
  292.         if (empty($this->_errors&& empty($this->_failures)) {
  293.             return TRUE;
  294.         else {
  295.             return FALSE;
  296.         }
  297.     }
  298. }
  299. ?>

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