|
| 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\JsonApi\Tests\Serializer; |
| 15 | + |
| 16 | +use ApiPlatform\JsonApi\Serializer\ErrorNormalizer; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 19 | + |
| 20 | +class ErrorNormalizerTest extends TestCase |
| 21 | +{ |
| 22 | + public function testTypeIsOnlyExposedUnderLinks(): void |
| 23 | + { |
| 24 | + $itemNormalizer = $this->createStub(NormalizerInterface::class); |
| 25 | + $itemNormalizer->method('normalize')->willReturn(['data' => [ |
| 26 | + 'id' => '/errors/500', |
| 27 | + 'attributes' => ['type' => '/errors/500', 'title' => 'An error occurred', 'status' => 500], |
| 28 | + ]]); |
| 29 | + |
| 30 | + $errors = (new ErrorNormalizer($itemNormalizer))->normalize(new \Exception('boom'), 'jsonapi')['errors']; |
| 31 | + |
| 32 | + $this->assertCount(1, $errors); |
| 33 | + $this->assertArrayNotHasKey('type', $errors[0]); |
| 34 | + $this->assertSame(['type' => '/errors/500'], $errors[0]['links']); |
| 35 | + $this->assertSame('500', $errors[0]['status']); |
| 36 | + } |
| 37 | + |
| 38 | + public function testViolationsAreExposedWithoutTopLevelType(): void |
| 39 | + { |
| 40 | + $itemNormalizer = $this->createStub(NormalizerInterface::class); |
| 41 | + $itemNormalizer->method('normalize')->willReturn(['data' => [ |
| 42 | + 'id' => '/validation_errors/abc', |
| 43 | + 'attributes' => [ |
| 44 | + 'type' => '/validation_errors/abc', |
| 45 | + 'title' => 'Validation Failed', |
| 46 | + 'status' => 422, |
| 47 | + 'violations' => [['propertyPath' => 'name', 'message' => 'This value should not be blank.']], |
| 48 | + ], |
| 49 | + ]]); |
| 50 | + |
| 51 | + $errors = (new ErrorNormalizer($itemNormalizer))->normalize(new \Exception('boom'), 'jsonapi')['errors']; |
| 52 | + |
| 53 | + $this->assertCount(1, $errors); |
| 54 | + $this->assertArrayNotHasKey('type', $errors[0]); |
| 55 | + $this->assertArrayNotHasKey('violations', $errors[0]); |
| 56 | + $this->assertSame(['type' => '/validation_errors/abc/name'], $errors[0]['links']); |
| 57 | + $this->assertSame('This value should not be blank.', $errors[0]['detail']); |
| 58 | + } |
| 59 | +} |
0 commit comments