1 | <?php |
||
18 | abstract class AbstractBaseReporter implements ReporterInterface |
||
19 | { |
||
20 | use HasEventEmitterTrait; |
||
21 | |||
22 | /** |
||
23 | * @var \Peridot\Configuration |
||
24 | */ |
||
25 | protected $configuration; |
||
26 | |||
27 | /** |
||
28 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
29 | */ |
||
30 | protected $output; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $errors = []; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $passing = 0; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $pending = 0; |
||
46 | |||
47 | /** |
||
48 | * @var double|integer |
||
49 | */ |
||
50 | protected $time; |
||
51 | |||
52 | /** |
||
53 | * Maps color names to left and right color sequences. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $colors = array( |
||
58 | 'white' => ['left' => "\033[37m", 'right' => "\033[39m"], |
||
59 | 'success' => ['left' => "\033[32m", 'right' => "\033[39m"], |
||
60 | 'error' => ['left' => "\033[31m", 'right' => "\033[39m"], |
||
61 | 'muted' => ['left' => "\033[90m", 'right' => "\033[0m"], |
||
62 | 'pending' => ['left' => "\033[36m", 'right' => "\033[39m"], |
||
63 | ); |
||
64 | |||
65 | /** |
||
66 | * Maps symbol names to symbols |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $symbols = array( |
||
71 | 'check' => '✓' |
||
72 | ); |
||
73 | |||
74 | /** |
||
75 | * @param Configuration $configuration |
||
76 | * @param OutputInterface $output |
||
77 | * @param EventEmitterInterface $eventEmitter |
||
78 | */ |
||
79 | public function __construct( |
||
80 | Configuration $configuration, |
||
81 | OutputInterface $output, |
||
82 | EventEmitterInterface $eventEmitter |
||
83 | ) { |
||
84 | $this->configuration = $configuration; |
||
85 | $this->output = $output; |
||
86 | $this->eventEmitter = $eventEmitter; |
||
87 | |||
88 | $this->registerSymbols(); |
||
89 | |||
90 | $this->registerEvents(); |
||
91 | |||
92 | $this->init(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Given a color name, colorize the provided text in that |
||
97 | * color |
||
98 | * |
||
99 | * @param $key |
||
100 | * @param $text |
||
101 | * @return string |
||
102 | */ |
||
103 | public function color($key, $text) |
||
104 | { |
||
105 | $colorsEnabled = $this->configuration->areColorsEnabled() && $this->hasColorSupport(); |
||
106 | $colorsEnabledExplicit = $this->configuration->areColorsEnabledExplicit(); |
||
107 | |||
108 | if (!$colorsEnabled && !$colorsEnabledExplicit) { |
||
109 | return $text; |
||
110 | } |
||
111 | |||
112 | $color = $this->colors[$key]; |
||
113 | |||
114 | return sprintf("%s%s%s", $color['left'], $text, $color['right']); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Fetch a symbol by name |
||
119 | * |
||
120 | * @param $name |
||
121 | * @return string |
||
122 | */ |
||
123 | public function symbol($name) |
||
127 | |||
128 | /** |
||
129 | * Return the OutputInterface associated with the Reporter |
||
130 | * |
||
131 | * @return \Symfony\Component\Console\Output\OutputInterface |
||
132 | */ |
||
133 | public function getOutput() |
||
137 | |||
138 | /** |
||
139 | * Return the Configuration associated with the Reporter |
||
140 | * |
||
141 | * @return \Peridot\Configuration |
||
142 | */ |
||
143 | public function getConfiguration() |
||
147 | |||
148 | /** |
||
149 | * Set the run time to report. |
||
150 | * |
||
151 | * @param float $time |
||
152 | */ |
||
153 | public function setTime($time) |
||
154 | { |
||
155 | $this->time = $time; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get the run time to report. |
||
160 | * |
||
161 | * @return float |
||
162 | */ |
||
163 | public function getTime() |
||
167 | |||
168 | /** |
||
169 | * Output result footer |
||
170 | */ |
||
171 | public function footer() |
||
189 | |||
190 | /** |
||
191 | * Output a test failure. |
||
192 | * |
||
193 | * @param int $errorNumber |
||
194 | * @param TestInterface $test |
||
195 | * @param $exception - an exception like interface with ->getMessage(), ->getTrace() |
||
196 | */ |
||
197 | protected function outputError($errorNumber, TestInterface $test, $exception) |
||
209 | |||
210 | /** |
||
211 | * Output a stack trace. |
||
212 | * |
||
213 | * @param array $trace |
||
214 | */ |
||
215 | protected function outputTrace(array $trace) |
||
231 | |||
232 | /** |
||
233 | * Determine if colorized output is supported by the reporters output. |
||
234 | * Taken from Symfony's console output with some slight modifications |
||
235 | * to use the reporter's output stream |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | protected function hasColorSupport() |
||
247 | |||
248 | /** |
||
249 | * Register reporter symbols, additionally checking OS compatibility. |
||
250 | */ |
||
251 | protected function registerSymbols() |
||
258 | |||
259 | /** |
||
260 | * Return true if reporter is being used on windows |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | protected function isOnWindows() |
||
268 | |||
269 | /** |
||
270 | * Register events tracking state relevant to all reporters. |
||
271 | */ |
||
272 | private function registerEvents() |
||
288 | |||
289 | /** |
||
290 | * Determine if the terminal has ansicon support |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | private function hasAnsiSupport() |
||
298 | |||
299 | /** |
||
300 | * Determine if reporter is reporting to a tty terminal. |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | private function hasTty() |
||
316 | |||
317 | /** |
||
318 | * See if stream output is a tty terminal. |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | private function isTtyTerminal(StreamOutput $output) |
||
330 | |||
331 | /** |
||
332 | * Initialize reporter. Setup and listen for events |
||
333 | * |
||
334 | * @return void |
||
335 | */ |
||
336 | abstract public function init(); |
||
337 | |||
338 | private function renderTraceEntry($index, array $entry, $function) |
||
348 | } |
||
349 |