|
| 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\Issue6465\Bar; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Foo; |
| 19 | +use Doctrine\ORM\EntityManagerInterface; |
| 20 | +use Doctrine\ORM\Tools\SchemaTool; |
| 21 | + |
| 22 | +class JsonLd extends ApiTestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * The input DTO denormalizes an existing Doctrine entity. |
| 26 | + */ |
| 27 | + public function testIssue6465(): void |
| 28 | + { |
| 29 | + $container = static::getContainer(); |
| 30 | + if ('mongodb' === $container->getParameter('kernel.environment')) { |
| 31 | + $this->markTestSkipped(); |
| 32 | + } |
| 33 | + |
| 34 | + $response = self::createClient()->request('POST', '/foo/1/validate', [ |
| 35 | + 'json' => ['bar' => '/bar6465s/2'], |
| 36 | + ]); |
| 37 | + |
| 38 | + $res = $response->toArray(); |
| 39 | + $this->assertEquals('Bar two', $res['title']); |
| 40 | + } |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + self::bootKernel(); |
| 45 | + |
| 46 | + $container = static::getContainer(); |
| 47 | + $registry = $container->get('doctrine'); |
| 48 | + $manager = $registry->getManager(); |
| 49 | + if (!$manager instanceof EntityManagerInterface) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $classes = []; |
| 54 | + foreach ([Foo::class, Bar::class] as $entityClass) { |
| 55 | + $classes[] = $manager->getClassMetadata($entityClass); |
| 56 | + } |
| 57 | + |
| 58 | + $schemaTool = new SchemaTool($manager); |
| 59 | + @$schemaTool->createSchema($classes); |
| 60 | + |
| 61 | + $foo = new Foo(); |
| 62 | + $foo->title = 'Foo'; |
| 63 | + $manager->persist($foo); |
| 64 | + $bar = new Bar(); |
| 65 | + $bar->title = 'Bar one'; |
| 66 | + $manager->persist($bar); |
| 67 | + $bar2 = new Bar(); |
| 68 | + $bar2->title = 'Bar two'; |
| 69 | + $manager->persist($bar2); |
| 70 | + $manager->flush(); |
| 71 | + } |
| 72 | + |
| 73 | + protected function tearDown(): void |
| 74 | + { |
| 75 | + $container = static::getContainer(); |
| 76 | + $registry = $container->get('doctrine'); |
| 77 | + $manager = $registry->getManager(); |
| 78 | + if (!$manager instanceof EntityManagerInterface) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + $classes = []; |
| 83 | + foreach ([Foo::class, Bar::class] as $entityClass) { |
| 84 | + $classes[] = $manager->getClassMetadata($entityClass); |
| 85 | + } |
| 86 | + |
| 87 | + $schemaTool = new SchemaTool($manager); |
| 88 | + @$schemaTool->dropSchema($classes); |
| 89 | + parent::tearDown(); |
| 90 | + } |
| 91 | +} |
0 commit comments