-
-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?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\State\Provider; | ||
|
||
use ApiPlatform\Metadata\CollectionOperationInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
final class BackedEnumProvider implements ProviderInterface | ||
{ | ||
public function __construct(private ProviderInterface $decorated) | ||
{ | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
$resourceClass = $operation->getClass(); | ||
if (!$resourceClass || !is_a($resourceClass, \BackedEnum::class, true)) { | ||
return $this->decorated->provide($operation, $uriVariables, $context); | ||
} | ||
|
||
if ($operation instanceof CollectionOperationInterface) { | ||
return $resourceClass::cases(); | ||
} | ||
|
||
$id = $uriVariables['id'] ?? null; | ||
if (null === $id) { | ||
throw new NotFoundHttpException('Not Found'); | ||
} | ||
|
||
if ($enum = $this->resolveEnum($resourceClass, $id)) { | ||
return $enum; | ||
} | ||
|
||
throw new NotFoundHttpException('Not Found'); | ||
} | ||
|
||
/** | ||
* @param class-string $resourceClass | ||
*/ | ||
private function resolveEnum(string $resourceClass, string|int $id): ?\BackedEnum | ||
{ | ||
$reflectEnum = new \ReflectionEnum($resourceClass); | ||
$type = (string) $reflectEnum->getBackingType(); | ||
|
||
if ('int' === $type) { | ||
if (!is_numeric($id)) { | ||
return null; | ||
} | ||
$enum = $resourceClass::tryFrom((int) $id); | ||
} else { | ||
$enum = $resourceClass::tryFrom($id); | ||
} | ||
|
||
// @deprecated enums will be indexable only by value in 4.0 | ||
$enum ??= array_reduce($resourceClass::cases(), static fn ($c, \BackedEnum $case) => $id === $case->name ? $case : $c, null); | ||
|
||
return $enum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/Fixtures/TestBundle/ApiResource/BackedEnumIntegerResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?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\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
#[ApiResource] | ||
enum BackedEnumIntegerResource: int | ||
{ | ||
case Yes = 1; | ||
case No = 2; | ||
case Maybe = 3; | ||
|
||
public function getDescription(): string | ||
{ | ||
return match ($this) { | ||
self::Yes => 'We say yes', | ||
self::No => 'Computer says no', | ||
self::Maybe => 'Let me think about it', | ||
}; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Fixtures/TestBundle/ApiResource/BackedEnumStringResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?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\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
#[ApiResource] | ||
enum BackedEnumStringResource: string | ||
{ | ||
case Yes = 'yes'; | ||
case No = 'no'; | ||
case Maybe = 'maybe'; | ||
|
||
public function getDescription(): string | ||
{ | ||
return match ($this) { | ||
self::Yes => 'We say yes', | ||
self::No => 'Computer says no', | ||
self::Maybe => 'Let me think about it', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?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\Functional; | ||
|
||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Link; | ||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumIntegerResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumStringResource; | ||
|
||
final class BackedEnumResourceTest extends ApiTestCase | ||
{ | ||
public static function providerEnums(): iterable | ||
{ | ||
yield 'Int enum collection' => [BackedEnumIntegerResource::class, GetCollection::class, '_api_/backed_enum_integer_resources{._format}_get_collection']; | ||
yield 'Int enum item' => [BackedEnumIntegerResource::class, Get::class, '_api_/backed_enum_integer_resources/{id}{._format}_get']; | ||
|
||
yield 'String enum collection' => [BackedEnumStringResource::class, GetCollection::class, '_api_/backed_enum_string_resources{._format}_get_collection']; | ||
yield 'String enum item' => [BackedEnumStringResource::class, Get::class, '_api_/backed_enum_string_resources/{id}{._format}_get']; | ||
} | ||
|
||
/** @dataProvider providerEnums */ | ||
public function testOnlyGetOperationsAddedWhenNonSpecified(string $resourceClass, string $operationClass, string $operationName): void | ||
{ | ||
$factory = self::getContainer()->get('api_platform.metadata.resource.metadata_collection_factory'); | ||
$resourceMetadata = $factory->create($resourceClass); | ||
|
||
$this->assertCount(1, $resourceMetadata); | ||
$resource = $resourceMetadata[0]; | ||
$operations = iterator_to_array($resource->getOperations()); | ||
$this->assertCount(2, $operations); | ||
|
||
$this->assertInstanceOf($operationClass, $operations[$operationName]); | ||
} | ||
|
||
public function testEnumsAreAssignedValuePropertyAsIdentifierByDefault(): void | ||
{ | ||
$linkFactory = self::getContainer()->get('api_platform.metadata.resource.link_factory'); | ||
$result = $linkFactory->completeLink(new Link(fromClass: BackedEnumIntegerResource::class)); | ||
$identifiers = $result->getIdentifiers(); | ||
|
||
$this->assertCount(1, $identifiers); | ||
$this->assertNotContains('id', $identifiers); | ||
$this->assertContains('value', $identifiers); | ||
} | ||
|
||
public function testCollection(): void | ||
{ | ||
self::createClient()->request('GET', '/backed_enum_integer_resources', ['headers' => ['Accept' => 'application/json']]); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertJsonEquals([ | ||
[ | ||
'name' => 'Yes', | ||
'value' => 1, | ||
'description' => 'We say yes', | ||
], | ||
[ | ||
'name' => 'No', | ||
'value' => 2, | ||
'description' => 'Computer says no', | ||
], | ||
[ | ||
'name' => 'Maybe', | ||
'value' => 3, | ||
'description' => 'Let me think about it', | ||
], | ||
]); | ||
} | ||
|
||
public function testItem(): void | ||
{ | ||
self::createClient()->request('GET', '/backed_enum_integer_resources/1', ['headers' => ['Accept' => 'application/json']]); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertJsonEquals([ | ||
'name' => 'Yes', | ||
'value' => 1, | ||
'description' => 'We say yes', | ||
]); | ||
} | ||
|
||
public static function provider404s(): iterable | ||
{ | ||
yield ['/backed_enum_integer_resources/42']; | ||
yield ['/backed_enum_integer_resources/fortytwo']; | ||
} | ||
|
||
/** @dataProvider provider404s */ | ||
public function testItem404(string $uri): void | ||
{ | ||
self::createClient()->request('GET', $uri); | ||
|
||
$this->assertResponseStatusCodeSame(404); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?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\State\Provider; | ||
|
||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\Provider\BackedEnumProvider; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumIntegerResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumStringResource; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
final class BackedEnumProviderTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public static function provideCollection(): iterable | ||
{ | ||
yield 'Integer case enum' => [BackedEnumIntegerResource::class, BackedEnumIntegerResource::cases()]; | ||
yield 'String case enum' => [BackedEnumStringResource::class, BackedEnumStringResource::cases()]; | ||
} | ||
|
||
/** @dataProvider provideCollection */ | ||
public function testProvideCollection(string $class, array $expected): void | ||
{ | ||
$operation = new GetCollection(class: $class); | ||
|
||
$this->testProvide($expected, $operation); | ||
} | ||
|
||
public static function provideItem(): iterable | ||
{ | ||
yield 'Integer case enum' => [BackedEnumIntegerResource::class, 1, BackedEnumIntegerResource::Yes]; | ||
yield 'String case enum' => [BackedEnumStringResource::class, 'yes', BackedEnumStringResource::Yes]; | ||
} | ||
|
||
/** @dataProvider provideItem */ | ||
public function testProvideItem(string $class, string|int $id, \BackedEnum $expected): void | ||
{ | ||
$operation = new Get(class: $class); | ||
|
||
$this->testProvide($expected, $operation, ['id' => $id]); | ||
} | ||
|
||
private function testProvide($expected, Operation $operation, array $uriVariables = [], array $context = []): void | ||
{ | ||
$decorated = $this->prophesize(ProviderInterface::class); | ||
$decorated->provide(Argument::any())->shouldNotBeCalled(); | ||
$provider = new BackedEnumProvider($decorated->reveal()); | ||
|
||
$this->assertSame($expected, $provider->provide($operation, $uriVariables, $context)); | ||
} | ||
} |