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

FinalPrinter::analyzeProcess()   D

Complexity

Conditions 9
Paths 192

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 9.9324

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 39
ccs 24
cts 31
cp 0.7742
rs 4.6666
cc 9
eloc 23
nc 192
nop 1
crap 9.9324
1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Lifecycle\EngineEvent;
6
use Paraunit\Process\ProcessResultInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class FinalPrinter.
11
 */
12
class FinalPrinter
13
{
14
    /** @var OutputContainer[] */
15
    protected $outputContainers;
16
17
    /** @var OutputContainer */
18
    protected $segmentationFaults;
19
20
    /** @var OutputContainer */
21
    protected $unknownStatus;
22
23
    /** @var OutputContainer */
24
    protected $fatalErrors;
25
26
    /** @var OutputContainer */
27
    protected $errors;
28
29
    /** @var OutputContainer */
30
    protected $failures;
31
32
    /** @var OutputContainer */
33
    protected $warnings;
34
35
    /** @var OutputContainer */
36
    protected $skipped;
37
38
    /** @var OutputContainer */
39
    protected $incomplete;
40
41 7
    public function __construct()
42
    {
43 7
        $this->segmentationFaults = new OutputContainer('error', 'SEGMENTATION FAULTS');
44 7
        $this->unknownStatus = new OutputContainer('error', 'UNKNOWN STATUS');
45 7
        $this->fatalErrors = new OutputContainer('error', 'FATAL ERRORS');
46 7
        $this->errors = new OutputContainer('error', 'ERRORS');
47 7
        $this->failures = new OutputContainer('fail', 'FAILURES');
48 7
        $this->warnings = new OutputContainer('warning', 'WARNINGS');
49 7
        $this->skipped = new OutputContainer('skipped', 'SKIPPED');
50 7
        $this->incomplete = new OutputContainer('incomplete', 'INCOMPLETE');
51
52 7
        $this->outputContainers = array(
53 7
            $this->segmentationFaults,
54 7
            $this->unknownStatus,
55 7
            $this->fatalErrors,
56 7
            $this->errors,
57 7
            $this->failures,
58 7
            $this->warnings,
59 7
            $this->skipped,
60 7
            $this->incomplete,
61
        );
62 7
    }
63
64
    /**
65
     * @param EngineEvent $engineEvent
66
     */
67 7
    public function onEngineEnd(EngineEvent $engineEvent)
68
    {
69 7
        if (!$engineEvent->has('start') || !$engineEvent->has('end') || !$engineEvent->has('process_completed')) {
70
            throw new \BadMethodCallException('missing argument/s');
71
        }
72
73 7
        $outputInterface =  $engineEvent->getOutputInterface();
74 7
        $elapsedTime =  $engineEvent->get('start')->diff($engineEvent->get('end'));
75 7
        $completedProcesses =  $engineEvent->get('process_completed');
76
77 7
        $outputInterface->writeln('');
78 7
        $outputInterface->writeln('');
79 7
        $outputInterface->writeln($elapsedTime->format('Execution time -- %H:%I:%S '));
80
81 7
        $outputInterface->writeln('');
82 7
        $outputInterface->write('Executed: ');
83 7
        $outputInterface->write(count($completedProcesses).' test classes, ');
84
85 7
        $testsCount = 0;
86 7
        foreach ($completedProcesses as $process) {
87 7
            $this->analyzeProcess($process);
88 7
            $testsCount += count($process->getTestResults());
89 7
        }
90
91 7
        $outputInterface->writeln($testsCount.' tests');
92
93 7
        foreach ($this->outputContainers as $outputContainer) {
94 7
            $this->printFailuresOutput($outputInterface, $outputContainer);
95 7
        }
96
97 7
        foreach ($this->outputContainers as $outputContainer) {
98 7
            $this->printFileRecap($outputInterface, $outputContainer);
99 7
        }
100
101 7
        $outputInterface->writeln('');
102 7
    }
103
104
    /**
105
     * @param ProcessResultInterface $process
106
     */
107 7
    protected function analyzeProcess(ProcessResultInterface $process)
108
    {
109 7
        $filename = $process->getFilename();
110
111 7
        if ($process->hasSegmentationFaults()) {
112 1
            $this->segmentationFaults->addFileName($filename);
113 1
        }
114
115 7
        if ($process->hasFatalErrors()) {
116 1
            $this->fatalErrors->addFileName($filename);
117 1
            $this->fatalErrors->addToOutputBuffer($process->getFatalErrors());
118 7
        } elseif (in_array('X', $process->getTestResults())) {
119 1
            $this->unknownStatus->addFileName($filename);
120 1
            $this->unknownStatus->addToOutputBuffer($process->getOutput());
121 1
        }
122
123 7
        if ($process->hasErrors()) {
124 3
            $this->errors->addFileName($filename);
125 3
            $this->errors->addToOutputBuffer($process->getErrors());
126 3
        }
127
128 7
        if ($process->hasFailures()) {
129
            $this->failures->addFileName($filename);
130
            $this->failures->addToOutputBuffer($process->getFailures());
131
        }
132
133 7
        if ($process->hasWarnings()) {
134 1
            $this->warnings->addFileName($filename);
135 1
            $this->warnings->addToOutputBuffer($process->getWarnings());
136 1
        }
137
138 7
        if (in_array('S', $process->getTestResults())) {
139
            $this->skipped->addFileName($filename);
140
        }
141
142 7
        if (in_array('I', $process->getTestResults())) {
143
            $this->incomplete->addFileName($filename);
144
        }
145 7
    }
146
147
    /**
148
     * @param OutputInterface $outputInterface
149
     * @param OutputContainer $outputContainer
150
     */
151 7
    protected function printFileRecap(OutputInterface $outputInterface, OutputContainer $outputContainer)
152
    {
153 7
        $tag = $outputContainer->getTag();
154 7
        if ($outputContainer->count()) {
155 6
            $outputInterface->writeln('');
156 6
            $outputInterface->writeln(
157 6
                sprintf(
158 6
                    '<%s>%d files with %s:</%s>',
159 6
                    $tag,
160 6
                    $outputContainer->count(),
161 6
                    $outputContainer->getTitle(),
162
                    $tag
163 6
                )
164 6
            );
165
166 6
            foreach ($outputContainer->getFileNames() as $fileName) {
167 6
                $outputInterface->writeln(sprintf(' <%s>%s</%s>', $tag, $fileName, $tag));
168 6
            }
169 6
        }
170 7
    }
171
172
    /**
173
     * @param OutputInterface $outputInterface
174
     * @param OutputContainer $outputContainer
175
     */
176 7
    protected function printFailuresOutput(OutputInterface $outputInterface, OutputContainer $outputContainer)
177
    {
178 7
        $buffer = $outputContainer->getOutputBuffer();
179 7
        $tag = $outputContainer->getTag();
180 7
        if (count($buffer)) {
181 6
            $outputInterface->writeln('');
182 6
            $outputInterface->writeln(sprintf('<%s>%s output:</%s>', $tag, $outputContainer->getTitle(), $tag));
183
184 6
            $i = 1;
185
186 6
            foreach ($buffer as $line) {
187 6
                $outputInterface->writeln('');
188 6
                $outputInterface->writeln(
189 6
                    sprintf('<%s>%d)</%s> %s', $tag, $i++, $tag, $line)
190 6
                );
191 6
            }
192 6
        }
193 7
    }
194
}
195