Completed
Push — master ( 6ceb86...83aeed )
by Alessandro
05:54
created

TestResultContainer::addProcessOutputToResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

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 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Paraunit\TestResult;
4
5
use Paraunit\Parser\JSONParserChainElementInterface;
6
use Paraunit\Process\OutputAwareInterface;
7
use Paraunit\Process\ProcessWithResultsInterface;
8
use Paraunit\TestResult\Interfaces\PrintableTestResultInterface;
9
use Paraunit\TestResult\Interfaces\TestResultContainerInterface;
10
use Paraunit\TestResult\Interfaces\TestResultInterface;
11
12
/**
13
 * Class TestResultContainer
14
 * @package Paraunit\TestResult
15
 */
16
class TestResultContainer implements TestResultContainerInterface, JSONParserChainElementInterface
17
{
18
    /** @var JSONParserChainElementInterface */
19
    private $parser;
20
21
    /** @var  TestResultFormat */
22
    private $testResultFormat;
23
24
    /** @var  PrintableTestResultInterface[] */
25
    private $testResults;
26
27
    /** @var  string[] */
28
    private $filenames;
29
30
    /**
31
     * TestResultContainer constructor.
32
     * @param JSONParserChainElementInterface $parser
33
     * @param TestResultFormat $testResultFormat
34
     */
35 32
    public function __construct(JSONParserChainElementInterface $parser, TestResultFormat $testResultFormat)
36
    {
37 32
        $this->parser = $parser;
38 32
        $this->testResultFormat = $testResultFormat;
39 32
        $this->testResults = array();
40 32
        $this->filenames = array();
41 32
    }
42
43
    /**
44
     * @param ProcessWithResultsInterface $process
45
     * @param \stdClass $logItem
46
     * @return null|TestResultInterface|PrintableTestResultInterface Returned when the chain needs to stop
47
     */
48 24
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
49
    {
50 24
        $result = $this->parser->handleLogItem($process, $logItem);
51
52 24
        if ($result instanceof TestResultWithAbnormalTermination && $process instanceof OutputAwareInterface) {
53 10
            $this->addProcessOutputToResult($result, $process);
54 10
        }
55
56 24
        if ($result instanceof PrintableTestResultInterface) {
57 24
            $result->setTestResultFormat($this->testResultFormat);
58 24
            $this->addTestResult($process, $result);
59 24
            $process->addTestResult($result);
60 24
        }
61
62 24
        return $result;
63
    }
64
65
    /**
66
     * @param TestResultWithAbnormalTermination $result
67
     * @param OutputAwareInterface $process
68
     */
69 10
    private function addProcessOutputToResult(TestResultWithAbnormalTermination $result, OutputAwareInterface $process)
70
    {
71 10
        $tag = $this->testResultFormat->getTag();
72 10
        $output = $process->getOutput() ?: sprintf('<%s><[NO OUTPUT FOUND]></%s>', $tag, $tag);
73 10
        $result->setTestOutput($output);
74 10
    }
75
76
    /**
77
     * @param ProcessWithResultsInterface $process
78
     * @param PrintableTestResultInterface $testResult
79
     */
80 24
    protected function addTestResult(ProcessWithResultsInterface $process, PrintableTestResultInterface $testResult)
81
    {
82 24
        $this->testResults[] = $testResult;
83
        // trick for unique
84 24
        $this->filenames[$process->getFilename()] = $process->getFilename();
85 24
    }
86
87
    /**
88
     * @return TestResultFormat
89
     */
90 18
    public function getTestResultFormat()
91
    {
92 18
        return $this->testResultFormat;
93
    }
94
95
    /**
96
     * @return PrintableTestResultInterface[]
97
     */
98 10
    public function getTestResults()
99
    {
100 10
        return $this->testResults;
101
    }
102
103
    /**
104
     * @return int
105
     */
106
    public function countFilenames()
107
    {
108
        return count($this->filenames);
109
    }
110
111
    /**
112
     * @return int
113
     */
114 10
    public function countTestResults()
115
    {
116 10
        return count($this->testResults);
117
    }
118
    /**
119
     * @return string[]
120
     */
121 10
    public function getFileNames()
122
    {
123 10
        return $this->filenames;
124
    }
125
}
126