diff --git a/src/Presentation/Api/V1/Exceptions/ApiExceptionRenderer.php b/src/Presentation/Api/V1/Exceptions/ApiExceptionRenderer.php index 73cef45..402359b 100644 --- a/src/Presentation/Api/V1/Exceptions/ApiExceptionRenderer.php +++ b/src/Presentation/Api/V1/Exceptions/ApiExceptionRenderer.php @@ -33,9 +33,9 @@ final readonly class ApiExceptionRenderer ], 404, ['Content-Type' => self::CONTENT_TYPE]), $e instanceof ValidationException => response()->json([ - 'type' => (string) $baseUri->withPath('/errors/400'), - 'title' => 'Bad Request', - 'status' => 400, + 'type' => (string) $baseUri->withPath('/errors/422'), + 'title' => 'Unprocessable Entity', + 'status' => 422, 'detail' => $e->getMessage(), 'invalidParams' => array_values(array_map( static function (int | string $field, mixed $messages): array { @@ -56,7 +56,7 @@ final readonly class ApiExceptionRenderer array_keys($e->errors()), array_values($e->errors()) )), - ], 400, ['Content-Type' => self::CONTENT_TYPE]), + ], 422, ['Content-Type' => self::CONTENT_TYPE]), $e instanceof \DomainException => response()->json([ 'type' => (string) $baseUri->withPath('/errors/422'), diff --git a/tests/Feature/ErrorDocumentationTest.php b/tests/Feature/ErrorDocumentationTest.php index 3abc3ea..b1870fa 100644 --- a/tests/Feature/ErrorDocumentationTest.php +++ b/tests/Feature/ErrorDocumentationTest.php @@ -4,23 +4,38 @@ declare(strict_types=1); namespace Tests\Feature; +use Symfony\Component\HttpFoundation\Response; + describe('Error Documentation Pages', function (): void { - it('renders the succinct documentation view for valid error codes', function (): void { + it('renders the documentation view for all valid HTTP error codes', function (int $code): void { // GIVEN /** @var \Tests\TestCase $test */ - $test = $this; - $code = 404; + $test = $this; + $expectedTitle = Response::$statusTexts[$code] ?? 'Error'; // WHEN $response = $test->get("/errors/{$code}"); // THEN $response->assertStatus(200); - $response->assertSee('Not Found'); - $response->assertSee('404'); - }); + $response->assertSee($expectedTitle); + $response->assertSee((string) $code); + $response->assertSee('Conforme à la spécification RFC 7807 Problem Details'); + })->with([400, 404, 422, 429, 500, 503]); - it('returns a 404 for an invalid error code slug', function (): void { + it('returns a 404 when requesting documentation for an unsupported error code', function (int $code): void { + // GIVEN + /** @var \Tests\TestCase $test */ + $test = $this; + + // WHEN + $response = $test->get("/errors/{$code}"); + + // THEN + $response->assertStatus(404); + })->with([0, 418, 999]); + + it('returns a 404 for a non-numeric error code slug', function (): void { // GIVEN /** @var \Tests\TestCase $test */ $test = $this;