Skip to content

Commit

Permalink
Merge c99011d into 8a232a4
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Apr 15, 2024
2 parents 8a232a4 + c99011d commit 0f8ea4a
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@
"symfony/maker-bundle": "^1.24",
"symfony/mercure-bundle": "*",
"symfony/messenger": "^6.4 || ^7.0",
"symfony/phpunit-bridge": "^6.4 || ^7.0",
"symfony/phpunit-bridge": "^6.4.1 || ^7.0",
"symfony/routing": "^6.4 || ^7.0",
"symfony/security-bundle": "^6.4 || ^7.0",
"symfony/security-core": "^6.4 || ^7.0",
"symfony/stopwatch": "^6.4 || ^7.0",
"symfony/string": "^6.4 || ^7.0",
"symfony/twig-bundle": "^6.4 || ^7.0",
"symfony/uid": "^6.4 || ^7.0",
"symfony/validator": "^6.4 || ^7.0",
Expand Down
30 changes: 30 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;

#[ApiResource(normalizationContext: ['groups' => ['get']])]
#[GetCollection(provider: Availability::class.'::getCases')]
#[Get(provider: Availability::class.'::getCase')]
enum Availability: int
{
use BackedEnumTrait;

case Available = 0;
case Cancelled = 10;
case Postponed = 200;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;

#[ApiResource(normalizationContext: ['groups' => ['get']])]
#[GetCollection(provider: AvailabilityStatus::class.'::getCases')]
#[Get(provider: AvailabilityStatus::class.'::getCase')]
enum AvailabilityStatus: string
{
use BackedEnumTrait;

case Pending = 'pending';
case Reviewed = 'reviewed';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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 BackedEnumTrait
{
public static function values(): array
{
return array_map(static fn (\BackedEnum $feature) => $feature->value, self::cases());
}

public function getId(): string|int
{
return $this->value;
}

#[Groups(['get'])]
public function getValue(): string|int
{
return $this->value;
}

public static function getCases(): array
{
return self::cases();
}

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);
}
}
96 changes: 96 additions & 0 deletions tests/Functional/BackedEnumPropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Person;
use ApiPlatform\Tests\Fixtures\TestBundle\Enum\GenderTypeEnum;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Component\HttpClient\HttpOptions;

final class BackedEnumPropertyTest extends ApiTestCase
{
public function testJson(): void
{
$person = $this->createPerson();

self::createClient()->request('GET', '/people/'.$person->getId(), ['headers' => ['Accept' => 'application/json']]);

$this->assertResponseIsSuccessful();
$this->assertJsonEquals([
'genderType' => GenderTypeEnum::FEMALE->value,
'name' => 'Sonja',
'academicGrades' => [],
'pets' => [],
]);
}

/** @group legacy */
public function testGraphQl(): void
{
$person = $this->createPerson();

$query = <<<'GRAPHQL'
query GetPerson($identifier: ID!) {
person(id: $identifier) {
genderType
}
}
GRAPHQL;
$options = (new HttpOptions())
->setJson(['query' => $query, 'variables' => ['identifier' => '/people/'.$person->getId()]])
->setHeaders(['Content-Type' => 'application/json']);
self::createClient()->request('POST', '/graphql', $options->toArray());

$this->assertResponseIsSuccessful();
$this->assertJsonEquals([
'data' => [
'person' => [
'genderType' => GenderTypeEnum::FEMALE->name,
],
],
]);
}

private function createPerson(): Person
{
$this->recreateSchema();

/** @var EntityManagerInterface $manager */
$manager = static::getContainer()->get('doctrine')->getManager();
$person = new Person();
$person->name = 'Sonja';
$person->genderType = GenderTypeEnum::FEMALE;
$manager->persist($person);
$manager->flush();

return $person;
}

private function recreateSchema(array $options = []): void
{
self::bootKernel($options);

/** @var EntityManagerInterface $manager */
$manager = static::getContainer()->get('doctrine')->getManager();
/** @var ClassMetadata[] $classes */
$classes = $manager->getMetadataFactory()->getAllMetadata();
$schemaTool = new SchemaTool($manager);

@$schemaTool->dropSchema($classes);
@$schemaTool->createSchema($classes);
}
}
Loading

0 comments on commit 0f8ea4a

Please sign in to comment.