Completed
Push — master ( 0e338e...cdc67b )
by Alessandro
09:41 queued 04:45
created

TestStartParser::handleLogTermination()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Process\ProcessWithResultsInterface;
6
use Paraunit\TestResult\NullTestResult;
7
8
/**
9
 * Class TestStartParser
10
 * @package Paraunit\Parser
11
 */
12
class TestStartParser implements JSONParserChainElementInterface
13
{
14
    const UNKNOWN_FUNCTION = 'UNKNOWN -- log not found';
15
16
    /** @var ProcessWithResultsInterface */
17
    private $lastProcess;
18
19
    /** @var string */
20
    private $lastFunction;
21
22 28
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
23
    {
24 28
        if (property_exists($logItem, 'status') && $logItem->status == JSONLogFetcher::LOG_ENDING_STATUS) {
25 24
            return $this->handleLogTermination($process, $logItem);
26
        }
27
28 26
        if (property_exists($logItem, 'event')) {
29 26
            if ($logItem->event == 'testStart' || $logItem->event == 'suiteStart') {
30 23
                $process->setWaitingForTestResult(true);
31 23
                $this->saveProcessFunction($process, $logItem);
32
33 23
                return new NullTestResult();
34
            }
35 20
        }
36
37 20
        return null;
38
    }
39
40
    /**
41
     * @param ProcessWithResultsInterface $process
42
     * @param \stdClass $logItem
43
     * @return null|NullTestResult
44
     */
45 24
    private function handleLogTermination(ProcessWithResultsInterface $process, \stdClass $logItem)
46
    {
47 24
        if ($process->isWaitingForTestResult()) {
48 9
            $this->injectLastFunctionInEndingLog($process, $logItem);
49
50 9
            return null;
51
        }
52
53 16
        return new NullTestResult();
54
    }
55
56
    /**
57
     * @param ProcessWithResultsInterface $process
58
     * @param \stdClass $logItem
59
     */
60 23
    private function saveProcessFunction(ProcessWithResultsInterface $process, \stdClass $logItem)
61
    {
62 23
        $this->lastProcess = $process;
63 23
        $this->lastFunction = property_exists($logItem, 'test') ? $logItem->test : self::UNKNOWN_FUNCTION;
64 23
    }
65
66
    /**
67
     * @param ProcessWithResultsInterface $process
68
     * @param \stdClass $logItem
69
     */
70 9
    private function injectLastFunctionInEndingLog(ProcessWithResultsInterface $process, \stdClass $logItem)
71
    {
72 9
        $logItem->test = $this->lastFunction;
73
74 9
        if (is_null($this->lastFunction) || $process !== $this->lastProcess) {
75 2
            $logItem->test = self::UNKNOWN_FUNCTION;
76 2
        }
77 9
    }
78
}
79