|
| 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