.../DefaultExceptionSubscriberTest.php | 80 +++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/core/modules/serialization/tests/src/Unit/EventSubscriber/DefaultExceptionSubscriberTest.php b/core/modules/serialization/tests/src/Unit/EventSubscriber/DefaultExceptionSubscriberTest.php index e20f6f7..bead596 100644 --- a/core/modules/serialization/tests/src/Unit/EventSubscriber/DefaultExceptionSubscriberTest.php +++ b/core/modules/serialization/tests/src/Unit/EventSubscriber/DefaultExceptionSubscriberTest.php @@ -1,6 +1,6 @@ reveal(), $request, 'GET', $e); - $subscriber = new DefaultExceptionSubscriber(new Serializer([], [new JsonEncoder()]), []); + $subscriber = new DefaultExceptionSubscriber(new Serializer([], [new JsonEncoder()]), ['json']); $subscriber->on4xx($event); $response = $event->getResponse(); @@ -39,4 +40,79 @@ public function testOn4xx() { $this->assertEquals('application/json', $response->headers->get('Content-Type')); } + /** + * @covers ::on500 + */ + public function testOn500() { + $kernel = $this->prophesize(HttpKernelInterface::class); + $request = Request::create('/test'); + $request->setRequestFormat('json'); + + $e = new HttpException(500, 'Internal Server Error'); + $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e); + $subscriber = new DefaultExceptionSubscriber(new Serializer([], [new JsonEncoder()]), ['json']); + $subscriber->on500($event); + $response = $event->getResponse(); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('{"message":"Internal Server Error"}', $response->getContent()); + $this->assertEquals(500, $response->getStatusCode()); + $this->assertEquals('application/json', $response->headers->get('Content-Type')); + } + + /** + * @covers ::on500 + */ + public function testOnFatalErrorDefault() { + $kernel = $this->prophesize(HttpKernelInterface::class); + $request = Request::create('/test'); + $request->setRequestFormat('json'); + + $e = new \InvalidArgumentException('Some explanation here.'); + $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e); + $subscriber = new DefaultExceptionSubscriber(new Serializer([], [new JsonEncoder()]), ['json']); + $subscriber->onException($event); + $response = $event->getResponse(); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('{"message":"A fatal error occurred."}', $response->getContent()); + $this->assertEquals(500, $response->getStatusCode()); + $this->assertEquals('application/json', $response->headers->get('Content-Type')); + } + + /** + * @covers ::on500 + */ + public function testOnFatalErrorVerbose() { + $kernel = $this->prophesize(HttpKernelInterface::class); + $request = Request::create('/test'); + $request->setRequestFormat('json'); + + $e = new \LogicException('Some explanation here.'); + $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e); + $subscriber = new DefaultExceptionSubscriber(new Serializer([], [new JsonEncoder()]), ['json']); + $subscriber->onException($event); + $response = $event->getResponse(); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('{"message":"A fatal error occurred: Some explanation here."}', $response->getContent()); + $this->assertEquals(500, $response->getStatusCode()); + $this->assertEquals('application/json', $response->headers->get('Content-Type')); + } + +} + +} + +namespace { + if (!function_exists('error_displayable')) { + function error_displayable($error = NULL) { + if ($error['%type'] === 'LogicException') { + return TRUE; + } + else { + return FALSE; + } + } + } }