8511c2dace641008dd61defffb096c2e3c55d1ca
[yaffs-website] / vendor / phpunit / phpunit / src / Util / TestDox / ResultPrinter.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Base class for printers of TestDox documentation.
13  *
14  * @since Class available since Release 2.1.0
15  */
16 abstract class PHPUnit_Util_TestDox_ResultPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
17 {
18     /**
19      * @var PHPUnit_Util_TestDox_NamePrettifier
20      */
21     protected $prettifier;
22
23     /**
24      * @var string
25      */
26     protected $testClass = '';
27
28     /**
29      * @var int
30      */
31     protected $testStatus = false;
32
33     /**
34      * @var array
35      */
36     protected $tests = array();
37
38     /**
39      * @var int
40      */
41     protected $successful = 0;
42
43     /**
44      * @var int
45      */
46     protected $failed = 0;
47
48     /**
49      * @var int
50      */
51     protected $risky = 0;
52
53     /**
54      * @var int
55      */
56     protected $skipped = 0;
57
58     /**
59      * @var int
60      */
61     protected $incomplete = 0;
62
63     /**
64      * @var string
65      */
66     protected $currentTestClassPrettified;
67
68     /**
69      * @var string
70      */
71     protected $currentTestMethodPrettified;
72
73     /**
74      * Constructor.
75      *
76      * @param resource $out
77      */
78     public function __construct($out = null)
79     {
80         parent::__construct($out);
81
82         $this->prettifier = new PHPUnit_Util_TestDox_NamePrettifier;
83         $this->startRun();
84     }
85
86     /**
87      * Flush buffer and close output.
88      */
89     public function flush()
90     {
91         $this->doEndClass();
92         $this->endRun();
93
94         parent::flush();
95     }
96
97     /**
98      * An error occurred.
99      *
100      * @param PHPUnit_Framework_Test $test
101      * @param Exception              $e
102      * @param float                  $time
103      */
104     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
105     {
106         if (!$this->isOfInterest($test)) {
107             return;
108         }
109
110         $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
111         $this->failed++;
112     }
113
114     /**
115      * A failure occurred.
116      *
117      * @param PHPUnit_Framework_Test                 $test
118      * @param PHPUnit_Framework_AssertionFailedError $e
119      * @param float                                  $time
120      */
121     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
122     {
123         if (!$this->isOfInterest($test)) {
124             return;
125         }
126
127         $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
128         $this->failed++;
129     }
130
131     /**
132      * Incomplete test.
133      *
134      * @param PHPUnit_Framework_Test $test
135      * @param Exception              $e
136      * @param float                  $time
137      */
138     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
139     {
140         if (!$this->isOfInterest($test)) {
141             return;
142         }
143
144         $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
145         $this->incomplete++;
146     }
147
148     /**
149      * Risky test.
150      *
151      * @param PHPUnit_Framework_Test $test
152      * @param Exception              $e
153      * @param float                  $time
154      *
155      * @since  Method available since Release 4.0.0
156      */
157     public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
158     {
159         if (!$this->isOfInterest($test)) {
160             return;
161         }
162
163         $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_RISKY;
164         $this->risky++;
165     }
166
167     /**
168      * Skipped test.
169      *
170      * @param PHPUnit_Framework_Test $test
171      * @param Exception              $e
172      * @param float                  $time
173      *
174      * @since  Method available since Release 3.0.0
175      */
176     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
177     {
178         if (!$this->isOfInterest($test)) {
179             return;
180         }
181
182         $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
183         $this->skipped++;
184     }
185
186     /**
187      * A testsuite started.
188      *
189      * @param PHPUnit_Framework_TestSuite $suite
190      *
191      * @since  Method available since Release 2.2.0
192      */
193     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
194     {
195     }
196
197     /**
198      * A testsuite ended.
199      *
200      * @param PHPUnit_Framework_TestSuite $suite
201      *
202      * @since  Method available since Release 2.2.0
203      */
204     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
205     {
206     }
207
208     /**
209      * A test started.
210      *
211      * @param PHPUnit_Framework_Test $test
212      */
213     public function startTest(PHPUnit_Framework_Test $test)
214     {
215         if (!$this->isOfInterest($test)) {
216             return;
217         }
218
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         $prettified = false;
234
235         $annotations = $test->getAnnotations();
236
237         if (isset($annotations['method']['testdox'][0])) {
238             $this->currentTestMethodPrettified = $annotations['method']['testdox'][0];
239             $prettified                        = true;
240         }
241
242         if (!$prettified) {
243             $this->currentTestMethodPrettified = $this->prettifier->prettifyTestMethod($test->getName(false));
244         }
245
246         $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
247     }
248
249     /**
250      * A test ended.
251      *
252      * @param PHPUnit_Framework_Test $test
253      * @param float                  $time
254      */
255     public function endTest(PHPUnit_Framework_Test $test, $time)
256     {
257         if (!$this->isOfInterest($test)) {
258             return;
259         }
260
261         if (!isset($this->tests[$this->currentTestMethodPrettified])) {
262             if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
263                 $this->tests[$this->currentTestMethodPrettified]['success'] = 1;
264                 $this->tests[$this->currentTestMethodPrettified]['failure'] = 0;
265             } else {
266                 $this->tests[$this->currentTestMethodPrettified]['success'] = 0;
267                 $this->tests[$this->currentTestMethodPrettified]['failure'] = 1;
268             }
269         } else {
270             if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
271                 $this->tests[$this->currentTestMethodPrettified]['success']++;
272             } else {
273                 $this->tests[$this->currentTestMethodPrettified]['failure']++;
274             }
275         }
276
277         $this->currentTestClassPrettified  = null;
278         $this->currentTestMethodPrettified = null;
279     }
280
281     /**
282      * @since  Method available since Release 2.3.0
283      */
284     protected function doEndClass()
285     {
286         foreach ($this->tests as $name => $data) {
287             $this->onTest($name, $data['failure'] == 0);
288         }
289
290         $this->endClass($this->testClass);
291     }
292
293     /**
294      * Handler for 'start run' event.
295      */
296     protected function startRun()
297     {
298     }
299
300     /**
301      * Handler for 'start class' event.
302      *
303      * @param string $name
304      */
305     protected function startClass($name)
306     {
307     }
308
309     /**
310      * Handler for 'on test' event.
311      *
312      * @param string $name
313      * @param bool   $success
314      */
315     protected function onTest($name, $success = true)
316     {
317     }
318
319     /**
320      * Handler for 'end class' event.
321      *
322      * @param string $name
323      */
324     protected function endClass($name)
325     {
326     }
327
328     /**
329      * Handler for 'end run' event.
330      */
331     protected function endRun()
332     {
333     }
334
335     private function isOfInterest(PHPUnit_Framework_Test $test)
336     {
337         return $test instanceof PHPUnit_Framework_TestCase && get_class($test) != 'PHPUnit_Framework_Warning';
338     }
339 }