TestResultContainer   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 83
ccs 28
cts 28
cp 1
rs 10

8 Methods

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