3 // +------------------------------------------------------------------------+
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 // +------------------------------------------------------------------------+
15 // $Id: TestResult.php,v 1.11 2004/10/01 06:11:39 sebastian Exp $
18 require_once 'PHPUnit/TestFailure.php';
19 require_once 'PHPUnit/TestListener.php';
22 * A TestResult collects the results of executing a test case.
24 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
25 * @copyright Copyright © 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
26 * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
30 class PHPUnit_TestResult
{
35 var $_errors = array();
41 var $_failures = array();
47 var $_listeners = array();
53 var $_passedTests = array();
68 * Adds an error to the list of errors.
69 * The passed in exception caused the error.
75 function addError(&$test, &$t) {
76 $this->_errors
[] = new PHPUnit_TestFailure($test, $t);
78 for ($i = 0; $i < sizeof($this->_listeners
); $i++
) {
79 $this->_listeners
[$i]->addError($test, $t);
84 * Adds a failure to the list of failures.
85 * The passed in exception caused the failure.
91 function addFailure(&$test, &$t) {
92 $this->_failures
[] = new PHPUnit_TestFailure($test, $t);
94 for ($i = 0; $i < sizeof($this->_listeners
); $i++
) {
95 $this->_listeners
[$i]->addFailure($test, $t);
100 * Registers a TestListener.
105 function addListener(&$listener) {
106 if (is_object($listener) &&
107 is_a($listener, 'PHPUnit_TestListener')) {
108 $this->_listeners
[] = $listener;
113 * Adds a passed test to the list of passed tests.
118 function addPassedTest(&$test) {
119 $this->_passedTests
[] = $test;
123 * Informs the result that a test was completed.
128 function endTest(&$test) {
129 for ($i = 0; $i < sizeof($this->_listeners
); $i++
) {
130 $this->_listeners
[$i]->endTest($test);
135 * Gets the number of detected errors.
140 function errorCount() {
141 return sizeof($this->_errors
);
145 * Returns an Enumeration for the errors.
151 return $this->_errors
;
155 * Gets the number of detected failures.
160 function failureCount() {
161 return sizeof($this->_failures
);
165 * Returns an Enumeration for the failures.
170 function &failures() {
171 return $this->_failures
;
175 * Returns an Enumeration for the passed tests.
180 function &passedTests() {
181 return $this->_passedTests
;
185 * Unregisters a TestListener.
186 * This requires the Zend Engine 2 (to work properly).
191 function removeListener(&$listener) {
192 for ($i = 0; $i < sizeof($this->_listeners
); $i++
) {
193 if ($this->_listeners
[$i] === $listener) {
194 unset($this->_listeners
[$i]);
205 function run(&$test) {
206 $this->startTest($test);
209 $this->endTest($test);
213 * Gets the number of run tests.
218 function runCount() {
219 return $this->_runTests
;
223 * Checks whether the test run should stop.
227 function shouldStop() {
232 * Informs the result that a test will be started.
237 function startTest(&$test) {
238 for ($i = 0; $i < sizeof($this->_listeners
); $i++
) {
239 $this->_listeners
[$i]->startTest($test);
244 * Marks that the test run should stop.
253 * Returns a HTML representation of the test result.
259 return '<pre>' . htmlspecialchars($this->toString()) . '</pre>';
263 * Returns a text representation of the test result.
268 function toString() {
271 foreach ($this->_passedTests
as $passedTest) {
273 "TestCase %s->%s() passed\n",
275 get_class($passedTest),
276 $passedTest->getName()
280 foreach ($this->_failures
as $failedTest) {
281 $result .= $failedTest->toString();
287 * Returns whether the entire test was successful or not.
292 function wasSuccessful() {
293 if (empty($this->_errors
) && empty($this->_failures
)) {