From 8f96cfb4769206e05218b292d033ed9c2d94ff1b Mon Sep 17 00:00:00 2001 From: Frederik Bosch Date: Wed, 21 Aug 2024 22:20:19 +0200 Subject: [PATCH] add class and attribute specification tests, plus one bug fix --- src/ClassScanner/ClassSpecification.php | 15 +++--- tests/ClassScanner/ClassSpecificationTest.php | 47 +++++++++++++++++++ tests/Fake/FakeAllAttributes.php | 2 +- tests/Resolver/ReflectorTest.php | 38 ++++++++++++++- 4 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 tests/ClassScanner/ClassSpecificationTest.php diff --git a/src/ClassScanner/ClassSpecification.php b/src/ClassScanner/ClassSpecification.php index ce5c25e..bbdeeb9 100644 --- a/src/ClassScanner/ClassSpecification.php +++ b/src/ClassScanner/ClassSpecification.php @@ -70,15 +70,18 @@ function (AttributeSpecification $specification) { } /** - * @return array + * @return array */ public function getParameterAttributesForMethod(string $methodName): array { - return \array_filter( - $this->attributes, - function (AttributeSpecification $specification) use ($methodName) { - return $specification->getTargetMethod() === $methodName; + $result = []; + + foreach ($this->attributes as $specification) { + if ($specification->getTargetMethod() === $methodName && $param = $specification->getTargetParameter()) { + $result[$param] = $specification; } - ); + } + + return $result; } } \ No newline at end of file diff --git a/tests/ClassScanner/ClassSpecificationTest.php b/tests/ClassScanner/ClassSpecificationTest.php new file mode 100644 index 0000000..2754378 --- /dev/null +++ b/tests/ClassScanner/ClassSpecificationTest.php @@ -0,0 +1,47 @@ +yieldAttributes(FakeAllAttributes::class)]; + $spec = new ClassSpecification(FakeAllAttributes::class, FakeAllAttributes::FILE, $attributes); + + $this->assertSame(FakeAllAttributes::class, $spec->getClassName()); + $this->assertSame(FakeAllAttributes::FILE, $spec->getFilename()); + $this->assertCount(\count($attributes), $spec->getAttributes()); + $this->assertCount(1, $spec->getClassAttributes()); + $this->assertCount(1, $spec->getParameterAttributesForMethod('method')); + $this->assertArrayHasKey('methodParameter', $spec->getParameterAttributesForMethod('method')); + } + + public function testAttributeClass(): void + { + $reflector = new Reflector(); + + $spec1 = new ClassSpecification( + FakeAllAttributes::class, + FakeAllAttributes::FILE, + [...$reflector->yieldAttributes(FakeAllAttributes::class)] + ); + + $spec2 = new ClassSpecification( + FakeWorkerAttribute::class, + FakeWorkerAttribute::FILE, + [...$reflector->yieldAttributes(FakeWorkerAttribute::class)] + ); + + $this->assertFalse($spec1->isAttributeClass()); + $this->assertTrue($spec2->isAttributeClass()); + } +} \ No newline at end of file diff --git a/tests/Fake/FakeAllAttributes.php b/tests/Fake/FakeAllAttributes.php index 954da3c..eb65ac2 100644 --- a/tests/Fake/FakeAllAttributes.php +++ b/tests/Fake/FakeAllAttributes.php @@ -23,7 +23,7 @@ public function __construct( #[FakeAttribute(\Attribute::TARGET_METHOD)] public function method( #[FakeAttribute(\Attribute::TARGET_PARAMETER)] - $methodParameters + $methodParameter ) { } diff --git a/tests/Resolver/ReflectorTest.php b/tests/Resolver/ReflectorTest.php index cd22870..20a3d9e 100644 --- a/tests/Resolver/ReflectorTest.php +++ b/tests/Resolver/ReflectorTest.php @@ -19,12 +19,48 @@ public function testYieldAttributes() $instance = $specification->getAttributeInstance(); $this->assertSame($specification->getAttributeTarget(), $instance->getValue() & $specification->getAttributeTarget()); - if ($specification->getTargetParameter() === 'methodParameters') { + if ($specification->isClassConstantAttribute()) { + $this->assertTrue($specification->isClassConstantAttribute()); + $this->assertFalse($specification->isConstructorParameterAttribute()); + $this->assertFalse($specification->isMethodAttribute()); + $this->assertFalse($specification->isPropertyAttribute()); + $this->assertFalse($specification->isClassAttribute()); + } + + if ($specification->isClassAttribute()) { + $this->assertTrue($specification->isClassAttribute()); + $this->assertFalse($specification->isClassConstantAttribute()); + $this->assertFalse($specification->isConstructorParameterAttribute()); + $this->assertFalse($specification->isMethodAttribute()); + $this->assertFalse($specification->isPropertyAttribute()); + } + + if ($specification->isPropertyAttribute()) { + $this->assertTrue($specification->isPropertyAttribute()); + $this->assertFalse($specification->isClassAttribute()); + $this->assertFalse($specification->isClassConstantAttribute()); + $this->assertFalse($specification->isConstructorParameterAttribute()); + $this->assertFalse($specification->isMethodAttribute()); + } + + if ($specification->getTargetParameter() === 'methodParameter') { $this->assertSame('method', $specification->getTargetMethod()); + $this->assertFalse($specification->isConstructorParameterAttribute()); + $this->assertFalse($specification->isMethodAttribute()); + $this->assertFalse($specification->isPropertyAttribute()); + $this->assertFalse($specification->isClassConstantAttribute()); + $this->assertFalse($specification->isClassAttribute()); + $this->assertTrue($specification->isParameterAttribute()); } if ($specification->getTargetParameter() === 'parameter') { $this->assertSame('__construct', $specification->getTargetMethod()); + $this->assertTrue($specification->isConstructorParameterAttribute()); + $this->assertFalse($specification->isMethodAttribute()); + $this->assertFalse($specification->isPropertyAttribute()); + $this->assertFalse($specification->isClassConstantAttribute()); + $this->assertFalse($specification->isClassAttribute()); + $this->assertTrue($specification->isParameterAttribute()); } } }