Completed
Push — master ( 3f2b21...64f10e )
by Alessandro
06:51
created

TestResultContainer::addProcessToFilenames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Paraunit\TestResult;
4
5
use Paraunit\Process\OutputAwareInterface;
6
use Paraunit\Process\ProcessWithResultsInterface;
7
use Paraunit\TestResult\Interfaces\PrintableTestResultInterface;
8
use Paraunit\TestResult\Interfaces\TestResultContainerInterface;
9
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface;
10
use Paraunit\TestResult\Interfaces\TestResultInterface;
11
12
/**
13
 * Class TestResultContainer
14
 * @package Paraunit\TestResult
15
 */
16
class TestResultContainer implements TestResultContainerInterface, TestResultHandlerInterface
17
{
18
    /** @var  TestResultFormat */
19
    private $testResultFormat;
20
21
    /** @var  string[] */
22
    private $filenames;
23
24
    /** @var  PrintableTestResultInterface[] */
25
    private $testResults;
26
    
27
    /**
28
     * TestResultContainer constructor.
29
     * @param TestResultFormat $testResultFormat
30
     */
31 44
    public function __construct(TestResultFormat $testResultFormat)
32
    {
33 44
        $this->testResultFormat = $testResultFormat;
34 44
        $this->filenames = array();
35 44
        $this->testResults = array();
36 44
    }
37
38
    /**
39
     * @param ProcessWithResultsInterface $process
40
     * @param TestResultInterface $testResult
41
     */
42 27
    public function handleTestResult(ProcessWithResultsInterface $process, TestResultInterface $testResult)
43
    {
44 27
        $this->addProcessToFilenames($process);
45
46 27
        if ($testResult instanceof TestResultWithAbnormalTermination && $process instanceof OutputAwareInterface) {
47 12
            $this->addProcessOutputToResult($testResult, $process);
48 12
        }
49
50 27
        if ($testResult instanceof PrintableTestResultInterface) {
51 27
            $testResult->setTestResultFormat($this->testResultFormat);
52 27
            $this->testResults[] = $testResult;
53
54 27
            $process->addTestResult($testResult);
55 27
        }
56 27
    }
57
58 29
    public function addProcessToFilenames(ProcessWithResultsInterface $process)
59
    {
60
        // trick for unique
61 29
        $this->filenames[$process->getUniqueId()] = $process->getFilename();
62 29
    }
63
64
    /**
65
     * @return TestResultFormat
66
     */
67 21
    public function getTestResultFormat()
68
    {
69 21
        return $this->testResultFormat;
70
    }
71
72
    /**
73
     * @return string[]
74
     */
75 13
    public function getFileNames()
76
    {
77 13
        return $this->filenames;
78
    }
79
80
    /**
81
     * @return PrintableTestResultInterface[]
82
     */
83 12
    public function getTestResults()
84
    {
85 12
        return $this->testResults;
86
    }
87
88
    /**
89
     * @return int
90
     */
91 11
    public function countTestResults()
92
    {
93 11
        return count($this->testResults);
94
    }
95
96
    /**
97
     * @param TestResultWithAbnormalTermination $result
98
     * @param OutputAwareInterface $process
99
     */
100 12
    private function addProcessOutputToResult(TestResultWithAbnormalTermination $result, OutputAwareInterface $process)
101
    {
102 12
        $tag = $this->testResultFormat->getTag();
103 12
        $output = $process->getOutput() ?: sprintf('<%s><[NO OUTPUT FOUND]></%s>', $tag, $tag);
104 12
        $result->setTestOutput($output);
105 12
    }
106
}
107