Completed
Push — output_parsers_refactor ( 755930...b5c5df )
by Alessandro
07:17
created

JSONLogParser::onProcessTerminated()   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 1
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\Process\ParaunitProcessAbstract;
9
use Paraunit\Process\ProcessResultInterface;
10
11
/**
12
 * Class JSONLogParser
13
 * @package Paraunit\Parser
14
 */
15
class JSONLogParser
16
{
17
    /** @var  JSONLogFetcher */
18
    private $logLocator;
19
20
    /** @var JSONParserChainElementInterface[] */
21 10
    protected $parsers;
22
23 10
    /**
24 10
     * JSONLogParser constructor.
25
     * @param JSONLogFetcher $logLocator
26 10
     */
27
    public function __construct(JSONLogFetcher $logLocator)
28
    {
29 10
        $this->logLocator = $logLocator;
30 10
        $this->parsers = array();
31 1
    }
32
33
    /**
34 9
     * @param JSONParserChainElementInterface $parser
35 9
     */
36 9
    public function addParser(JSONParserChainElementInterface $parser)
37
    {
38 9
        $this->parsers[] = $parser;
39
    }
40
41
    /**
42
     * @return JSONParserChainElementInterface[]
43
     */
44
    public function getParsers()
45 9
    {
46
        return $this->parsers;
47 9
    }
48 9
49 9
    /**
50 9
     * @param ProcessEvent $processEvent
51 9
     */
52 6
    public function onProcessTerminated(ProcessEvent $processEvent)
53 1
    {
54 1
        $this->parse($processEvent->getProcess());
55 6
    }
56 5
57 5
    /**
58 1
     * @param ParaunitProcessAbstract $process
59 1
     */
60 1
    public function parse(ParaunitProcessAbstract $process)
61
    {
62
        try {
63 9
            $logs = $this->logLocator->fetch($process);
64 9
        } catch (JSONLogNotFoundException $exception) {
65 9
            $process->addTestResult('X');
66
            $process->reportAbnormalTerminationInFunction('Unknown function -- test log not found');
67 5
68
            return;
69 5
        }
70 5
71 1
        $expectingTestResult = false;
72 4
73 1
        foreach ($logs as $singleLog) {
74 3
            if ($singleLog->event == 'test') {
75 1
                $expectingTestResult = false;
76 2
                $this->extractTestResult($process, $singleLog);
77 2
            } else {
78 2
                $expectingTestResult = true;
79
            }
80
        }
81
82
        if ($expectingTestResult) {
83
            $process->addTestResult('X');
84
            $process->reportAbnormalTerminationInFunction($singleLog->test);
1 ignored issue
show
Bug introduced by
The variable $singleLog seems to be defined by a foreach iteration on line 73. 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...
85
        }
86
    }
87
88
    /**
89
     * @param ProcessResultInterface $process
90
     * @param \stdClass $logItem
91
     * @return bool False if the parsing is still waiting for a test to give results
92
     */
93
    private function extractTestResult(ProcessResultInterface $process, \stdClass $logItem)
94
    {
95
        foreach ($this->parsers as $parser) {
96
            if ($parser->parsingFoundResult($process, $logItem)) {
97
                return true;
98
            }
99
        }
100
101
        return false;
102
    }
103
}
104