Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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