]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/TestDox/ResultPrinter.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / TestDox / ResultPrinter.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.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  * @package    PHPUnit
38  * @subpackage Util_TestDox
39  * @author     Sebastian Bergmann <sebastian@phpunit.de>
40  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42  * @link       http://www.phpunit.de/
43  * @since      File available since Release 2.3.0
44  */
45
46 /**
47  * Base class for printers of TestDox documentation.
48  *
49  * @package    PHPUnit
50  * @subpackage Util_TestDox
51  * @author     Sebastian Bergmann <sebastian@phpunit.de>
52  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
53  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
54  * @version    Release: 3.5.13
55  * @link       http://www.phpunit.de/
56  * @since      Class available since Release 2.1.0
57  */
58 abstract class PHPUnit_Util_TestDox_ResultPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
59 {
60     /**
61      * @var PHPUnit_Util_TestDox_NamePrettifier
62      */
63     protected $prettifier;
64
65     /**
66      * @var string
67      */
68     protected $testClass = '';
69
70     /**
71      * @var integer
72      */
73     protected $testStatus = FALSE;
74
75     /**
76      * @var array
77      */
78     protected $tests = array();
79
80     /**
81      * @var integer
82      */
83     protected $successful = 0;
84
85     /**
86      * @var integer
87      */
88     protected $failed = 0;
89
90     /**
91      * @var integer
92      */
93     protected $skipped = 0;
94
95     /**
96      * @var integer
97      */
98     protected $incomplete = 0;
99
100     /**
101      * @var string
102      */
103     protected $testTypeOfInterest = 'PHPUnit_Framework_TestCase';
104
105     /**
106      * @var string
107      */
108     protected $currentTestClassPrettified;
109
110     /**
111      * @var string
112      */
113     protected $currentTestMethodPrettified;
114
115     /**
116      * Constructor.
117      *
118      * @param  resource  $out
119      */
120     public function __construct($out = NULL)
121     {
122         parent::__construct($out);
123
124         $this->prettifier = new PHPUnit_Util_TestDox_NamePrettifier;
125         $this->startRun();
126     }
127
128     /**
129      * Flush buffer and close output.
130      *
131      */
132     public function flush()
133     {
134         $this->doEndClass();
135         $this->endRun();
136
137         parent::flush();
138     }
139
140     /**
141      * An error occurred.
142      *
143      * @param  PHPUnit_Framework_Test $test
144      * @param  Exception              $e
145      * @param  float                  $time
146      */
147     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
148     {
149         if ($test instanceof $this->testTypeOfInterest) {
150             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
151             $this->failed++;
152         }
153     }
154
155     /**
156      * A failure occurred.
157      *
158      * @param  PHPUnit_Framework_Test                 $test
159      * @param  PHPUnit_Framework_AssertionFailedError $e
160      * @param  float                                  $time
161      */
162     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
163     {
164         if ($test instanceof $this->testTypeOfInterest) {
165             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
166             $this->failed++;
167         }
168     }
169
170     /**
171      * Incomplete test.
172      *
173      * @param  PHPUnit_Framework_Test $test
174      * @param  Exception              $e
175      * @param  float                  $time
176      */
177     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
178     {
179         if ($test instanceof $this->testTypeOfInterest) {
180             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
181             $this->incomplete++;
182         }
183     }
184
185     /**
186      * Skipped test.
187      *
188      * @param  PHPUnit_Framework_Test $test
189      * @param  Exception              $e
190      * @param  float                  $time
191      * @since  Method available since Release 3.0.0
192      */
193     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
194     {
195         if ($test instanceof $this->testTypeOfInterest) {
196             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
197             $this->skipped++;
198         }
199     }
200
201     /**
202      * A testsuite started.
203      *
204      * @param  PHPUnit_Framework_TestSuite $suite
205      * @since  Method available since Release 2.2.0
206      */
207     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
208     {
209     }
210
211     /**
212      * A testsuite ended.
213      *
214      * @param  PHPUnit_Framework_TestSuite $suite
215      * @since  Method available since Release 2.2.0
216      */
217     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
218     {
219     }
220
221     /**
222      * A test started.
223      *
224      * @param  PHPUnit_Framework_Test $test
225      */
226     public function startTest(PHPUnit_Framework_Test $test)
227     {
228         if ($test instanceof $this->testTypeOfInterest) {
229             $class = get_class($test);
230
231             if ($this->testClass != $class) {
232                 if ($this->testClass != '') {
233                     $this->doEndClass();
234                 }
235
236                 $this->currentTestClassPrettified = $this->prettifier->prettifyTestClass($class);
237                 $this->startClass($class);
238
239                 $this->testClass = $class;
240                 $this->tests     = array();
241             }
242
243             $prettified = FALSE;
244
245             if ($test instanceof PHPUnit_Framework_TestCase &&
246                !$test instanceof PHPUnit_Framework_Warning) {
247                 $annotations = $test->getAnnotations();
248
249                 if (isset($annotations['method']['testdox'][0])) {
250                     $this->currentTestMethodPrettified = $annotations['method']['testdox'][0];
251                     $prettified                        = TRUE;
252                 }
253             }
254
255             if (!$prettified) {
256                 $this->currentTestMethodPrettified = $this->prettifier->prettifyTestMethod($test->getName(FALSE));
257             }
258
259             $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
260         }
261     }
262
263     /**
264      * A test ended.
265      *
266      * @param  PHPUnit_Framework_Test $test
267      * @param  float                  $time
268      */
269     public function endTest(PHPUnit_Framework_Test $test, $time)
270     {
271         if ($test instanceof $this->testTypeOfInterest) {
272             if (!isset($this->tests[$this->currentTestMethodPrettified])) {
273                 if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
274                     $this->tests[$this->currentTestMethodPrettified]['success'] = 1;
275                     $this->tests[$this->currentTestMethodPrettified]['failure'] = 0;
276                 } else {
277                     $this->tests[$this->currentTestMethodPrettified]['success'] = 0;
278                     $this->tests[$this->currentTestMethodPrettified]['failure'] = 1;
279                 }
280             } else {
281                 if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
282                     $this->tests[$this->currentTestMethodPrettified]['success']++;
283                 } else {
284                     $this->tests[$this->currentTestMethodPrettified]['failure']++;
285                 }
286             }
287
288             $this->currentTestClassPrettified  = NULL;
289             $this->currentTestMethodPrettified = NULL;
290         }
291     }
292
293     /**
294      * @since  Method available since Release 2.3.0
295      */
296     protected function doEndClass()
297     {
298         foreach ($this->tests as $name => $data) {
299             $this->onTest($name, $data['failure'] == 0);
300         }
301
302         $this->endClass($this->testClass);
303     }
304
305     /**
306      * Handler for 'start run' event.
307      *
308      */
309     protected function startRun()
310     {
311     }
312
313     /**
314      * Handler for 'start class' event.
315      *
316      * @param  string $name
317      */
318     protected function startClass($name)
319     {
320     }
321
322     /**
323      * Handler for 'on test' event.
324      *
325      * @param  string  $name
326      * @param  boolean $success
327      */
328     protected function onTest($name, $success = TRUE)
329     {
330     }
331
332     /**
333      * Handler for 'end class' event.
334      *
335      * @param  string $name
336      */
337     protected function endClass($name)
338     {
339     }
340
341     /**
342      * Handler for 'end run' event.
343      *
344      */
345     protected function endRun()
346     {
347     }
348 }