|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DF\PHPCoverFish\Common\Base; |
|
4
|
|
|
|
|
5
|
|
|
use DF\PHPCoverFish\CoverFishScanner; |
|
6
|
|
|
use DF\PHPCoverFish\Common\CoverFishHelper; |
|
7
|
|
|
use DF\PHPCoverFish\Common\CoverFishResult; |
|
8
|
|
|
use DF\PHPCoverFish\Common\CoverFishPHPUnitFile; |
|
9
|
|
|
use DF\PHPCoverFish\Common\CoverFishColor as Color; |
|
10
|
|
|
use DF\PHPCoverFish\Exception\CoverFishFailExit; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class BaseCoverFishOutput |
|
15
|
|
|
* |
|
16
|
|
|
* @package DF\PHPCoverFish |
|
17
|
|
|
* @author Patrick Paechnatz <[email protected]> |
|
18
|
|
|
* @copyright 2015 Patrick Paechnatz <[email protected]> |
|
19
|
|
|
* @license http://www.opensource.org/licenses/MIT |
|
20
|
|
|
* @link http://github.com/dunkelfrosch/phpcoverfish/tree |
|
21
|
|
|
* @since class available since Release 0.9.2 |
|
22
|
|
|
* @version 1.0.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class BaseCoverFishOutput |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @const MACRO_DETAIL_LINE_INDENT set line indent for detailed error message block |
|
28
|
|
|
*/ |
|
29
|
|
|
const MACRO_CONFIG_DETAIL_LINE_INDENT = 3; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @const MACRO_SKIPPED code for skipped/coverage missing testFunctions |
|
33
|
|
|
*/ |
|
34
|
|
|
const MACRO_SKIPPED = 0; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @const MACRO_PASS code for passed testFunctions |
|
38
|
|
|
*/ |
|
39
|
|
|
const MACRO_PASS = 1; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @const MACRO_FAILURE code for failing testFunctions |
|
43
|
|
|
*/ |
|
44
|
|
|
const MACRO_FAILURE = 2; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @const MACRO_FAILURE code for error/exception thrown testFunctions |
|
48
|
|
|
*/ |
|
49
|
|
|
const MACRO_ERROR = 3; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @const MACRO_WARNING code for warning in testFunctions |
|
53
|
|
|
*/ |
|
54
|
|
|
const MACRO_WARNING = 4; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @const FILE_PASS code for finally successfully closed single test file scan |
|
58
|
|
|
*/ |
|
59
|
|
|
const FILE_PASS = 10; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @const FILE_PASS code for tragically failed single test file scan exit |
|
63
|
|
|
*/ |
|
64
|
|
|
const FILE_FAILURE = 20; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var OutputInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $output; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var CoverFishScanner |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $scanner; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var CoverFishHelper |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $coverFishHelper; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var CoverFishResult |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $coverFishResult; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var CoverFishPHPUnitFile |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $coverFishUnitFile; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected $outputFormat; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @var int |
|
98
|
|
|
*/ |
|
99
|
|
|
protected $outputLevel; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var bool |
|
103
|
|
|
*/ |
|
104
|
|
|
protected $preventAnsiColors = false; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @var bool |
|
108
|
|
|
*/ |
|
109
|
|
|
protected $preventEcho = false; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @var bool |
|
113
|
|
|
*/ |
|
114
|
|
|
protected $outputFormatJson = false; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @var bool |
|
118
|
|
|
*/ |
|
119
|
|
|
protected $outputFormatText = true; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @var array |
|
123
|
|
|
*/ |
|
124
|
|
|
protected $jsonResult = array(); |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @var array |
|
128
|
|
|
*/ |
|
129
|
|
|
protected $jsonResults = array(); |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @var bool |
|
133
|
|
|
*/ |
|
134
|
|
|
protected $scanFailure; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* initializer for json result set in write progress method |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function resetJsonResult() |
|
140
|
|
|
{ |
|
141
|
|
|
$this->jsonResult['skipped'] = false; |
|
142
|
|
|
$this->jsonResult['pass'] = false; |
|
143
|
|
|
$this->jsonResult['failure'] = false; |
|
144
|
|
|
$this->jsonResult['error'] = false; |
|
145
|
|
|
$this->jsonResult['unknown'] = false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function writeLine($content) |
|
149
|
|
|
{ |
|
150
|
|
|
if (false === $this->outputFormatJson) { |
|
151
|
|
|
$this->output->writeln($content); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $content |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function write($content) |
|
159
|
|
|
{ |
|
160
|
|
|
if (false === $this->outputFormatJson) { |
|
161
|
|
|
$this->output->write($content); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @codeCoverageIgnore |
|
167
|
|
|
* |
|
168
|
|
|
* @param int $count |
|
169
|
|
|
* @param string $char |
|
170
|
|
|
* |
|
171
|
|
|
* @return null|string |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function setIndent($count, $char = ' ') |
|
174
|
|
|
{ |
|
175
|
|
|
$outChar = null; |
|
176
|
|
|
for ($i = 1; $i <= $count; $i++) { |
|
177
|
|
|
$outChar .= $char; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $outChar; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @codeCoverageIgnore |
|
185
|
|
|
* |
|
186
|
|
|
* @param CoverFishResult $coverFishResult |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function getScanFailPassStatistic(CoverFishResult $coverFishResult) |
|
191
|
|
|
{ |
|
192
|
|
|
$errorPercent = round($coverFishResult->getTestCount() / 100 * $coverFishResult->getFailureCount(), 2); |
|
193
|
|
|
$passPercent = 100 - $errorPercent; |
|
194
|
|
|
$errorStatistic = '%s %% failure rate%s%s %% pass rate%s'; |
|
195
|
|
|
$errorStatistic = sprintf($errorStatistic, |
|
196
|
|
|
$errorPercent, |
|
197
|
|
|
PHP_EOL, |
|
198
|
|
|
$passPercent, |
|
199
|
|
|
PHP_EOL |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
$scanResultMacro = '%s'; |
|
203
|
|
|
$scanResult = sprintf($scanResultMacro, |
|
204
|
|
|
(false === $this->preventAnsiColors) |
|
205
|
|
|
? Color::tplRedColor($errorStatistic) |
|
206
|
|
|
: $errorStatistic |
|
207
|
|
|
); |
|
208
|
|
|
|
|
209
|
|
|
return $scanResult; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param string $colorCode |
|
214
|
|
|
* @param string $statusMinimal |
|
215
|
|
|
* @param string $statusDetailed |
|
216
|
|
|
* @param null|string $streamMessage |
|
217
|
|
|
* |
|
218
|
|
|
* @return string |
|
219
|
|
|
* |
|
220
|
|
|
* @throws \Exception |
|
221
|
|
|
*/ |
|
222
|
|
|
private function getFileResultTemplate($colorCode, $statusMinimal, $statusDetailed, $streamMessage = null) |
|
223
|
|
|
{ |
|
224
|
|
|
$output = ($this->outputLevel > 1) |
|
225
|
|
|
? $statusDetailed |
|
226
|
|
|
: $statusMinimal; |
|
227
|
|
|
|
|
228
|
|
|
return sprintf('%s%s %s%s%s', |
|
229
|
|
|
($this->outputLevel > 1) |
|
230
|
|
|
? PHP_EOL |
|
231
|
|
|
: null |
|
232
|
|
|
, |
|
233
|
|
|
($this->outputLevel > 1) |
|
234
|
|
|
? '=>' |
|
235
|
|
|
: null |
|
236
|
|
|
, |
|
237
|
|
|
(false === $this->preventAnsiColors) |
|
238
|
|
|
? Color::setColor($colorCode, $output) |
|
239
|
|
|
: $output |
|
240
|
|
|
, |
|
241
|
|
|
($this->outputLevel > 1) |
|
242
|
|
|
? PHP_EOL |
|
243
|
|
|
: null |
|
244
|
|
|
, |
|
245
|
|
|
$streamMessage |
|
246
|
|
|
); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* main progress output rendering function |
|
251
|
|
|
* |
|
252
|
|
|
* @param int $status |
|
253
|
|
|
* @param string $message |
|
254
|
|
|
* |
|
255
|
|
|
* @return null |
|
256
|
|
|
*/ |
|
257
|
|
|
protected function writeFileResult($status, $message) |
|
258
|
|
|
{ |
|
259
|
|
|
$output = null; |
|
260
|
|
|
|
|
261
|
|
|
if (0 === $this->outputLevel) { |
|
262
|
|
|
return null; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
switch ($status) { |
|
266
|
|
|
case self::FILE_FAILURE: |
|
267
|
|
|
$output = $this->getFileResultTemplate('bg_red_fg_white', 'FAIL', 'file/test FAILURE', $message); |
|
268
|
|
|
break; |
|
269
|
|
|
|
|
270
|
|
|
case self::FILE_PASS: |
|
271
|
|
|
$output = $this->getFileResultTemplate('green', 'OK', 'file/test OK', $message); |
|
272
|
|
|
break; |
|
273
|
|
|
|
|
274
|
|
|
default: break; |
|
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
$this->writeLine($output); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @param string $colorCode |
|
282
|
|
|
* @param string $charMinimal |
|
283
|
|
|
* @param string $charDetailed |
|
284
|
|
|
* |
|
285
|
|
|
* @return string |
|
286
|
|
|
* |
|
287
|
|
|
* @throws \Exception |
|
288
|
|
|
*/ |
|
289
|
|
|
private function getProgressTemplate($colorCode, $charMinimal, $charDetailed) |
|
290
|
|
|
{ |
|
291
|
|
|
$output = ($this->outputLevel > 1) |
|
292
|
|
|
? $charDetailed // detailed output required? |
|
293
|
|
|
: $charMinimal // otherwise "normal" progress output will be provided |
|
294
|
|
|
; |
|
295
|
|
|
|
|
296
|
|
|
return (false === $this->preventAnsiColors) |
|
297
|
|
|
? $output = Color::setColor($colorCode, $output) |
|
298
|
|
|
: $output; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* main progress output rendering function |
|
303
|
|
|
* |
|
304
|
|
|
* @param int $status |
|
305
|
|
|
* |
|
306
|
|
|
* @return null |
|
307
|
|
|
*/ |
|
308
|
|
|
protected function writeProgress($status) |
|
309
|
|
|
{ |
|
310
|
|
|
$this->resetJsonResult(); |
|
311
|
|
|
|
|
312
|
|
|
switch ($status) { |
|
313
|
|
|
case self::MACRO_SKIPPED: |
|
314
|
|
|
$this->jsonResult['skipped'] = true; |
|
315
|
|
|
$output = $this->getProgressTemplate('green', '_', 'S'); |
|
316
|
|
|
|
|
317
|
|
|
break; |
|
318
|
|
|
|
|
319
|
|
|
case self::MACRO_PASS: |
|
320
|
|
|
$this->jsonResult['pass'] = true; |
|
321
|
|
|
$output = $this->getProgressTemplate('green', '.', '+'); |
|
322
|
|
|
|
|
323
|
|
|
break; |
|
324
|
|
|
|
|
325
|
|
|
case self::MACRO_FAILURE: |
|
326
|
|
|
$this->jsonResult['failure'] = true; |
|
327
|
|
|
$output = $this->getProgressTemplate('bg_red_fg_yellow', 'f', 'F'); |
|
328
|
|
|
|
|
329
|
|
|
break; |
|
330
|
|
|
|
|
331
|
|
|
case self::MACRO_ERROR: |
|
332
|
|
|
$this->jsonResult['error'] = true; |
|
333
|
|
|
$output = $this->getProgressTemplate('bg_red_fg_white', 'e', 'E'); |
|
334
|
|
|
|
|
335
|
|
|
break; |
|
336
|
|
|
|
|
337
|
|
|
case self::MACRO_WARNING: |
|
338
|
|
|
$this->jsonResult['warning'] = true; |
|
339
|
|
|
$output = $this->getProgressTemplate('bg_yellow_fg_black', 'w', 'W'); |
|
340
|
|
|
|
|
341
|
|
|
break; |
|
342
|
|
|
|
|
343
|
|
|
default: |
|
344
|
|
|
$this->jsonResult['unknown'] = true; |
|
345
|
|
|
$output = $output = $this->getProgressTemplate('bg_yellow_fg_black', '?', '?'); |
|
346
|
|
|
|
|
347
|
|
|
break; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
$this->write($output); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* write scan pass results |
|
355
|
|
|
* |
|
356
|
|
|
* @param CoverFishResult $coverFishResult |
|
357
|
|
|
*/ |
|
358
|
|
|
protected function writeScanPassStatistic(CoverFishResult $coverFishResult) |
|
359
|
|
|
{ |
|
360
|
|
|
$passStatistic = '%s file(s) and %s method(s) scanned, scan succeeded, no problems found.%s'; |
|
361
|
|
|
$passStatistic = sprintf($passStatistic, |
|
362
|
|
|
$coverFishResult->getFileCount(), |
|
363
|
|
|
$coverFishResult->getTestCount(), |
|
364
|
|
|
PHP_EOL |
|
365
|
|
|
); |
|
366
|
|
|
|
|
367
|
|
|
$scanResultMacro = '%s%s%s'; |
|
368
|
|
|
$scanResult = sprintf($scanResultMacro, |
|
369
|
|
|
($this->outputLevel === 0) |
|
370
|
|
|
? PHP_EOL |
|
371
|
|
|
: null, |
|
372
|
|
|
PHP_EOL, |
|
373
|
|
|
(false === $this->preventAnsiColors) |
|
374
|
|
|
? Color::tplGreenColor($passStatistic) |
|
375
|
|
|
: $passStatistic |
|
376
|
|
|
); |
|
377
|
|
|
|
|
378
|
|
|
$this->write($scanResult); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* write scan fail result |
|
383
|
|
|
* |
|
384
|
|
|
* @param CoverFishResult $coverFishResult |
|
385
|
|
|
* |
|
386
|
|
|
* @throws CoverFishFailExit |
|
387
|
|
|
*/ |
|
388
|
|
|
protected function writeScanFailStatistic(CoverFishResult $coverFishResult) |
|
389
|
|
|
{ |
|
390
|
|
|
$errorStatistic = '%s file(s) and %s method(s) scanned, coverage failed: %s cover annotation problem(s) found!%s'; |
|
391
|
|
|
$errorStatistic = sprintf($errorStatistic, |
|
392
|
|
|
$coverFishResult->getFileCount(), |
|
393
|
|
|
$coverFishResult->getTestCount(), |
|
394
|
|
|
$coverFishResult->getFailureCount(), |
|
395
|
|
|
PHP_EOL |
|
396
|
|
|
); |
|
397
|
|
|
|
|
398
|
|
|
$scanResultMacro = '%s%s%s%s'; |
|
399
|
|
|
$scanResult = sprintf($scanResultMacro, |
|
400
|
|
|
($this->outputLevel === 0) |
|
401
|
|
|
? PHP_EOL |
|
402
|
|
|
: null, |
|
403
|
|
|
PHP_EOL, |
|
404
|
|
|
(false === $this->preventAnsiColors) |
|
405
|
|
|
? Color::tplRedColor($errorStatistic) |
|
406
|
|
|
: $errorStatistic, |
|
407
|
|
|
$this->getScanFailPassStatistic($coverFishResult) |
|
408
|
|
|
); |
|
409
|
|
|
|
|
410
|
|
|
$this->write($scanResult); |
|
411
|
|
|
|
|
412
|
|
|
throw new CoverFishFailExit(); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* @param CoverFishPHPUnitFile $coverFishUnitFile |
|
417
|
|
|
* |
|
418
|
|
|
* @return null |
|
419
|
|
|
*/ |
|
420
|
|
|
protected function writeFileName(CoverFishPHPUnitFile $coverFishUnitFile) |
|
421
|
|
|
{ |
|
422
|
|
|
if (true === $this->outputFormatJson || 0 === $this->outputLevel) { |
|
423
|
|
|
return null; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
$file = $this->coverFishHelper->getFileNameFromPath($coverFishUnitFile->getFile()); |
|
427
|
|
|
$fileNameLine = sprintf('%s%s%s', |
|
428
|
|
|
(false === $this->preventAnsiColors) |
|
429
|
|
|
? Color::tplNormalColor(($this->outputLevel > 1) ? 'scan file ' : null) |
|
430
|
|
|
: 'scan file ' |
|
431
|
|
|
, |
|
432
|
|
|
(false === $this->preventAnsiColors) |
|
433
|
|
|
? Color::tplYellowColor($file) |
|
434
|
|
|
: $file |
|
435
|
|
|
, |
|
436
|
|
|
($this->outputLevel > 1) ? PHP_EOL : ' ' |
|
437
|
|
|
); |
|
438
|
|
|
|
|
439
|
|
|
$this->write($fileNameLine); |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
/** |
|
443
|
|
|
* handle scanner output by default/parametric output format settings |
|
444
|
|
|
* |
|
445
|
|
|
* @param CoverFishResult $coverFishResult |
|
446
|
|
|
* |
|
447
|
|
|
* @return null |
|
448
|
|
|
* |
|
449
|
|
|
* @throws CoverFishFailExit |
|
450
|
|
|
*/ |
|
451
|
|
|
protected function outputResult(CoverFishResult $coverFishResult) |
|
452
|
|
|
{ |
|
453
|
|
|
if (false === $this->outputFormatJson) { |
|
454
|
|
|
|
|
455
|
|
|
if ($coverFishResult->getFailureCount() > 0) { |
|
456
|
|
|
$this->writeScanFailStatistic($coverFishResult); |
|
457
|
|
|
} else { |
|
458
|
|
|
$this->writeScanPassStatistic($coverFishResult); |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
return null; |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
if (true === $this->preventEcho) { |
|
465
|
|
|
return json_encode($this->jsonResults); |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
$this->output->write(json_encode($this->jsonResults)); |
|
469
|
|
|
|
|
470
|
|
|
return null; |
|
471
|
|
|
} |
|
472
|
|
|
} |
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.