Completed
Pull Request — master (#37)
by Alessandro
02:23
created

TestResultContainer::addTestResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 29
    public function __construct(JSONParserChainElementInterface $parser, TestResultFormat $testResultFormat)
36
    {
37 29
        $this->parser = $parser;
38 29
        $this->testResultFormat = $testResultFormat;
39 29
        $this->testResults = array();
40 29
        $this->filenames = array();
41 29
    }
42
43
    /**
44
     * @param ProcessWithResultsInterface $process
45
     * @param \stdClass $logItem
46
     * @return null|TestResultInterface|PrintableTestResultInterface Returned when the chain needs to stop
47
     */
48 21
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
49
    {
50 21
        $result = $this->parser->handleLogItem($process, $logItem);
51
52 21
        if ($result instanceof TestResultWithAbnormalTermination && $process instanceof OutputAwareInterface) {
53 7
            $result->setTestOutput($process->getOutput());
54 7
        }
55
56 21
        if ($result instanceof PrintableTestResultInterface) {
57 21
            $result->setTestResultFormat($this->testResultFormat);
58 21
            $this->addTestResult($process, $result);
59 21
            $process->addTestResult($result);
60 21
        }
61
62 21
        return $result;
63
    }
64
65
    /**
66
     * @return TestResultFormat
67
     */
68 17
    public function getTestResultFormat()
69
    {
70 17
        return $this->testResultFormat;
71
    }
72
73
    /**
74
     * @return PrintableTestResultInterface[]
75
     */
76 9
    public function getTestResults()
77
    {
78 9
        return $this->testResults;
79
    }
80
81
    /**
82
     * @param ProcessWithResultsInterface $process
83
     * @param PrintableTestResultInterface $testResult
84
     */
85 21
    protected function addTestResult(ProcessWithResultsInterface $process, PrintableTestResultInterface $testResult)
86
    {
87 21
        $this->testResults[] = $testResult;
88
        // trick for unique
89 21
        $this->filenames[$process->getFilename()] = $process->getFilename();
90 21
    }
91
92
    /**
93
     * @return int
94
     * @todo test
95
     */
96
    public function countFilenames()
97
    {
98
        return count($this->filenames);
99
    }
100
101
    /**
102
     * @return int
103
     */
104 9
    public function countTestResults()
105
    {
106 9
        return count($this->testResults);
107
    }
108
109
    /**
110
     * @return string[]
111
     */
112 9
    public function getFileNames()
113
    {
114 9
        return $this->filenames;
115
    }
116
}
117