Skip to content

Commit 966ce2c

Browse files
committed
Introduce attribute #[IgnorePHPUnitWarnings]
1 parent f807247 commit 966ce2c

File tree

8 files changed

+234
-0
lines changed

8 files changed

+234
-0
lines changed

src/Event/Emitter/DispatchingEmitter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use PHPUnit\Event\TestSuite\Started as TestSuiteStarted;
2929
use PHPUnit\Event\TestSuite\TestSuite;
3030
use PHPUnit\Framework\TestCase;
31+
use PHPUnit\Metadata\IgnorePHPUnitWarnings;
32+
use PHPUnit\Metadata\Parser\Registry;
3133
use PHPUnit\TextUI\Configuration\Configuration;
3234
use SebastianBergmann\Comparator\Comparator;
3335

@@ -1008,6 +1010,16 @@ public function testTriggeredPhpunitError(Code\Test $test, string $message): voi
10081010
*/
10091011
public function testTriggeredPhpunitWarning(Code\Test $test, string $message): void
10101012
{
1013+
if (
1014+
$test->isTestMethod() &&
1015+
($metadata = Registry::parser()->forMethod($test->className(), $test->methodName())->isIgnoreWarnings())->isNotEmpty() &&
1016+
($ignoreWarnings = $metadata->asArray()[0] ?? null) !== null &&
1017+
$ignoreWarnings instanceof IgnorePHPUnitWarnings &&
1018+
$ignoreWarnings->shouldIgnore($message)
1019+
) {
1020+
return;
1021+
}
1022+
10111023
$this->dispatcher->dispatch(
10121024
new Test\PhpunitWarningTriggered(
10131025
$this->telemetryInfo(),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Framework\Attributes;
11+
12+
use Attribute;
13+
14+
/**
15+
* @immutable
16+
*
17+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
18+
*/
19+
#[Attribute(Attribute::TARGET_METHOD)]
20+
final readonly class IgnorePHPUnitWarnings
21+
{
22+
/** @var null|non-empty-string */
23+
private ?string $messagePattern;
24+
25+
/**
26+
* @param null|non-empty-string $messagePattern
27+
*/
28+
public function __construct(null|string $messagePattern = null)
29+
{
30+
$this->messagePattern = $messagePattern;
31+
}
32+
33+
/**
34+
* @return null|non-empty-string
35+
*/
36+
public function messagePattern(): ?string
37+
{
38+
return $this->messagePattern;
39+
}
40+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Metadata;
11+
12+
use function preg_match;
13+
14+
/**
15+
* @immutable
16+
*
17+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
18+
*/
19+
final readonly class IgnorePHPUnitWarnings extends Metadata
20+
{
21+
/** @var null|non-empty-string */
22+
private ?string $messagePattern;
23+
24+
/**
25+
* @param int<0, 1> $level
26+
* @param null|non-empty-string $messagePattern
27+
*/
28+
protected function __construct(int $level, null|string $messagePattern)
29+
{
30+
parent::__construct($level);
31+
32+
$this->messagePattern = $messagePattern;
33+
}
34+
35+
public function isIgnorePHPUnitWarnings(): true
36+
{
37+
return true;
38+
}
39+
40+
public function shouldIgnore(string $message): bool
41+
{
42+
return $this->messagePattern === null ||
43+
(bool) preg_match("/{$this->messagePattern}/", $message);
44+
}
45+
}

src/Metadata/Metadata.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,14 @@ public static function withoutErrorHandler(): WithoutErrorHandler
534534
return new WithoutErrorHandler(self::METHOD_LEVEL);
535535
}
536536

537+
/**
538+
* @param null|non-empty-string $messagePattern
539+
*/
540+
public static function ignorePHPUnitWarnings(?string $messagePattern): IgnorePHPUnitWarnings
541+
{
542+
return new IgnorePHPUnitWarnings(self::METHOD_LEVEL, $messagePattern);
543+
}
544+
537545
/**
538546
* @param int<0, 1> $level
539547
*/
@@ -969,4 +977,12 @@ public function isWithoutErrorHandler(): bool
969977
{
970978
return false;
971979
}
980+
981+
/**
982+
* @phpstan-assert-if-true IgnorePHPUnitWarnings $this
983+
*/
984+
public function isIgnorePHPUnitWarnings(): bool
985+
{
986+
return false;
987+
}
972988
}

src/Metadata/MetadataCollection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,4 +640,14 @@ public function isWithoutErrorHandler(): self
640640
),
641641
);
642642
}
643+
644+
public function isIgnoreWarnings(): self
645+
{
646+
return new self(
647+
...array_filter(
648+
$this->metadata,
649+
static fn (Metadata $metadata): bool => $metadata->isIgnorePHPUnitWarnings(),
650+
),
651+
);
652+
}
643653
}

src/Metadata/Parser/AttributeParser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use PHPUnit\Framework\Attributes\Group;
5353
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
5454
use PHPUnit\Framework\Attributes\IgnorePhpunitDeprecations;
55+
use PHPUnit\Framework\Attributes\IgnorePHPUnitWarnings;
5556
use PHPUnit\Framework\Attributes\Large;
5657
use PHPUnit\Framework\Attributes\Medium;
5758
use PHPUnit\Framework\Attributes\PostCondition;
@@ -882,6 +883,13 @@ public function forMethod(string $className, string $methodName): MetadataCollec
882883

883884
$result[] = Metadata::withoutErrorHandler();
884885

886+
break;
887+
888+
case IgnorePHPUnitWarnings::class:
889+
assert($attributeInstance instanceof IgnorePHPUnitWarnings);
890+
891+
$result[] = Metadata::ignorePHPUnitWarnings($attributeInstance->messagePattern());
892+
885893
break;
886894
}
887895
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\TestFixture\Event;
11+
12+
use PHPUnit\Event\Facade as EventFacade;
13+
use PHPUnit\Framework\Attributes\DataProvider;
14+
use PHPUnit\Framework\Attributes\IgnorePHPUnitWarnings;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class PhpunitWarningIgnoredTest extends TestCase
18+
{
19+
public static function dataProvider(): iterable
20+
{
21+
yield [true];
22+
}
23+
24+
#[IgnorePHPUnitWarnings]
25+
public function testPhpunitWarning(): void
26+
{
27+
EventFacade::emitter()->testTriggeredPhpunitWarning(
28+
$this->valueObjectForEvents(),
29+
'warning message',
30+
);
31+
32+
$this->assertTrue(true);
33+
}
34+
35+
#[IgnorePHPUnitWarnings('warning message')]
36+
public function testPhpunitWarningWithExactMessage(): void
37+
{
38+
EventFacade::emitter()->testTriggeredPhpunitWarning(
39+
$this->valueObjectForEvents(),
40+
'warning message',
41+
);
42+
43+
$this->assertTrue(true);
44+
}
45+
46+
#[IgnorePHPUnitWarnings('warn(.*)mess(.*)')]
47+
public function testPhpunitWarningWithRegex(): void
48+
{
49+
EventFacade::emitter()->testTriggeredPhpunitWarning(
50+
$this->valueObjectForEvents(),
51+
'warning message',
52+
);
53+
54+
$this->assertTrue(true);
55+
}
56+
57+
#[IgnorePHPUnitWarnings('warn(.*)mess(.*)')]
58+
public function testPhpunitWarningWithWrongPattern(): void
59+
{
60+
EventFacade::emitter()->testTriggeredPhpunitWarning(
61+
$this->valueObjectForEvents(),
62+
'another message',
63+
);
64+
65+
$this->assertTrue(true);
66+
}
67+
68+
#[DataProvider('dataProvider')]
69+
#[IgnorePHPUnitWarnings]
70+
public function testTooManyArgumentsInDataProvider(): void
71+
{
72+
$this->assertTrue(true);
73+
}
74+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
The right events are emitted in the right order for a test that runs code which triggers a PHPUnit warning
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/_files/PhpunitWarningIgnoredTest.php';
8+
9+
require __DIR__ . '/../../bootstrap.php';
10+
11+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
12+
--EXPECTF--
13+
PHPUnit %s by Sebastian Bergmann and contributors.
14+
15+
Runtime: %s
16+
17+
...W. 5 / 5 (100%)
18+
19+
Time: %s, Memory: %s
20+
21+
1 test triggered 1 PHPUnit warning:
22+
23+
1) PHPUnit\TestFixture\Event\PhpunitWarningIgnoredTest::testPhpunitWarningWithWrongPattern
24+
another message
25+
26+
%sPhpunitWarningIgnoredTest.php:%d
27+
28+
OK, but there were issues!
29+
Tests: 5, Assertions: 5, PHPUnit Warnings: 1.

0 commit comments

Comments
 (0)