Completed
Pull Request — master (#94)
by Alessandro
03:19
created

FinalPrinter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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