1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * PHP Version 4
6 *
7 * Copyright (c) 2002-2005, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 *   * Redistributions of source code must retain the above copyright
15 *     notice, this list of conditions and the following disclaimer.
16 *
17 *   * Redistributions in binary form must reproduce the above copyright
18 *     notice, this list of conditions and the following disclaimer in
19 *     the documentation and/or other materials provided with the
20 *     distribution.
21 *
22 *   * Neither the name of Sebastian Bergmann nor the names of his
23 *     contributors may be used to endorse or promote products derived
24 *     from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 *
39 * @category   Testing
40 * @package    PHPUnit
41 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
42 * @copyright  2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
43 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
44 * @version    CVS: $Id: TestResult.php,v 1.18 2005/11/10 09:47:15 sebastian Exp $
45 * @link       http://pear.php.net/package/PHPUnit
46 * @since      File available since Release 1.0.0
47 */
48
49require_once 'PHPUnit/TestFailure.php';
50require_once 'PHPUnit/TestListener.php';
51
52if (!function_exists('is_a')) {
53    require_once 'PHP/Compat/Function/is_a.php';
54}
55
56/**
57 * A TestResult collects the results of executing a test case.
58 *
59 * @category   Testing
60 * @package    PHPUnit
61 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
62 * @copyright  2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
63 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
64 * @version    Release: 1.3.2
65 * @link       http://pear.php.net/package/PHPUnit
66 * @since      Class available since Release 1.0.0
67 */
68class PHPUnit_TestResult {
69    /**
70     * @var    array
71     * @access protected
72     */
73    var $_errors = array();
74
75    /**
76     * @var    array
77     * @access protected
78     */
79    var $_failures = array();
80
81     /**
82     * @var    array
83     * @access protected
84     */
85    var $_listeners = array();
86
87    /**
88     * @var    array
89     * @access protected
90     */
91    var $_passedTests = array();
92
93    /**
94     * @var    integer
95     * @access protected
96     */
97    var $_runTests = 0;
98
99    /**
100     * @var    boolean
101     * @access private
102     */
103    var $_stop = FALSE;
104
105    /**
106     * Adds an error to the list of errors.
107     * The passed in exception caused the error.
108     *
109     * @param  object
110     * @param  object
111     * @access public
112     */
113    function addError(&$test, &$t) {
114        $this->_errors[] = new PHPUnit_TestFailure($test, $t);
115
116        for ($i = 0; $i < sizeof($this->_listeners); $i++) {
117            $this->_listeners[$i]->addError($test, $t);
118        }
119    }
120
121    /**
122     * Adds a failure to the list of failures.
123     * The passed in exception caused the failure.
124     *
125     * @param  object
126     * @param  object
127     * @access public
128     */
129    function addFailure(&$test, &$t) {
130        $this->_failures[] = new PHPUnit_TestFailure($test, $t);
131
132        for ($i = 0; $i < sizeof($this->_listeners); $i++) {
133            $this->_listeners[$i]->addFailure($test, $t);
134        }
135    }
136
137    /**
138     * Registers a TestListener.
139     *
140     * @param  object
141     * @access public
142     */
143    function addListener(&$listener) {
144        if (is_object($listener) &&
145            is_a($listener, 'PHPUnit_TestListener')) {
146            $this->_listeners[] = &$listener;
147        }
148    }
149
150    /**
151     * Adds a passed test to the list of passed tests.
152     *
153     * @param  object
154     * @access public
155     */
156    function addPassedTest(&$test) {
157        $this->_passedTests[] = &$test;
158    }
159
160    /**
161     * Informs the result that a test was completed.
162     *
163     * @param  object
164     * @access public
165     */
166    function endTest(&$test) {
167        for ($i = 0; $i < sizeof($this->_listeners); $i++) {
168            $this->_listeners[$i]->endTest($test);
169        }
170    }
171
172    /**
173     * Gets the number of detected errors.
174     *
175     * @return integer
176     * @access public
177     */
178    function errorCount() {
179        return sizeof($this->_errors);
180    }
181
182    /**
183     * Returns an Enumeration for the errors.
184     *
185     * @return array
186     * @access public
187     */
188    function &errors() {
189        return $this->_errors;
190    }
191
192    /**
193     * Gets the number of detected failures.
194     *
195     * @return integer
196     * @access public
197     */
198    function failureCount() {
199        return sizeof($this->_failures);
200    }
201
202    /**
203     * Returns an Enumeration for the failures.
204     *
205     * @return array
206     * @access public
207     */
208    function &failures() {
209        return $this->_failures;
210    }
211
212    /**
213     * Returns an Enumeration for the passed tests.
214     *
215     * @return array
216     * @access public
217     */
218    function &passedTests() {
219        return $this->_passedTests;
220    }
221
222    /**
223     * Unregisters a TestListener.
224     * This requires the Zend Engine 2 (to work properly).
225     *
226     * @param  object
227     * @access public
228     */
229    function removeListener(&$listener) {
230        for ($i = 0; $i < sizeof($this->_listeners); $i++) {
231            if ($this->_listeners[$i] === $listener) {
232                unset($this->_listeners[$i]);
233            }
234        }
235    }
236
237    /**
238     * Runs a TestCase.
239     *
240     * @param  object
241     * @access public
242     */
243    function run(&$test) {
244        $this->startTest($test);
245        $this->_runTests++;
246        $test->runBare();
247        $this->endTest($test);
248    }
249
250    /**
251     * Gets the number of run tests.
252     *
253     * @return integer
254     * @access public
255     */
256    function runCount() {
257        return $this->_runTests;
258    }
259
260    /**
261     * Checks whether the test run should stop.
262     *
263     * @access public
264     */
265    function shouldStop() {
266        return $this->_stop;
267    }
268
269    /**
270     * Informs the result that a test will be started.
271     *
272     * @param  object
273     * @access public
274     */
275    function startTest(&$test) {
276        for ($i = 0; $i < sizeof($this->_listeners); $i++) {
277            $this->_listeners[$i]->startTest($test);
278        }
279    }
280
281    /**
282     * Marks that the test run should stop.
283     *
284     * @access public
285     */
286    function stop() {
287        $this->_stop = TRUE;
288    }
289
290    /**
291     * Returns a HTML representation of the test result.
292     *
293     * @return string
294     * @access public
295     */
296    function toHTML() {
297        return '<pre>' . htmlspecialchars($this->toString()) . '</pre>';
298    }
299
300    /**
301     * Returns a text representation of the test result.
302     *
303     * @return string
304     * @access public
305     */
306    function toString() {
307        $result = '';
308
309        foreach ($this->_passedTests as $passedTest) {
310            $result .= sprintf(
311              "TestCase %s->%s() passed\n",
312
313              get_class($passedTest),
314              $passedTest->getName()
315            );
316        }
317
318        foreach ($this->_failures as $failedTest) {
319            $result .= $failedTest->toString();
320        }
321
322        return $result;
323    }
324
325    /**
326     * Returns whether the entire test was successful or not.
327     *
328     * @return boolean
329     * @access public
330     */
331    function wasSuccessful() {
332        if (empty($this->_errors) && empty($this->_failures)) {
333            return TRUE;
334        } else {
335            return FALSE;
336        }
337    }
338}
339
340/*
341 * Local variables:
342 * tab-width: 4
343 * c-basic-offset: 4
344 * c-hanging-comment-ender-p: nil
345 * End:
346 */
347?>
348