From d66ba80ec97934d5842a70da590d4d74acac4d8c Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Mon, 6 Apr 2015 19:12:16 -0400 Subject: [PATCH] Allow Router to assign middleware to MethodMap --- src/Routing/Router.php | 8 ++++++++ test/tests/unit/Routing/RouterTest.php | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 5f9900d..71e1b46 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -43,6 +43,9 @@ class Router implements MiddlewareInterface */ public function add($target, $middleware, $defaultPattern = null, $variablePatterns = null) { + if (is_array($middleware)) { + $middleware = $this->getMethodMap($middleware); + } $this->routeFactory->registerRoute($target, $middleware, $defaultPattern, $variablePatterns); } @@ -66,4 +69,9 @@ class Router implements MiddlewareInterface $dispatcher->dispatch($middleware, $request, $response); } } + + protected function getMethodMap(array $map) + { + return new MethodMap($map); + } } diff --git a/test/tests/unit/Routing/RouterTest.php b/test/tests/unit/Routing/RouterTest.php index 4b146bd..7d37c6e 100644 --- a/test/tests/unit/Routing/RouterTest.php +++ b/test/tests/unit/Routing/RouterTest.php @@ -9,6 +9,7 @@ use WellRESTed\Routing\Router; /** * @covers WellRESTed\Routing\Router * @uses WellRESTed\Routing\Dispatcher + * @uses WellRESTed\Routing\MethodMap * @uses WellRESTed\Routing\RouteTable * @uses WellRESTed\Routing\Route\RouteFactory * @uses WellRESTed\Routing\Route\RegexRoute @@ -73,4 +74,16 @@ class RouterTest extends \PHPUnit_Framework_TestCase $statusMiddleware->dispatch(Argument::cetera())->shouldHaveBeenCalled(); } + + public function testRegisterRouteWithMethodMap() + { + $this->request->getRequestTarget()->willReturn("/cats/"); + $this->request->getMethod()->willReturn("GET"); + + $router = new Router(); + $router->add("/cats/", ["GET" => $this->middleware->reveal()]); + $router->dispatch($this->request->reveal(), $this->response->reveal()); + + $this->middleware->dispatch(Argument::cetera())->shouldHaveBeenCalled(); + } }