Skip to content

Commit 9a8adff

Browse files
test: Backed enum resource tests (#6288)
* test: backed enum resources * chore: update minimal dev dependencies
1 parent b47edb2 commit 9a8adff

File tree

6 files changed

+463
-0
lines changed

6 files changed

+463
-0
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"symfony/security-bundle": "^6.4 || ^7.0",
9595
"symfony/security-core": "^6.4 || ^7.0",
9696
"symfony/stopwatch": "^6.4 || ^7.0",
97+
"symfony/string": "^6.4 || ^7.0",
9798
"symfony/twig-bundle": "^6.4 || ^7.0",
9899
"symfony/uid": "^6.4 || ^7.0",
99100
"symfony/validator": "^6.4 || ^7.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
19+
20+
#[ApiResource(normalizationContext: ['groups' => ['get']])]
21+
#[GetCollection(provider: Availability::class.'::getCases')]
22+
#[Get(provider: Availability::class.'::getCase')]
23+
enum Availability: int
24+
{
25+
use BackedEnumTrait;
26+
27+
case Available = 0;
28+
case Cancelled = 10;
29+
case Postponed = 200;
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
19+
20+
#[ApiResource(normalizationContext: ['groups' => ['get']])]
21+
#[GetCollection(provider: AvailabilityStatus::class.'::getCases')]
22+
#[Get(provider: AvailabilityStatus::class.'::getCase')]
23+
enum AvailabilityStatus: string
24+
{
25+
use BackedEnumTrait;
26+
27+
case Pending = 'pending';
28+
case Reviewed = 'reviewed';
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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 BackedEnumTrait
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|int
27+
{
28+
return $this->value;
29+
}
30+
31+
#[Groups(['get'])]
32+
public function getValue(): string|int
33+
{
34+
return $this->value;
35+
}
36+
37+
public static function getCases(): array
38+
{
39+
return self::cases();
40+
}
41+
42+
public static function getCase(Operation $operation, array $uriVariables): ?self
43+
{
44+
return array_reduce(self::cases(), static fn ($c, \BackedEnum $case) => $case->value == $uriVariables['id'] ? $case : $c, null);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\Functional;
15+
16+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Person;
18+
use ApiPlatform\Tests\Fixtures\TestBundle\Enum\GenderTypeEnum;
19+
use Doctrine\ORM\EntityManagerInterface;
20+
use Doctrine\ORM\Mapping\ClassMetadata;
21+
use Doctrine\ORM\Tools\SchemaTool;
22+
use Symfony\Component\HttpClient\HttpOptions;
23+
24+
final class BackedEnumPropertyTest extends ApiTestCase
25+
{
26+
public function testJson(): void
27+
{
28+
$person = $this->createPerson();
29+
30+
self::createClient()->request('GET', '/people/'.$person->getId(), ['headers' => ['Accept' => 'application/json']]);
31+
32+
$this->assertResponseIsSuccessful();
33+
$this->assertJsonEquals([
34+
'genderType' => GenderTypeEnum::FEMALE->value,
35+
'name' => 'Sonja',
36+
'academicGrades' => [],
37+
'pets' => [],
38+
]);
39+
}
40+
41+
/** @group legacy */
42+
public function testGraphQl(): void
43+
{
44+
$person = $this->createPerson();
45+
46+
$query = <<<'GRAPHQL'
47+
query GetPerson($identifier: ID!) {
48+
person(id: $identifier) {
49+
genderType
50+
}
51+
}
52+
GRAPHQL;
53+
$options = (new HttpOptions())
54+
->setJson(['query' => $query, 'variables' => ['identifier' => '/people/'.$person->getId()]])
55+
->setHeaders(['Content-Type' => 'application/json']);
56+
self::createClient()->request('POST', '/graphql', $options->toArray());
57+
58+
$this->assertResponseIsSuccessful();
59+
$this->assertJsonEquals([
60+
'data' => [
61+
'person' => [
62+
'genderType' => GenderTypeEnum::FEMALE->name,
63+
],
64+
],
65+
]);
66+
}
67+
68+
private function createPerson(): Person
69+
{
70+
$this->recreateSchema();
71+
72+
/** @var EntityManagerInterface $manager */
73+
$manager = static::getContainer()->get('doctrine')->getManager();
74+
$person = new Person();
75+
$person->name = 'Sonja';
76+
$person->genderType = GenderTypeEnum::FEMALE;
77+
$manager->persist($person);
78+
$manager->flush();
79+
80+
return $person;
81+
}
82+
83+
private function recreateSchema(array $options = []): void
84+
{
85+
self::bootKernel($options);
86+
87+
/** @var EntityManagerInterface $manager */
88+
$manager = static::getContainer()->get('doctrine')->getManager();
89+
/** @var ClassMetadata[] $classes */
90+
$classes = $manager->getMetadataFactory()->getAllMetadata();
91+
$schemaTool = new SchemaTool($manager);
92+
93+
@$schemaTool->dropSchema($classes);
94+
@$schemaTool->createSchema($classes);
95+
}
96+
}

0 commit comments

Comments
 (0)