diff options
Diffstat (limited to 'tests/Router/Middleware')
-rw-r--r-- | tests/Router/Middleware/BasePathTest.php | 142 | ||||
-rw-r--r-- | tests/Router/Middleware/ErrorHandlerTest.php | 86 | ||||
-rw-r--r-- | tests/Router/Middleware/NotFoundTest.php | 363 |
3 files changed, 233 insertions, 358 deletions
diff --git a/tests/Router/Middleware/BasePathTest.php b/tests/Router/Middleware/BasePathTest.php index eb9a2e8..d615d70 100644 --- a/tests/Router/Middleware/BasePathTest.php +++ b/tests/Router/Middleware/BasePathTest.php @@ -1,25 +1,22 @@ <?php +namespace Jasny\Router\Middleware; + use Jasny\Router\Middleware\BasePath; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; -class BasePathTest extends PHPUnit_Framework_TestCase -{ - /** - * Test creating middleware with invalid parameter - * - * @dataProvider invalidConstructProvider - */ - public function testInvalidConstruct($basePath) - { - $this->expectException(\InvalidArgumentException::class); - - $pathHandler = new BasePath($basePath); - } +use Jasny\Router\TestHelpers; +/** + * @covers Jasny\Router\Middleware\BasePath + */ +class BasePathTest extends \PHPUnit_Framework_TestCase +{ + use TestHelpers; + /** * Provide data for testing invalid BasePath creation * @@ -39,17 +36,14 @@ class BasePathTest extends PHPUnit_Framework_TestCase } /** - * Test creating BasePath instance + * Test creating middleware with invalid parameter * - * @dataProvider validConstructProvider - * @param string $basePath + * @dataProvider invalidConstructProvider + * @expectedException InvalidArgumentException */ - public function testValidConstruct($basePath, $validBasePath) + public function testInvalidConstruct($basePath) { - $pathHandler = new BasePath($basePath); - - $this->assertNotEmpty($pathHandler->getBasePath(), "Empty base path"); - $this->assertEquals($validBasePath, $pathHandler->getBasePath(), "Base path was not set correctly"); + new BasePath($basePath); } /** @@ -71,41 +65,32 @@ class BasePathTest extends PHPUnit_Framework_TestCase } /** - * Test invoke with invalid 'next' param + * Test creating BasePath instance + * + * @dataProvider validConstructProvider + * @param string $basePath */ - public function testInvokeInvalidNext() + public function testValidConstruct($basePath, $validBasePath) { - $middleware = new BasePath('/foo'); - list($request, $response) = $this->getRequests(); - - $this->expectException(\InvalidArgumentException::class); + $pathHandler = new BasePath($basePath); - $result = $middleware($request, $response, 'not_callable'); + $this->assertNotEmpty($pathHandler->getBasePath(), "Empty base path"); + $this->assertEquals($validBasePath, $pathHandler->getBasePath(), "Base path was not set correctly"); } /** - * Test case when given request path does not starts with given base path - * - * @dataProvider notFoundProvider - * @param string $basePath - * @param string $path + * Test invoke with invalid 'next' param + * + * @expectedException InvalidArgumentException */ - public function testNotFound($basePath, $path) + public function testInvokeInvalidNext() { - $middleware = new BasePath($basePath); - list($request, $response) = $this->getRequests(); - - $this->expectRequestGetPath($request, $path); - $this->expectNotFound($response); - - $result = $middleware($request, $response, function($response, $request) { - $response->nextCalled = true; + $request = $this->createMock(ServerRequestInterface::class); + $response = $this->createMock(ResponseInterface::class); - return $response; - }); + $middleware = new BasePath('/foo'); - $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object"); - $this->assertFalse(isset($response->nextCalled), "'next' was called"); + $middleware($request, $response, 'not_callable'); } /** @@ -128,28 +113,27 @@ class BasePathTest extends PHPUnit_Framework_TestCase } /** - * Test correct case, when path contains base path - * - * @dataProvider foundProvider + * Test case when given request path does not starts with given base path + * @dataProvider notFoundProvider + * * @param string $basePath * @param string $path - * @param string $noBasePath */ - public function testFound($basePath, $path, $noBasePath) + public function testNotFound($basePath, $path) { - $middleware = new BasePath($basePath); - list($request, $response) = $this->getRequests(); - - $this->expectRequestSetBasePath($request, $basePath, $path, $noBasePath); + $request = $this->createMock(ServerRequestInterface::class); + $response = $this->createMock(ResponseInterface::class); - $result = $middleware($request, $response, function($request, $response) { - $response->nextCalled = true; + $middleware = new BasePath($basePath); + + $this->expectRequestGetPath($request, $path); + $finalResponse = $this->expectNotFound($response); - return $response; - }); + $next = $this->createCallbackMock($this->never()); + + $result = $middleware($request, $response, $next); - $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object"); - $this->assertTrue($response->nextCalled, "'next' was not called"); + $this->assertSame($finalResponse, $result); } /** @@ -172,19 +156,31 @@ class BasePathTest extends PHPUnit_Framework_TestCase } /** - * Get requests for testing - * - * @param string $path - * @return array + * Test correct case, when path contains base path + * @dataProvider foundProvider + * + * @param string $basePath + * @param string $path + * @param string $noBasePath */ - public function getRequests($path = null) + public function testFound($basePath, $path, $noBasePath) { $request = $this->createMock(ServerRequestInterface::class); $response = $this->createMock(ResponseInterface::class); + $finalRespose = $this->createMock(ResponseInterface::class); + + $middleware = new BasePath($basePath); + + $this->expectRequestSetBasePath($request, $basePath, $path, $noBasePath); - return [$request, $response]; + $next = $this->createCallbackMock($this->once(), [$request, $response], $finalRespose); + + $result = $middleware($request, $response, $next); + + $this->assertSame($finalRespose, $result); } + /** * Expect that request will return a path * @@ -221,15 +217,19 @@ class BasePathTest extends PHPUnit_Framework_TestCase * Expect for not found error * * @param ResponseInterface $response + * @return ResponseInterface */ public function expectNotFound(ResponseInterface $response) { + $finalResponse = $this->createMock(ResponseInterface::class); + + $response->expects($this->once())->method('withStatus')->with(404)->willReturn($finalResponse); + $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once())->method('rewind'); $stream->expects($this->once())->method('write')->with($this->equalTo('Not Found')); - $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(404), $this->equalTo('Not Found'))->will($this->returnSelf()); + $finalResponse->expects($this->once())->method('getBody')->willReturn($stream); + + return $finalResponse; } } diff --git a/tests/Router/Middleware/ErrorHandlerTest.php b/tests/Router/Middleware/ErrorHandlerTest.php deleted file mode 100644 index c4b6313..0000000 --- a/tests/Router/Middleware/ErrorHandlerTest.php +++ /dev/null @@ -1,86 +0,0 @@ -<?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()); - } -} diff --git a/tests/Router/Middleware/NotFoundTest.php b/tests/Router/Middleware/NotFoundTest.php index 0c40a68..b8cd657 100644 --- a/tests/Router/Middleware/NotFoundTest.php +++ b/tests/Router/Middleware/NotFoundTest.php @@ -5,261 +5,222 @@ use Jasny\Router\Middleware\NotFound; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +/** + * @covers Jasny\Router\Middleware\NotFound + */ class NotFoundTest extends PHPUnit_Framework_TestCase { - /** - * Test creating object with false parameters - * - * @dataProvider constructProvider - * @param string $notFound - * @param string $notAllowed - * @param boolean $positive - */ - public function testConstruct($notFound, $notAllowed, $positive) - { - if (!$positive) $this->expectException(\InvalidArgumentException::class); - - $middleware = new NotFound($this->getRoutes(), $notFound, $notAllowed); - - if ($positive) $this->skipTest(); - } - - /** - * Provide data for testing '__contruct' - */ - public function constructProvider() + public function invalidStatusProvider() { return [ - [null, 405, false], - [true, true, false], - [99, null, false], - [600, null, false], - [404, 99, false], - [404, 600, false], - [200, 405, true], - [404, 200, true] + [0], + [true], + ['foo bar zoo'], + [1000], + [['abc']] ]; } /** - * Test invoke with invalid 'next' param + * @dataProvider invalidStatusProvider + * @expectedException InvalidArgumentException + * + * @param string $status */ - public function testInvokeInvalidNext() - { - $middleware = new NotFound($this->getRoutes(), 404, 405); - list($request, $response) = $this->getRequests(); - - $this->expectException(\InvalidArgumentException::class); - - $result = $middleware($request, $response, 'not_callable'); - } - - /** - * Test that 'next' callback is invoked when route is found - * - * @dataProvider invokeProvider - * @param callback|int $notFound - * @param callback|int $notAllowed - * @param callback $next - */ - public function testInvokeFound($notFound, $notAllowed, $next) + public function testConstructInvalidNotFound($status) { - if (!$next) return $this->skipTest(); - - list($request, $response) = $this->getRequests(); - $routes = $this->getRoutes(); - $middleware = new NotFound($routes, $notFound, $notAllowed); - - $this->expectRoute($routes, $request, 'found'); - $this->notExpectSimpleError($response); - - $result = $middleware($request, $response, $next); - - $this->assertEquals(get_class($response), get_class($result), "Result must be an instance of 'ResponseInterface'"); - $this->assertTrue($result->nextCalled, "'next' was not called"); - $this->assertFalse(isset($result->notAllowedCalled), "'Not allowed' callback was called"); - $this->assertFalse(isset($result->notFoundCalled), "'Not found' callback was called"); + new NotFound($this->createMock(Routes::class), $status); } /** - * Test __invoke method in case of route is found with another method - * - * @dataProvider invokeProvider - * @param callback|int $notFound - * @param callback|int $notAllowed - * @param callback $next + * @expectedException InvalidArgumentException */ - public function testInvokeNotAllowed($notFound, $notAllowed, $next) + public function testConstructNotFoundNotNull() { - if (!$notAllowed) return $this->skipTest(); - - list($request, $response) = $this->getRequests(); - $routes = $this->getRoutes(); - $middleware = new NotFound($routes, $notFound, $notAllowed); - - $this->expectRoute($routes, $request, 'notAllowed'); - if (is_numeric($notAllowed)) { - $this->expectSimpleError($response, $notAllowed); - } - - $result = $middleware($request, $response, $next); - - $this->assertEquals(get_class($response), get_class($result), "Result must be an instance of 'ResponseInterface'"); - $this->assertFalse(isset($result->nextCalled), "'next' was called"); - - if (is_callable($notAllowed)) { - $this->assertTrue($result->notAllowedCalled, "'Not allowed' callback was not called"); - } + new NotFound($this->createMock(Routes::class), null); } /** - * Test __invoke method in case of route not found at all - * - * @dataProvider invokeProvider - * @param callback|int $notFound - * @param callback|int $notAllowed - * @param callback $next + * @dataProvider invalidStatusProvider + * @expectedException InvalidArgumentException + * + * @param string $status */ - public function testInvokeNotFound($notFound, $notAllowed, $next) + public function testConstructInvalidMethodNotAllowed($status) { - list($request, $response) = $this->getRequests(); - $routes = $this->getRoutes(); - $middleware = new NotFound($routes, $notFound, $notAllowed); - - $case = $notAllowed ? 'notFoundTwice' : 'notFoundOnce'; - $this->expectRoute($routes, $request, $case); - - if (is_numeric($notFound)) { - $this->expectSimpleError($response, $notFound); - } - - $result = $middleware($request, $response, $next); - - $this->assertEquals(get_class($response), get_class($result), "Result must be an instance of 'ResponseInterface'"); - $this->assertFalse(isset($result->nextCalled), "'next' was called"); - - if (is_callable($notAllowed)) { - $this->assertFalse(isset($result->notAllowedCalled), "'Not allowed' callback was called"); - } - if (is_callable($notFound)) { - $this->assertTrue($result->notFoundCalled, "'Not found' callback was not called"); - } + new NotFound($this->createMock(Routes::class), 404, $status); } /** - * Set expectations on finding route - * - * @param Routes $routes - * @param ServerRequestInterface $request - * @param string $case + * @expectedException InvalidArgumentException */ - public function expectRoute($routes, $request, $case) - { - if ($case === 'found' || $case === 'notFoundOnce') { - $found = $case === 'found'; + public function testInvokeInvalidNext() + { + $request = $this->createMock(ServerRequestInterface::class); + $response = $this->createMock(ResponseInterface::class); + + $middleware = new NotFound($this->createMock(Routes::class)); - $routes->expects($this->once())->method('hasRoute') - ->with($this->equalTo($request))->will($this->returnValue($found)); - } elseif ($case === 'notAllowed' || $case === 'notFoundTwice') { - $routes->expects($this->exactly(2))->method('hasRoute') - ->withConsecutive( - [$this->equalTo($request)], - [$this->equalTo($request), $this->equalTo(false)] - )->will($this->returnCallback(function($request, $searchMethod = true) use ($case) { - return $case === 'notFoundTwice' ? false : !$searchMethod; - })); - } + $middleware($request, $response, 'foo bar zoo'); } + /** * Provide data for testing invoke method */ public function invokeProvider() { - $callbacks = []; - foreach (['notFound', 'notAllowed', 'next'] as $type) { - $var = $type . 'Called'; - - $callbacks[$type] = function($request, $response) use ($var) { - $response->$var = true; - return $response; - }; - } - + $request = $this->createMock(ServerRequestInterface::class); + $response = $this->createMock(ResponseInterface::class); + + $mockCallback = function() { + return $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); + }; + return [ - [404, 405, $callbacks['next']], - [404, 405, null], - [404, null, $callbacks['next']], - [404, null, null], - [$callbacks['notFound'], $callbacks['notAllowed'], $callbacks['next']], - [$callbacks['notFound'], $callbacks['notAllowed'], null], - [$callbacks['notFound'], null, $callbacks['next']], - [$callbacks['notFound'], null, null] + [$request, $response, 404, 405, $mockCallback()], + [$request, $response, 404, null, $mockCallback()], + [$request, $response, '200', '402', $mockCallback()], + [$request, $response, $mockCallback(), $mockCallback(), $mockCallback()], + [$request, $response, $mockCallback(), null, $mockCallback()] ]; } /** - * Expect that response is set to simple deny answer - * - * @param ResponseInterface $response - * @param int $statusCode + * Test that 'next' callback is invoked when route is found + * @dataProvider invokeProvider + * + * @param ServerRequestInterface|MockObject $request + * @param ResponseInterface|MockObject $response + * @param callback|MockObject|int $notFound + * @param callback|MockObject|int $methodNotAllowed + * @param callback|MockObject $next */ - public function expectSimpleError(ResponseInterface $response, $statusCode) + public function testInvokeFound($request, $response, $notFound, $methodNotAllowed, $next) { - $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once())->method('rewind'); - $stream->expects($this->once())->method('write')->with($this->equalTo('Not Found')); + $finalResponse = $this->createMock(ResponseInterface::class); + + if ($notFound instanceof MockObject) { + $notFound->expects($this->never())->method('__invoke'); + } + + if ($methodNotAllowed instanceof MockObject) { + $methodNotAllowed->expects($this->never())->method('__invoke'); + } + + $next->expects($this->once())->method('__invoke')->with($request, $response)->willReturn($finalResponse); + + $response->expects($this->never())->method('withStatus'); + + $routes = $this->createMock(Routes::class); + $routes->expects($this->once())->method('hasRoute')->with($request)->willReturn(true); + + $middleware = new NotFound($routes, $notFound, $methodNotAllowed); + + $result = $middleware($request, $response, $next); - $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($statusCode), $this->equalTo('Not Found'))->will($this->returnSelf()); + $this->assertSame($finalResponse, $result); } /** - * Expect that there would be no simple error response - * - * @param ResponseInterface $response + * Test __invoke method in case of route is found with another method + * @dataProvider invokeProvider + * + * @param ServerRequestInterface|MockObject $request + * @param ResponseInterface|MockObject $response + * @param callback|MockObject|int $notFound + * @param callback|MockObject|int $methodNotAllowed + * @param callback|MockObject $next */ - public function notExpectSimpleError(ResponseInterface $response) + public function testInvokeNotFound($request, $response, $notFound, $methodNotAllowed, $next) { + $finalResponse = $this->createMock(ResponseInterface::class); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->never())->method('rewind'); - $stream->expects($this->never())->method('write'); - - $response->expects($this->never())->method('getBody'); - $response->expects($this->never())->method('withBody'); - $response->expects($this->never())->method('withStatus'); - } + + if ($notFound instanceof MockObject) { + $notFound->expects($this->once())->method('__invoke') + ->with($request, $response) + ->willReturn($finalResponse); + + $response->expects($this->never())->method('withStatus'); + } else { + $response->expects($this->once())->method('withStatus') + ->with($notFound) + ->willReturn($finalResponse); + + $finalResponse->expects($this->once())->method('getBody')->willReturn($stream); + $stream->expects($this->once())->method('write')->with('Not found'); + } + + if ($methodNotAllowed instanceof MockObject) { + $methodNotAllowed->expects($this->never())->method('__invoke'); + } + + $next->expects($this->never())->method('__invoke'); + + $routes = $this->createMock(Routes::class); + + $routes->expects($this->exactly(isset($methodNotAllowed) ? 2 : 1))->method('hasRoute') + ->withConsecutive([$request], [$request, false]) + ->willReturn(false); + + $middleware = new NotFound($routes, $notFound, $methodNotAllowed); - /** - * Get requests for testing - * - * @return array - */ - public function getRequests() - { - $request = $this->createMock(ServerRequestInterface::class); - $response = $this->createMock(ResponseInterface::class); + $result = $middleware($request, $response, $next); - return [$request, $response]; + $this->assertSame($finalResponse, $result); } /** - * Get routes array - * - * @return Routes + * Test __invoke method in case of route is found with another method + * @dataProvider invokeProvider + * + * @param ServerRequestInterface|MockObject $request + * @param ResponseInterface|MockObject $response + * @param callback|MockObject|int $notFound + * @param callback|MockObject|int $methodNotAllowed + * @param callback|MockObject $next */ - public function getRoutes() + public function testInvokeMethodNotAllowed($request, $response, $notFound, $methodNotAllowed, $next) { - return $this->getMockBuilder(Routes::class)->disableOriginalConstructor()->getMock(); - } + $finalResponse = $this->createMock(ResponseInterface::class); + $stream = $this->createMock(StreamInterface::class); + + $expect = $methodNotAllowed ?: $notFound; + + if ($expect !== $notFound && $notFound instanceof MockObject) { + $notFound->expects($this->never())->method('__invoke'); + } + + if ($expect instanceof MockObject) { + $expect->expects($this->once())->method('__invoke') + ->with($request, $response) + ->willReturn($finalResponse); + + $response->expects($this->never())->method('withStatus'); + } else { + $response->expects($this->once())->method('withStatus') + ->with($expect) + ->willReturn($finalResponse); + + $finalResponse->expects($this->once())->method('getBody')->willReturn($stream); + $stream->expects($this->once())->method('write')->with('Not found'); + } + + $next->expects($this->never())->method('__invoke'); + + $routes = $this->createMock(Routes::class); + + $routes->expects($this->exactly(isset($methodNotAllowed) ? 2 : 1))->method('hasRoute') + ->withConsecutive([$request], [$request, false]) + ->will($this->onConsecutiveCalls(false, true)); + + $middleware = new NotFound($routes, $notFound, $methodNotAllowed); - /** - * Skip the test pass - */ - public function skipTest() - { - return $this->assertTrue(true); + $result = $middleware($request, $response, $next); + + $this->assertSame($finalResponse, $result); } } |