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

TestResultContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\TestResultBearerInterface;
10
use Paraunit\TestResult\Interfaces\TestResultInterface;
11
12
/**
13
 * Class TestResultContainer
14
 * @package Paraunit\TestResult
15
 */
16
class TestResultContainer extends DumbTestResultContainer implements TestResultBearerInterface, JSONParserChainElementInterface
17
{
18
    /** @var JSONParserChainElementInterface */
19
    private $parser;
20
21
    /** @var  PrintableTestResultInterface[] */
22
    private $testResults;
23
24
    /**
25
     * TestResultContainer constructor.
26
     * @param JSONParserChainElementInterface $parser
27
     * @param TestResultFormat $testResultFormat
28
     */
29 34
    public function __construct(JSONParserChainElementInterface $parser, TestResultFormat $testResultFormat)
30
    {
31 34
        parent::__construct($testResultFormat);
32 34
        $this->parser = $parser;
33 34
        $this->testResults = array();
34 34
    }
35
36
    /**
37
     * @param ProcessWithResultsInterface $process
38
     * @param \stdClass $logItem
39
     * @return null|TestResultInterface|PrintableTestResultInterface Returned when the chain needs to stop
40
     */
41 25
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
42
    {
43 25
        $result = $this->parser->handleLogItem($process, $logItem);
44
45 25
        if ($result instanceof TestResultWithAbnormalTermination && $process instanceof OutputAwareInterface) {
46 11
            $this->addProcessOutputToResult($result, $process);
47 11
        }
48
49 25
        if ($result instanceof PrintableTestResultInterface) {
50 25
            $result->setTestResultFormat($this->testResultFormat);
51 25
            $this->testResults[] = $result;
52
53 25
            $this->addProcessToFilenames($process);
54 25
            $process->addTestResult($result);
55 25
        }
56
57 25
        return $result;
58
    }
59
60
    /**
61
     * @param TestResultWithAbnormalTermination $result
62
     * @param OutputAwareInterface $process
63
     */
64 11
    private function addProcessOutputToResult(TestResultWithAbnormalTermination $result, OutputAwareInterface $process)
65
    {
66 11
        $tag = $this->testResultFormat->getTag();
67 11
        $output = $process->getOutput() ?: sprintf('<%s><[NO OUTPUT FOUND]></%s>', $tag, $tag);
68 11
        $result->setTestOutput($output);
69 11
    }
70
71
    /**
72
     * @return PrintableTestResultInterface[]
73
     */
74 11
    public function getTestResults()
75
    {
76 11
        return $this->testResults;
77
    }
78
79
    /**
80
     * @return int
81
     */
82 10
    public function countTestResults()
83
    {
84 10
        return count($this->testResults);
85
    }
86
}
87