]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/TestDox/ResultPrinter.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / TestDox / ResultPrinter.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   Testing
38  * @package    PHPUnit
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 2.3.0
45  */
46
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49 require_once 'PHPUnit/Util/TestDox/NamePrettifier.php';
50 require_once 'PHPUnit/Util/Printer.php';
51
52 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
53
54 /**
55  * Base class for printers of TestDox documentation.
56  *
57  * @category   Testing
58  * @package    PHPUnit
59  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
60  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
61  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
62  * @version    Release: 3.3.17
63  * @link       http://www.phpunit.de/
64  * @since      Class available since Release 2.1.0
65  * @abstract
66  */
67 abstract class PHPUnit_Util_TestDox_ResultPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
68 {
69     /**
70      * @var    PHPUnit_Util_TestDox_NamePrettifier
71      */
72     protected $prettifier;
73
74     /**
75      * @var    string
76      */
77     protected $testClass = '';
78
79     /**
80      * @var    integer
81      */
82     protected $testStatus = FALSE;
83
84     /**
85      * @var    array
86      */
87     protected $tests = array();
88
89     protected $successful = 0;
90     protected $failed = 0;
91     protected $skipped = 0;
92     protected $incomplete = 0;
93     protected $testTypeOfInterest = 'PHPUnit_Framework_TestCase';
94
95     /**
96      * @var    string
97      */
98     protected $currentTestClassPrettified;
99
100     /**
101      * @var    string
102      */
103     protected $currentTestMethodPrettified;
104
105     /**
106      * Constructor.
107      *
108      * @param  resource  $out
109      */
110     public function __construct($out = NULL)
111     {
112         parent::__construct($out);
113
114         $this->prettifier = new PHPUnit_Util_TestDox_NamePrettifier;
115         $this->startRun();
116     }
117
118     /**
119      * Flush buffer and close output.
120      *
121      */
122     public function flush()
123     {
124         $this->doEndClass();
125         $this->endRun();
126
127         parent::flush();
128     }
129
130     /**
131      * An error occurred.
132      *
133      * @param  PHPUnit_Framework_Test $test
134      * @param  Exception              $e
135      * @param  float                  $time
136      */
137     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
138     {
139         if ($test instanceof $this->testTypeOfInterest) {
140             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
141             $this->failed++;
142         }
143     }
144
145     /**
146      * A failure occurred.
147      *
148      * @param  PHPUnit_Framework_Test                 $test
149      * @param  PHPUnit_Framework_AssertionFailedError $e
150      * @param  float                                  $time
151      */
152     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
153     {
154         if ($test instanceof $this->testTypeOfInterest) {
155             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
156             $this->failed++;
157         }
158     }
159
160     /**
161      * Incomplete test.
162      *
163      * @param  PHPUnit_Framework_Test $test
164      * @param  Exception              $e
165      * @param  float                  $time
166      */
167     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
168     {
169         if ($test instanceof $this->testTypeOfInterest) {
170             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
171             $this->incomplete++;
172         }
173     }
174
175     /**
176      * Skipped test.
177      *
178      * @param  PHPUnit_Framework_Test $test
179      * @param  Exception              $e
180      * @param  float                  $time
181      * @since  Method available since Release 3.0.0
182      */
183     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
184     {
185         if ($test instanceof $this->testTypeOfInterest) {
186             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
187             $this->skipped++;
188         }
189     }
190
191     /**
192      * A testsuite started.
193      *
194      * @param  PHPUnit_Framework_TestSuite $suite
195      * @since  Method available since Release 2.2.0
196      */
197     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
198     {
199     }
200
201     /**
202      * A testsuite ended.
203      *
204      * @param  PHPUnit_Framework_TestSuite $suite
205      * @since  Method available since Release 2.2.0
206      */
207     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
208     {
209     }
210
211     /**
212      * A test started.
213      *
214      * @param  PHPUnit_Framework_Test $test
215      */
216     public function startTest(PHPUnit_Framework_Test $test)
217     {
218         if ($test instanceof $this->testTypeOfInterest) {
219             $class = get_class($test);
220
221             if ($this->testClass != $class) {
222                 if ($this->testClass != '') {
223                     $this->doEndClass();
224                 }
225
226                 $this->currentTestClassPrettified = $this->prettifier->prettifyTestClass($class);
227                 $this->startClass($class);
228
229                 $this->testClass = $class;
230                 $this->tests     = array();
231             }
232
233             $this->currentTestMethodPrettified = $this->prettifier->prettifyTestMethod($test->getName());
234             $this->testStatus                  = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
235         }
236     }
237
238     /**
239      * A test ended.
240      *
241      * @param  PHPUnit_Framework_Test $test
242      * @param  float                  $time
243      */
244     public function endTest(PHPUnit_Framework_Test $test, $time)
245     {
246         if ($test instanceof $this->testTypeOfInterest) {
247             if (!isset($this->tests[$this->currentTestMethodPrettified])) {
248                 if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
249                     $this->tests[$this->currentTestMethodPrettified]['success'] = 1;
250                     $this->tests[$this->currentTestMethodPrettified]['failure'] = 0;
251                 } else {
252                     $this->tests[$this->currentTestMethodPrettified]['success'] = 0;
253                     $this->tests[$this->currentTestMethodPrettified]['failure'] = 1;
254                 }
255             } else {
256                 if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
257                     $this->tests[$this->currentTestMethodPrettified]['success']++;
258                 } else {
259                     $this->tests[$this->currentTestMethodPrettified]['failure']++;
260                 }
261             }
262
263             $this->currentTestClassPrettified  = NULL;
264             $this->currentTestMethodPrettified = NULL;
265         }
266     }
267
268     /**
269      * @since  Method available since Release 2.3.0
270      */
271     protected function doEndClass()
272     {
273         foreach ($this->tests as $name => $data) {
274             $this->onTest($name, $data['failure'] == 0);
275         }
276
277         $this->endClass($this->testClass);
278     }
279
280     /**
281      * Handler for 'start run' event.
282      *
283      */
284     protected function startRun()
285     {
286     }
287
288     /**
289      * Handler for 'start class' event.
290      *
291      * @param  string $name
292      */
293     protected function startClass($name)
294     {
295     }
296
297     /**
298      * Handler for 'on test' event.
299      *
300      * @param  string  $name
301      * @param  boolean $success
302      */
303     protected function onTest($name, $success = TRUE)
304     {
305     }
306
307     /**
308      * Handler for 'end class' event.
309      *
310      * @param  string $name
311      */
312     protected function endClass($name)
313     {
314     }
315
316     /**
317      * Handler for 'end run' event.
318      *
319      */
320     protected function endRun()
321     {
322     }
323 }
324 ?>