Skip to content

Commit 5b7a797

Browse files
committed
tests: add tests for #[IgnorePHPUnitWarnings]
1 parent 0e6020d commit 5b7a797

File tree

7 files changed

+140
-2
lines changed

7 files changed

+140
-2
lines changed

src/Event/Emitter/DispatchingEmitter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ public function testTriggeredPhpunitWarning(Code\Test $test, string $message): v
10141014
if (
10151015
$test->isTestMethod() &&
10161016
class_exists($test->className()) &&
1017-
($metadata = Registry::parser()->forMethod($test->className(), $test->methodName())->isIgnoreWarnings())->isNotEmpty() &&
1017+
($metadata = Registry::parser()->forMethod($test->className(), $test->methodName())->isIgnorePHPUnitWarnings())->isNotEmpty() &&
10181018
($ignoreWarnings = $metadata->asArray()[0] ?? null) !== null &&
10191019
$ignoreWarnings instanceof IgnorePHPUnitWarnings &&
10201020
$ignoreWarnings->shouldIgnore($message)

src/Metadata/MetadataCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public function isWithoutErrorHandler(): self
641641
);
642642
}
643643

644-
public function isIgnoreWarnings(): self
644+
public function isIgnorePHPUnitWarnings(): self
645645
{
646646
return new self(
647647
...array_filter(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Metadata\Attribute;
11+
12+
use PHPUnit\Framework\Attributes\IgnorePHPUnitWarnings;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class IgnorePHPUnitWarningsTest extends TestCase
16+
{
17+
#[IgnorePHPUnitWarnings]
18+
public function testOne(): void
19+
{
20+
$this->assertTrue(true);
21+
}
22+
23+
#[IgnorePHPUnitWarnings('warning.*pattern')]
24+
public function testTwo(): void
25+
{
26+
$this->assertTrue(true);
27+
}
28+
29+
#[IgnorePHPUnitWarnings('exact message')]
30+
public function testThree(): void
31+
{
32+
$this->assertTrue(true);
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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(IgnorePHPUnitWarnings::class)]
18+
#[Small]
19+
final class IgnorePHPUnitWarningsTest 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 testShouldIgnore(?string $messagePattern, string $message, bool $expected): void
34+
{
35+
$metadata = Metadata::ignorePHPUnitWarnings($messagePattern);
36+
37+
$this->assertSame($expected, $metadata->shouldIgnore($message));
38+
}
39+
}

tests/unit/Metadata/MetadataTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4759,6 +4759,68 @@ public function testCanBeUsesTrait(): void
47594759
$this->assertFalse($metadata->isMethodLevel());
47604760
}
47614761

4762+
public function testCanBeIgnorePHPUnitWarnings(): void
4763+
{
4764+
$metadata = Metadata::ignorePHPUnitWarnings(null);
4765+
4766+
$this->assertFalse($metadata->isAfter());
4767+
$this->assertFalse($metadata->isAfterClass());
4768+
$this->assertFalse($metadata->isBackupGlobals());
4769+
$this->assertFalse($metadata->isBackupStaticProperties());
4770+
$this->assertFalse($metadata->isBeforeClass());
4771+
$this->assertFalse($metadata->isBefore());
4772+
$this->assertFalse($metadata->isCoversNamespace());
4773+
$this->assertFalse($metadata->isCoversClass());
4774+
$this->assertFalse($metadata->isCoversClassesThatExtendClass());
4775+
$this->assertFalse($metadata->isCoversClassesThatImplementInterface());
4776+
$this->assertFalse($metadata->isCoversFunction());
4777+
$this->assertFalse($metadata->isCoversMethod());
4778+
$this->assertFalse($metadata->isCoversNothing());
4779+
$this->assertFalse($metadata->isCoversTrait());
4780+
$this->assertFalse($metadata->isDataProvider());
4781+
$this->assertFalse($metadata->isDependsOnClass());
4782+
$this->assertFalse($metadata->isDependsOnMethod());
4783+
$this->assertFalse($metadata->isDisableReturnValueGenerationForTestDoubles());
4784+
$this->assertFalse($metadata->isDoesNotPerformAssertions());
4785+
$this->assertFalse($metadata->isExcludeGlobalVariableFromBackup());
4786+
$this->assertFalse($metadata->isExcludeStaticPropertyFromBackup());
4787+
$this->assertFalse($metadata->isGroup());
4788+
$this->assertFalse($metadata->isIgnoreDeprecations());
4789+
$this->assertFalse($metadata->isIgnorePhpunitDeprecations());
4790+
$this->assertFalse($metadata->isRunClassInSeparateProcess());
4791+
$this->assertFalse($metadata->isRunInSeparateProcess());
4792+
$this->assertFalse($metadata->isRunTestsInSeparateProcesses());
4793+
$this->assertFalse($metadata->isTest());
4794+
$this->assertFalse($metadata->isPreCondition());
4795+
$this->assertFalse($metadata->isPostCondition());
4796+
$this->assertFalse($metadata->isPreserveGlobalState());
4797+
$this->assertFalse($metadata->isRequiresMethod());
4798+
$this->assertFalse($metadata->isRequiresFunction());
4799+
$this->assertFalse($metadata->isRequiresOperatingSystem());
4800+
$this->assertFalse($metadata->isRequiresOperatingSystemFamily());
4801+
$this->assertFalse($metadata->isRequiresPhp());
4802+
$this->assertFalse($metadata->isRequiresPhpExtension());
4803+
$this->assertFalse($metadata->isRequiresPhpunit());
4804+
$this->assertFalse($metadata->isRequiresPhpunitExtension());
4805+
$this->assertFalse($metadata->isRequiresEnvironmentVariable());
4806+
$this->assertFalse($metadata->isWithEnvironmentVariable());
4807+
$this->assertFalse($metadata->isRequiresSetting());
4808+
$this->assertFalse($metadata->isTestDox());
4809+
$this->assertFalse($metadata->isTestWith());
4810+
$this->assertFalse($metadata->isUsesNamespace());
4811+
$this->assertFalse($metadata->isUsesClass());
4812+
$this->assertFalse($metadata->isUsesClassesThatExtendClass());
4813+
$this->assertFalse($metadata->isUsesClassesThatImplementInterface());
4814+
$this->assertFalse($metadata->isUsesFunction());
4815+
$this->assertFalse($metadata->isUsesMethod());
4816+
$this->assertFalse($metadata->isUsesTrait());
4817+
$this->assertFalse($metadata->isWithoutErrorHandler());
4818+
$this->assertTrue($metadata->isIgnorePHPUnitWarnings());
4819+
4820+
$this->assertTrue($metadata->isMethodLevel());
4821+
$this->assertFalse($metadata->isClassLevel());
4822+
}
4823+
47624824
public function testCanBeWithoutErrorHandler(): void
47634825
{
47644826
$metadata = Metadata::withoutErrorHandler();

tests/unit/Metadata/Parser/AttributeParserTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
use PHPUnit\Framework\Attributes\UsesMethod;
7070
use PHPUnit\Framework\Attributes\UsesNamespace;
7171
use PHPUnit\Framework\Attributes\UsesTrait;
72+
use PHPUnit\Framework\Attributes\IgnorePHPUnitWarnings;
7273
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
7374
use PHPUnit\Metadata\DisableReturnValueGenerationForTestDoubles;
7475
use PHPUnit\Metadata\InvalidAttributeException;
@@ -139,6 +140,7 @@
139140
#[CoversClass(UsesTrait::class)]
140141
#[CoversClass(WithEnvironmentVariable::class)]
141142
#[CoversClass(WithoutErrorHandler::class)]
143+
#[CoversClass(IgnorePHPUnitWarnings::class)]
142144
#[Small]
143145
#[Group('metadata')]
144146
#[Group('metadata/attributes')]

tests/unit/Metadata/Parser/AttributeParserTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
use PHPUnit\TestFixture\Metadata\Attribute\TestDoxTest;
6262
use PHPUnit\TestFixture\Metadata\Attribute\TestWithTest;
6363
use PHPUnit\TestFixture\Metadata\Attribute\UsesTest;
64+
use PHPUnit\TestFixture\Metadata\Attribute\IgnorePHPUnitWarningsTest;
6465
use PHPUnit\TestFixture\Metadata\Attribute\WithEnvironmentVariableTest;
6566
use PHPUnit\TestFixture\Metadata\Attribute\WithoutErrorHandlerTest;
6667

0 commit comments

Comments
 (0)