|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ApiPlatform\Tests\Functional\PullRequests; |
| 6 | + |
| 7 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 8 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\PullRequest7135\Bar; |
| 9 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\PullRequest7135\Foo; |
| 10 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 11 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 12 | +use Doctrine\ORM\EntityManagerInterface; |
| 13 | +use Doctrine\ORM\Tools\SchemaTool; |
| 14 | +use Symfony\Component\Uid\Uuid; |
| 15 | + |
| 16 | +class PullRequest7135Test extends ApiTestCase |
| 17 | +{ |
| 18 | + use RecreateSchemaTrait; |
| 19 | + use SetupClassResourcesTrait; |
| 20 | + |
| 21 | + protected static ?bool $alwaysBootKernel = false; |
| 22 | + |
| 23 | + /** |
| 24 | + * @return class-string[] |
| 25 | + */ |
| 26 | + public static function getResources(): array |
| 27 | + { |
| 28 | + return [Bar::class, Foo::class]; |
| 29 | + } |
| 30 | + |
| 31 | + public function testValidPostRequestWithIriWhenIdentifierIsUuid(): void |
| 32 | + { |
| 33 | + $this->recreateSchema(self::getResources()); |
| 34 | + $bar = $this->loadFixtures(); |
| 35 | + |
| 36 | + $response = self::createClient()->request('POST', '/pull-request-7135/foo/', [ |
| 37 | + 'json' => [ |
| 38 | + 'bar' => 'pull-request-7135/bar/' . $bar->id |
| 39 | + ] |
| 40 | + ]); |
| 41 | + |
| 42 | + self::assertEquals(201, $response->getStatusCode()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testInvalidPostRequestWithIriWhenIdentifierIsUuid(): void |
| 46 | + { |
| 47 | + $response = self::createClient()->request('POST', '/pull-request-7135/foo/', [ |
| 48 | + 'json' => [ |
| 49 | + 'bar' => 'pull-request-7135/bar/invalid-uuid' |
| 50 | + ] |
| 51 | + ]); |
| 52 | + |
| 53 | + self::assertEquals(400, $response->getStatusCode()); |
| 54 | + self::assertJsonContains(['detail' => 'Identifier "id" could not be transformed.']); |
| 55 | + } |
| 56 | + |
| 57 | + public function testInvalidGetRequestWhenIdentifierIsUuid(): void |
| 58 | + { |
| 59 | + $response = self::createClient()->request('GET', '/pull-request-7135/bar/invalid-uuid'); |
| 60 | + |
| 61 | + self::assertEquals(404, $response->getStatusCode()); |
| 62 | + self::assertJsonContains(['detail' => 'Invalid uri variables.']); |
| 63 | + } |
| 64 | + |
| 65 | + protected function loadFixtures(): Bar |
| 66 | + { |
| 67 | + $container = static::getContainer(); |
| 68 | + $registry = $container->get('doctrine'); |
| 69 | + $manager = $registry->getManager(); |
| 70 | + |
| 71 | + $bar = new Bar(Uuid::fromString('0196b66f-66bd-780b-95fe-0ce987a32357')); |
| 72 | + $bar->title = 'Bar one'; |
| 73 | + $manager->persist($bar); |
| 74 | + |
| 75 | + $manager->flush(); |
| 76 | + |
| 77 | + return $bar; |
| 78 | + } |
| 79 | + |
| 80 | + protected function tearDown(): void |
| 81 | + { |
| 82 | + $container = static::getContainer(); |
| 83 | + $registry = $container->get('doctrine'); |
| 84 | + $manager = $registry->getManager(); |
| 85 | + if (!$manager instanceof EntityManagerInterface) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + $classes = []; |
| 90 | + foreach (self::getResources() as $entityClass) { |
| 91 | + $classes[] = $manager->getClassMetadata($entityClass); |
| 92 | + } |
| 93 | + |
| 94 | + $schemaTool = new SchemaTool($manager); |
| 95 | + @$schemaTool->dropSchema($classes); |
| 96 | + parent::tearDown(); |
| 97 | + } |
| 98 | +} |
0 commit comments