|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 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\ApiResource\MappedResourceWithOutput; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedOutputEntity; |
| 19 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 20 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 21 | + |
| 22 | +/** |
| 23 | + * Verifies the behavior of write operations declaring an `output` DTO with the ObjectMapper. |
| 24 | + */ |
| 25 | +final class MappedResourceOutputTest extends ApiTestCase |
| 26 | +{ |
| 27 | + use RecreateSchemaTrait; |
| 28 | + use SetupClassResourcesTrait; |
| 29 | + protected static ?bool $alwaysBootKernel = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * @return class-string[] |
| 33 | + */ |
| 34 | + public static function getResources(): array |
| 35 | + { |
| 36 | + return [MappedResourceWithOutput::class]; |
| 37 | + } |
| 38 | + |
| 39 | + public function testPostWithOutputDtoReturnsOutputDto(): void |
| 40 | + { |
| 41 | + if (!$this->getContainer()->has('api_platform.object_mapper')) { |
| 42 | + $this->markTestSkipped('ObjectMapper not installed'); |
| 43 | + } |
| 44 | + |
| 45 | + if ($this->isMongoDB()) { |
| 46 | + $this->markTestSkipped('MongoDB not tested'); |
| 47 | + } |
| 48 | + |
| 49 | + $this->recreateSchema([MappedOutputEntity::class]); |
| 50 | + |
| 51 | + $client = self::createClient(); |
| 52 | + $response = $client->request('POST', '/mapped_resource_with_outputs', [ |
| 53 | + 'json' => ['name' => 'a name', 'description' => 'a description'], |
| 54 | + ]); |
| 55 | + |
| 56 | + fwrite(\STDERR, "\n=== POST status: ".$response->getStatusCode()."\n"); |
| 57 | + fwrite(\STDERR, "=== POST response body ===\n".substr($response->getContent(false), 0, 2000)."\n"); |
| 58 | + fwrite(\STDERR, '=== Location header: '.var_export($response->getHeaders(false)['location'][0] ?? null, true)."\n"); |
| 59 | + fwrite(\STDERR, '=== Content-Location header: '.var_export($response->getHeaders(false)['content-location'][0] ?? null, true)."\n"); |
| 60 | + |
| 61 | + self::assertResponseStatusCodeSame(201); |
| 62 | + $data = $response->toArray(false); |
| 63 | + |
| 64 | + // The output DTO exposes "name" but not "description" |
| 65 | + $this->assertArrayHasKey('name', $data); |
| 66 | + $this->assertArrayNotHasKey('description', $data, 'Response should be the output DTO, not the resource'); |
| 67 | + |
| 68 | + // Item IRI expected on a 201, not the collection IRI |
| 69 | + $location = $response->getHeaders(false)['location'][0] ?? null; |
| 70 | + $this->assertNotNull($location); |
| 71 | + $this->assertMatchesRegularExpression('~^/mapped_resource_with_outputs/\d+$~', $location, 'Location must be the item IRI'); |
| 72 | + $this->assertSame($location, $data['@id'] ?? null, '@id must be the item IRI'); |
| 73 | + } |
| 74 | + |
| 75 | + public function testPatchWithOutputDtoPreservesUnsentFields(): void |
| 76 | + { |
| 77 | + if (!$this->getContainer()->has('api_platform.object_mapper')) { |
| 78 | + $this->markTestSkipped('ObjectMapper not installed'); |
| 79 | + } |
| 80 | + |
| 81 | + if ($this->isMongoDB()) { |
| 82 | + $this->markTestSkipped('MongoDB not tested'); |
| 83 | + } |
| 84 | + |
| 85 | + $this->recreateSchema([MappedOutputEntity::class]); |
| 86 | + |
| 87 | + $manager = $this->getManager(); |
| 88 | + $entity = new MappedOutputEntity(); |
| 89 | + $entity->name = 'original name'; |
| 90 | + $entity->description = 'original description'; |
| 91 | + $manager->persist($entity); |
| 92 | + $manager->flush(); |
| 93 | + $id = $entity->id; |
| 94 | + $manager->clear(); |
| 95 | + |
| 96 | + $client = self::createClient(); |
| 97 | + $response = $client->request('PATCH', '/mapped_resource_with_outputs/'.$id, [ |
| 98 | + 'headers' => ['content-type' => 'application/merge-patch+json'], |
| 99 | + 'json' => ['name' => 'updated name'], |
| 100 | + ]); |
| 101 | + |
| 102 | + fwrite(\STDERR, "\n=== PATCH status: ".$response->getStatusCode()."\n"); |
| 103 | + fwrite(\STDERR, "=== PATCH response body ===\n".substr($response->getContent(false), 0, 2000)."\n"); |
| 104 | + |
| 105 | + self::assertResponseIsSuccessful(); |
| 106 | + $data = $response->toArray(false); |
| 107 | + |
| 108 | + // The response must be the output DTO with a proper item IRI |
| 109 | + $this->assertArrayHasKey('name', $data); |
| 110 | + $this->assertArrayNotHasKey('description', $data, 'Response should be the output DTO, not the resource'); |
| 111 | + $this->assertSame('/mapped_resource_with_outputs/'.$id, $data['@id']); |
| 112 | + |
| 113 | + // PATCH semantics: unsent fields must be preserved on the entity |
| 114 | + $manager = $this->getManager(); |
| 115 | + $manager->clear(); |
| 116 | + $persisted = $manager->getRepository(MappedOutputEntity::class)->find($id); |
| 117 | + $this->assertSame('updated name', $persisted->name); |
| 118 | + $this->assertSame('original description', $persisted->description, 'PATCH must not touch fields the client did not send'); |
| 119 | + } |
| 120 | +} |
0 commit comments