Skip to content

Commit

Permalink
test: backed enums
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Jun 18, 2024
1 parent 469fe64 commit 84612ab
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand Down
16 changes: 7 additions & 9 deletions tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#[Get(provider: AvailabilityStatus::class.'::getCase')]
enum AvailabilityStatus: string
{
use BackedEnumTrait;
use BackedEnumStringTrait;

case Pending = 'pending';
case Reviewed = 'reviewed';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\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<string, string> $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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -39,6 +39,9 @@ public static function getCases(): array
return self::cases();
}

/**
* @param array<string, string> $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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Entity/DummyDtoNoOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @author Vincent Chalamon <vincentchalamon@gmail.com>
*/
#[ApiResource(input: InputDto::class, output: false)]
#[ApiResource(input: InputDto::class, output: false, graphQlOperations: [])]
#[ORM\Entity]
class DummyDtoNoOutput
{
Expand Down
143 changes: 74 additions & 69 deletions tests/Functional/BackedEnumResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,75 +471,78 @@ 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);
}

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'
Expand All @@ -559,7 +562,9 @@ public function testItemGraphQlInteger(): void
$this->assertResponseIsSuccessful();
$this->assertJsonEquals([
'data' => [
'status' => [
'backedEnumIntegerResource' => [
'description' => 'We say yes',
'name' => 'Yes',
'value' => 1,
],
],
Expand Down

0 comments on commit 84612ab

Please sign in to comment.