Completed
Push — master ( 2131d8...f75438 )
by Alessandro
08:33 queued 06:06
created

TestStartParser::saveProcessFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Paraunit\Parser\JSON;
4
5
use Paraunit\Process\ProcessWithResultsInterface;
6
use Paraunit\TestResult\NullTestResult;
7
8
/**
9
 * Class TestStartParser
10
 * @package Paraunit\Parser\JSON
11
 */
12
class TestStartParser implements ParserChainElementInterface
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 36
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
23
    {
24 36
        if (property_exists($logItem, 'status') && $logItem->status === LogFetcher::LOG_ENDING_STATUS) {
25 32
            return $this->handleLogTermination($process, $logItem);
26
        }
27
28 32
        if (property_exists($logItem, 'event')) {
29 32
            if ($logItem->event === 'testStart' || $logItem->event === 'suiteStart') {
30 29
                $process->setWaitingForTestResult(true);
31 29
                $this->saveProcessFunction($process, $logItem);
32 29
                $this->saveTestFQCN($process, $logItem);
33
34 29
                return new NullTestResult();
35
            }
36
        }
37
38 26
        return null;
39
    }
40
41
    /**
42
     * @param ProcessWithResultsInterface $process
43
     * @param \stdClass $logItem
44
     * @return null|NullTestResult
45
     */
46 32
    private function handleLogTermination(ProcessWithResultsInterface $process, \stdClass $logItem)
47
    {
48 32
        if ($process->isWaitingForTestResult()) {
49 14
            $this->injectLastFunctionInEndingLog($process, $logItem);
50
51 14
            return null;
52
        }
53
54 22
        return new NullTestResult();
55
    }
56
57
    /**
58
     * @param ProcessWithResultsInterface $process
59
     * @param \stdClass $logItem
60
     */
61 29
    private function saveProcessFunction(ProcessWithResultsInterface $process, \stdClass $logItem)
62
    {
63 29
        $this->lastProcess = $process;
64 29
        $this->lastFunction = property_exists($logItem, 'test') ? $logItem->test : self::UNKNOWN_FUNCTION;
65 29
    }
66
67
    /**
68
     * @param ProcessWithResultsInterface $process
69
     * @param \stdClass $logItem
70
     */
71 14
    private function injectLastFunctionInEndingLog(ProcessWithResultsInterface $process, \stdClass $logItem)
72
    {
73 14
        $logItem->test = $this->lastFunction;
74
75 14
        if ($this->lastFunction === null || $process !== $this->lastProcess) {
76 6
            $logItem->test = self::UNKNOWN_FUNCTION;
77
        }
78 14
    }
79
80 29
    private function saveTestFQCN(ProcessWithResultsInterface $process, \stdClass $logItem)
81
    {
82 29
        if ($process->getTestClassName()) {
83 22
            return;
84
        }
85
86 29
        if (! property_exists($logItem, 'suite')) {
87 4
            return;
88
        }
89
90 25
        if (! property_exists($logItem, 'test')) {
91 25
            return;
92
        }
93
94 25
        $suiteName = explode('::', $logItem->suite);
95 25
        $process->setTestClassName($suiteName[0]);
96 25
    }
97
}
98