Completed
Pull Request — master (#37)
by Alessandro
02:23
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
6
use Paraunit\Process\ProcessWithResultsInterface;
7
use Paraunit\TestResult\NullTestResult;
8
9
/**
10
 * Class TestStartParser
11
 * @package Paraunit\Parser
12
 */
13
class TestStartParser implements JSONParserChainElementInterface
14
{
15
    const UNKNOWN_FUNCTION = 'UNKNOWN -- log not found';
16
17
    /** @var ProcessWithResultsInterface */
18
    private $lastProcess;
19
20
    /** @var string */
21
    private $lastFunction;
22
23 28
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
24
    {
25 28
        if (property_exists($logItem, 'status') && $logItem->status == JSONLogFetcher::LOG_ENDING_STATUS) {
26 24
            return $this->handleLogTermination($process, $logItem);
27
        }
28
29 26
        if (property_exists($logItem, 'event')) {
30 26
            if ($logItem->event == 'testStart' || $logItem->event == 'suiteStart') {
31 23
                $process->setWaitingForTestResult(true);
32 23
                $this->saveProcessFunction($process, $logItem);
33
34 23
                return new NullTestResult();
35
            }
36 20
        }
37
38 20
        return null;
39
    }
40
41
    /**
42
     * @param ProcessWithResultsInterface $process
43
     * @param \stdClass $logItem
44
     * @return null|NullTestResult
45
     */
46 24
    private function handleLogTermination(ProcessWithResultsInterface $process, \stdClass $logItem)
47
    {
48 24
        if ($process->isWaitingForTestResult()) {
49 9
            $this->injectLastFunctionInEndingLog($process, $logItem);
50
51 9
            return null;
52
        }
53
54 16
        return new NullTestResult();
55
    }
56
57
    /**
58
     * @param ProcessWithResultsInterface $process
59
     * @param \stdClass $logItem
60
     */
61 23
    private function saveProcessFunction(ProcessWithResultsInterface $process, \stdClass $logItem)
62
    {
63 23
        $this->lastProcess = $process;
64 23
        $this->lastFunction = property_exists($logItem, 'test') ? $logItem->test : self::UNKNOWN_FUNCTION;
65 23
    }
66
67
    /**
68
     * @param ProcessWithResultsInterface $process
69
     * @param \stdClass $logItem
70
     */
71 9
    private function injectLastFunctionInEndingLog(ProcessWithResultsInterface $process, \stdClass $logItem)
72
    {
73 9
        $logItem->test = $this->lastFunction;
74
75 9
        if (is_null($this->lastFunction) || $process !== $this->lastProcess) {
76 2
            $logItem->test = self::UNKNOWN_FUNCTION;
77 2
        }
78 9
    }
79
}
80