diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-29 13:58:50 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-29 23:46:32 +0200 |
commit | a2c47304d8230f1937c2a4f019aba89fa74203bc (patch) | |
tree | 7c981fc1577f865600a22b655349587f188c7410 /tests/Unit/Middleware/ErrorHandlerTest.php | |
parent | 427315195bdd379a0207fc9b2aaf69a5b5b86c79 (diff) |
Added generic error pages
Diffstat (limited to 'tests/Unit/Middleware/ErrorHandlerTest.php')
-rw-r--r-- | tests/Unit/Middleware/ErrorHandlerTest.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/Unit/Middleware/ErrorHandlerTest.php b/tests/Unit/Middleware/ErrorHandlerTest.php new file mode 100644 index 00000000..abf9c52f --- /dev/null +++ b/tests/Unit/Middleware/ErrorHandlerTest.php @@ -0,0 +1,88 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware; + +use Engelsystem\Http\Response; +use Engelsystem\Middleware\ErrorHandler; +use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Twig_LoaderInterface as TwigLoader; + +class ErrorHandlerTest extends TestCase +{ + /** + * @covers \Engelsystem\Middleware\ErrorHandler::__construct + * @covers \Engelsystem\Middleware\ErrorHandler::process + * @covers \Engelsystem\Middleware\ErrorHandler::selectView + */ + public function testProcess() + { + /** @var TwigLoader|MockObject $twigLoader */ + $twigLoader = $this->createMock(TwigLoader::class); + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + /** @var ResponseInterface|MockObject $psrResponse */ + $psrResponse = $this->getMockForAbstractClass(ResponseInterface::class); + $returnResponseHandler = new ReturnResponseMiddlewareHandler($psrResponse); + + $psrResponse->expects($this->once()) + ->method('getStatusCode') + ->willReturn(505); + + $errorHandler = new ErrorHandler($twigLoader); + + $return = $errorHandler->process($request, $returnResponseHandler); + $this->assertEquals($psrResponse, $return, 'Plain PSR-7 Response should be passed directly'); + + /** @var Response|MockObject $response */ + $response = $this->createMock(Response::class); + + $response->expects($this->exactly(3)) + ->method('getStatusCode') + ->willReturnOnConsecutiveCalls( + 200, + 418, + 505 + ); + + $returnResponseHandler->setResponse($response); + $return = $errorHandler->process($request, $returnResponseHandler); + $this->assertEquals($response, $return, 'Only Responses >= 400 should be processed'); + + $twigLoader->expects($this->exactly(4)) + ->method('exists') + ->withConsecutive( + ['errors/418'], + ['errors/4'], + ['errors/400'], + ['errors/505'] + ) + ->willReturnOnConsecutiveCalls( + false, + false, + false, + true + ); + + $response->expects($this->exactly(2)) + ->method('getContent') + ->willReturnOnConsecutiveCalls( + 'Teapot', + 'Internal Error!' + ); + + $response->expects($this->exactly(2)) + ->method('withView') + ->withConsecutive( + ['errors/default', ['status' => 418, 'content' => 'Teapot'], 418], + ['errors/505', ['status' => 505, 'content' => 'Internal Error!'], 505] + ) + ->willReturn($response); + + $errorHandler->process($request, $returnResponseHandler); + $errorHandler->process($request, $returnResponseHandler); + } +} |