Skip to content

test: Backed enum resource tests #6288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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
Loading