Skip to content

Commit 7888430

Browse files
Do not try to instantiate attribute classes that do not exist
1 parent c083e8e commit 7888430

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

ChangeLog-10.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi
77
### Fixed
88

99
* [#5950](https://github.com/sebastianbergmann/phpunit/pull/5950): TestDox text should not be `trim()`med when it contains `$` character
10+
* The attribute parser will no longer try to instantiate attribute classes that do not exist
1011

1112
## [10.5.34] - 2024-09-13
1213

src/Metadata/Parser/AttributeParser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public function forClass(string $className): MetadataCollection
9595
continue;
9696
}
9797

98+
if (!class_exists($attribute->getName())) {
99+
continue;
100+
}
101+
98102
$attributeInstance = $attribute->newInstance();
99103

100104
switch ($attribute->getName()) {
@@ -349,6 +353,10 @@ public function forMethod(string $className, string $methodName): MetadataCollec
349353
continue;
350354
}
351355

356+
if (!class_exists($attribute->getName())) {
357+
continue;
358+
}
359+
352360
$attributeInstance = $attribute->newInstance();
353361

354362
switch ($attribute->getName()) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
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\PhpunitAttributeThatDoesNotExist;
13+
use PHPUnit\Framework\TestCase;
14+
15+
#[PhpunitAttributeThatDoesNotExist]
16+
final class PhpunitAttributeThatDoesNotExistTest extends TestCase
17+
{
18+
#[PhpunitAttributeThatDoesNotExist]
19+
public function testOne(): void
20+
{
21+
}
22+
}

tests/unit/Metadata/Parser/AttributeParserTestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use PHPUnit\TestFixture\Metadata\Attribute\LargeTest;
3535
use PHPUnit\TestFixture\Metadata\Attribute\MediumTest;
3636
use PHPUnit\TestFixture\Metadata\Attribute\NonPhpunitAttributeTest;
37+
use PHPUnit\TestFixture\Metadata\Attribute\PhpunitAttributeThatDoesNotExistTest;
3738
use PHPUnit\TestFixture\Metadata\Attribute\PreserveGlobalStateTest;
3839
use PHPUnit\TestFixture\Metadata\Attribute\ProcessIsolationTest;
3940
use PHPUnit\TestFixture\Metadata\Attribute\RequiresFunctionTest;
@@ -926,5 +927,12 @@ public function test_ignores_attributes_not_owned_by_PHPUnit(): void
926927
$this->assertTrue($metadata->isEmpty());
927928
}
928929

930+
public function test_ignores_attributes_in_PHPUnit_namespace_that_do_not_exist(): void
931+
{
932+
$metadata = $this->parser()->forClassAndMethod(PhpunitAttributeThatDoesNotExistTest::class, 'testOne');
933+
934+
$this->assertTrue($metadata->isEmpty());
935+
}
936+
929937
abstract protected function parser(): Parser;
930938
}

0 commit comments

Comments
 (0)