Skip to content

Commit

Permalink
Fix ReflectionAttribute adapter newInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 13, 2024
1 parent 77bfc36 commit c17736c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
28 changes: 28 additions & 0 deletions test/unit/Fixture/EnumAttributeConstantFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Roave\BetterReflectionTest\Fixture;

enum Foo: int {
case ONE = 1;
const MAPPING = [
self::ONE->value => 'one',
];
}

#[\Attribute]
class MyAttr
{
public function __construct(private array $mapping)
{
}
}

#[MyAttr(Foo::MAPPING)]
class Bar {

}

#[MyAttr([Foo::ONE])]
class Baz {

}
35 changes: 33 additions & 2 deletions test/unit/Reflection/Adapter/ReflectionAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use Roave\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute;

use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
Expand All @@ -25,6 +23,7 @@
use Roave\BetterReflectionTest\Fixture\ClassWithAttributeThatAcceptsArgument;
use Roave\BetterReflectionTest\Fixture\ClassWithAttributeThatHasNestedClassUsingNamedArguments;
use Roave\BetterReflectionTest\Fixture\ClassWithAttributeThatNeedsNamedArguments;
use Roave\BetterReflectionTest\Fixture\MyAttr;
use Roave\BetterReflectionTest\Fixture\SomeEnum;
use function array_combine;
use function array_map;
Expand Down Expand Up @@ -120,4 +119,36 @@ public function testNewInstanceWithNestedClassUsingNamedArguments(): void
$this->assertNull($instance->nested->e);
$this->assertSame('string', $instance->nested->s);
}

public function testNewInstanceWithEnumConstantAsArgument(): void
{
$path = __DIR__ . '/../../Fixture/EnumAttributeConstantFixtures.php';
require_once $path;
$configuration = BetterReflectionSingleton::instance();
$reflector = new DefaultReflector(new AggregateSourceLocator([
new SingleFileSourceLocator($path, $configuration->astLocator()),
new PhpInternalSourceLocator($configuration->astLocator(), $configuration->sourceStubber()),
]));
$classInfo = $reflector->reflectClass(\Roave\BetterReflectionTest\Fixture\Bar::class);
$attribute = $classInfo->getAttributes()[0];
$adapter = new ReflectionAttributeAdapter($attribute);

self::assertEquals(new MyAttr([1 => 'one']), $adapter->newInstance());
}

public function testNewInstanceWithEnumCaseAsArgument(): void
{
$path = __DIR__ . '/../../Fixture/EnumAttributeConstantFixtures.php';
require_once $path;
$configuration = BetterReflectionSingleton::instance();
$reflector = new DefaultReflector(new AggregateSourceLocator([
new SingleFileSourceLocator($path, $configuration->astLocator()),
new PhpInternalSourceLocator($configuration->astLocator(), $configuration->sourceStubber()),
]));
$classInfo = $reflector->reflectClass(\Roave\BetterReflectionTest\Fixture\Baz::class);
$attribute = $classInfo->getAttributes()[0];
$adapter = new ReflectionAttributeAdapter($attribute);

self::assertEquals(new MyAttr([\Roave\BetterReflectionTest\Fixture\Foo::ONE]), $adapter->newInstance());
}
}

0 comments on commit c17736c

Please sign in to comment.