Skip to content

Commit

Permalink
add class and attribute specification tests, plus one bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Aug 21, 2024
1 parent 2cd7c6a commit 8f96cfb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/ClassScanner/ClassSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@ function (AttributeSpecification $specification) {
}

/**
* @return array<int, AttributeSpecification>
* @return array<string, AttributeSpecification>
*/
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;
}
}
47 changes: 47 additions & 0 deletions tests/ClassScanner/ClassSpecificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Aura\Di\ClassScanner;

use Aura\Di\Fake\FakeAllAttributes;
use Aura\Di\Fake\FakeWorkerAttribute;
use Aura\Di\Resolver\Reflector;
use PHPUnit\Framework\TestCase;

final class ClassSpecificationTest extends TestCase
{
public function testClassInfo(): void
{
$reflector = new Reflector();
$attributes = [...$reflector->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());
}
}
2 changes: 1 addition & 1 deletion tests/Fake/FakeAllAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
#[FakeAttribute(\Attribute::TARGET_METHOD)]
public function method(
#[FakeAttribute(\Attribute::TARGET_PARAMETER)]
$methodParameters
$methodParameter
)
{
}
Expand Down
38 changes: 37 additions & 1 deletion tests/Resolver/ReflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down

0 comments on commit 8f96cfb

Please sign in to comment.