]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/TextUI/ResultPrinter.php
Add .gitignore
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / TextUI / 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 TextUI
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.0.0
44  */
45
46 require_once 'PHP/Timer.php';
47
48 /**
49  * Prints the result of a TextUI TestRunner run.
50  *
51  * @package    PHPUnit
52  * @subpackage TextUI
53  * @author     Sebastian Bergmann <sebastian@phpunit.de>
54  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
55  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
56  * @version    Release: 3.5.13
57  * @link       http://www.phpunit.de/
58  * @since      Class available since Release 2.0.0
59  */
60 class PHPUnit_TextUI_ResultPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
61 {
62     const EVENT_TEST_START      = 0;
63     const EVENT_TEST_END        = 1;
64     const EVENT_TESTSUITE_START = 2;
65     const EVENT_TESTSUITE_END   = 3;
66
67     /**
68      * @var integer
69      */
70     protected $column = 0;
71
72     /**
73      * @var integer
74      */
75     protected $maxColumn;
76
77     /**
78      * @var boolean
79      */
80     protected $lastTestFailed = FALSE;
81
82     /**
83      * @var integer
84      */
85     protected $numAssertions = 0;
86
87     /**
88      * @var integer
89      */
90     protected $numTests = -1;
91
92     /**
93      * @var integer
94      */
95     protected $numTestsRun = 0;
96
97     /**
98      * @var integer
99      */
100     protected $numTestsWidth;
101
102     /**
103      * @var boolean
104      */
105     protected $colors = FALSE;
106
107     /**
108      * @var boolean
109      */
110     protected $debug = FALSE;
111
112     /**
113      * @var boolean
114      */
115     protected $verbose = FALSE;
116
117     /**
118      * Constructor.
119      *
120      * @param  mixed   $out
121      * @param  boolean $verbose
122      * @param  boolean $colors
123      * @param  boolean $debug
124      * @throws InvalidArgumentException
125      * @since  Method available since Release 3.0.0
126      */
127     public function __construct($out = NULL, $verbose = FALSE, $colors = FALSE, $debug = FALSE)
128     {
129         parent::__construct($out);
130
131         if (is_bool($verbose)) {
132             $this->verbose = $verbose;
133         } else {
134             throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'boolean');
135         }
136
137         if (is_bool($colors)) {
138             $this->colors = $colors;
139         } else {
140             throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'boolean');
141         }
142
143         if (is_bool($debug)) {
144             $this->debug = $debug;
145         } else {
146             throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'boolean');
147         }
148     }
149
150     /**
151      * @param  PHPUnit_Framework_TestResult $result
152      */
153     public function printResult(PHPUnit_Framework_TestResult $result)
154     {
155         $this->printHeader();
156
157         if ($result->errorCount() > 0) {
158             $this->printErrors($result);
159         }
160
161         if ($result->failureCount() > 0) {
162             if ($result->errorCount() > 0) {
163                 print "\n--\n\n";
164             }
165
166             $this->printFailures($result);
167         }
168
169         if ($this->verbose) {
170             if ($result->deprecatedFeaturesCount() > 0) {
171                 if ($result->failureCount() > 0) {
172                     print "\n--\n\nDeprecated PHPUnit features are being used";
173                 }
174
175                 foreach ($result->deprecatedFeatures() as $deprecatedFeature) {
176                     $this->write($deprecatedFeature . "\n\n");
177                 }
178             }
179
180             if ($result->notImplementedCount() > 0) {
181                 if ($result->failureCount() > 0) {
182                     print "\n--\n\n";
183                 }
184
185                 $this->printIncompletes($result);
186             }
187
188             if ($result->skippedCount() > 0) {
189                 if ($result->notImplementedCount() > 0) {
190                     print "\n--\n\n";
191                 }
192
193                 $this->printSkipped($result);
194             }
195         }
196
197         $this->printFooter($result);
198     }
199
200     /**
201      * @param  array   $defects
202      * @param  integer $count
203      * @param  string  $type
204      */
205     protected function printDefects(array $defects, $count, $type)
206     {
207         static $called = FALSE;
208
209         if ($count == 0) {
210             return;
211         }
212
213         $this->write(
214           sprintf(
215             "%sThere %s %d %s%s:\n",
216
217             $called ? "\n" : '',
218             ($count == 1) ? 'was' : 'were',
219             $count,
220             $type,
221             ($count == 1) ? '' : 's'
222           )
223         );
224
225         $i = 1;
226
227         foreach ($defects as $defect) {
228             $this->printDefect($defect, $i++);
229         }
230
231         $called = TRUE;
232     }
233
234     /**
235      * @param  PHPUnit_Framework_TestFailure $defect
236      * @param  integer                       $count
237      */
238     protected function printDefect(PHPUnit_Framework_TestFailure $defect, $count)
239     {
240         $this->printDefectHeader($defect, $count);
241         $this->printDefectTrace($defect);
242     }
243
244     /**
245      * @param  PHPUnit_Framework_TestFailure $defect
246      * @param  integer                       $count
247      */
248     protected function printDefectHeader(PHPUnit_Framework_TestFailure $defect, $count)
249     {
250         $failedTest = $defect->failedTest();
251
252         if ($failedTest instanceof PHPUnit_Framework_SelfDescribing) {
253             $testName = $failedTest->toString();
254         } else {
255             $testName = get_class($failedTest);
256         }
257
258         $this->write(
259           sprintf(
260             "\n%d) %s\n",
261
262             $count,
263             $testName
264           )
265         );
266     }
267
268     /**
269      * @param  PHPUnit_Framework_TestFailure $defect
270      */
271     protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect)
272     {
273         $this->write(
274           $defect->getExceptionAsString() . "\n" .
275           PHPUnit_Util_Filter::getFilteredStacktrace(
276             $defect->thrownException(),
277             FALSE
278           )
279         );
280     }
281
282     /**
283      * @param  PHPUnit_Framework_TestResult  $result
284      */
285     protected function printErrors(PHPUnit_Framework_TestResult $result)
286     {
287         $this->printDefects($result->errors(), $result->errorCount(), 'error');
288     }
289
290     /**
291      * @param  PHPUnit_Framework_TestResult  $result
292      */
293     protected function printFailures(PHPUnit_Framework_TestResult $result)
294     {
295         $this->printDefects(
296           $result->failures(),
297           $result->failureCount(),
298           'failure'
299         );
300     }
301
302     /**
303      * @param  PHPUnit_Framework_TestResult  $result
304      */
305     protected function printIncompletes(PHPUnit_Framework_TestResult $result)
306     {
307         $this->printDefects(
308           $result->notImplemented(),
309           $result->notImplementedCount(),
310           'incomplete test'
311         );
312     }
313
314     /**
315      * @param  PHPUnit_Framework_TestResult  $result
316      * @since  Method available since Release 3.0.0
317      */
318     protected function printSkipped(PHPUnit_Framework_TestResult $result)
319     {
320         $this->printDefects(
321           $result->skipped(),
322           $result->skippedCount(),
323           'skipped test'
324         );
325     }
326
327     protected function printHeader()
328     {
329         $this->write("\n\n" . PHP_Timer::resourceUsage() . "\n\n");
330     }
331
332     /**
333      * @param  PHPUnit_Framework_TestResult  $result
334      */
335     protected function printFooter(PHPUnit_Framework_TestResult $result)
336     {
337         if ($result->wasSuccessful() &&
338             $result->allCompletlyImplemented() &&
339             $result->noneSkipped()) {
340             if ($this->colors) {
341                 $this->write("\x1b[30;42m\x1b[2K");
342             }
343
344             $this->write(
345               sprintf(
346                 "OK (%d test%s, %d assertion%s)\n",
347
348                 count($result),
349                 (count($result) == 1) ? '' : 's',
350                 $this->numAssertions,
351                 ($this->numAssertions == 1) ? '' : 's'
352               )
353             );
354
355             if ($this->colors) {
356                 $this->write("\x1b[0m\x1b[2K");
357             }
358         }
359
360         else if ((!$result->allCompletlyImplemented() ||
361                   !$result->noneSkipped()) &&
362                  $result->wasSuccessful()) {
363             if ($this->colors) {
364                 $this->write(
365                   "\x1b[30;43m\x1b[2KOK, but incomplete or skipped tests!\n" .
366                   "\x1b[0m\x1b[30;43m\x1b[2K"
367                 );
368             } else {
369                 $this->write("OK, but incomplete or skipped tests!\n");
370             }
371
372             $this->write(
373               sprintf(
374                 "Tests: %d, Assertions: %d%s%s.\n",
375
376                 count($result),
377                 $this->numAssertions,
378                 $this->getCountString(
379                   $result->notImplementedCount(), 'Incomplete'
380                 ),
381                 $this->getCountString(
382                   $result->skippedCount(), 'Skipped'
383                 )
384               )
385             );
386
387             if ($this->colors) {
388                 $this->write("\x1b[0m\x1b[2K");
389             }
390         }
391
392         else {
393             $this->write("\n");
394
395             if ($this->colors) {
396                 $this->write(
397                   "\x1b[37;41m\x1b[2KFAILURES!\n\x1b[0m\x1b[37;41m\x1b[2K"
398                 );
399             } else {
400                 $this->write("FAILURES!\n");
401             }
402
403             $this->write(
404               sprintf(
405                 "Tests: %d, Assertions: %s%s%s%s%s.\n",
406
407                 count($result),
408                 $this->numAssertions,
409                 $this->getCountString($result->failureCount(), 'Failures'),
410                 $this->getCountString($result->errorCount(), 'Errors'),
411                 $this->getCountString(
412                   $result->notImplementedCount(), 'Incomplete'
413                 ),
414                 $this->getCountString($result->skippedCount(), 'Skipped')
415               )
416             );
417
418             if ($this->colors) {
419                 $this->write("\x1b[0m\x1b[2K");
420             }
421         }
422
423         if (!$this->verbose &&
424             $result->deprecatedFeaturesCount() > 0) {
425             $message = sprintf(
426               "Warning: Deprecated PHPUnit features are being used %s times!\n".
427               "Use --verbose for more information.\n",
428               $result->deprecatedFeaturesCount()
429             );
430
431             if ($this->colors) {
432                 $message = "\x1b[37;41m\x1b[2K" . $message .
433                            "\x1b[0m";
434             }
435
436             $this->write("\n" . $message);
437         }
438     }
439
440     /**
441      * @param  integer $count
442      * @param  string  $name
443      * @return string
444      * @since  Method available since Release 3.0.0
445      */
446     protected function getCountString($count, $name)
447     {
448         $string = '';
449
450         if ($count > 0) {
451             $string = sprintf(
452               ', %s: %d',
453
454               $name,
455               $count
456             );
457         }
458
459         return $string;
460     }
461
462     /**
463      */
464     public function printWaitPrompt()
465     {
466         $this->write("\n<RETURN> to continue\n");
467     }
468
469     /**
470      * An error occurred.
471      *
472      * @param  PHPUnit_Framework_Test $test
473      * @param  Exception              $e
474      * @param  float                  $time
475      */
476     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
477     {
478         $this->writeProgress('E');
479         $this->lastTestFailed = TRUE;
480     }
481
482     /**
483      * A failure occurred.
484      *
485      * @param  PHPUnit_Framework_Test                 $test
486      * @param  PHPUnit_Framework_AssertionFailedError $e
487      * @param  float                                  $time
488      */
489     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
490     {
491         $this->writeProgress('F');
492         $this->lastTestFailed = TRUE;
493     }
494
495     /**
496      * Incomplete test.
497      *
498      * @param  PHPUnit_Framework_Test $test
499      * @param  Exception              $e
500      * @param  float                  $time
501      */
502     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
503     {
504         $this->writeProgress('I');
505         $this->lastTestFailed = TRUE;
506     }
507
508     /**
509      * Skipped test.
510      *
511      * @param  PHPUnit_Framework_Test $test
512      * @param  Exception              $e
513      * @param  float                  $time
514      * @since  Method available since Release 3.0.0
515      */
516     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
517     {
518         $this->writeProgress('S');
519         $this->lastTestFailed = TRUE;
520     }
521
522     /**
523      * A testsuite started.
524      *
525      * @param  PHPUnit_Framework_TestSuite $suite
526      * @since  Method available since Release 2.2.0
527      */
528     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
529     {
530         if ($this->numTests == -1) {
531             $this->numTests      = count($suite);
532             $this->numTestsWidth = strlen((string)$this->numTests);
533             $this->maxColumn     = 69 - (2 * $this->numTestsWidth);
534         }
535     }
536
537     /**
538      * A testsuite ended.
539      *
540      * @param  PHPUnit_Framework_TestSuite $suite
541      * @since  Method available since Release 2.2.0
542      */
543     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
544     {
545     }
546
547     /**
548      * A test started.
549      *
550      * @param  PHPUnit_Framework_Test $test
551      */
552     public function startTest(PHPUnit_Framework_Test $test)
553     {
554         if ($this->debug) {
555             $this->write(
556               sprintf(
557                 "\nStarting test '%s'.\n", PHPUnit_Util_Test::describe($test)
558               )
559             );
560         }
561     }
562
563     /**
564      * A test ended.
565      *
566      * @param  PHPUnit_Framework_Test $test
567      * @param  float                  $time
568      */
569     public function endTest(PHPUnit_Framework_Test $test, $time)
570     {
571         if (!$this->lastTestFailed) {
572             $this->writeProgress('.');
573         }
574
575         if ($test instanceof PHPUnit_Framework_TestCase) {
576             $this->numAssertions += $test->getNumAssertions();
577         }
578
579         $this->lastTestFailed = FALSE;
580     }
581
582     /**
583      * @param  string $progress
584      */
585     protected function writeProgress($progress)
586     {
587         $this->write($progress);
588         $this->column++;
589         $this->numTestsRun++;
590
591         if ($this->column == $this->maxColumn) {
592             $this->write(
593               sprintf(
594                 ' %' . $this->numTestsWidth . 'd / %' .
595                        $this->numTestsWidth . 'd (%3s%%)',
596
597                 $this->numTestsRun,
598                 $this->numTests,
599                 floor(($this->numTestsRun / $this->numTests) * 100)
600               )
601             );
602
603             $this->writeNewLine();
604         }
605     }
606
607     protected function writeNewLine()
608     {
609         $this->column = 0;
610         $this->write("\n");
611     }
612 }