|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypervel\Foundation\Testing; |
| 6 | + |
| 7 | +use Hypervel\Foundation\Testing\Contracts\Attributes\Resolvable; |
| 8 | +use Hypervel\Foundation\Testing\Contracts\Attributes\TestingFeature; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use ReflectionAttribute; |
| 11 | +use ReflectionClass; |
| 12 | +use ReflectionMethod; |
| 13 | + |
| 14 | +/** |
| 15 | + * Parses PHPUnit test case attributes for testing features. |
| 16 | + */ |
| 17 | +class AttributeParser |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Parse attributes for a class. |
| 21 | + * |
| 22 | + * @param class-string $className |
| 23 | + * @return array<int, array{key: class-string, instance: object}> |
| 24 | + */ |
| 25 | + public static function forClass(string $className): array |
| 26 | + { |
| 27 | + $attributes = []; |
| 28 | + $reflection = new ReflectionClass($className); |
| 29 | + |
| 30 | + foreach ($reflection->getAttributes() as $attribute) { |
| 31 | + if (! static::validAttribute($attribute->getName())) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + [$name, $instance] = static::resolveAttribute($attribute); |
| 36 | + |
| 37 | + if ($name !== null && $instance !== null) { |
| 38 | + $attributes[] = ['key' => $name, 'instance' => $instance]; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + $parent = $reflection->getParentClass(); |
| 43 | + |
| 44 | + if ($parent !== false && $parent->isSubclassOf(TestCase::class)) { |
| 45 | + $attributes = [...static::forClass($parent->getName()), ...$attributes]; |
| 46 | + } |
| 47 | + |
| 48 | + return $attributes; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Parse attributes for a method. |
| 53 | + * |
| 54 | + * @param class-string $className |
| 55 | + * @return array<int, array{key: class-string, instance: object}> |
| 56 | + */ |
| 57 | + public static function forMethod(string $className, string $methodName): array |
| 58 | + { |
| 59 | + $attributes = []; |
| 60 | + |
| 61 | + foreach ((new ReflectionMethod($className, $methodName))->getAttributes() as $attribute) { |
| 62 | + if (! static::validAttribute($attribute->getName())) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + [$name, $instance] = static::resolveAttribute($attribute); |
| 67 | + |
| 68 | + if ($name !== null && $instance !== null) { |
| 69 | + $attributes[] = ['key' => $name, 'instance' => $instance]; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return $attributes; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Validate if a class is a valid testing attribute. |
| 78 | + * |
| 79 | + * @param class-string|object $class |
| 80 | + */ |
| 81 | + public static function validAttribute(object|string $class): bool |
| 82 | + { |
| 83 | + if (\is_string($class) && ! class_exists($class)) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + $implements = class_implements($class); |
| 88 | + |
| 89 | + return isset($implements[TestingFeature::class]) |
| 90 | + || isset($implements[Resolvable::class]); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Resolve the given attribute. |
| 95 | + * |
| 96 | + * @return array{0: null|class-string, 1: null|object} |
| 97 | + */ |
| 98 | + protected static function resolveAttribute(ReflectionAttribute $attribute): array |
| 99 | + { |
| 100 | + /** @var array{0: null|class-string, 1: null|object} */ |
| 101 | + return rescue(static function () use ($attribute): array { // @phpstan-ignore argument.unresolvableType |
| 102 | + $instance = isset(class_implements($attribute->getName())[Resolvable::class]) |
| 103 | + ? transform($attribute->newInstance(), static fn (Resolvable $instance) => $instance->resolve()) |
| 104 | + : $attribute->newInstance(); |
| 105 | + |
| 106 | + if ($instance === null) { |
| 107 | + return [null, null]; |
| 108 | + } |
| 109 | + |
| 110 | + return [$instance::class, $instance]; |
| 111 | + }, [null, null], false); |
| 112 | + } |
| 113 | +} |
0 commit comments