Completed
Pull Request — master (#94)
by Alessandro
08:00
created

FinalPrinter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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