-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Backed enum resource tests (#6288)
* test: backed enum resources * chore: update minimal dev dependencies
- Loading branch information
1 parent
b47edb2
commit 9a8adff
Showing
6 changed files
with
463 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/Fixtures/TestBundle/ApiResource/Issue6264/AvailabilityStatus.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
46 changes: 46 additions & 0 deletions
46
tests/Fixtures/TestBundle/ApiResource/Issue6264/BackedEnumTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.