Completed
Pull Request — master (#94)
by Alessandro
05:58
created

FailuresPrinter::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Printer;
5
6
use Paraunit\Lifecycle\EngineEvent;
7
use Paraunit\TestResult\Interfaces\FailureMessageInterface;
8
use Paraunit\TestResult\Interfaces\FunctionNameInterface;
9
use Paraunit\TestResult\Interfaces\StackTraceInterface;
10
use Paraunit\TestResult\TestResultContainer;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
13
/**
14
 * Class FailuresPrinter
15
 * @package Paraunit\Printer
16
 */
17
class FailuresPrinter extends AbstractFinalPrinter implements EventSubscriberInterface
18
{
19 55
    public static function getSubscribedEvents()
20
    {
21
        return [
22 55
            EngineEvent::END => ['onEngineEnd', 200],
23
        ];
24
    }
25
26 15
    public function onEngineEnd()
27
    {
28 15
        foreach ($this->testResultList->getTestResultContainers() as $parser) {
29 15
            if ($parser instanceof TestResultContainer) {
30 15
                $this->printFailuresOutput($parser);
31
            }
32
        }
33
    }
34
35
    /**
36
     * @param TestResultContainer $testResultContainer
37
     */
38 15
    private function printFailuresOutput(TestResultContainer $testResultContainer)
39
    {
40 15
        if (! $testResultContainer->getTestResultFormat()->shouldPrintTestOutput()) {
41 15
            return;
42
        }
43
44 15
        $tag = $testResultContainer->getTestResultFormat()->getTag();
45 15
        $title = $testResultContainer->getTestResultFormat()->getTitle();
46 15
        $i = 1;
47
48 15
        foreach ($testResultContainer->getTestResults() as $testResult) {
49 10
            if ($i === 1) {
50 10
                $this->getOutput()->writeln('');
51 10
                $this->getOutput()->writeln(sprintf('<%s>%s output:</%s>', $tag, ucwords($title), $tag));
52
            }
53
54 10
            $this->getOutput()->writeln('');
55 10
            $this->getOutput()->write(sprintf('<%s>%d) ', $tag, $i++));
56
57 10
            if ($testResult instanceof FunctionNameInterface) {
58 10
                $this->getOutput()->writeln($testResult->getFunctionName());
59
            }
60
61 10
            $this->getOutput()->write(sprintf('</%s>', $tag));
62
63 10
            if ($testResult instanceof FailureMessageInterface) {
64 10
                $this->getOutput()->writeln($testResult->getFailureMessage());
65
            }
66
67 10
            if ($testResult instanceof StackTraceInterface) {
68 7
                foreach ($testResult->getTrace() as $traceStep) {
69 10
                    $this->getOutput()->writeln((string)$traceStep);
70
                }
71
            }
72
        }
73
    }
74
}
75