Test Failed
Push — stable ( 8a29e6...9af85c )
by Nuno
03:32
created

State::getTestCaseTitle()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 0
cts 8
cp 0
rs 9.4222
c 0
b 0
f 0
cc 5
nc 7
nop 0
crap 30
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 PHPUnit\Framework\TestCase;
15
use NunoMaduro\Collision\Contracts\Adapters\Phpunit\HasPrintableTestCaseName;
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
     * @param  string  $testCaseName
54
     */
55 3
    private function __construct(string $testCaseName)
56
    {
57 3
        $this->testCaseName = $testCaseName;
58 3
    }
59
60
    /**
61
     * Creates a new State starting from the given test case.
62
     *
63
     * @param  TestCase  $test
64
     *
65
     * @return self
66
     */
67 3
    public static function from(TestCase $test): self
68
    {
69 3
        return new self(self::getPrintableTestCaseName($test));
70
    }
71
72
    /**
73
     * Adds the given test to the State.
74
     *
75
     * @param  TestResult  $test
76
     *
77
     * @return void
78
     */
79
    public function add(TestResult $test): void
80
    {
81
        $this->testCaseTests[] = $test;
82
83
        $this->suiteTests[] = $test;
84
    }
85
86
    /**
87
     * Gets the test case title.
88
     *
89
     * @return string
90
     */
91 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...
92
    {
93
        foreach ($this->testCaseTests as $test) {
94
            if ($test->type === TestResult::FAIL) {
95
                return 'FAIL';
96
            }
97
        }
98
99
        foreach ($this->testCaseTests as $test) {
100
            if ($test->type !== TestResult::PASS) {
101
                return 'WARN';
102
            }
103
        }
104
105
        return 'PASS';
106
    }
107
108
    /**
109
     * Gets the test case title color.
110
     *
111
     * @return string
112
     */
113 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...
114
    {
115
        foreach ($this->testCaseTests as $test) {
116
            if ($test->type === TestResult::FAIL) {
117
                return 'red';
118
            }
119
        }
120
121
        foreach ($this->testCaseTests as $test) {
122
            if ($test->type !== TestResult::PASS) {
123
                return 'yellow';
124
            }
125
        }
126
127
        return 'green';
128
    }
129
130
    /**
131
     * Returns the number of tests on the current test case.
132
     *
133
     * @return int
134
     */
135
    public function testCaseTestsCount(): int
136
    {
137
        return count($this->testCaseTests);
138
    }
139
140
    /**
141
     * Returns the number of tests on the complete test suite.
142
     *
143
     * @return int
144
     */
145
    public function testSuiteTestsCount(): int
146
    {
147
        return count($this->suiteTests);
148
    }
149
150
    /**
151
     * Checks if the given test case is different from the current one.
152
     *
153
     * @param  TestCase  $testCase
154
     *
155
     * @return bool
156
     */
157
    public function testCaseHasChanged(TestCase $testCase): bool
158
    {
159
        return self::getPrintableTestCaseName($testCase) !== $this->testCaseName;
160
    }
161
162
    /**
163
     * Moves the a new test case.
164
     *
165
     * @param  TestCase  $testCase
166
     *
167
     * @return void
168
     */
169
    public function moveTo(TestCase $testCase): void
170
    {
171
        $this->testCaseName = self::getPrintableTestCaseName($testCase);
172
173
        $this->testCaseTests = [];
174
    }
175
176
    /**
177
     * Foreach test in the test case.
178
     *
179
     * @param  callable  $callback
180
     *
181
     * @return void
182
     */
183
    public function eachTestCaseTests(callable $callback): void
184
    {
185
        foreach ($this->testCaseTests as $test) {
186
            $callback($test);
187
        }
188
    }
189
190
    /**
191
     * @param  string  $type
192
     *
193
     * @return int
194
     */
195
    public function countTestsInTestSuiteBy(string $type): int
196
    {
197
        return count(array_filter($this->suiteTests, function (TestResult $testResult) use ($type) {
198
            return $testResult->type === $type;
199
        }));
200
    }
201
202
    /**
203
     * Checks if the given test already contains a result.
204
     *
205
     * @param  TestCase  $test
206
     *
207
     * @return bool
208
     */
209
    public function existsInTestCase(TestCase $test): bool
210
    {
211
        foreach ($this->testCaseTests as $testResult) {
212
            if (TestResult::makeDescription($test) === $testResult->description) {
213
                return true;
214
            }
215
        }
216
217
        return false;
218
    }
219
220
    /**
221
     * Returns the printable test case name from the given `TestCase`.
222
     */
223 3
    private static function getPrintableTestCaseName(TestCase $test): string
224
    {
225 3
        if ($test instanceof HasPrintableTestCaseName) {
226
            $name = $test->getPrintableTestCaseName();
227
        } else {
228 3
            $name = get_class($test);
229
        }
230
231 3
        return $name;
232
    }
233
}
234