From 6537a86c5b893d488adc08a1e0914912ab1917dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20B=C3=83=C2=A9rub=C3=83=C2=A9?= Date: Sat, 4 Jan 2020 20:03:18 -0700 Subject: [PATCH] add tests for multiple error exception handlers --- tests/Middleware/ErrorMiddlewareTest.php | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/Middleware/ErrorMiddlewareTest.php b/tests/Middleware/ErrorMiddlewareTest.php index 3ebfc8d52..8326f0960 100644 --- a/tests/Middleware/ErrorMiddlewareTest.php +++ b/tests/Middleware/ErrorMiddlewareTest.php @@ -1,4 +1,5 @@ expectOutputString('Oops..'); } + public function testHandleMultipleExceptionsAddedAsArray() + { + $responseFactory = $this->getResponseFactory(); + $app = new App($responseFactory); + $callableResolver = $app->getCallableResolver(); + + $mw = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false); + + $app->add(function ($request, $handler) { + throw new InvalidArgumentException('This is an invalid argument exception...'); + }); + + $handler = (function (ServerRequestInterface $request, $exception) { + $response = $this->createResponse(); + $response->getBody()->write($exception->getMessage()); + return $response; + }); + + $mw->setErrorHandler([LogicException::class, InvalidArgumentException::class], $handler->bindTo($this)); + + $mw->setDefaultErrorHandler((function () { + $response = $this->createResponse(); + $response->getBody()->write('Oops..'); + return $response; + })->bindTo($this)); + + $app->add($mw); + + $app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) { + $response->getBody()->write('...'); + return $response; + }); + + $request = $this->createServerRequest('/foo'); + $app->run($request); + + $this->expectOutputString('This is an invalid argument exception...'); + } + public function testErrorHandlerHandlesThrowables() { $responseFactory = $this->getResponseFactory();