1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Paraunit\Printer; |
6
|
|
|
|
7
|
|
|
use Paraunit\Lifecycle\EngineEvent; |
8
|
|
|
use Paraunit\Lifecycle\ProcessEvent; |
9
|
|
|
use Paraunit\TestResult\Interfaces\TestResultContainerInterface; |
10
|
|
|
use Paraunit\TestResult\TestResultList; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
13
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
14
|
|
|
use Symfony\Component\Stopwatch\StopwatchEvent; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class FinalPrinter |
18
|
|
|
* @package Paraunit\Printer |
19
|
|
|
*/ |
20
|
|
|
class FinalPrinter extends AbstractFinalPrinter implements EventSubscriberInterface |
21
|
|
|
{ |
22
|
|
|
const STOPWATCH_NAME = 'engine'; |
23
|
|
|
|
24
|
|
|
/** @var Stopwatch */ |
25
|
|
|
private $stopWatch; |
26
|
|
|
|
27
|
|
|
/** @var int */ |
28
|
|
|
private $processCompleted; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
private $processRetried; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* FinalPrinter constructor. |
35
|
|
|
* @param TestResultList $testResultList |
36
|
|
|
* @param OutputInterface $output |
37
|
|
|
*/ |
38
|
26 |
|
public function __construct(TestResultList $testResultList, OutputInterface $output) |
39
|
|
|
{ |
40
|
26 |
|
parent::__construct($testResultList, $output); |
41
|
|
|
|
42
|
26 |
|
$this->stopWatch = new Stopwatch(); |
43
|
26 |
|
$this->processCompleted = 0; |
44
|
26 |
|
$this->processRetried = 0; |
45
|
|
|
} |
46
|
|
|
|
47
|
70 |
|
public static function getSubscribedEvents(): array |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
70 |
|
EngineEvent::START => 'onEngineStart', |
51
|
70 |
|
EngineEvent::END => ['onEngineEnd', 300], |
52
|
70 |
|
ProcessEvent::PROCESS_TERMINATED => 'onProcessTerminated', |
53
|
70 |
|
ProcessEvent::PROCESS_TO_BE_RETRIED => 'onProcessToBeRetried', |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
26 |
|
public function onEngineStart() |
58
|
|
|
{ |
59
|
26 |
|
$this->stopWatch->start(self::STOPWATCH_NAME); |
60
|
|
|
} |
61
|
|
|
|
62
|
26 |
|
public function onEngineEnd() |
63
|
|
|
{ |
64
|
26 |
|
$stopEvent = $this->stopWatch->stop(self::STOPWATCH_NAME); |
65
|
|
|
|
66
|
26 |
|
$this->printExecutionTime($stopEvent); |
67
|
26 |
|
$this->printTestCounters(); |
68
|
|
|
} |
69
|
|
|
|
70
|
25 |
|
public function onProcessTerminated() |
71
|
|
|
{ |
72
|
25 |
|
++$this->processCompleted; |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
public function onProcessToBeRetried() |
76
|
|
|
{ |
77
|
7 |
|
++$this->processRetried; |
78
|
|
|
} |
79
|
|
|
|
80
|
26 |
|
private function printExecutionTime(StopwatchEvent $stopEvent) |
81
|
|
|
{ |
82
|
26 |
|
$this->getOutput()->writeln(''); |
83
|
26 |
|
$this->getOutput()->writeln(''); |
84
|
26 |
|
$this->getOutput()->writeln('Execution time -- ' . gmdate('H:i:s', (int) ($stopEvent->getDuration() / 1000))); |
85
|
|
|
} |
86
|
|
|
|
87
|
26 |
|
private function printTestCounters() |
88
|
|
|
{ |
89
|
26 |
|
$testsCount = 0; |
90
|
26 |
|
foreach ($this->testResultList->getTestResultContainers() as $container) { |
91
|
26 |
|
if ($container instanceof TestResultContainerInterface) { |
92
|
26 |
|
$testsCount += $container->countTestResults(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
26 |
|
$this->getOutput()->writeln(''); |
97
|
26 |
|
$this->getOutput()->write(sprintf('Executed: %d test classes', $this->processCompleted - $this->processRetried)); |
98
|
26 |
|
if ($this->processRetried > 0) { |
99
|
7 |
|
$this->getOutput()->write(sprintf(' (%d retried)', $this->processRetried)); |
100
|
|
|
} |
101
|
26 |
|
$this->getOutput()->write(sprintf(', %d tests', $testsCount - $this->processRetried)); |
102
|
26 |
|
$this->getOutput()->writeln(''); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|