State::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of Collision.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace NunoMaduro\Collision\Adapters\Phpunit;
13
14
use NunoMaduro\Collision\Contracts\Adapters\Phpunit\HasPrintableTestCaseName;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * @internal
19
 */
20
final class State
21
{
22
    /**
23
     * The complete test suite number of tests.
24
     *
25
     * @var int|null
26
     */
27
    public $suiteTotalTests;
28
29
    /**
30
     * The complete test suite tests.
31
     *
32
     * @var array<int, TestResult>
33
     */
34
    public $suiteTests = [];
35
36
    /**
37
     * The current test case class.
38
     *
39
     * @var string
40
     */
41
    public $testCaseName;
42
43
    /**
44
     * The current test case tests.
45
     *
46
     * @var array<int, TestResult>
47
     */
48
    public $testCaseTests = [];
49
50
    /**
51
     * The state constructor.
52
     */
53 3
    private function __construct(string $testCaseName)
54
    {
55 3
        $this->testCaseName = $testCaseName;
56 3
    }
57
58
    /**
59
     * Creates a new State starting from the given test case.
60
     */
61 3
    public static function from(TestCase $test): self
62
    {
63 3
        return new self(self::getPrintableTestCaseName($test));
64
    }
65
66
    /**
67
     * Adds the given test to the State.
68
     */
69
    public function add(TestResult $test): void
70
    {
71
        $this->testCaseTests[] = $test;
72
73
        $this->suiteTests[] = $test;
74
    }
75
76
    /**
77
     * Gets the test case title.
78
     */
79 View Code Duplication
    public function getTestCaseTitle(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        foreach ($this->testCaseTests as $test) {
82
            if ($test->type === TestResult::FAIL) {
83
                return 'FAIL';
84
            }
85
        }
86
87
        foreach ($this->testCaseTests as $test) {
88
            if ($test->type !== TestResult::PASS) {
89
                return 'WARN';
90
            }
91
        }
92
93
        return 'PASS';
94
    }
95
96
    /**
97
     * Gets the test case title color.
98
     */
99 View Code Duplication
    public function getTestCaseTitleColor(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        foreach ($this->testCaseTests as $test) {
102
            if ($test->type === TestResult::FAIL) {
103
                return 'red';
104
            }
105
        }
106
107
        foreach ($this->testCaseTests as $test) {
108
            if ($test->type !== TestResult::PASS) {
109
                return 'yellow';
110
            }
111
        }
112
113
        return 'green';
114
    }
115
116
    /**
117
     * Returns the number of tests on the current test case.
118
     */
119
    public function testCaseTestsCount(): int
120
    {
121
        return count($this->testCaseTests);
122
    }
123
124
    /**
125
     * Returns the number of tests on the complete test suite.
126
     */
127
    public function testSuiteTestsCount(): int
128
    {
129
        return count($this->suiteTests);
130
    }
131
132
    /**
133
     * Checks if the given test case is different from the current one.
134
     */
135
    public function testCaseHasChanged(TestCase $testCase): bool
136
    {
137
        return self::getPrintableTestCaseName($testCase) !== $this->testCaseName;
138
    }
139
140
    /**
141
     * Moves the a new test case.
142
     */
143
    public function moveTo(TestCase $testCase): void
144
    {
145
        $this->testCaseName = self::getPrintableTestCaseName($testCase);
146
147
        $this->testCaseTests = [];
148
    }
149
150
    /**
151
     * Foreach test in the test case.
152
     */
153
    public function eachTestCaseTests(callable $callback): void
154
    {
155
        foreach ($this->testCaseTests as $test) {
156
            $callback($test);
157
        }
158
    }
159
160
    public function countTestsInTestSuiteBy(string $type): int
161
    {
162
        return count(array_filter($this->suiteTests, function (TestResult $testResult) use ($type) {
163
            return $testResult->type === $type;
164
        }));
165
    }
166
167
    /**
168
     * Checks if the given test already contains a result.
169
     */
170
    public function existsInTestCase(TestCase $test): bool
171
    {
172
        foreach ($this->testCaseTests as $testResult) {
173
            if (TestResult::makeDescription($test) === $testResult->description) {
174
                return true;
175
            }
176
        }
177
178
        return false;
179
    }
180
181
    /**
182
     * Returns the printable test case name from the given `TestCase`.
183
     */
184 3
    private static function getPrintableTestCaseName(TestCase $test): string
185
    {
186 3
        if ($test instanceof HasPrintableTestCaseName) {
187
            $name = $test->getPrintableTestCaseName();
188
        } else {
189 3
            $name = get_class($test);
190
        }
191
192 3
        return $name;
193
    }
194
}
195