Skip to content

Commit cc755bf

Browse files
committed
test: backed enums
1 parent 469fe64 commit cc755bf

File tree

7 files changed

+141
-82
lines changed

7 files changed

+141
-82
lines changed

src/Metadata/Resource/Factory/BackedEnumResourceMetadataCollectionFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
final class BackedEnumResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
2525
{
2626
public const PROVIDER = 'api_platform.state_provider.backed_enum';
27+
2728
public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated)
2829
{
2930
}

tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@
1717
use ApiPlatform\Metadata\Get;
1818
use ApiPlatform\Metadata\GetCollection;
1919
use ApiPlatform\Metadata\GraphQl\Query;
20-
use ApiPlatform\Metadata\GraphQl\QueryCollection;
2120

2221
#[ApiResource(
2322
normalizationContext: ['groups' => ['get']],
24-
operations: [
25-
new GetCollection(provider: Availability::class.'::getCases'),
26-
new Get(provider: Availability::class.'::getCase')
27-
],
28-
graphQlOperations: [
29-
new Query(),
30-
new QueryCollection(),
31-
]
23+
operations: [
24+
new GetCollection(provider: Availability::class.'::getCases'),
25+
new Get(provider: Availability::class.'::getCase'),
26+
],
27+
graphQlOperations: [
28+
new Query(provider: Availability::class.'getCase'),
29+
]
3230
)]
3331
enum Availability: int
3432
{

tests/Fixtures/TestBundle/ApiResource/Issue6264/AvailabilityStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#[Get(provider: AvailabilityStatus::class.'::getCase')]
2323
enum AvailabilityStatus: string
2424
{
25-
use BackedEnumTrait;
25+
use BackedEnumStringTrait;
2626

2727
case Pending = 'pending';
2828
case Reviewed = 'reviewed';
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6264;
15+
16+
use ApiPlatform\Metadata\Operation;
17+
use Symfony\Component\Serializer\Attribute\Groups;
18+
19+
trait BackedEnumStringTrait
20+
{
21+
public static function values(): array
22+
{
23+
return array_map(static fn (\BackedEnum $feature) => $feature->value, self::cases());
24+
}
25+
26+
public function getId(): string
27+
{
28+
return $this->value;
29+
}
30+
31+
#[Groups(['get'])]
32+
public function getValue(): string
33+
{
34+
return $this->value;
35+
}
36+
37+
public static function getCases(): array
38+
{
39+
return self::cases();
40+
}
41+
42+
/**
43+
* @param array<string, string> $uriVariables
44+
*/
45+
public static function getCase(Operation $operation, array $uriVariables): ?self
46+
{
47+
return array_reduce(self::cases(), static fn ($c, \BackedEnum $case) => $case->value == $uriVariables['id'] ? $case : $c, null);
48+
}
49+
}

tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumTrait.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public static function values(): array
2323
return array_map(static fn (\BackedEnum $feature) => $feature->value, self::cases());
2424
}
2525

26-
public function getId(): string|int
26+
public function getId(): int
2727
{
2828
return $this->value;
2929
}
3030

3131
#[Groups(['get'])]
32-
public function getValue(): string|int
32+
public function getValue(): int
3333
{
3434
return $this->value;
3535
}
@@ -39,6 +39,9 @@ public static function getCases(): array
3939
return self::cases();
4040
}
4141

42+
/**
43+
* @param array<string, string> $uriVariables
44+
*/
4245
public static function getCase(Operation $operation, array $uriVariables): ?self
4346
{
4447
return array_reduce(self::cases(), static fn ($c, \BackedEnum $case) => $case->value == $uriVariables['id'] ? $case : $c, null);

tests/Functional/BackedEnumResourceTest.php

Lines changed: 77 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -471,75 +471,81 @@ public function testItem404(string $uri): void
471471
$this->assertResponseStatusCodeSame(404);
472472
}
473473

474-
// public static function providerEnumItemsGraphQl(): iterable
475-
// {
476-
// // Integer cases
477-
// $query = <<<'GRAPHQL'
478-
// query GetAvailability($identifier: ID!) {
479-
// availability(id: $identifier) {
480-
// value
481-
// }
482-
// }
483-
// GRAPHQL;
484-
// foreach (Availability::cases() as $case) {
485-
// yield [$query, ['identifier' => '/availabilities/'.$case->value], ['data' => ['availability' => ['value' => $case->value]]]];
486-
// }
487-
//
488-
// // String cases
489-
// $query = <<<'GRAPHQL'
490-
// query GetAvailabilityStatus($identifier: ID!) {
491-
// availabilityStatus(id: $identifier) {
492-
// value
493-
// }
494-
// }
495-
// GRAPHQL;
496-
// foreach (AvailabilityStatus::cases() as $case) {
497-
// yield [$query, ['identifier' => '/availability_statuses/'.$case->value], ['data' => ['availability_status' => ['value' => $case->value]]]];
498-
// }
499-
// }
500-
//
501-
// /**
502-
// * @dataProvider providerEnumItemsGraphQl
503-
// *
504-
// * @group legacy
505-
// */
506-
// public function testItemGraphql(string $query, array $variables, array $expected): void
507-
// {
508-
// $options = (new HttpOptions())
509-
// ->setJson(['query' => $query, 'variables' => $variables])
510-
// ->setHeaders(['Content-Type' => 'application/json']);
511-
// self::createClient()->request('POST', '/graphql', $options->toArray());
512-
//
513-
// $this->assertResponseIsSuccessful();
514-
// $this->assertJsonEquals($expected);
515-
// }
516-
//
517-
// public function testCollectionGraphQl(): void
518-
// {
519-
// $query = <<<'GRAPHQL'
520-
// query {
521-
// backedEnumIntegerResources {
522-
// value
523-
// }
524-
// }
525-
// GRAPHQL;
526-
// $options = (new HttpOptions())
527-
// ->setJson(['query' => $query, 'variables' => []])
528-
// ->setHeaders(['Content-Type' => 'application/json']);
529-
// self::createClient()->request('POST', '/graphql', $options->toArray());
530-
//
531-
// $this->assertResponseIsSuccessful();
532-
// $this->assertJsonEquals([
533-
// 'data' => [
534-
// 'backedEnumIntegerResources' => [
535-
// ['value' => 1],
536-
// ['value' => 2],
537-
// ['value' => 3],
538-
// ],
539-
// ],
540-
// ]);
541-
// }
474+
public static function providerEnumItemsGraphQl(): iterable
475+
{
476+
// Integer cases
477+
$query = <<<'GRAPHQL'
478+
query GetAvailability($identifier: ID!) {
479+
availability(id: $identifier) {
480+
value
481+
}
482+
}
483+
GRAPHQL;
484+
foreach (Availability::cases() as $case) {
485+
yield [$query, ['identifier' => '/availabilities/'.$case->value], ['data' => ['availability' => ['value' => $case->value]]]];
486+
}
487+
488+
// String cases
489+
$query = <<<'GRAPHQL'
490+
query GetAvailabilityStatus($identifier: ID!) {
491+
availabilityStatus(id: $identifier) {
492+
value
493+
}
494+
}
495+
GRAPHQL;
496+
foreach (AvailabilityStatus::cases() as $case) {
497+
yield [$query, ['identifier' => '/availability_statuses/'.$case->value], ['data' => ['availabilityStatus' => ['value' => $case->value]]]];
498+
}
499+
}
500+
501+
/**
502+
* @dataProvider providerEnumItemsGraphQl
503+
*
504+
* @group legacy
505+
*/
506+
public function testItemGraphql(string $query, array $variables, array $expected): void
507+
{
508+
$options = (new HttpOptions())
509+
->setJson(['query' => $query, 'variables' => $variables])
510+
->setHeaders(['Content-Type' => 'application/json']);
511+
self::createClient()->request('POST', '/graphql', $options->toArray());
512+
513+
$this->assertResponseIsSuccessful();
514+
$this->assertJsonEquals($expected);
515+
}
542516

517+
/**
518+
* @group legacy
519+
*/
520+
public function testCollectionGraphQl(): void
521+
{
522+
$query = <<<'GRAPHQL'
523+
query {
524+
backedEnumIntegerResources {
525+
value
526+
}
527+
}
528+
GRAPHQL;
529+
$options = (new HttpOptions())
530+
->setJson(['query' => $query, 'variables' => []])
531+
->setHeaders(['Content-Type' => 'application/json']);
532+
self::createClient()->request('POST', '/graphql', $options->toArray());
533+
534+
$this->assertResponseIsSuccessful();
535+
$this->assertJsonEquals([
536+
'data' => [
537+
'backedEnumIntegerResources' => [
538+
['value' => 1],
539+
['value' => 2],
540+
['value' => 3],
541+
],
542+
],
543+
]);
544+
}
545+
546+
/**
547+
* @group legacy
548+
*/
543549
public function testItemGraphQlInteger(): void
544550
{
545551
$query = <<<'GRAPHQL'
@@ -559,7 +565,9 @@ public function testItemGraphQlInteger(): void
559565
$this->assertResponseIsSuccessful();
560566
$this->assertJsonEquals([
561567
'data' => [
562-
'status' => [
568+
'backedEnumIntegerResource' => [
569+
'description' => 'We say yes',
570+
'name' => 'Yes',
563571
'value' => 1,
564572
],
565573
],

tests/State/Provider/BackedEnumProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private function testProvide($expected, Operation $operation, array $uriVariable
6060
{
6161
$decorated = $this->prophesize(ProviderInterface::class);
6262
$decorated->provide(Argument::any())->shouldNotBeCalled();
63-
$provider = new BackedEnumProvider($decorated->reveal());
63+
$provider = new BackedEnumProvider();
6464

6565
$this->assertSame($expected, $provider->provide($operation, $uriVariables, $context));
6666
}

0 commit comments

Comments
 (0)