|
| 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\JsonApi; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonApi\ClientGeneratedId; |
| 18 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 19 | + |
| 20 | +final class ClientGeneratedIdTest extends ApiTestCase |
| 21 | +{ |
| 22 | + use SetupClassResourcesTrait; |
| 23 | + |
| 24 | + protected static ?bool $alwaysBootKernel = false; |
| 25 | + |
| 26 | + /** |
| 27 | + * @return class-string[] |
| 28 | + */ |
| 29 | + public static function getResources(): array |
| 30 | + { |
| 31 | + return [ClientGeneratedId::class]; |
| 32 | + } |
| 33 | + |
| 34 | + public function testPostWithClientIdSucceedsWhenOptedIn(): void |
| 35 | + { |
| 36 | + $response = self::createClient()->request('POST', '/jsonapi_client_generated_ids_opt_in', [ |
| 37 | + 'headers' => [ |
| 38 | + 'Accept' => 'application/vnd.api+json', |
| 39 | + 'Content-Type' => 'application/vnd.api+json', |
| 40 | + ], |
| 41 | + 'json' => [ |
| 42 | + 'data' => [ |
| 43 | + 'type' => 'JsonApiClientGeneratedId', |
| 44 | + 'id' => 'client-uuid-42', |
| 45 | + 'attributes' => ['name' => 'created with client id'], |
| 46 | + ], |
| 47 | + ], |
| 48 | + ]); |
| 49 | + |
| 50 | + $this->assertResponseStatusCodeSame(201); |
| 51 | + $body = $response->toArray(); |
| 52 | + // Default identifier mode emits the IRI as `data.id`. The IRI must reflect the client-supplied id. |
| 53 | + $this->assertSame('/jsonapi_client_generated_ids/client-uuid-42', $body['data']['id']); |
| 54 | + $this->assertSame('created with client id', $body['data']['attributes']['name']); |
| 55 | + } |
| 56 | + |
| 57 | + public function testPostWithClientIdRejectedByDefault(): void |
| 58 | + { |
| 59 | + self::createClient()->request('POST', '/jsonapi_client_generated_ids', [ |
| 60 | + 'headers' => [ |
| 61 | + 'Accept' => 'application/vnd.api+json', |
| 62 | + 'Content-Type' => 'application/vnd.api+json', |
| 63 | + ], |
| 64 | + 'json' => [ |
| 65 | + 'data' => [ |
| 66 | + 'type' => 'JsonApiClientGeneratedId', |
| 67 | + 'id' => 'client-uuid-43', |
| 68 | + 'attributes' => ['name' => 'should fail'], |
| 69 | + ], |
| 70 | + ], |
| 71 | + ]); |
| 72 | + |
| 73 | + $this->assertResponseStatusCodeSame(400); |
| 74 | + } |
| 75 | + |
| 76 | + public function testPostWithoutClientIdStillSucceeds(): void |
| 77 | + { |
| 78 | + $response = self::createClient()->request('POST', '/jsonapi_client_generated_ids', [ |
| 79 | + 'headers' => [ |
| 80 | + 'Accept' => 'application/vnd.api+json', |
| 81 | + 'Content-Type' => 'application/vnd.api+json', |
| 82 | + ], |
| 83 | + 'json' => [ |
| 84 | + 'data' => [ |
| 85 | + 'type' => 'JsonApiClientGeneratedId', |
| 86 | + 'attributes' => ['name' => 'server-side id'], |
| 87 | + ], |
| 88 | + ], |
| 89 | + ]); |
| 90 | + |
| 91 | + $this->assertResponseStatusCodeSame(201); |
| 92 | + $body = $response->toArray(); |
| 93 | + $this->assertSame('server-side id', $body['data']['attributes']['name']); |
| 94 | + } |
| 95 | +} |
0 commit comments