Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Util\TestDox;
11
 
12
use function get_class;
13
use function in_array;
14
use PHPUnit\Framework\AssertionFailedError;
15
use PHPUnit\Framework\ErrorTestCase;
16
use PHPUnit\Framework\Test;
17
use PHPUnit\Framework\TestCase;
18
use PHPUnit\Framework\TestSuite;
19
use PHPUnit\Framework\Warning;
20
use PHPUnit\Framework\WarningTestCase;
21
use PHPUnit\Runner\BaseTestRunner;
22
use PHPUnit\TextUI\ResultPrinter as ResultPrinterInterface;
23
use PHPUnit\Util\Printer;
24
use Throwable;
25
 
26
/**
27
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
28
 */
29
abstract class ResultPrinter extends Printer implements ResultPrinterInterface
30
{
31
    /**
32
     * @var NamePrettifier
33
     */
34
    protected $prettifier;
35
 
36
    /**
37
     * @var string
38
     */
39
    protected $testClass = '';
40
 
41
    /**
42
     * @var int
43
     */
44
    protected $testStatus;
45
 
46
    /**
47
     * @var array
48
     */
49
    protected $tests = [];
50
 
51
    /**
52
     * @var int
53
     */
54
    protected $successful = 0;
55
 
56
    /**
57
     * @var int
58
     */
59
    protected $warned = 0;
60
 
61
    /**
62
     * @var int
63
     */
64
    protected $failed = 0;
65
 
66
    /**
67
     * @var int
68
     */
69
    protected $risky = 0;
70
 
71
    /**
72
     * @var int
73
     */
74
    protected $skipped = 0;
75
 
76
    /**
77
     * @var int
78
     */
79
    protected $incomplete = 0;
80
 
81
    /**
82
     * @var null|string
83
     */
84
    protected $currentTestClassPrettified;
85
 
86
    /**
87
     * @var null|string
88
     */
89
    protected $currentTestMethodPrettified;
90
 
91
    /**
92
     * @var array
93
     */
94
    private $groups;
95
 
96
    /**
97
     * @var array
98
     */
99
    private $excludeGroups;
100
 
101
    /**
102
     * @param resource $out
103
     *
104
     * @throws \PHPUnit\Framework\Exception
105
     */
106
    public function __construct($out = null, array $groups = [], array $excludeGroups = [])
107
    {
108
        parent::__construct($out);
109
 
110
        $this->groups        = $groups;
111
        $this->excludeGroups = $excludeGroups;
112
 
113
        $this->prettifier = new NamePrettifier;
114
        $this->startRun();
115
    }
116
 
117
    /**
118
     * Flush buffer and close output.
119
     */
120
    public function flush(): void
121
    {
122
        $this->doEndClass();
123
        $this->endRun();
124
 
125
        parent::flush();
126
    }
127
 
128
    /**
129
     * An error occurred.
130
     */
131
    public function addError(Test $test, Throwable $t, float $time): void
132
    {
133
        if (!$this->isOfInterest($test)) {
134
            return;
135
        }
136
 
137
        $this->testStatus = BaseTestRunner::STATUS_ERROR;
138
        $this->failed++;
139
    }
140
 
141
    /**
142
     * A warning occurred.
143
     */
144
    public function addWarning(Test $test, Warning $e, float $time): void
145
    {
146
        if (!$this->isOfInterest($test)) {
147
            return;
148
        }
149
 
150
        $this->testStatus = BaseTestRunner::STATUS_WARNING;
151
        $this->warned++;
152
    }
153
 
154
    /**
155
     * A failure occurred.
156
     */
157
    public function addFailure(Test $test, AssertionFailedError $e, float $time): void
158
    {
159
        if (!$this->isOfInterest($test)) {
160
            return;
161
        }
162
 
163
        $this->testStatus = BaseTestRunner::STATUS_FAILURE;
164
        $this->failed++;
165
    }
166
 
167
    /**
168
     * Incomplete test.
169
     */
170
    public function addIncompleteTest(Test $test, Throwable $t, float $time): void
171
    {
172
        if (!$this->isOfInterest($test)) {
173
            return;
174
        }
175
 
176
        $this->testStatus = BaseTestRunner::STATUS_INCOMPLETE;
177
        $this->incomplete++;
178
    }
179
 
180
    /**
181
     * Risky test.
182
     */
183
    public function addRiskyTest(Test $test, Throwable $t, float $time): void
184
    {
185
        if (!$this->isOfInterest($test)) {
186
            return;
187
        }
188
 
189
        $this->testStatus = BaseTestRunner::STATUS_RISKY;
190
        $this->risky++;
191
    }
192
 
193
    /**
194
     * Skipped test.
195
     */
196
    public function addSkippedTest(Test $test, Throwable $t, float $time): void
197
    {
198
        if (!$this->isOfInterest($test)) {
199
            return;
200
        }
201
 
202
        $this->testStatus = BaseTestRunner::STATUS_SKIPPED;
203
        $this->skipped++;
204
    }
205
 
206
    /**
207
     * A testsuite started.
208
     */
209
    public function startTestSuite(TestSuite $suite): void
210
    {
211
    }
212
 
213
    /**
214
     * A testsuite ended.
215
     */
216
    public function endTestSuite(TestSuite $suite): void
217
    {
218
    }
219
 
220
    /**
221
     * A test started.
222
     *
223
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
224
     */
225
    public function startTest(Test $test): void
226
    {
227
        if (!$this->isOfInterest($test)) {
228
            return;
229
        }
230
 
231
        $class = get_class($test);
232
 
233
        if ($this->testClass !== $class) {
234
            if ($this->testClass !== '') {
235
                $this->doEndClass();
236
            }
237
 
238
            $this->currentTestClassPrettified = $this->prettifier->prettifyTestClass($class);
239
            $this->testClass                  = $class;
240
            $this->tests                      = [];
241
 
242
            $this->startClass($class);
243
        }
244
 
245
        if ($test instanceof TestCase) {
246
            $this->currentTestMethodPrettified = $this->prettifier->prettifyTestCase($test);
247
        }
248
 
249
        $this->testStatus = BaseTestRunner::STATUS_PASSED;
250
    }
251
 
252
    /**
253
     * A test ended.
254
     */
255
    public function endTest(Test $test, float $time): void
256
    {
257
        if (!$this->isOfInterest($test)) {
258
            return;
259
        }
260
 
261
        $this->tests[] = [$this->currentTestMethodPrettified, $this->testStatus];
262
 
263
        $this->currentTestClassPrettified  = null;
264
        $this->currentTestMethodPrettified = null;
265
    }
266
 
267
    protected function doEndClass(): void
268
    {
269
        foreach ($this->tests as $test) {
270
            $this->onTest($test[0], $test[1] === BaseTestRunner::STATUS_PASSED);
271
        }
272
 
273
        $this->endClass($this->testClass);
274
    }
275
 
276
    /**
277
     * Handler for 'start run' event.
278
     */
279
    protected function startRun(): void
280
    {
281
    }
282
 
283
    /**
284
     * Handler for 'start class' event.
285
     */
286
    protected function startClass(string $name): void
287
    {
288
    }
289
 
290
    /**
291
     * Handler for 'on test' event.
292
     */
293
    protected function onTest(string $name, bool $success = true): void
294
    {
295
    }
296
 
297
    /**
298
     * Handler for 'end class' event.
299
     */
300
    protected function endClass(string $name): void
301
    {
302
    }
303
 
304
    /**
305
     * Handler for 'end run' event.
306
     */
307
    protected function endRun(): void
308
    {
309
    }
310
 
311
    private function isOfInterest(Test $test): bool
312
    {
313
        if (!$test instanceof TestCase) {
314
            return false;
315
        }
316
 
317
        if ($test instanceof ErrorTestCase || $test instanceof WarningTestCase) {
318
            return false;
319
        }
320
 
321
        if (!empty($this->groups)) {
322
            foreach ($test->getGroups() as $group) {
323
                if (in_array($group, $this->groups, true)) {
324
                    return true;
325
                }
326
            }
327
 
328
            return false;
329
        }
330
 
331
        if (!empty($this->excludeGroups)) {
332
            foreach ($test->getGroups() as $group) {
333
                if (in_array($group, $this->excludeGroups, true)) {
334
                    return false;
335
                }
336
            }
337
 
338
            return true;
339
        }
340
 
341
        return true;
342
    }
343
}