Skip to content

Commit 82e35f3

Browse files
committed
Add regex pattern to #[IgnoreDeprecations]
1 parent 95ef9ef commit 82e35f3

File tree

8 files changed

+198
-7
lines changed

8 files changed

+198
-7
lines changed

src/Framework/Attributes/IgnoreDeprecations.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,22 @@
1919
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
2020
final readonly class IgnoreDeprecations
2121
{
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+
}
2240
}

src/Metadata/IgnoreDeprecations.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,37 @@
99
*/
1010
namespace PHPUnit\Metadata;
1111

12+
use function preg_match;
13+
1214
/**
1315
* @immutable
1416
*
1517
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
1618
*/
1719
final readonly class IgnoreDeprecations extends Metadata
1820
{
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+
1935
public function isIgnoreDeprecations(): true
2036
{
2137
return true;
2238
}
39+
40+
public function shouldIgnore(string $message): bool
41+
{
42+
return $this->messagePattern === null ||
43+
(bool) preg_match('{' . $this->messagePattern . '}', $message);
44+
}
2345
}

src/Metadata/Metadata.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,20 @@ public static function groupOnMethod(string $groupName): Group
225225
return new Group(self::METHOD_LEVEL, $groupName);
226226
}
227227

228-
public static function ignoreDeprecationsOnClass(): IgnoreDeprecations
228+
/**
229+
* @param null|non-empty-string $messagePattern
230+
*/
231+
public static function ignoreDeprecationsOnClass(?string $messagePattern = null): IgnoreDeprecations
229232
{
230-
return new IgnoreDeprecations(self::CLASS_LEVEL);
233+
return new IgnoreDeprecations(self::CLASS_LEVEL, $messagePattern);
231234
}
232235

233-
public static function ignoreDeprecationsOnMethod(): IgnoreDeprecations
236+
/**
237+
* @param null|non-empty-string $messagePattern
238+
*/
239+
public static function ignoreDeprecationsOnMethod(?string $messagePattern = null): IgnoreDeprecations
234240
{
235-
return new IgnoreDeprecations(self::METHOD_LEVEL);
241+
return new IgnoreDeprecations(self::METHOD_LEVEL, $messagePattern);
236242
}
237243

238244
/**

src/Metadata/Parser/AttributeParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function forClass(string $className): MetadataCollection
296296
case IgnoreDeprecations::class:
297297
assert($attributeInstance instanceof IgnoreDeprecations);
298298

299-
$result[] = Metadata::ignoreDeprecationsOnClass();
299+
$result[] = Metadata::ignoreDeprecationsOnClass($attributeInstance->messagePattern());
300300

301301
break;
302302

@@ -698,7 +698,7 @@ public function forMethod(string $className, string $methodName): MetadataCollec
698698
case IgnoreDeprecations::class:
699699
assert($attributeInstance instanceof IgnoreDeprecations);
700700

701-
$result[] = Metadata::ignoreDeprecationsOnMethod();
701+
$result[] = Metadata::ignoreDeprecationsOnMethod($attributeInstance->messagePattern());
702702

703703
break;
704704

src/Runner/ErrorHandler.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use const E_WARNING;
2727
use function array_keys;
2828
use function array_values;
29+
use function assert;
2930
use function debug_backtrace;
3031
use function defined;
3132
use function error_reporting;
@@ -37,6 +38,7 @@
3738
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
3839
use PHPUnit\Event\Code\TestMethod;
3940
use PHPUnit\Framework\TestCase;
41+
use PHPUnit\Metadata\IgnoreDeprecations;
4042
use PHPUnit\Runner\Baseline\Baseline;
4143
use PHPUnit\Runner\Baseline\Issue;
4244
use PHPUnit\TextUI\Configuration\Registry;
@@ -114,7 +116,7 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
114116
}
115117

116118
$ignoredByBaseline = $this->ignoredByBaseline($errorFile, $errorLine, $errorString);
117-
$ignoredByTest = $test->metadata()->isIgnoreDeprecations()->isNotEmpty();
119+
$ignoredByTest = $this->deprecationIgnoredByTest($test, $errorString);
118120

119121
switch ($errorNumber) {
120122
case E_NOTICE:
@@ -488,4 +490,19 @@ private function testCaseContext(string $className, string $methodName): string
488490
{
489491
return "{$className}::{$methodName}";
490492
}
493+
494+
private function deprecationIgnoredByTest(TestMethod $test, string $message): bool
495+
{
496+
$metadata = \PHPUnit\Metadata\Parser\Registry::parser()->forClassAndMethod($test->className(), $test->methodName())->isIgnoreDeprecations()->asArray();
497+
498+
foreach ($metadata as $metadatum) {
499+
assert($metadatum instanceof IgnoreDeprecations);
500+
501+
if ($metadatum->shouldIgnore($message)) {
502+
return true;
503+
}
504+
}
505+
506+
return false;
507+
}
491508
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 const E_USER_DEPRECATED;
13+
use function trigger_error;
14+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
15+
use PHPUnit\Framework\TestCase;
16+
17+
#[IgnoreDeprecations('foo')]
18+
final class IgnoreDeprecationsWithPatternTest extends TestCase
19+
{
20+
#[IgnoreDeprecations('bar')]
21+
public function testOne(): void
22+
{
23+
trigger_error('foo', E_USER_DEPRECATED);
24+
trigger_error('bar', E_USER_DEPRECATED);
25+
26+
$this->assertTrue(true);
27+
}
28+
29+
public function testTwo(): void
30+
{
31+
trigger_error('foo', E_USER_DEPRECATED);
32+
33+
$this->assertTrue(true);
34+
}
35+
36+
public function testThree(): void
37+
{
38+
trigger_error('foo', E_USER_DEPRECATED);
39+
trigger_error('baz', E_USER_DEPRECATED);
40+
41+
$this->assertTrue(true);
42+
}
43+
44+
#[IgnoreDeprecations('bar|baz')]
45+
public function testFour(): void
46+
{
47+
trigger_error('foo', E_USER_DEPRECATED);
48+
trigger_error('bar', E_USER_DEPRECATED);
49+
trigger_error('baz', E_USER_DEPRECATED);
50+
51+
$this->assertTrue(true);
52+
}
53+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/5532
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--display-deprecations';
8+
$_SERVER['argv'][] = __DIR__ . '/_files/IgnoreDeprecationsWithPatternTest.php';
9+
10+
require __DIR__ . '/../../bootstrap.php';
11+
12+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
13+
--EXPECTF--
14+
PHPUnit %s by Sebastian Bergmann and contributors.
15+
16+
Runtime: PHP %s
17+
18+
..D. 4 / 4 (100%)
19+
20+
Time: %s, Memory: %s MB
21+
22+
1 test triggered 1 deprecation:
23+
24+
1) %s/tests/end-to-end/event/_files/IgnoreDeprecationsWithPatternTest.php:%d
25+
baz
26+
27+
OK, but there were issues!
28+
Tests: 4, Assertions: 4, Deprecations: 1.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 PHPUnit\Framework\Attributes\CoversClass;
13+
use PHPUnit\Framework\Attributes\DataProvider;
14+
use PHPUnit\Framework\Attributes\Small;
15+
use PHPUnit\Framework\TestCase;
16+
17+
#[CoversClass(IgnoreDeprecations::class)]
18+
#[Small]
19+
final class IgnoreDeprecationsTest extends TestCase
20+
{
21+
public static function shouldIgnoreProvider(): array
22+
{
23+
return [
24+
'null pattern ignores any message' => [null, 'any warning message', true],
25+
'pattern matches message' => ['warning.*message', 'warning test message', true],
26+
'pattern does not match different message' => ['warning.*message', 'different message', false],
27+
'exact match works' => ['exact warning message', 'exact warning message', true],
28+
'exact match does not match different' => ['exact warning message', 'different warning message', false],
29+
];
30+
}
31+
32+
#[DataProvider('shouldIgnoreProvider')]
33+
public function testShouldIgnoreOnClass(?string $messagePattern, string $message, bool $expected): void
34+
{
35+
$metadata = Metadata::ignoreDeprecationsOnClass($messagePattern);
36+
37+
$this->assertSame($expected, $metadata->shouldIgnore($message));
38+
}
39+
40+
#[DataProvider('shouldIgnoreProvider')]
41+
public function testShouldIgnoreOnMethod(?string $messagePattern, string $message, bool $expected): void
42+
{
43+
$metadata = Metadata::ignoreDeprecationsOnMethod($messagePattern);
44+
45+
$this->assertSame($expected, $metadata->shouldIgnore($message));
46+
}
47+
}

0 commit comments

Comments
 (0)