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