home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / TestResult.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  6.6 KB  |  299 lines

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