Completed
Push — output_parsers_refactor ( ea70d9...668933 )
by Alessandro
02:32
created

getAbnormalTerminatedOutputContainer()   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\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
    /** @var JSONParserChainElementInterface[] */
22
    private $parsers;
23
24
    /** @var  OutputContainerInterface */
25
    private $abnormalTerminatedOutputContainer;
26
27
    /**
28
     * JSONLogParser constructor.
29
     * @param JSONLogFetcher $logLocator
30
     * @param OutputContainerInterface $abnormalTerminatedOutputContainer
31
     */
32 20
    public function __construct(JSONLogFetcher $logLocator, OutputContainerInterface $abnormalTerminatedOutputContainer)
33
    {
34 20
        $this->logLocator = $logLocator;
35 20
        $this->abnormalTerminatedOutputContainer = $abnormalTerminatedOutputContainer;
36 20
        $this->parsers = array();
37 20
    }
38
39
    /**
40
     * @param JSONParserChainElementInterface $parser
41
     */
42 18
    public function addParser(JSONParserChainElementInterface $parser)
43
    {
44 18
        $this->parsers[] = $parser;
45 18
    }
46
47
    /**
48
     * @return JSONParserChainElementInterface[]
49
     */
50 9
    public function getParsersForPrinting()
51
    {
52 9
        return array_reverse($this->parsers);
53
    }
54
55
    /**
56
     * @return OutputContainerInterface
57
     */
58 13
    public function getAbnormalTerminatedOutputContainer()
59
    {
60 13
        return $this->abnormalTerminatedOutputContainer;
61
    }
62
63
    /**
64
     * @param ProcessEvent $processEvent
65
     */
66 7
    public function onProcessTerminated(ProcessEvent $processEvent)
67
    {
68 7
        $this->parse($processEvent->getProcess());
69 7
    }
70
71
    /**
72
     * @param ParaunitProcessAbstract $process
73
     */
74 18
    public function parse(ParaunitProcessAbstract $process)
75
    {
76
        try {
77 18
            $logs = $this->logLocator->fetch($process);
78 18
        } catch (JSONLogNotFoundException $exception) {
79 1
            $process->addTestResult('X');
80 1
            $process->reportAbnormalTerminationInFunction('Unknown function -- test log not found');
81 1
            $this->getAbnormalTerminatedOutputContainer()->addToOutputBuffer($process, $process->getOutput());
82
83 1
            return;
84
        }
85
86 17
        $expectingTestResult = false;
87
88 17
        foreach ($logs as $singleLog) {
89 17
            if ($singleLog->event == 'test') {
90 14
                $expectingTestResult = false;
91 14
                $this->extractTestResult($process, $singleLog);
92 14
            } else {
93 17
                $expectingTestResult = true;
94
            }
95 17
        }
96
97 17
        if ($expectingTestResult) {
98 5
            $process->addTestResult('X');
99 5
            $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 5
            $this->getAbnormalTerminatedOutputContainer()->addToOutputBuffer($process, $process->getOutput());
101 5
        }
102 17
    }
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 14
    private function extractTestResult(ProcessResultInterface $process, \stdClass $logItem)
110
    {
111 14
        foreach ($this->parsers as $parser) {
112 14
            if ($parser->parsingFoundResult($process, $logItem)) {
113 14
                return true;
114
            }
115 10
        }
116
117
        return false;
118
    }
119
}
120