diff options
author | Arnold Daniels <arnold@jasny.net> | 2016-10-20 19:50:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-20 19:50:05 +0200 |
commit | 17e7a0af39e479c554d5dc4a064678d9fde71fc0 (patch) | |
tree | ab4a4f26492ac3c0262f801a8f90251eff1cfdf2 /tests/Router/Middleware/ErrorHandlerTest.php | |
parent | bbd7fe10d23e838271c94dbca59dd986cb7c8620 (diff) | |
parent | af57d0a805b1abdf1bd529902d820a586b3b7455 (diff) | |
download | router-origin/router-cleanup.zip router-origin/router-cleanup.tar.gz router-origin/router-cleanup.tar.bz2 |
Merge pull request #10 from Minstel/middleware-errorsorigin/router-cleanup
Middleware errors
Diffstat (limited to 'tests/Router/Middleware/ErrorHandlerTest.php')
-rw-r--r-- | tests/Router/Middleware/ErrorHandlerTest.php | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/Router/Middleware/ErrorHandlerTest.php b/tests/Router/Middleware/ErrorHandlerTest.php new file mode 100644 index 0000000..c4b6313 --- /dev/null +++ b/tests/Router/Middleware/ErrorHandlerTest.php @@ -0,0 +1,86 @@ +<?php + +use Jasny\Router\Middleware\ErrorHandler; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; + +class ErrorHandlerTest extends PHPUnit_Framework_TestCase +{ + /** + * Test invoke with invalid 'next' param + */ + public function testInvokeInvalidNext() + { + $middleware = new ErrorHandler(); + list($request, $response) = $this->getRequests(); + + $this->expectException(\InvalidArgumentException::class); + + $result = $middleware($request, $response, 'not_callable'); + } + + /** + * Test that exception in 'next' callback is caught + */ + public function testInvokeCatchError() + { + $middleware = new ErrorHandler(); + list($request, $response) = $this->getRequests(); + + $this->expectCatchError($response); + + $result = $middleware($request, $response, function($request, $response) { + throw new Exception('Test exception'); + }); + + $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object"); + } + + /** + * Test case when there is no error + */ + public function testInvokeNoError() + { + $middleware = new ErrorHandler(); + list($request, $response) = $this->getRequests(); + + $result = $middleware($request, $response, function($request, $response) { + $response->nextCalled = true; + + return $response; + }); + + $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object"); + $this->assertTrue($result->nextCalled, "'next' was not called"); + } + + /** + * Get requests for testing + * + * @return array + */ + public function getRequests() + { + $request = $this->createMock(ServerRequestInterface::class); + $response = $this->createMock(ResponseInterface::class); + + return [$request, $response]; + } + + /** + * Expect for error + * + * @param ResponseInterface $response + */ + public function expectCatchError($response) + { + $stream = $this->createMock(StreamInterface::class); + $stream->expects($this->once())->method('rewind'); + $stream->expects($this->once())->method('write')->with($this->equalTo('Unexpected error')); + + $response->method('getBody')->will($this->returnValue($stream)); + $response->expects($this->once())->method('withBody')->with($this->equalTo($stream))->will($this->returnSelf()); + $response->expects($this->once())->method('withStatus')->with($this->equalTo(500), $this->equalTo('Internal Server Error'))->will($this->returnSelf()); + } +} |