From f3b3b6683ca90b70ec4d4daae002dc0caac9ebdd Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 11 Aug 2018 23:46:28 +0200 Subject: Added middleware tests --- tests/Unit/Middleware/ExceptionHandlerTest.php | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/Unit/Middleware/ExceptionHandlerTest.php (limited to 'tests/Unit/Middleware/ExceptionHandlerTest.php') diff --git a/tests/Unit/Middleware/ExceptionHandlerTest.php b/tests/Unit/Middleware/ExceptionHandlerTest.php new file mode 100644 index 00000000..6d2a20e6 --- /dev/null +++ b/tests/Unit/Middleware/ExceptionHandlerTest.php @@ -0,0 +1,58 @@ +getMockForAbstractClass(ContainerInterface::class); + /** @var MockObject|ServerRequestInterface $request */ + $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); + /** @var MockObject|ResponseInterface $response */ + $response = $this->getMockBuilder(Response::class)->getMock(); + /** @var MockObject|Handler $errorHandler */ + $errorHandler = $this->getMockBuilder(Handler::class)->getMock(); + $returnResponseHandler = new ReturnResponseMiddlewareHandler($response); + $throwExceptionHandler = new ExceptionMiddlewareHandler(); + + Application::setInstance($container); + + $container->expects($this->exactly(2)) + ->method('get') + ->withConsecutive(['error.handler'], ['psr7.response']) + ->willReturnOnConsecutiveCalls($errorHandler, $response); + + $response->expects($this->once()) + ->method('withContent') + ->willReturn($response); + $response->expects($this->once()) + ->method('withStatus') + ->with(500) + ->willReturn($response); + + $handler = new ExceptionHandler($container); + $return = $handler->process($request, $returnResponseHandler); + $this->assertEquals($response, $return); + + $return = $handler->process($request, $throwExceptionHandler); + $this->assertEquals($response, $return); + } +} -- cgit v1.2.3-54-g00ecf