1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Tester\Runner\Output;
9:
10: use Tester,
11: Tester\Runner\Runner;
12:
13:
14: 15: 16: 17: 18:
19: class ConsolePrinter implements Tester\Runner\OutputHandler
20: {
21:
22: const PRINT_LINES = 15;
23:
24:
25: private $runner;
26:
27:
28: private $displaySkipped = FALSE;
29:
30:
31: private $buffer;
32:
33:
34: private $time;
35:
36:
37: public function __construct(Runner $runner, $displaySkipped = FALSE)
38: {
39: $this->runner = $runner;
40: $this->displaySkipped = $displaySkipped;
41: }
42:
43:
44: public function begin()
45: {
46: $this->time = -microtime(TRUE);
47: echo 'PHP ' . $this->runner->getPhp()->getVersion()
48: . ' | ' . $this->runner->getPhp()->getCommandLine()
49: . " | {$this->runner->threadCount} threads\n\n";
50: }
51:
52:
53: public function result($testName, $result, $message)
54: {
55: $outputs = array(
56: Runner::PASSED => '.',
57: Runner::SKIPPED => 's',
58: Runner::FAILED => "\033[1;41;37mF\033[0m",
59: );
60: echo $outputs[$result];
61:
62: if ($result === Runner::FAILED) {
63: $lines = explode("\n", trim($message), self::PRINT_LINES + 1);
64: $lines[self::PRINT_LINES] = isset($lines[self::PRINT_LINES]) ? '...' : '';
65: $this->buffer .= "\033[1;31m-- FAILED: $testName\033[0m\n " . implode("\n ", $lines) . "\n";
66:
67: } elseif ($result === Runner::SKIPPED && $this->displaySkipped) {
68: $this->buffer .= "-- Skipped: $testName\n $message\n\n";
69: }
70: }
71:
72:
73: public function end()
74: {
75: $jobCount = $this->runner->getJobCount();
76: $results = $this->runner->getResults();
77: $count = array_sum($results);
78: echo !$jobCount ? "No tests found\n" :
79: "\n\n" . $this->buffer . "\n"
80: . ($results[Runner::FAILED] ? "\033[1;41;37mFAILURES!" : "\033[1;42;37mOK")
81: . " ($jobCount tests, "
82: . ($results[Runner::FAILED] ? $results[Runner::FAILED] . ' failures, ' : '')
83: . ($results[Runner::SKIPPED] ? $results[Runner::SKIPPED] . ' skipped, ' : '')
84: . ($jobCount !== $count ? ($jobCount - $count) . ' not run, ' : '')
85: . sprintf('%0.1f', $this->time + microtime(TRUE)) . " seconds)\033[0m\n";
86:
87: $this->buffer = NULL;
88: }
89:
90: }
91: