Completed
Push — master ( 9a7b87...ad20a3 )
by Alessandro
03:07
created

TestResultContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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