Skip to content

Commit

Permalink
Merge branch '11.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 15, 2024
2 parents 3671c6b + 88673ab commit 98b9586
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Metadata/Parser/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function array_merge;
use function assert;
use function class_exists;
use function count;
use function explode;
use function method_exists;
Expand Down Expand Up @@ -60,6 +61,8 @@ final class AnnotationParser implements Parser
*/
public function forClass(string $className): MetadataCollection
{
assert(class_exists($className));

$result = [];

foreach (AnnotationRegistry::getInstance()->forClassName($className)->symbolAnnotations() as $annotation => $values) {
Expand Down Expand Up @@ -206,6 +209,9 @@ public function forClass(string $className): MetadataCollection
*/
public function forMethod(string $className, string $methodName): MetadataCollection
{
assert(class_exists($className));
assert(method_exists($className, $methodName));

$result = [];

foreach (AnnotationRegistry::getInstance()->forMethod($className, $methodName)->symbolAnnotations() as $annotation => $values) {
Expand Down
15 changes: 15 additions & 0 deletions src/Metadata/Parser/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

use const JSON_THROW_ON_ERROR;
use function assert;
use function class_exists;
use function json_decode;
use function method_exists;
use function sprintf;
use function str_starts_with;
use function strtolower;
Expand Down Expand Up @@ -92,13 +94,19 @@
*/
public function forClass(string $className): MetadataCollection
{
assert(class_exists($className));

$result = [];

foreach ((new ReflectionClass($className))->getAttributes() as $attribute) {
if (!str_starts_with($attribute->getName(), 'PHPUnit\\Framework\\Attributes\\')) {
continue;
}

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

$attributeInstance = $attribute->newInstance();

switch ($attribute->getName()) {
Expand Down Expand Up @@ -379,13 +387,20 @@ public function forClass(string $className): MetadataCollection
*/
public function forMethod(string $className, string $methodName): MetadataCollection
{
assert(class_exists($className));
assert(method_exists($className, $methodName));

$result = [];

foreach ((new ReflectionMethod($className, $methodName))->getAttributes() as $attribute) {
if (!str_starts_with($attribute->getName(), 'PHPUnit\\Framework\\Attributes\\')) {
continue;
}

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

$attributeInstance = $attribute->newInstance();

switch ($attribute->getName()) {
Expand Down
8 changes: 8 additions & 0 deletions src/Metadata/Parser/CachingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
namespace PHPUnit\Metadata\Parser;

use function assert;
use function class_exists;
use function method_exists;
use PHPUnit\Metadata\MetadataCollection;

/**
Expand Down Expand Up @@ -45,6 +48,8 @@ public function __construct(Parser $reader)
*/
public function forClass(string $className): MetadataCollection
{
assert(class_exists($className));

if (isset($this->classCache[$className])) {
return $this->classCache[$className];
}
Expand All @@ -60,6 +65,9 @@ public function forClass(string $className): MetadataCollection
*/
public function forMethod(string $className, string $methodName): MetadataCollection
{
assert(class_exists($className));
assert(method_exists($className, $methodName));

$key = $className . '::' . $methodName;

if (isset($this->methodCache[$key])) {
Expand Down
8 changes: 8 additions & 0 deletions src/Metadata/Parser/ParserChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
namespace PHPUnit\Metadata\Parser;

use function assert;
use function class_exists;
use function method_exists;
use PHPUnit\Metadata\MetadataCollection;

/**
Expand All @@ -32,6 +35,8 @@ public function __construct(Parser $attributeReader, Parser $annotationReader)
*/
public function forClass(string $className): MetadataCollection
{
assert(class_exists($className));

$metadata = $this->attributeReader->forClass($className);

if (!$metadata->isEmpty()) {
Expand All @@ -47,6 +52,9 @@ public function forClass(string $className): MetadataCollection
*/
public function forMethod(string $className, string $methodName): MetadataCollection
{
assert(class_exists($className));
assert(method_exists($className, $methodName));

$metadata = $this->attributeReader->forMethod($className, $methodName);

if (!$metadata->isEmpty()) {
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 @@ -38,6 +38,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 @@ -1031,5 +1032,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 98b9586

Please sign in to comment.