Completed
Pull Request — master (#93)
by Alessandro
03:14
created

ProcessPrinter::onProcessTerminated()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 9
cp 0.7778
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4.1755
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Printer;
5
6
use Paraunit\Lifecycle\ProcessEvent;
7
use Paraunit\TestResult\Interfaces\PrintableTestResultInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class ProcessPrinter
12
 * @package Paraunit\Printer
13
 */
14
class ProcessPrinter
15
{
16
    /** @var  SingleResultFormatter */
17
    private $singleResultFormatter;
18
19
    /** @var  OutputInterface */
20
    private $output;
21
22
    /** @var int */
23
    private $counter = 0;
24
25
    /**
26
     * ProcessPrinter constructor.
27
     * @param SingleResultFormatter $singleResultFormatter
28
     */
29 23
    public function __construct(SingleResultFormatter $singleResultFormatter)
30
    {
31 23
        $this->singleResultFormatter = $singleResultFormatter;
32
    }
33
34
    /**
35
     * @param ProcessEvent $processEvent
36
     * @throws \BadMethodCallException
37
     */
38 23
    public function onProcessTerminated(ProcessEvent $processEvent)
39
    {
40 23
        $process = $processEvent->getProcess();
41
42 23
        if (! $processEvent->has('output_interface')) {
43
            throw new \BadMethodCallException('missing output_interface');
44
        }
45
46 23
        $this->output = $processEvent->get('output_interface');
47
48 23
        if (! $this->output instanceof OutputInterface) {
49
            throw new \BadMethodCallException('output_interface, unexpected type: ' . get_class($this->output));
50
        }
51
52 23
        foreach ($process->getTestResults() as $testResult) {
53 21
            $this->printFormattedWithCounter($testResult);
54
        }
55
    }
56
57
    /**
58
     * @param PrintableTestResultInterface $testResult
59
     */
60 21
    private function printFormattedWithCounter(PrintableTestResultInterface $testResult)
61
    {
62 21
        if ($this->counter % 80 === 0 && $this->counter > 1) {
63 4
            $this->output->writeln('');
64
        }
65
66 21
        ++$this->counter;
67
68 21
        $this->output->write(
69 21
            $this->singleResultFormatter->formatSingleResult($testResult)
70
        );
71
    }
72
}
73