|
| 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\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonApi\ErrorProblem; |
| 18 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 19 | + |
| 20 | +final class ErrorObjectMembersTest extends ApiTestCase |
| 21 | +{ |
| 22 | + use SetupClassResourcesTrait; |
| 23 | + |
| 24 | + protected static ?bool $alwaysBootKernel = false; |
| 25 | + |
| 26 | + public static function getResources(): array |
| 27 | + { |
| 28 | + return [ErrorProblem::class]; |
| 29 | + } |
| 30 | + |
| 31 | + public function testValidationErrorHasStatusAndTitle(): void |
| 32 | + { |
| 33 | + $response = self::createClient()->request('POST', '/jsonapi_validation_problem', [ |
| 34 | + 'headers' => ['accept' => 'application/vnd.api+json', 'content-type' => 'application/vnd.api+json'], |
| 35 | + 'json' => ['data' => ['type' => 'JsonApiErrorProblem', 'attributes' => new \stdClass()]], |
| 36 | + ]); |
| 37 | + |
| 38 | + $this->assertResponseStatusCodeSame(422); |
| 39 | + $error = $response->toArray(false)['errors'][0]; |
| 40 | + $this->assertSame('422', $error['status'] ?? null); |
| 41 | + $this->assertArrayHasKey('title', $error); |
| 42 | + } |
| 43 | + |
| 44 | + public function testValidationErrorPointerIsAJsonPointer(): void |
| 45 | + { |
| 46 | + $response = self::createClient()->request('POST', '/jsonapi_validation_problem', [ |
| 47 | + 'headers' => ['accept' => 'application/vnd.api+json', 'content-type' => 'application/vnd.api+json'], |
| 48 | + 'json' => ['data' => ['type' => 'JsonApiErrorProblem', 'attributes' => new \stdClass()]], |
| 49 | + ]); |
| 50 | + |
| 51 | + $this->assertResponseStatusCodeSame(422); |
| 52 | + $this->assertSame('/data/attributes/name', $response->toArray(false)['errors'][0]['source']['pointer']); |
| 53 | + } |
| 54 | + |
| 55 | + public function testValidationErrorStatusFollowsTheResponseStatus(): void |
| 56 | + { |
| 57 | + $response = self::createClient()->request('POST', '/jsonapi_validation_problem_bad_request', [ |
| 58 | + 'headers' => ['accept' => 'application/vnd.api+json', 'content-type' => 'application/vnd.api+json'], |
| 59 | + 'json' => ['data' => ['type' => 'JsonApiErrorProblem', 'attributes' => new \stdClass()]], |
| 60 | + ]); |
| 61 | + |
| 62 | + $this->assertResponseStatusCodeSame(400); |
| 63 | + $this->assertSame('400', $response->toArray(false)['errors'][0]['status'] ?? null); |
| 64 | + } |
| 65 | +} |
0 commit comments