Completed
Push — output_parsers_refactor ( ea70d9...668933 )
by Alessandro
02:32
created

ProcessPrinter::printSingleTestResult()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 8

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 29
ccs 27
cts 27
cp 1
rs 5.3846
cc 8
eloc 26
nc 8
nop 1
crap 8
1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Lifecycle\ProcessEvent;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/**
9
 * Class ProcessPrinter
10
 * @package Paraunit\Printer
11
 */
12
class ProcessPrinter
13
{
14
    /** @var  OutputInterface */
15
    private $output;
16
    
17
    /** @var int */
18
    private $counter = 0;
19
20
    /**
21
     * @param ProcessEvent $processEvent
22
     */
23 22
    public function onProcessTerminated(ProcessEvent $processEvent)
24
    {
25 22
        $process = $processEvent->getProcess();
26
27 22
        if ( ! $processEvent->has('output_interface')) {
28
            throw new \BadMethodCallException('missing output_interface');
29
        }
30
31 22
        $this->output = $processEvent->get('output_interface');
32
33 22
        if ( ! $this->output instanceof OutputInterface) {
34
            throw new \BadMethodCallException('output_interface, unexpected type: ' . get_class($this->output));
35
        }
36
37 22
        switch (true) {
38 22
            case $process->isToBeRetried():
39 4
                $this->printWithCounter('<ok>A</ok>');
40 4
                break;
41 21
            case $process->hasAbnormalTermination():
42 3
                $this->printWithCounter('<halted>X</halted>');
43 3
                break;
44 18
            default:
45 18
                foreach ($process->getTestResults() as $testResult) {
46 18
                    $this->printSingleTestResult($testResult);
47 18
                }
48 18
        }
49 22
    }
50
51
    /**
52
     * @param string $testResult
53
     */
54 18
    private function printSingleTestResult($testResult)
55
    {
56
        switch ($testResult) {
57 18
            case 'E':
58 4
                $this->printWithCounter('<error>E</error>');
59 4
                break;
60 14
            case 'F':
61 7
                $this->printWithCounter('<fail>F</fail>');
62 7
                break;
63 7
            case 'W':
64 2
                $this->printWithCounter('<warning>W</warning>');
65 2
                break;
66 5
            case 'I':
67 1
                $this->printWithCounter('<incomplete>I</incomplete>');
68 1
                break;
69 4
            case 'S':
70 1
                $this->printWithCounter('<skipped>S</skipped>');
71 1
                break;
72 3
            case 'R':
73 1
                $this->printWithCounter('<risky>R</risky>');
74 1
                break;
75 2
            case '.':
76 1
                $this->printWithCounter('<ok>.</ok>');
77 1
                break;
78 1
            default:
79 1
                $this->printWithCounter('<warning>?</warning>');
80 1
                break;
81 1
        }
82 18
    }
83
84
    /**
85
     * @param string $string
86
     */
87 22
    private function printWithCounter($string)
88
    {
89 22
        if ($this->counter % 80 == 0 && $this->counter > 1) {
90 4
            $this->output->writeln('');
91 4
        }
92
93 22
        ++$this->counter;
94
95 22
        $this->output->write($string);
96 22
    }
97
}
98