Skip to content

Commit

Permalink
Add explicit test for #12 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspray authored Jun 15, 2024
1 parent fdb1867 commit 37fd010
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
10 changes: 10 additions & 0 deletions fixture_src/AliasedAttribute/FooClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedTargetFixture\AliasedAttribute;

use Cspray\AnnotatedTargetFixture\ClassOnly as MyAttributeName;

#[MyAttributeName('my aliased value')]
class FooClass {

}
17 changes: 17 additions & 0 deletions fixture_src/AliasedAttributeFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedTargetFixture;

use Cspray\Typiphy\ObjectType;
use function Cspray\Typiphy\objectType;

final class AliasedAttributeFixture implements Fixture {

public function getPath() : string {
return __DIR__ . '/AliasedAttribute';
}

public function fooClass() : ObjectType {
return objectType(AliasedAttribute\FooClass::class);
}
}
4 changes: 4 additions & 0 deletions fixture_src/Fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ public static function targetAttributeInterface() : TargetAttributeInterfaceFixt
public static function invalidSyntax() : BadPhpFileFixture {
return new BadPhpFileFixture();
}

public static function aliasedAttribute() : AliasedAttributeFixture {
return new AliasedAttributeFixture();
}
}
2 changes: 1 addition & 1 deletion src/PhpParserAnnotatedTargetParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function parse(AnnotatedTargetParserOptions $options) : Generator {
$data = new \stdClass();
$data->targets = [];
$nodeTraverser->addVisitor($this->getVisitor(
fn($target) => $data->targets[] = $target,
static fn($target) => $data->targets[] = $target,
$options->getAttributeTypes()
));

Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/AliasedAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedTarget\Unit;

use Cspray\AnnotatedTargetFixture\ClassOnly;
use Cspray\AnnotatedTargetFixture\Fixtures;
use function Cspray\Typiphy\objectType;

uses(AnnotatedTargetParserTestCase::class);

beforeEach()->withFixtures(Fixtures::aliasedAttribute());

it('counts parsed targets for single class')
->expect(fn() => $this->getTargets())
->toHaveCount(1);

it('ensures all targets are correct type')
->expect(fn() => $this->getTargets())
->toContainOnlyAnnotatedTargets();

it('ensures all targets share target reflection')
->expect(fn() => $this->getTargets())
->toShareTargetReflection();

it('ensures all targets share attribute reflection')
->expect(fn() => $this->getTargets())
->toShareAttributeReflection();

it('ensures all targets share attribute instance')
->expect(fn() => $this->getTargets())
->toShareAttributeInstance();

it('includes target reflection class')
->expect(fn() => $this->getTargets())
->toContainTargetClass(Fixtures::aliasedAttribute()->fooClass());

it('includes attribute reflection class')
->expect(fn() => $this->getTargets())
->toContainTargetClassWithAttribute(Fixtures::aliasedAttribute()->fooClass(), objectType(ClassOnly::class));

it('includes attribute instance with correct value')
->expect(fn() => $this->getTargets())
->toContainTargetClassWithAttributeInstance(
Fixtures::aliasedAttribute()->fooClass(),
objectType(ClassOnly::class),
fn(ClassOnly $classOnly) => $classOnly->value === 'my aliased value'
);
1 change: 0 additions & 1 deletion tests/Unit/ClassOnlyAttributeGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cspray\AnnotatedTargetFixture\ClassOnly;
use Cspray\AnnotatedTargetFixture\Fixtures;
use Cspray\AnnotatedTargetFixture\ParameterOnly;
use Cspray\AnnotatedTargetFixture\RepeatableClassOnly;
use function Cspray\Typiphy\objectType;

Expand Down

0 comments on commit 37fd010

Please sign in to comment.