|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Common\Self; |
| 6 | + |
| 7 | +use Testo\Assert; |
| 8 | +use Testo\Common\Reflection; |
| 9 | +use Tests\Common\Stub\AnotherMarkerAttribute; |
| 10 | +use Tests\Common\Stub\BaseClass; |
| 11 | +use Tests\Common\Stub\ChildClass; |
| 12 | +use Tests\Common\Stub\ClassWithoutMarkedMethods; |
| 13 | +use Tests\Common\Stub\MarkerAttribute; |
| 14 | +use Tests\Common\Stub\MiddleClass; |
| 15 | + |
| 16 | +function testFindsDirectlyMarkedMethods(): void |
| 17 | +{ |
| 18 | + $methods = Reflection::findMethodsWithAttribute(BaseClass::class, MarkerAttribute::class); |
| 19 | + |
| 20 | + $names = \array_map(static fn(\ReflectionMethod $m) => $m->getName(), $methods); |
| 21 | + \sort($names); |
| 22 | + |
| 23 | + Assert::same(['baseMethod', 'overriddenMethod'], $names); |
| 24 | +} |
| 25 | + |
| 26 | +function testFindsInheritedMarkedMethods(): void |
| 27 | +{ |
| 28 | + $methods = Reflection::findMethodsWithAttribute(MiddleClass::class, MarkerAttribute::class); |
| 29 | + |
| 30 | + $names = \array_map(static fn(\ReflectionMethod $m) => $m->getName(), $methods); |
| 31 | + \sort($names); |
| 32 | + |
| 33 | + Assert::same(['baseMethod', 'overriddenMethod'], $names); |
| 34 | +} |
| 35 | + |
| 36 | +function testFindsMethodsMarkedInPrototype(): void |
| 37 | +{ |
| 38 | + $methods = Reflection::findMethodsWithAttribute(ChildClass::class, MarkerAttribute::class); |
| 39 | + |
| 40 | + $names = \array_map(static fn(\ReflectionMethod $m) => $m->getName(), $methods); |
| 41 | + \sort($names); |
| 42 | + |
| 43 | + Assert::same(['baseMethod', 'childMethod', 'interfaceMethod', 'overriddenMethod'], $names); |
| 44 | +} |
| 45 | + |
| 46 | +function testFindsMethodsFromInterface(): void |
| 47 | +{ |
| 48 | + $methods = Reflection::findMethodsWithAttribute(ChildClass::class, MarkerAttribute::class); |
| 49 | + |
| 50 | + $names = \array_map(static fn(\ReflectionMethod $m) => $m->getName(), $methods); |
| 51 | + |
| 52 | + Assert::true(\in_array('interfaceMethod', $names, true)); |
| 53 | +} |
| 54 | + |
| 55 | +function testWithoutPrototypesSkipsPrototypeSearch(): void |
| 56 | +{ |
| 57 | + $methods = Reflection::findMethodsWithAttribute( |
| 58 | + ChildClass::class, |
| 59 | + MarkerAttribute::class, |
| 60 | + includePrototypes: false, |
| 61 | + ); |
| 62 | + |
| 63 | + $names = \array_map(static fn(\ReflectionMethod $m) => $m->getName(), $methods); |
| 64 | + \sort($names); |
| 65 | + |
| 66 | + // baseMethod has attribute directly, childMethod has attribute directly |
| 67 | + // interfaceMethod and overriddenMethod only have attributes in prototypes |
| 68 | + Assert::same(['baseMethod', 'childMethod'], $names); |
| 69 | +} |
| 70 | + |
| 71 | +function testReturnsEmptyArrayWhenNoMatches(): void |
| 72 | +{ |
| 73 | + $methods = Reflection::findMethodsWithAttribute(ClassWithoutMarkedMethods::class, MarkerAttribute::class); |
| 74 | + |
| 75 | + Assert::same([], $methods); |
| 76 | +} |
| 77 | + |
| 78 | +function testFiltersByAttributeClass(): void |
| 79 | +{ |
| 80 | + $methods = Reflection::findMethodsWithAttribute(ChildClass::class, AnotherMarkerAttribute::class); |
| 81 | + |
| 82 | + $names = \array_map(static fn(\ReflectionMethod $m) => $m->getName(), $methods); |
| 83 | + |
| 84 | + Assert::same(['middleMethod'], $names); |
| 85 | +} |
| 86 | + |
| 87 | +function testAcceptsReflectionClassInstance(): void |
| 88 | +{ |
| 89 | + $ref = new \ReflectionClass(BaseClass::class); |
| 90 | + $methods = Reflection::findMethodsWithAttribute($ref, MarkerAttribute::class); |
| 91 | + |
| 92 | + $names = \array_map(static fn(\ReflectionMethod $m) => $m->getName(), $methods); |
| 93 | + \sort($names); |
| 94 | + |
| 95 | + Assert::same(['baseMethod', 'overriddenMethod'], $names); |
| 96 | +} |
0 commit comments