Completed
Push — output_parsers_refactor ( d2cb12...a7c18a )
by Alessandro
03:46
created

JSONLogParser::getOutputContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Exception\JSONLogNotFoundException;
6
use Paraunit\Lifecycle\ProcessEvent;
7
use Paraunit\Printer\OutputContainerInterface;
8
use Paraunit\Process\ParaunitProcessAbstract;
9
use Paraunit\Process\ProcessResultInterface;
10
11
/**
12
 * Class JSONLogParser
13
 * @package Paraunit\Parser
14
 */
15
class JSONLogParser implements OutputContainerBearerInterface
16
{
17
    /** @var  JSONLogFetcher */
18
    private $logLocator;
19
20
    /** @var JSONParserChainElementInterface[] */
21
    private $parsers;
22
23
    /** @var  OutputContainerInterface */
24
    private $abnormalTerminatedOutputContainer;
25
26
    /**
27
     * JSONLogParser constructor.
28
     * @param JSONLogFetcher $logLocator
29
     * @param OutputContainerInterface $abnormalTerminatedOutputContainer
30
     */
31 21
    public function __construct(JSONLogFetcher $logLocator, OutputContainerInterface $abnormalTerminatedOutputContainer)
32
    {
33 21
        $this->logLocator = $logLocator;
34 21
        $this->abnormalTerminatedOutputContainer = $abnormalTerminatedOutputContainer;
35 21
        $this->parsers = array();
36 21
    }
37
38
    /**
39
     * @param JSONParserChainElementInterface $parser
40
     */
41 19
    public function addParser(JSONParserChainElementInterface $parser)
42
    {
43 19
        $this->parsers[] = $parser;
44 19
    }
45
46
    /**
47
     * @return JSONParserChainElementInterface[]
48
     */
49 10
    public function getParsersForPrinting()
50
    {
51 10
        return array_reverse($this->parsers);
52
    }
53
54
    /**
55
     * @return OutputContainerInterface
56
     */
57 8
    public function getOutputContainer()
58
    {
59 8
        return $this->getAbnormalTerminatedOutputContainer();
60
    }
61
62
    /**
63
     * @return OutputContainerInterface
64
     */
65 14
    public function getAbnormalTerminatedOutputContainer()
66
    {
67 14
        return $this->abnormalTerminatedOutputContainer;
68
    }
69
70
    /**
71
     * @param ProcessEvent $processEvent
72
     */
73 7
    public function onProcessTerminated(ProcessEvent $processEvent)
74
    {
75 7
        $this->parse($processEvent->getProcess());
76 7
    }
77
78
    /**
79
     * @param ParaunitProcessAbstract $process
80
     */
81 18
    public function parse(ParaunitProcessAbstract $process)
82
    {
83
        try {
84 18
            $logs = $this->logLocator->fetch($process);
85 18
        } catch (JSONLogNotFoundException $exception) {
86 1
            $this->reportAbnormalTermination($process);
87
88 1
            return;
89
        }
90
91 17
        $expectingTestResult = false;
92
93 17
        foreach ($logs as $singleLog) {
94 17
            if ($singleLog->event == 'test') {
95 14
                $expectingTestResult = false;
96 14
                $this->extractTestResult($process, $singleLog);
97 14
            } else {
98 17
                $expectingTestResult = true;
99
            }
100 17
        }
101
102 17
        if ($expectingTestResult) {
103 5
            $this->reportAbnormalTermination($process, $singleLog->test);
104 5
        }
105 17
    }
106
107
    /**
108
     * @param ProcessResultInterface $process
109
     * @param \stdClass $logItem
110
     * @return bool False if the parsing is still waiting for a test to give results
111
     */
112 14
    private function extractTestResult(ProcessResultInterface $process, \stdClass $logItem)
113
    {
114 14
        foreach ($this->parsers as $parser) {
115 14
            if ($parser->parsingFoundResult($process, $logItem)) {
116 14
                return true;
117
            }
118 10
        }
119
120
        return false;
121
    }
122
123
    /**
124
     * @param ParaunitProcessAbstract $process
125
     * @param string $culpableFunctionName
126
     */
127 6
    private function reportAbnormalTermination(ParaunitProcessAbstract $process, $culpableFunctionName = 'Unknown function -- test log not found')
128
    {
129 6
        $process->addTestResult('X');
130 6
        $process->reportAbnormalTermination();
131 6
        $this->getAbnormalTerminatedOutputContainer()->addToOutputBuffer(
132 6
            $process,
133 6
            'Culpable test function: ' . $culpableFunctionName . " -- complete test output:\n\n" . $process->getOutput()
134 6
        );
135 6
    }
136
}
137