Skip to content

Commit

Permalink
Do not try to instantiate attribute classes that do not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 15, 2024
1 parent c083e8e commit 7888430
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog-10.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi
### Fixed

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

## [10.5.34] - 2024-09-13

Expand Down
8 changes: 8 additions & 0 deletions src/Metadata/Parser/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function forClass(string $className): MetadataCollection
continue;
}

if (!class_exists($attribute->getName())) {
continue;
}

$attributeInstance = $attribute->newInstance();

switch ($attribute->getName()) {
Expand Down Expand Up @@ -349,6 +353,10 @@ public function forMethod(string $className, string $methodName): MetadataCollec
continue;
}

if (!class_exists($attribute->getName())) {
continue;
}

$attributeInstance = $attribute->newInstance();

switch ($attribute->getName()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Metadata\Attribute;

use PHPUnit\Framework\Attributes\PhpunitAttributeThatDoesNotExist;
use PHPUnit\Framework\TestCase;

#[PhpunitAttributeThatDoesNotExist]
final class PhpunitAttributeThatDoesNotExistTest extends TestCase
{
#[PhpunitAttributeThatDoesNotExist]
public function testOne(): void
{
}
}
8 changes: 8 additions & 0 deletions tests/unit/Metadata/Parser/AttributeParserTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PHPUnit\TestFixture\Metadata\Attribute\LargeTest;
use PHPUnit\TestFixture\Metadata\Attribute\MediumTest;
use PHPUnit\TestFixture\Metadata\Attribute\NonPhpunitAttributeTest;
use PHPUnit\TestFixture\Metadata\Attribute\PhpunitAttributeThatDoesNotExistTest;
use PHPUnit\TestFixture\Metadata\Attribute\PreserveGlobalStateTest;
use PHPUnit\TestFixture\Metadata\Attribute\ProcessIsolationTest;
use PHPUnit\TestFixture\Metadata\Attribute\RequiresFunctionTest;
Expand Down Expand Up @@ -926,5 +927,12 @@ public function test_ignores_attributes_not_owned_by_PHPUnit(): void
$this->assertTrue($metadata->isEmpty());
}

public function test_ignores_attributes_in_PHPUnit_namespace_that_do_not_exist(): void
{
$metadata = $this->parser()->forClassAndMethod(PhpunitAttributeThatDoesNotExistTest::class, 'testOne');

$this->assertTrue($metadata->isEmpty());
}

abstract protected function parser(): Parser;
}

0 comments on commit 7888430

Please sign in to comment.