Completed
Push — master ( 6a83c2...6c3c1b )
by Alessandro
04:16
created

ProcessPrinter::printSingleTestResult()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.0283

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 26
ccs 22
cts 24
cp 0.9167
rs 6.7272
cc 7
eloc 23
nc 7
nop 2
crap 7.0283
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 int */
15
    protected $counter = 0;
16
17
    /**
18
     * @param ProcessEvent $processEvent
19
     */
20 22
    public function onProcessTerminated(ProcessEvent $processEvent)
21
    {
22 22
        $process = $processEvent->getProcess();
23
24 22
        if ( ! $processEvent->has('output_interface')) {
25
            throw new \BadMethodCallException('missing output_interface');
26
        }
27
28 22
        $output = $processEvent->get('output_interface');
29
30 22
        if ( ! $output instanceof OutputInterface) {
31
            throw new \BadMethodCallException('output_interface, unexpected type: ' . get_class($output));
32
        }
33
34 22
        if ($process->isToBeRetried()) {
35 4
            $this->printWithCounter($output, '<ok>A</ok>');
36
37 4
            return;
38
        }
39
40 21
        if (0 == count($process->getTestResults())) {
41
            // TODO --- this operation should be done somewhere else!
42 5
            $process->setTestResults(array('X'));
43 5
        }
44
45 21
        foreach ($process->getTestResults() as $testResult) {
46 21
            $this->printSingleTestResult($output, $testResult);
47 21
        }
48 21
    }
49
50
    /**
51
     * @param OutputInterface $output
52
     * @param int $testResult
53
     */
54 21
    protected function printSingleTestResult(OutputInterface $output, $testResult)
55
    {
56
        switch ($testResult) {
57 21
            case 'E': 
58 4
                $this->printWithCounter($output, '<error>E</error>');
59 4
                break;
60 17
            case 'F':
61 7
                $this->printWithCounter($output, '<fail>F</fail>');
62 7
                break;
63 10
            case 'W':
64 1
                $this->printWithCounter($output, '<warning>W</warning>');
65 1
                break;
66 9
            case 'I':
67 1
                $this->printWithCounter($output, '<incomplete>I</incomplete>');
68 1
                break;
69 8
            case 'S':
70
                $this->printWithCounter($output, '<skipped>S</skipped>');
71
                break;
72 8
            case '.':
73 1
                $this->printWithCounter($output, '<ok>.</ok>');
74 1
                break;
75 7
            default:
76 7
                $this->printWithCounter($output, '<error>X</error>');
77 7
                break;
78 7
        }
79 21
    }
80
81
    /**
82
     * @param OutputInterface $output
83
     * @param string $string
84
     */
85 22
    protected function printWithCounter(OutputInterface $output, $string)
86
    {
87 22
        if ($this->counter % 80 == 0 && $this->counter > 1) {
88 4
            $output->writeln('');
89 4
        }
90
91 22
        ++$this->counter;
92
93 22
        $output->write($string);
94 22
    }
95
}
96