Completed
Pull Request — master (#59)
by Alessandro
06:35
created

JSONLogParser::onProcessTerminated()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Lifecycle\ProcessEvent;
6
use Paraunit\Process\AbstractParaunitProcess;
7
use Paraunit\TestResult\Interfaces\TestFilenameBearerInterface;
8
use Paraunit\TestResult\Interfaces\TestResultBearerInterface;
9
use Paraunit\TestResult\Interfaces\TestResultInterface;
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
    private $parsers;
22
23
    /** @var TestFilenameBearerInterface */
24
    private $noTestExecutedResultContainer;
25
26
    /**
27
     * JSONLogParser constructor.
28
     * @param JSONLogFetcher $logLocator
29
     * @param TestFilenameBearerInterface $noTestExecutedResultContainer
30
     */
31 29
    public function __construct(JSONLogFetcher $logLocator, TestFilenameBearerInterface $noTestExecutedResultContainer)
32
    {
33 29
        $this->logLocator = $logLocator;
34 29
        $this->parsers = array();
35 29
        $this->noTestExecutedResultContainer = $noTestExecutedResultContainer;
36 29
    }
37
38
    /**
39
     * @param JSONParserChainElementInterface $container
40
     */
41 29
    public function addParser(JSONParserChainElementInterface $container)
42
    {
43 29
        $this->parsers[] = $container;
44 29
    }
45
46
    /**
47
     * @return TestResultBearerInterface[]
48
     */
49 11
    public function getParsers()
50
    {
51 11
        return $this->parsers;
52
    }
53
54
    /**
55
     * @return TestResultBearerInterface[]
56
     */
57 13
    public function getParsersForPrinting()
58
    {
59 13
        $reversedPrinters = array_reverse($this->parsers);
60 13
        array_unshift($reversedPrinters, $this->noTestExecutedResultContainer);
61
62 13
        return $reversedPrinters;
63
    }
64
65
    /**
66
     * @param ProcessEvent $processEvent
67
     */
68 27
    public function onProcessTerminated(ProcessEvent $processEvent)
69
    {
70 27
        $process = $processEvent->getProcess();
71 27
        $logs = $this->logLocator->fetch($process);
72
73 27
        if ($this->noTestsExecuted($process, $logs)) {
74 2
            $this->noTestExecutedResultContainer->addProcessToFilenames($process);
75
76 2
            return;
77
        }
78
79 25
        foreach ($logs as $singleLog) {
80 25
            $this->processLog($process, $singleLog);
81 25
        }
82 25
    }
83
84
    /**
85
     * @param AbstractParaunitProcess $process
86
     * @param \stdClass $logItem
87
     */
88 25
    private function processLog(AbstractParaunitProcess $process, \stdClass $logItem)
89
    {
90
        /** @var JSONParserChainElementInterface $resultContainer */
91 25
        foreach ($this->parsers as $resultContainer) {
92 25
            if ($resultContainer->handleLogItem($process, $logItem) instanceof TestResultInterface) {
93 25
                return;
94
            }
95 24
        }
96
    }
97
98
    /**
99
     * @param AbstractParaunitProcess $process
100
     * @param array $logs
101
     * @return bool
102
     */
103 27
    private function noTestsExecuted(AbstractParaunitProcess $process, array $logs)
104
    {
105 27
        return $process->getExitCode() === 0 && count($logs) === 1;
106
    }
107
}
108