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

TestStartParser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 13
c 5
b 0
f 0
lcom 1
cbo 2
dl 0
loc 67
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B handleLogItem() 0 17 6
A handleLogTermination() 0 10 2
A saveProcessFunction() 0 5 2
A injectLastFunctionInEndingLog() 0 8 3
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