2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
7 * LICENSE: This source file is subject to version 3.0 of the PHP license
8 * that is available through the world-wide-web at the following URI:
9 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
10 * the PHP License and are unable to obtain it through the web, please
11 * send a note to license@php.net so we can mail you a copy immediately.
15 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
16 * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
17 * @license http://www.php.net/license/3_0.txt PHP License 3.0
19 * @link http://pear.php.net/package/PHPUnit
20 * @since File available since Release 1.0.0
23 require_once 'PHPUnit/TestFailure.php';
24 require_once 'PHPUnit/TestListener.php';
26 if (!function_exists('is_a')) {
27 require_once 'PHP/Compat/Function/is_a.php';
31 * A TestResult collects the results of executing a test case.
35 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
36 * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
37 * @license http://www.php.net/license/3_0.txt PHP License 3.0
38 * @version Release: @package_version@
39 * @link http://pear.php.net/package/PHPUnit
40 * @since Class available since Release 1.0.0
42 class PHPUnit_TestResult {
47 var $_errors = array();
53 var $_failures = array();
59 var $_listeners = array();
65 var $_passedTests = array();
80 * Adds an error to the list of errors.
81 * The passed in exception caused the error.
87 function addError(&$test, &$t) {
88 $this->_errors[] = new PHPUnit_TestFailure($test, $t);
90 for ($i = 0; $i < sizeof($this->_listeners); $i++) {
91 $this->_listeners[$i]->addError($test, $t);
96 * Adds a failure to the list of failures.
97 * The passed in exception caused the failure.
103 function addFailure(&$test, &$t) {
104 $this->_failures[] = new PHPUnit_TestFailure($test, $t);
106 for ($i = 0; $i < sizeof($this->_listeners); $i++) {
107 $this->_listeners[$i]->addFailure($test, $t);
112 * Registers a TestListener.
117 function addListener(&$listener) {
118 if (is_object($listener) &&
119 is_a($listener, 'PHPUnit_TestListener')) {
120 $this->_listeners[] = &$listener;
125 * Adds a passed test to the list of passed tests.
130 function addPassedTest(&$test) {
131 $this->_passedTests[] = &$test;
135 * Informs the result that a test was completed.
140 function endTest(&$test) {
141 for ($i = 0; $i < sizeof($this->_listeners); $i++) {
142 $this->_listeners[$i]->endTest($test);
147 * Gets the number of detected errors.
152 function errorCount() {
153 return sizeof($this->_errors);
157 * Returns an Enumeration for the errors.
163 return $this->_errors;
167 * Gets the number of detected failures.
172 function failureCount() {
173 return sizeof($this->_failures);
177 * Returns an Enumeration for the failures.
182 function &failures() {
183 return $this->_failures;
187 * Returns an Enumeration for the passed tests.
192 function &passedTests() {
193 return $this->_passedTests;
197 * Unregisters a TestListener.
198 * This requires the Zend Engine 2 (to work properly).
203 function removeListener(&$listener) {
204 for ($i = 0; $i < sizeof($this->_listeners); $i++) {
205 if ($this->_listeners[$i] === $listener) {
206 unset($this->_listeners[$i]);
217 function run(&$test) {
218 $this->startTest($test);
221 $this->endTest($test);
225 * Gets the number of run tests.
230 function runCount() {
231 return $this->_runTests;
235 * Checks whether the test run should stop.
239 function shouldStop() {
244 * Informs the result that a test will be started.
249 function startTest(&$test) {
250 for ($i = 0; $i < sizeof($this->_listeners); $i++) {
251 $this->_listeners[$i]->startTest($test);
256 * Marks that the test run should stop.
265 * Returns a HTML representation of the test result.
271 return '<pre>' . htmlspecialchars($this->toString()) . '</pre>';
275 * Returns a text representation of the test result.
280 function toString() {
283 foreach ($this->_passedTests as $passedTest) {
285 "TestCase %s->%s() passed\n",
287 get_class($passedTest),
288 $passedTest->getName()
292 foreach ($this->_failures as $failedTest) {
293 $result .= $failedTest->toString();
300 * Returns whether the entire test was successful or not.
305 function wasSuccessful() {
306 if (empty($this->_errors) && empty($this->_failures)) {
318 * c-hanging-comment-ender-p: nil