diff --git a/tests/EventListener/JsonResponseExceptionListener.php b/tests/EventListener/JsonResponseExceptionListener.php new file mode 100644 index 00000000..675edc30 --- /dev/null +++ b/tests/EventListener/JsonResponseExceptionListener.php @@ -0,0 +1,38 @@ +createMock(HttpKernelInterface::class); + $request = new Request(); + $request->headers->set('Accept', 'application/json'); + $request->headers->set('Content-Type', 'application/json'); + + $exception = new HttpException(400, 'Bad Request'); + $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); + + $listener->onKernelException($event); + + $response = $event->getResponse(); + + $this->assertInstanceOf(JsonResponse::class, $response); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertJson($response->getContent()); + $data = json_decode($response->getContent(), true); + $this->assertArrayHasKey('error', $data); + $this->assertStringContainsString('Bad Request', $data['error']); + } +}