premier commit
[bazdig.git] / test / PHPUnit / TestResult.php
blob0be4ea5df0528372e5b3499947ca9ce572f97788
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 // +------------------------------------------------------------------------+
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';
21 /**
22 * A TestResult collects the results of executing a test case.
24 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
25 * @copyright Copyright &copy; 2002-2004 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
30 class PHPUnit_TestResult {
31 /**
32 * @var array
33 * @access protected
35 var $_errors = array();
37 /**
38 * @var array
39 * @access protected
41 var $_failures = array();
43 /**
44 * @var array
45 * @access protected
47 var $_listeners = array();
49 /**
50 * @var array
51 * @access protected
53 var $_passedTests = array();
55 /**
56 * @var integer
57 * @access protected
59 var $_runTests = 0;
61 /**
62 * @var boolean
63 * @access private
65 var $_stop = FALSE;
67 /**
68 * Adds an error to the list of errors.
69 * The passed in exception caused the error.
71 * @param object
72 * @param object
73 * @access public
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);
83 /**
84 * Adds a failure to the list of failures.
85 * The passed in exception caused the failure.
87 * @param object
88 * @param object
89 * @access public
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);
99 /**
100 * Registers a TestListener.
102 * @param object
103 * @access public
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.
115 * @param object
116 * @access public
118 function addPassedTest(&$test) {
119 $this->_passedTests[] = $test;
123 * Informs the result that a test was completed.
125 * @param object
126 * @access public
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.
137 * @return integer
138 * @access public
140 function errorCount() {
141 return sizeof($this->_errors);
145 * Returns an Enumeration for the errors.
147 * @return array
148 * @access public
150 function &errors() {
151 return $this->_errors;
155 * Gets the number of detected failures.
157 * @return integer
158 * @access public
160 function failureCount() {
161 return sizeof($this->_failures);
165 * Returns an Enumeration for the failures.
167 * @return array
168 * @access public
170 function &failures() {
171 return $this->_failures;
175 * Returns an Enumeration for the passed tests.
177 * @return array
178 * @access public
180 function &passedTests() {
181 return $this->_passedTests;
185 * Unregisters a TestListener.
186 * This requires the Zend Engine 2 (to work properly).
188 * @param object
189 * @access public
191 function removeListener(&$listener) {
192 for ($i = 0; $i < sizeof($this->_listeners); $i++) {
193 if ($this->_listeners[$i] === $listener) {
194 unset($this->_listeners[$i]);
200 * Runs a TestCase.
202 * @param object
203 * @access public
205 function run(&$test) {
206 $this->startTest($test);
207 $this->_runTests++;
208 $test->runBare();
209 $this->endTest($test);
213 * Gets the number of run tests.
215 * @return integer
216 * @access public
218 function runCount() {
219 return $this->_runTests;
223 * Checks whether the test run should stop.
225 * @access public
227 function shouldStop() {
228 return $this->_stop;
232 * Informs the result that a test will be started.
234 * @param object
235 * @access public
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.
246 * @access public
248 function stop() {
249 $this->_stop = TRUE;
253 * Returns a HTML representation of the test result.
255 * @return string
256 * @access public
258 function toHTML() {
259 return '<pre>' . htmlspecialchars($this->toString()) . '</pre>';
263 * Returns a text representation of the test result.
265 * @return string
266 * @access public
268 function toString() {
269 $result = '';
271 foreach ($this->_passedTests as $passedTest) {
272 $result .= sprintf(
273 "TestCase %s->%s() passed\n",
275 get_class($passedTest),
276 $passedTest->getName()
280 foreach ($this->_failures as $failedTest) {
281 $result .= $failedTest->toString();
284 return $result;
287 * Returns whether the entire test was successful or not.
289 * @return boolean
290 * @access public
292 function wasSuccessful() {
293 if (empty($this->_errors) && empty($this->_failures)) {
294 return TRUE;
295 } else {
296 return FALSE;