Completed
Push — output_parsers_refactor ( b5c5df...ea70d9 )
by Alessandro
10:48
created

JSONLogParser::getParsersForPrinting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
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\Exception\RecoverableTestErrorException;
7
use Paraunit\Lifecycle\ProcessEvent;
8
use Paraunit\Printer\OutputContainerInterface;
9
use Paraunit\Process\ParaunitProcessAbstract;
10
use Paraunit\Process\ProcessResultInterface;
11
12
/**
13
 * Class JSONLogParser
14
 * @package Paraunit\Parser
15
 */
16
class JSONLogParser
17
{
18
    /** @var  JSONLogFetcher */
19
    private $logLocator;
20
21 10
    /** @var JSONParserChainElementInterface[] */
22
    private $parsers;
23 10
24 10
    /** @var  OutputContainerInterface */
25
    private $abnormalTerminatedOutputContainer;
26 10
27
    /**
28
     * JSONLogParser constructor.
29 10
     * @param JSONLogFetcher $logLocator
30 10
     * @param OutputContainerInterface $abnormalTerminatedOutputContainer
31 1
     */
32
    public function __construct(JSONLogFetcher $logLocator, OutputContainerInterface $abnormalTerminatedOutputContainer)
33
    {
34 9
        $this->logLocator = $logLocator;
35 9
        $this->abnormalTerminatedOutputContainer = $abnormalTerminatedOutputContainer;
36 9
        $this->parsers = array();
37
    }
38 9
39
    /**
40
     * @param JSONParserChainElementInterface $parser
41
     */
42
    public function addParser(JSONParserChainElementInterface $parser)
43
    {
44
        $this->parsers[] = $parser;
45 9
    }
46
47 9
    /**
48 9
     * @return JSONParserChainElementInterface[]
49 9
     */
50 9
    public function getParsersForPrinting()
51 9
    {
52 6
        return array_reverse($this->parsers);
53 1
    }
54 1
55 6
    /**
56 5
     * @return OutputContainerInterface
57 5
     */
58 1
    public function getAbnormalTerminatedOutputContainer()
59 1
    {
60 1
        return $this->abnormalTerminatedOutputContainer;
61
    }
62
63 9
    /**
64 9
     * @param ProcessEvent $processEvent
65 9
     */
66
    public function onProcessTerminated(ProcessEvent $processEvent)
67 5
    {
68
        $this->parse($processEvent->getProcess());
69 5
    }
70 5
71 1
    /**
72 4
     * @param ParaunitProcessAbstract $process
73 1
     */
74 3
    public function parse(ParaunitProcessAbstract $process)
75 1
    {
76 2
        try {
77 2
            $logs = $this->logLocator->fetch($process);
78 2
        } catch (JSONLogNotFoundException $exception) {
79
            $process->addTestResult('X');
80
            $process->reportAbnormalTerminationInFunction('Unknown function -- test log not found');
81
            $this->getAbnormalTerminatedOutputContainer()->addToOutputBuffer($process, $process->getOutput());
82
83
            return;
84
        }
85
86
        $expectingTestResult = false;
87
88
        foreach ($logs as $singleLog) {
89
            if ($singleLog->event == 'test') {
90
                $expectingTestResult = false;
91
                $this->extractTestResult($process, $singleLog);
92
            } else {
93
                $expectingTestResult = true;
94
            }
95
        }
96
97
        if ($expectingTestResult) {
98
            $process->addTestResult('X');
99
            $process->reportAbnormalTerminationInFunction($singleLog->test);
1 ignored issue
show
Bug introduced by
The variable $singleLog seems to be defined by a foreach iteration on line 88. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
100
            $this->getAbnormalTerminatedOutputContainer()->addToOutputBuffer($process, $process->getOutput());
101
        }
102
    }
103
104
    /**
105
     * @param ProcessResultInterface $process
106
     * @param \stdClass $logItem
107
     * @return bool False if the parsing is still waiting for a test to give results
108
     */
109
    private function extractTestResult(ProcessResultInterface $process, \stdClass $logItem)
110
    {
111
        foreach ($this->parsers as $parser) {
112
            if ($parser->parsingFoundResult($process, $logItem)) {
113
                return true;
114
            }
115
        }
116
117
        return false;
118
    }
119
}
120