Skip to content

Commit f36e89b

Browse files
Eliminate code duplication
1 parent b9ee8a6 commit f36e89b

File tree

5 files changed

+149
-145
lines changed

5 files changed

+149
-145
lines changed

src/Logging/TestDox/TestResult/TestResultCollector.php

Lines changed: 10 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
use PHPUnit\Event\UnknownSubscriberTypeException;
4242
use PHPUnit\Framework\TestStatus\TestStatus;
4343
use PHPUnit\Logging\TestDox\TestResult as TestDoxTestMethod;
44-
use PHPUnit\TextUI\Configuration\Source;
45-
use PHPUnit\TextUI\Configuration\SourceFilter;
44+
use PHPUnit\TestRunner\IssueFilter;
4645
use ReflectionMethod;
4746

4847
/**
@@ -52,7 +51,7 @@
5251
*/
5352
final class TestResultCollector
5453
{
55-
private readonly Source $source;
54+
private readonly IssueFilter $issueFilter;
5655

5756
/**
5857
* @var array<string, list<TestDoxTestMethod>>
@@ -66,9 +65,9 @@ final class TestResultCollector
6665
* @throws EventFacadeIsSealedException
6766
* @throws UnknownSubscriberTypeException
6867
*/
69-
public function __construct(Facade $facade, Source $source)
68+
public function __construct(Facade $facade, IssueFilter $issueFilter)
7069
{
71-
$this->source = $source;
70+
$this->issueFilter = $issueFilter;
7271

7372
$this->registerSubscribers($facade);
7473
}
@@ -216,135 +215,79 @@ public function testConsideredRisky(ConsideredRisky $event): void
216215

217216
public function testTriggeredDeprecation(DeprecationTriggered $event): void
218217
{
219-
if (!$event->test()->isTestMethod()) {
220-
return;
221-
}
222-
223-
if ($event->ignoredByTest()) {
218+
if (!$this->issueFilter->shouldBeProcessed($event, true)) {
224219
return;
225220
}
226221

227222
if ($event->ignoredByBaseline()) {
228223
return;
229224
}
230225

231-
if (!$this->source->ignoreSuppressionOfDeprecations() && $event->wasSuppressed()) {
232-
return;
233-
}
234-
235-
if ($this->source->restrictDeprecations() && !(new SourceFilter)->includes($this->source, $event->file())) {
236-
return;
237-
}
238-
239226
$this->updateTestStatus(TestStatus::deprecation());
240227
}
241228

242229
public function testTriggeredNotice(NoticeTriggered $event): void
243230
{
244-
if (!$event->test()->isTestMethod()) {
231+
if (!$this->issueFilter->shouldBeProcessed($event, true)) {
245232
return;
246233
}
247234

248235
if ($event->ignoredByBaseline()) {
249236
return;
250237
}
251238

252-
if (!$this->source->ignoreSuppressionOfNotices() && $event->wasSuppressed()) {
253-
return;
254-
}
255-
256-
if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
257-
return;
258-
}
259-
260239
$this->updateTestStatus(TestStatus::notice());
261240
}
262241

263242
public function testTriggeredWarning(WarningTriggered $event): void
264243
{
265-
if (!$event->test()->isTestMethod()) {
244+
if (!$this->issueFilter->shouldBeProcessed($event, true)) {
266245
return;
267246
}
268247

269248
if ($event->ignoredByBaseline()) {
270249
return;
271250
}
272251

273-
if (!$this->source->ignoreSuppressionOfWarnings() && $event->wasSuppressed()) {
274-
return;
275-
}
276-
277-
if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
278-
return;
279-
}
280-
281252
$this->updateTestStatus(TestStatus::warning());
282253
}
283254

284255
public function testTriggeredPhpDeprecation(PhpDeprecationTriggered $event): void
285256
{
286-
if (!$event->test()->isTestMethod()) {
257+
if (!$this->issueFilter->shouldBeProcessed($event, true)) {
287258
return;
288259
}
289260

290261
if ($event->ignoredByTest()) {
291262
return;
292263
}
293264

294-
if ($event->ignoredByBaseline()) {
295-
return;
296-
}
297-
298-
if (!$this->source->ignoreSuppressionOfPhpDeprecations() && $event->wasSuppressed()) {
299-
return;
300-
}
301-
302-
if ($this->source->restrictDeprecations() && !(new SourceFilter)->includes($this->source, $event->file())) {
303-
return;
304-
}
305-
306265
$this->updateTestStatus(TestStatus::deprecation());
307266
}
308267

309268
public function testTriggeredPhpNotice(PhpNoticeTriggered $event): void
310269
{
311-
if (!$event->test()->isTestMethod()) {
270+
if (!$this->issueFilter->shouldBeProcessed($event, true)) {
312271
return;
313272
}
314273

315274
if ($event->ignoredByBaseline()) {
316275
return;
317276
}
318277

319-
if (!$this->source->ignoreSuppressionOfPhpNotices() && $event->wasSuppressed()) {
320-
return;
321-
}
322-
323-
if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
324-
return;
325-
}
326-
327278
$this->updateTestStatus(TestStatus::notice());
328279
}
329280

330281
public function testTriggeredPhpWarning(PhpWarningTriggered $event): void
331282
{
332-
if (!$event->test()->isTestMethod()) {
283+
if (!$this->issueFilter->shouldBeProcessed($event, true)) {
333284
return;
334285
}
335286

336287
if ($event->ignoredByBaseline()) {
337288
return;
338289
}
339290

340-
if (!$this->source->ignoreSuppressionOfPhpWarnings() && $event->wasSuppressed()) {
341-
return;
342-
}
343-
344-
if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
345-
return;
346-
}
347-
348291
$this->updateTestStatus(TestStatus::warning());
349292
}
350293

src/Runner/IssueFilter.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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\TestRunner;
11+
12+
use PHPUnit\Event\Test\DeprecationTriggered;
13+
use PHPUnit\Event\Test\ErrorTriggered;
14+
use PHPUnit\Event\Test\NoticeTriggered;
15+
use PHPUnit\Event\Test\PhpDeprecationTriggered;
16+
use PHPUnit\Event\Test\PhpNoticeTriggered;
17+
use PHPUnit\Event\Test\PhpWarningTriggered;
18+
use PHPUnit\Event\Test\WarningTriggered;
19+
use PHPUnit\TextUI\Configuration\Source;
20+
use PHPUnit\TextUI\Configuration\SourceFilter;
21+
22+
/**
23+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
24+
*
25+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
26+
*/
27+
final readonly class IssueFilter
28+
{
29+
private Source $source;
30+
31+
public function __construct(Source $source)
32+
{
33+
$this->source = $source;
34+
}
35+
36+
public function shouldBeProcessed(DeprecationTriggered|ErrorTriggered|NoticeTriggered|PhpDeprecationTriggered|PhpNoticeTriggered|PhpWarningTriggered|WarningTriggered $event, bool $onlyTestMethods = false): bool
37+
{
38+
if ($onlyTestMethods && !$event->test()->isTestMethod()) {
39+
return false;
40+
}
41+
42+
if ($event instanceof DeprecationTriggered || $event instanceof PhpDeprecationTriggered) {
43+
if ($event->ignoredByTest()) {
44+
return false;
45+
}
46+
47+
if ($this->source->ignoreSelfDeprecations() && $event->trigger()->isSelf()) {
48+
return false;
49+
}
50+
51+
if ($this->source->ignoreDirectDeprecations() && $event->trigger()->isDirect()) {
52+
return false;
53+
}
54+
55+
if ($this->source->ignoreIndirectDeprecations() && $event->trigger()->isIndirect()) {
56+
return false;
57+
}
58+
59+
if (!$this->source->ignoreSuppressionOfDeprecations() && $event->wasSuppressed()) {
60+
return false;
61+
}
62+
63+
if ($this->source->restrictDeprecations() && !(new SourceFilter)->includes($this->source, $event->file())) {
64+
return false;
65+
}
66+
}
67+
68+
if ($event instanceof NoticeTriggered) {
69+
if (!$this->source->ignoreSuppressionOfNotices() && $event->wasSuppressed()) {
70+
return false;
71+
}
72+
73+
if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
74+
return false;
75+
}
76+
}
77+
78+
if ($event instanceof PhpNoticeTriggered) {
79+
if (!$this->source->ignoreSuppressionOfPhpNotices() && $event->wasSuppressed()) {
80+
return false;
81+
}
82+
83+
if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
84+
return false;
85+
}
86+
}
87+
88+
if ($event instanceof WarningTriggered) {
89+
if (!$this->source->ignoreSuppressionOfWarnings() && $event->wasSuppressed()) {
90+
return false;
91+
}
92+
93+
if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
94+
return false;
95+
}
96+
}
97+
98+
if ($event instanceof PhpWarningTriggered) {
99+
if (!$this->source->ignoreSuppressionOfPhpWarnings() && $event->wasSuppressed()) {
100+
return false;
101+
}
102+
103+
if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
104+
return false;
105+
}
106+
}
107+
108+
if ($event instanceof ErrorTriggered) {
109+
if (!$this->source->ignoreSuppressionOfErrors() && $event->wasSuppressed()) {
110+
return false;
111+
}
112+
}
113+
114+
return true;
115+
}
116+
}

0 commit comments

Comments
 (0)