From a41c0438764ca0733d6adbf1b39801c998ed1171 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 9 Nov 2024 10:22:58 -0600 Subject: [PATCH] Add test --- .../ExceptionHandlerMiddlewareTest.php | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/Middleware/ExceptionHandlerMiddlewareTest.php diff --git a/test/Middleware/ExceptionHandlerMiddlewareTest.php b/test/Middleware/ExceptionHandlerMiddlewareTest.php new file mode 100644 index 00000000..13d26c08 --- /dev/null +++ b/test/Middleware/ExceptionHandlerMiddlewareTest.php @@ -0,0 +1,59 @@ +createMock(Client::class), 'GET', Http::createFromString('/')); + + $requestHandler = $this->createMock(RequestHandler::class); + $requestHandler->expects(self::once()) + ->method('handleRequest') + ->with($request) + ->willThrowException($exception); + + $middleware = new ExceptionHandlerMiddleware($exceptionHandler); + + return $middleware->handleRequest($request, $requestHandler); + } + + public function testUncaughtException(): void + { + $exception = new TestException(); + + $exceptionHandler = $this->createMock(ExceptionHandler::class); + $exceptionHandler->expects(self::once()) + ->method('handleException') + ->with($exception) + ->willReturn(new Response(HttpStatus::INTERNAL_SERVER_ERROR)); + + $this->setupAndInvokeMiddleware($exceptionHandler, $exception); + } + + public function testHttpErrorException(): void + { + $exception = new HttpErrorException(HttpStatus::BAD_REQUEST); + + $exceptionHandler = $this->createMock(ExceptionHandler::class); + $exceptionHandler->expects(self::never()) + ->method('handleException'); + + $this->expectExceptionObject($exception); + + $this->setupAndInvokeMiddleware($exceptionHandler, $exception); + } +}