|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ApiPlatform\Tests\Functional; |
| 4 | + |
| 5 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 6 | +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource; |
| 7 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity; |
| 8 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 9 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 10 | + |
| 11 | +final class MappingTest extends ApiTestCase |
| 12 | +{ |
| 13 | + use SetupClassResourcesTrait; |
| 14 | + use RecreateSchemaTrait; |
| 15 | + |
| 16 | + /** |
| 17 | + * @return class-string[] |
| 18 | + */ |
| 19 | + public static function getResources(): array |
| 20 | + { |
| 21 | + return [MappedResource::class]; |
| 22 | + } |
| 23 | + |
| 24 | + public function testShouldMapBetweenResourceAndEntity(): void |
| 25 | + { |
| 26 | + $this->recreateSchema([MappedEntity::class]); |
| 27 | + $this->loadFixtures(); |
| 28 | + self::createClient()->request('GET', 'mapped_resources'); |
| 29 | + $this->assertJsonContains(['member' => [ |
| 30 | + ['username' => 'B0 A0'], |
| 31 | + ['username' => 'B1 A1'], |
| 32 | + ['username' => 'B2 A2'], |
| 33 | + ]]); |
| 34 | + |
| 35 | + $r = self::createClient()->request('POST', 'mapped_resources', ['json' => ['username' => 'so yuka']]); |
| 36 | + $this->assertJsonContains(['username' => 'so yuka']); |
| 37 | + |
| 38 | + $manager = $this->getManager(); |
| 39 | + $repo = $manager->getRepository(MappedEntity::class); |
| 40 | + $persisted = $repo->findOneBy(['id' => $r->toArray()['id']]); |
| 41 | + $this->assertSame('so', $persisted->getFirstName()); |
| 42 | + $this->assertSame('yuka', $persisted->getLastName()); |
| 43 | + |
| 44 | + $uri = $r->toArray()['@id']; |
| 45 | + self::createClient()->request('GET', $uri); |
| 46 | + $this->assertJsonContains(['username' => 'so yuka']); |
| 47 | + |
| 48 | + $r = self::createClient()->request('PATCH', $uri, ['json' => ['username' => 'ba zar'], 'headers' => ['content-type' => 'application/merge-patch+json']]); |
| 49 | + $this->assertJsonContains(['username' => 'ba zar']); |
| 50 | + } |
| 51 | + |
| 52 | + private function loadFixtures(): void { |
| 53 | + $manager = $this->getManager(); |
| 54 | + |
| 55 | + for ($i=0; $i < 10; $i++) { |
| 56 | + $e = new MappedEntity; |
| 57 | + $e->setLastName('A'.$i); |
| 58 | + $e->setFirstName('B'.$i); |
| 59 | + $manager->persist($e); |
| 60 | + } |
| 61 | + |
| 62 | + $manager->flush(); |
| 63 | + } |
| 64 | +} |
0 commit comments