diff options
-rw-r--r-- | src/Router.php | 6 | ||||
-rw-r--r-- | tests/RouterTest.php | 67 |
2 files changed, 71 insertions, 2 deletions
diff --git a/src/Router.php b/src/Router.php index 63a8fbc..7a4f5c8 100644 --- a/src/Router.php +++ b/src/Router.php @@ -141,10 +141,12 @@ class Router $uriPath = $request->getUri()->getPath(); if ($uriPath === $path || strpos($uriPath, rtrim($path, '/') . '/') === 0) { - return $middleware($request, $response, $next); + $ret = $middleware($request, $response, $next); } else { - return $next($request, $response); + $ret = $next($request, $response); } + + return $ret; }; } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index e1c080c..3e5accf 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -10,6 +10,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; +use PHPUnit_Framework_MockObject_Matcher_InvokedCount as InvokedCount; use Jasny\Router\TestHelpers; @@ -170,6 +171,50 @@ class RouterTest extends \PHPUnit_Framework_TestCase $this->assertSame([$middlewareOne, $middlewareTwo], $router->getMiddlewares()); } + + public function middlewareWithPathProvider() + { + return [ + ['/', $this->never(), $this->once(), 'zoo'], + ['/foo', $this->once(), $this->never(), 'foo'], + ['/foo/bar', $this->once(), $this->never(), 'foo'], + ['/zoo', $this->never(), $this->once(), 'zoo'] + ]; + } + + /** + * Test adding middleware action specifying a path + * @dataProvider middlewareWithPathProvider + * + * @param string $path + * @param InvokedCount $invokeMiddleware + * @param InvokedCount $invokeNext + * @param string $expect + */ + public function testAddMiddlewareWithPath($path, $invokeMiddleware, $invokeNext, $expect) + { + $uri = $this->createMock(UriInterface::class); + $uri->method('getPath')->willReturn($path); + + $request = $this->createMock(ServerRequestInterface::class); + $request->method('getUri')->willReturn($uri); + + $response = $this->createMock(ResponseInterface::class); + + $middleware = $this->createCallbackMock($invokeMiddleware, [$request, $response], 'foo'); + $next = $this->createCallbackMock($invokeNext, [$request, $response], 'zoo'); + + $router = new Router($this->createMock(Routes::class)); + $router->add('/foo', $middleware); + + list($fn) = $router->getMiddlewares() + [null]; + + $this->assertNotSame($middleware, $fn); + + $result = $fn($request, $response, $next); + + $this->assertEquals($expect, $result); + } /** * @expectedException \InvalidArgumentException @@ -179,6 +224,28 @@ class RouterTest extends \PHPUnit_Framework_TestCase $router = new Router($this->createMock(Routes::class)); $router->add('foo bar zoo'); } + + /** + * @expectedException PHPUnit_Framework_Error + * @expectedException Middleware path 'foobar' doesn't start with a '/' + */ + public function testAddNoticeWeirdPath() + { + $middleware = $this->createCallbackMock($this->never()); + + $router = new Router($this->createMock(Routes::class)); + $router->add('foobar', $middleware); + } + + public function testAddNoticeWeirdPathSkip() + { + $middleware = $this->createCallbackMock($this->never()); + + $router = new Router($this->createMock(Routes::class)); + @$router->add('foobar', $middleware); + + $this->assertCount(1, $router->getMiddlewares()); + } /** * Test executing router with middlewares chain |