|
| 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\Issues; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7135\Bar; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7135\Foo; |
| 19 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 20 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 21 | +use Doctrine\ORM\EntityManagerInterface; |
| 22 | +use Doctrine\ORM\Tools\SchemaTool; |
| 23 | +use Symfony\Component\Uid\Uuid; |
| 24 | + |
| 25 | +class Issue7135Test extends ApiTestCase |
| 26 | +{ |
| 27 | + use RecreateSchemaTrait; |
| 28 | + use SetupClassResourcesTrait; |
| 29 | + |
| 30 | + protected static ?bool $alwaysBootKernel = false; |
| 31 | + |
| 32 | + /** |
| 33 | + * @return class-string[] |
| 34 | + */ |
| 35 | + public static function getResources(): array |
| 36 | + { |
| 37 | + return [Bar::class, Foo::class]; |
| 38 | + } |
| 39 | + |
| 40 | + public function testValidPostRequestWithIriWhenIdentifierIsUuid(): void |
| 41 | + { |
| 42 | + $container = static::getContainer(); |
| 43 | + if ('mongodb' === $container->getParameter('kernel.environment')) { |
| 44 | + $this->markTestSkipped(); |
| 45 | + } |
| 46 | + |
| 47 | + $this->recreateSchema(self::getResources()); |
| 48 | + $bar = $this->loadBarFixture(); |
| 49 | + |
| 50 | + $response = self::createClient()->request('POST', '/pull-request-7135/foo/', [ |
| 51 | + 'json' => [ |
| 52 | + 'bar' => 'pull-request-7135/bar/'.$bar->id, |
| 53 | + ], |
| 54 | + ]); |
| 55 | + |
| 56 | + self::assertEquals(201, $response->getStatusCode()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testInvalidPostRequestWithIriWhenIdentifierIsUuid(): void |
| 60 | + { |
| 61 | + $container = static::getContainer(); |
| 62 | + if ('mongodb' === $container->getParameter('kernel.environment')) { |
| 63 | + $this->markTestSkipped(); |
| 64 | + } |
| 65 | + |
| 66 | + $response = self::createClient()->request('POST', '/pull-request-7135/foo/', [ |
| 67 | + 'json' => [ |
| 68 | + 'bar' => 'pull-request-7135/bar/invalid-uuid', |
| 69 | + ], |
| 70 | + ]); |
| 71 | + |
| 72 | + self::assertEquals(400, $response->getStatusCode()); |
| 73 | + self::assertJsonContains(['detail' => 'Identifier "id" could not be transformed.']); |
| 74 | + } |
| 75 | + |
| 76 | + public function testInvalidGetRequestWhenIdentifierIsUuid(): void |
| 77 | + { |
| 78 | + $container = static::getContainer(); |
| 79 | + if ('mongodb' === $container->getParameter('kernel.environment')) { |
| 80 | + $this->markTestSkipped(); |
| 81 | + } |
| 82 | + |
| 83 | + $response = self::createClient()->request('GET', '/pull-request-7135/bar/invalid-uuid'); |
| 84 | + |
| 85 | + self::assertEquals(404, $response->getStatusCode()); |
| 86 | + } |
| 87 | + |
| 88 | + protected function loadBarFixture(): Bar |
| 89 | + { |
| 90 | + $container = static::getContainer(); |
| 91 | + $registry = $container->get('doctrine'); |
| 92 | + $manager = $registry->getManager(); |
| 93 | + |
| 94 | + $bar = new Bar(Uuid::fromString('0196b66f-66bd-780b-95fe-0ce987a32357')); |
| 95 | + $bar->title = 'Bar one'; |
| 96 | + $manager->persist($bar); |
| 97 | + |
| 98 | + $manager->flush(); |
| 99 | + |
| 100 | + return $bar; |
| 101 | + } |
| 102 | + |
| 103 | + protected function tearDown(): void |
| 104 | + { |
| 105 | + $container = static::getContainer(); |
| 106 | + $registry = $container->get('doctrine'); |
| 107 | + $manager = $registry->getManager(); |
| 108 | + if (!$manager instanceof EntityManagerInterface) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + $classes = []; |
| 113 | + foreach (self::getResources() as $entityClass) { |
| 114 | + $classes[] = $manager->getClassMetadata($entityClass); |
| 115 | + } |
| 116 | + |
| 117 | + $schemaTool = new SchemaTool($manager); |
| 118 | + @$schemaTool->dropSchema($classes); |
| 119 | + parent::tearDown(); |
| 120 | + } |
| 121 | +} |
0 commit comments