Skip to content

Commit

Permalink
feat(metadata): class IdentifiersExtractor now handles enums (#5411)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Feb 18, 2023
1 parent 95478c5 commit d3783d6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Api/IdentifiersExtractor.php
Expand Up @@ -155,6 +155,10 @@ private function resolveIdentifierValue(mixed $identifierValue, string $paramete
return (string) $identifierValue;
}

if ($identifierValue instanceof \BackedEnum) {
return (string) $identifierValue->value;
}

throw new RuntimeException(sprintf('We were not able to resolve the identifier matching parameter "%s".', $parameterName));
}
}
41 changes: 41 additions & 0 deletions tests/Api/IdentifiersExtractorTest.php
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyWithEnumIdentifier;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelationMultiple;
use ApiPlatform\Tests\Fixtures\TestBundle\State\RelationMultipleProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -174,4 +175,44 @@ public function testGetIdentifiersFromItemWithToProperty(): void
$identifiersExtractor->getIdentifiersFromItem($item, $operation)
);
}

public function testGetIdentifierWithEnumValues(): void
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);

$resourceClassResolverProphecy->isResourceClass(Argument::any())->willReturn(true);
$resourceClassResolver = $resourceClassResolverProphecy->reveal();

$identifiersExtractor = new IdentifiersExtractor(
$resourceMetadataFactoryProphecy->reveal(),
$resourceClassResolver,
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal()
);

$operation = (new Get())
->withUriTemplate('/resources/{stringEnumAsIdentifier}/{intEnumAsIdentifier}')
->withUriVariables([
'stringEnumAsIdentifier' => (new Link())
->withFromClass(DummyWithEnumIdentifier::class)
->withParameterName('stringEnumAsIdentifier')
->withIdentifiers(['stringEnumAsIdentifier']),
'intEnumAsIdentifier' => (new Link())
->withFromClass(DummyWithEnumIdentifier::class)
->withParameterName('intEnumAsIdentifier')
->withIdentifiers(['intEnumAsIdentifier']),
])
->withClass(DummyWithEnumIdentifier::class);

$this->assertSame(
[
'stringEnumAsIdentifier' => 'foo',
'intEnumAsIdentifier' => '1',
],
$identifiersExtractor->getIdentifiersFromItem(new DummyWithEnumIdentifier(), $operation)
);
}
}
37 changes: 37 additions & 0 deletions tests/Fixtures/TestBundle/Entity/DummyWithEnumIdentifier.php
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;

class DummyWithEnumIdentifier
{
public $id;

public function __construct(
public StringEnumAsIdentifier $stringEnumAsIdentifier = StringEnumAsIdentifier::FOO,
public IntEnumAsIdentifier $intEnumAsIdentifier = IntEnumAsIdentifier::FOO,
) {
}
}

enum IntEnumAsIdentifier: int
{
case FOO = 1;
case BAR = 2;
}

enum StringEnumAsIdentifier: string
{
case FOO = 'foo';
case BAR = 'bar';
}

0 comments on commit d3783d6

Please sign in to comment.