Completed
Push — stable ( 8ba0c8...0d8cdb )
by Nuno
17:40 queued 15:16
created

State::testCaseTestsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
16
/**
17
 * @internal
18
 */
19
final class State
20
{
21
    /**
22
     * The complete test suite number of tests.
23
     *
24
     * @var int|null
25
     */
26
    public $suiteTotalTests;
27
28
    /**
29
     * The complete test suite tests.
30
     *
31
     * @var array<int, TestResult>
32
     */
33
    public $suiteTests = [];
34
35
    /**
36
     * The current test case class.
37
     *
38
     * @var string
39
     */
40
    public $testCaseClass;
41
42
    /**
43
     * The current test case tests.
44
     *
45
     * @var array<int, TestResult>
46
     */
47
    public $testCaseTests = [];
48
49
    /**
50
     * The state constructor.
51
     *
52
     * @param  string  $testCaseClass
53
     */
54 3
    private function __construct(string $testCaseClass)
55
    {
56 3
        $this->testCaseClass = $testCaseClass;
57 3
    }
58
59
    /**
60
     * Creates a new State starting from the given test case.
61
     *
62
     * @param  TestCase  $test
63
     *
64
     * @return self
65
     */
66 3
    public static function from(TestCase $test): self
67
    {
68 3
        return new self(get_class($test));
69
    }
70
71
    /**
72
     * Adds the given test to the State.
73
     *
74
     * @param  TestResult  $test
75
     *
76
     * @return void
77
     */
78
    public function add(TestResult $test): void
79
    {
80
        $this->testCaseTests[] = $test;
81
82
        $this->suiteTests[] = $test;
83
    }
84
85
    /**
86
     * Gets the test case title.
87
     *
88
     * @return string
89
     */
90 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...
91
    {
92
        foreach ($this->testCaseTests as $test) {
93
            if ($test->type === TestResult::FAIL) {
94
                return 'FAIL';
95
            }
96
97
            if ($test->type !== TestResult::PASS) {
98
                return 'WARN';
99
            }
100
        }
101
102
        return 'PASS';
103
    }
104
105
    /**
106
     * Gets the test case title color.
107
     *
108
     * @return string
109
     */
110 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...
111
    {
112
        foreach ($this->testCaseTests as $test) {
113
            if ($test->type === TestResult::FAIL) {
114
                return 'red';
115
            }
116
117
            if ($test->type !== TestResult::PASS) {
118
                return 'yellow';
119
            }
120
        }
121
122
        return 'green';
123
    }
124
125
    /**
126
     * Returns the number of tests on the current test case.
127
     *
128
     * @return int
129
     */
130
    public function testCaseTestsCount(): int
131
    {
132
        return count($this->testCaseTests);
133
    }
134
135
    /**
136
     * Returns the number of tests on the complete test suite.
137
     *
138
     * @return int
139
     */
140
    public function testSuiteTestsCount(): int
141
    {
142
        return count($this->suiteTests);
143
    }
144
145
    /**
146
     * Checks if the given test case is different from the current one.
147
     *
148
     * @param  TestCase  $testCase
149
     *
150
     * @return bool
151
     */
152
    public function testCaseHasChanged(TestCase $testCase): bool
153
    {
154
        return get_class($testCase) !== $this->testCaseClass;
155
    }
156
157
    /**
158
     * Moves the a new test case.
159
     *
160
     * @param  TestCase  $testCase
161
     *
162
     * @return void
163
     */
164
    public function moveTo(TestCase $testCase): void
165
    {
166
        $this->testCaseClass = get_class($testCase);
167
168
        $this->testCaseTests = [];
169
    }
170
171
    /**
172
     * Foreach test in the test case.
173
     *
174
     * @param  callable  $callback
175
     *
176
     * @return void
177
     */
178
    public function eachTestCaseTests(callable $callback): void
179
    {
180
        foreach ($this->testCaseTests as $test) {
181
            $callback($test);
182
        }
183
    }
184
185
    /**
186
     * @param  string  $type
187
     *
188
     * @return int
189
     */
190
    public function countTestsInTestSuiteBy(string $type): int
191
    {
192
        return count(array_filter($this->suiteTests, function (TestResult $testResult) use ($type) {
193
            return $testResult->type === $type;
194
        }));
195
    }
196
197
    /**
198
     * Checks if the given test already contains a result.
199
     *
200
     * @param  TestCase  $test
201
     *
202
     * @return bool
203
     */
204
    public function existsInTestCase(TestCase $test): bool
205
    {
206
        foreach ($this->testCaseTests as $testResult) {
207
            if (TestResult::makeDescription($test) === $testResult->description) {
208
                return true;
209
            }
210
        }
211
212
        return false;
213
    }
214
}
215