Completed
Push — master ( d308b8...b73d2a )
by Alessandro
08:43
created

TestResultContainer::getFileNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 34
     * @param TestResultFormat $testResultFormat
30
     */
31 34
    public function __construct(TestResultFormat $testResultFormat)
32 34
    {
33 34
        $this->testResultFormat = $testResultFormat;
34 34
        $this->filenames = array();
35
        $this->testResults = array();
36
    }
37
38
    /**
39
     * @param ProcessWithResultsInterface $process
40
     * @param TestResultInterface $testResult
41 25
     */
42
    public function handleTestResult(ProcessWithResultsInterface $process, TestResultInterface $testResult)
43 25
    {
44
        $this->addProcessToFilenames($process);
45 25
46 11
        if ($testResult instanceof TestResultWithAbnormalTermination && $process instanceof OutputAwareInterface) {
47 11
            $this->addProcessOutputToResult($testResult, $process);
48
        }
49 25
50 25
        if ($testResult instanceof PrintableTestResultInterface) {
51 25
            $testResult->setTestResultFormat($this->testResultFormat);
52
            $this->testResults[] = $testResult;
53 25
54 25
            $process->addTestResult($testResult);
55 25
        }
56
    }
57 25
58
    public function addProcessToFilenames(ProcessWithResultsInterface $process)
59
    {
60
        // trick for unique
61
        $this->filenames[$process->getUniqueId()] = $process->getFilename();
62
    }
63
64 11
    /**
65
     * @return TestResultFormat
66 11
     */
67 11
    public function getTestResultFormat()
68 11
    {
69 11
        return $this->testResultFormat;
70
    }
71
72
    /**
73
     * @return string[]
74 11
     */
75
    public function getFileNames()
76 11
    {
77
        return $this->filenames;
78
    }
79
80
    /**
81
     * @return PrintableTestResultInterface[]
82 10
     */
83
    public function getTestResults()
84 10
    {
85
        return $this->testResults;
86
    }
87
88
    /**
89
     * @return int
90
     */
91
    public function countTestResults()
92
    {
93
        return count($this->testResults);
94
    }
95
96
    /**
97
     * @param TestResultWithAbnormalTermination $result
98
     * @param OutputAwareInterface $process
99
     */
100
    private function addProcessOutputToResult(TestResultWithAbnormalTermination $result, OutputAwareInterface $process)
101
    {
102
        $tag = $this->testResultFormat->getTag();
103
        $output = $process->getOutput() ?: sprintf('<%s><[NO OUTPUT FOUND]></%s>', $tag, $tag);
104
        $result->setTestOutput($output);
105
    }
106
}
107