From cc755bf98ad3ab9d7903da3e218693301351148c Mon Sep 17 00:00:00 2001 From: soyuka Date: Tue, 18 Jun 2024 10:20:46 +0200 Subject: [PATCH] test: backed enums --- ...dEnumResourceMetadataCollectionFactory.php | 1 + .../ApiResource/Issue6264/Availability.php | 16 +- .../Issue6264/AvailabilityStatus.php | 2 +- .../Issue6264/BackedEnumStringTrait.php | 49 ++++++ .../ApiResource/Issue6264/BackedEnumTrait.php | 7 +- tests/Functional/BackedEnumResourceTest.php | 146 +++++++++--------- .../State/Provider/BackedEnumProviderTest.php | 2 +- 7 files changed, 141 insertions(+), 82 deletions(-) create mode 100644 tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumStringTrait.php diff --git a/src/Metadata/Resource/Factory/BackedEnumResourceMetadataCollectionFactory.php b/src/Metadata/Resource/Factory/BackedEnumResourceMetadataCollectionFactory.php index 5eea5e3128e..9f7411b1c3b 100644 --- a/src/Metadata/Resource/Factory/BackedEnumResourceMetadataCollectionFactory.php +++ b/src/Metadata/Resource/Factory/BackedEnumResourceMetadataCollectionFactory.php @@ -24,6 +24,7 @@ final class BackedEnumResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface { public const PROVIDER = 'api_platform.state_provider.backed_enum'; + public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated) { } diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php b/tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php index 77135546386..fecf4cdabfe 100644 --- a/tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php @@ -17,18 +17,16 @@ use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\GetCollection; use ApiPlatform\Metadata\GraphQl\Query; -use ApiPlatform\Metadata\GraphQl\QueryCollection; #[ApiResource( normalizationContext: ['groups' => ['get']], - operations: [ - new GetCollection(provider: Availability::class.'::getCases'), - new Get(provider: Availability::class.'::getCase') - ], - graphQlOperations: [ - new Query(), - new QueryCollection(), - ] + operations: [ + new GetCollection(provider: Availability::class.'::getCases'), + new Get(provider: Availability::class.'::getCase'), + ], + graphQlOperations: [ + new Query(provider: Availability::class.'getCase'), + ] )] enum Availability: int { diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6264/AvailabilityStatus.php b/tests/Fixtures/TestBundle/ApiResource/Issue6264/AvailabilityStatus.php index a6825d72e8d..ef406b2a4ce 100644 --- a/tests/Fixtures/TestBundle/ApiResource/Issue6264/AvailabilityStatus.php +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6264/AvailabilityStatus.php @@ -22,7 +22,7 @@ #[Get(provider: AvailabilityStatus::class.'::getCase')] enum AvailabilityStatus: string { - use BackedEnumTrait; + use BackedEnumStringTrait; case Pending = 'pending'; case Reviewed = 'reviewed'; diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumStringTrait.php b/tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumStringTrait.php new file mode 100644 index 00000000000..f6f90be4cc7 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumStringTrait.php @@ -0,0 +1,49 @@ + + * + * 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\Issue6264; + +use ApiPlatform\Metadata\Operation; +use Symfony\Component\Serializer\Attribute\Groups; + +trait BackedEnumStringTrait +{ + public static function values(): array + { + return array_map(static fn (\BackedEnum $feature) => $feature->value, self::cases()); + } + + public function getId(): string + { + return $this->value; + } + + #[Groups(['get'])] + public function getValue(): string + { + return $this->value; + } + + public static function getCases(): array + { + return self::cases(); + } + + /** + * @param array $uriVariables + */ + public static function getCase(Operation $operation, array $uriVariables): ?self + { + return array_reduce(self::cases(), static fn ($c, \BackedEnum $case) => $case->value == $uriVariables['id'] ? $case : $c, null); + } +} diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumTrait.php b/tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumTrait.php index 20b2d60eee2..3bd48c3ebd5 100644 --- a/tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumTrait.php +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumTrait.php @@ -23,13 +23,13 @@ public static function values(): array return array_map(static fn (\BackedEnum $feature) => $feature->value, self::cases()); } - public function getId(): string|int + public function getId(): int { return $this->value; } #[Groups(['get'])] - public function getValue(): string|int + public function getValue(): int { return $this->value; } @@ -39,6 +39,9 @@ public static function getCases(): array return self::cases(); } + /** + * @param array $uriVariables + */ public static function getCase(Operation $operation, array $uriVariables): ?self { return array_reduce(self::cases(), static fn ($c, \BackedEnum $case) => $case->value == $uriVariables['id'] ? $case : $c, null); diff --git a/tests/Functional/BackedEnumResourceTest.php b/tests/Functional/BackedEnumResourceTest.php index 284a72a1f4b..6bbd2b0f448 100644 --- a/tests/Functional/BackedEnumResourceTest.php +++ b/tests/Functional/BackedEnumResourceTest.php @@ -471,75 +471,81 @@ public function testItem404(string $uri): void $this->assertResponseStatusCodeSame(404); } -// public static function providerEnumItemsGraphQl(): iterable -// { -// // Integer cases -// $query = <<<'GRAPHQL' -// query GetAvailability($identifier: ID!) { -// availability(id: $identifier) { -// value -// } -// } -// GRAPHQL; -// foreach (Availability::cases() as $case) { -// yield [$query, ['identifier' => '/availabilities/'.$case->value], ['data' => ['availability' => ['value' => $case->value]]]]; -// } -// -// // String cases -// $query = <<<'GRAPHQL' -// query GetAvailabilityStatus($identifier: ID!) { -// availabilityStatus(id: $identifier) { -// value -// } -// } -// GRAPHQL; -// foreach (AvailabilityStatus::cases() as $case) { -// yield [$query, ['identifier' => '/availability_statuses/'.$case->value], ['data' => ['availability_status' => ['value' => $case->value]]]]; -// } -// } -// -// /** -// * @dataProvider providerEnumItemsGraphQl -// * -// * @group legacy -// */ -// public function testItemGraphql(string $query, array $variables, array $expected): void -// { -// $options = (new HttpOptions()) -// ->setJson(['query' => $query, 'variables' => $variables]) -// ->setHeaders(['Content-Type' => 'application/json']); -// self::createClient()->request('POST', '/graphql', $options->toArray()); -// -// $this->assertResponseIsSuccessful(); -// $this->assertJsonEquals($expected); -// } -// -// public function testCollectionGraphQl(): void -// { -// $query = <<<'GRAPHQL' -// query { -// backedEnumIntegerResources { -// value -// } -// } -// GRAPHQL; -// $options = (new HttpOptions()) -// ->setJson(['query' => $query, 'variables' => []]) -// ->setHeaders(['Content-Type' => 'application/json']); -// self::createClient()->request('POST', '/graphql', $options->toArray()); -// -// $this->assertResponseIsSuccessful(); -// $this->assertJsonEquals([ -// 'data' => [ -// 'backedEnumIntegerResources' => [ -// ['value' => 1], -// ['value' => 2], -// ['value' => 3], -// ], -// ], -// ]); -// } + public static function providerEnumItemsGraphQl(): iterable + { + // Integer cases + $query = <<<'GRAPHQL' +query GetAvailability($identifier: ID!) { + availability(id: $identifier) { + value + } +} +GRAPHQL; + foreach (Availability::cases() as $case) { + yield [$query, ['identifier' => '/availabilities/'.$case->value], ['data' => ['availability' => ['value' => $case->value]]]]; + } + + // String cases + $query = <<<'GRAPHQL' +query GetAvailabilityStatus($identifier: ID!) { + availabilityStatus(id: $identifier) { + value + } +} +GRAPHQL; + foreach (AvailabilityStatus::cases() as $case) { + yield [$query, ['identifier' => '/availability_statuses/'.$case->value], ['data' => ['availabilityStatus' => ['value' => $case->value]]]]; + } + } + + /** + * @dataProvider providerEnumItemsGraphQl + * + * @group legacy + */ + public function testItemGraphql(string $query, array $variables, array $expected): void + { + $options = (new HttpOptions()) + ->setJson(['query' => $query, 'variables' => $variables]) + ->setHeaders(['Content-Type' => 'application/json']); + self::createClient()->request('POST', '/graphql', $options->toArray()); + + $this->assertResponseIsSuccessful(); + $this->assertJsonEquals($expected); + } + /** + * @group legacy + */ + public function testCollectionGraphQl(): void + { + $query = <<<'GRAPHQL' +query { + backedEnumIntegerResources { + value + } +} +GRAPHQL; + $options = (new HttpOptions()) + ->setJson(['query' => $query, 'variables' => []]) + ->setHeaders(['Content-Type' => 'application/json']); + self::createClient()->request('POST', '/graphql', $options->toArray()); + + $this->assertResponseIsSuccessful(); + $this->assertJsonEquals([ + 'data' => [ + 'backedEnumIntegerResources' => [ + ['value' => 1], + ['value' => 2], + ['value' => 3], + ], + ], + ]); + } + + /** + * @group legacy + */ public function testItemGraphQlInteger(): void { $query = <<<'GRAPHQL' @@ -559,7 +565,9 @@ public function testItemGraphQlInteger(): void $this->assertResponseIsSuccessful(); $this->assertJsonEquals([ 'data' => [ - 'status' => [ + 'backedEnumIntegerResource' => [ + 'description' => 'We say yes', + 'name' => 'Yes', 'value' => 1, ], ], diff --git a/tests/State/Provider/BackedEnumProviderTest.php b/tests/State/Provider/BackedEnumProviderTest.php index 78f67a9ca62..934744681cb 100644 --- a/tests/State/Provider/BackedEnumProviderTest.php +++ b/tests/State/Provider/BackedEnumProviderTest.php @@ -60,7 +60,7 @@ private function testProvide($expected, Operation $operation, array $uriVariable { $decorated = $this->prophesize(ProviderInterface::class); $decorated->provide(Argument::any())->shouldNotBeCalled(); - $provider = new BackedEnumProvider($decorated->reveal()); + $provider = new BackedEnumProvider(); $this->assertSame($expected, $provider->provide($operation, $uriVariables, $context)); }