From 1a49a4ac6cd50b5d5d9de94ddec3b0cfbb146e75 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 7 May 2015 22:01:11 -0400 Subject: [PATCH] RouteMap routes patterns --- src/Routing/RouteMap.php | 15 +++++ test/tests/unit/Routing/RouteMapTest.php | 75 +++++++++++++++++++++++- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/Routing/RouteMap.php b/src/Routing/RouteMap.php index 6042da7..fb15067 100644 --- a/src/Routing/RouteMap.php +++ b/src/Routing/RouteMap.php @@ -79,6 +79,21 @@ class RouteMap implements RouteMapInterface return; } + // Try each of the routes. + foreach ($this->patternRoutes as $route) { + if ($route->matchesRequestTarget($requestTarget, $captures)) { + if (is_array($captures)) { + foreach ($captures as $key => $value) { + $request = $request->withAttribute($key, $value); + } + } + $route->dispatch($request, $response); + return; + } + } + + // If no route exists, set the status code of the response to 404. + $response = $response->withStatus(404); } /** diff --git a/test/tests/unit/Routing/RouteMapTest.php b/test/tests/unit/Routing/RouteMapTest.php index 25aa07d..3358e01 100644 --- a/test/tests/unit/Routing/RouteMapTest.php +++ b/test/tests/unit/Routing/RouteMapTest.php @@ -31,6 +31,7 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase $this->route->getMethodMap()->willReturn($this->methodMap->reveal()); $this->route->getType()->willReturn(RouteInterface::TYPE_STATIC); $this->route->getTarget()->willReturn("/"); + $this->route->matchesRequestTarget(Argument::cetera())->willReturn(true); $this->factory = $this->prophesize('WellRESTed\Routing\Route\RouteFactory'); $this->factory->create(Argument::any())->willReturn($this->route->reveal()); @@ -128,9 +129,8 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase */ public function testDispatchesPrefixRoute() { - $target = "/*"; - - $this->request->getRequestTarget()->willReturn($target); + $target = "/animals/cats/*"; + $this->request->getRequestTarget()->willReturn("/animals/cats/molly"); $this->route->getTarget()->willReturn($target); $this->route->getType()->willReturn(RouteInterface::TYPE_PREFIX); @@ -142,4 +142,73 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase $this->route->dispatch(Argument::cetera())->shouldHaveBeenCalled(); } + + /** + * @covers ::getPrefixRoute + */ + public function testDispatchesLongestMatchingPrefixRoute() + { + $routeAnimals = $this->prophesize('WellRESTed\Routing\Route\RouteInterface'); + $routeAnimals->getMethodMap()->willReturn($this->methodMap->reveal()); + $routeAnimals->getTarget()->willReturn("/animals/*"); + $routeAnimals->getType()->willReturn(RouteInterface::TYPE_PREFIX); + $routeAnimals->dispatch(Argument::cetera())->willReturn(); + + $routeCats = $this->prophesize('WellRESTed\Routing\Route\RouteInterface'); + $routeCats->getMethodMap()->willReturn($this->methodMap->reveal()); + $routeCats->getTarget()->willReturn("/animals/cats/*"); + $routeCats->getType()->willReturn(RouteInterface::TYPE_PREFIX); + $routeCats->dispatch(Argument::cetera())->willReturn(); + + $this->request->getRequestTarget()->willReturn("/animals/cats/molly"); + + $this->factory->create("/animals/*")->willReturn($routeAnimals->reveal()); + $this->factory->create("/animals/cats/*")->willReturn($routeCats->reveal()); + + $this->routeMap->add("GET", "/animals/*", "middleware"); + $this->routeMap->add("GET", "/animals/cats/*", "middleware"); + + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $this->routeMap->dispatch($request, $response); + + $routeCats->dispatch(Argument::cetera())->shouldHaveBeenCalled(); + } + + /** + * @covers ::dispatch + * @covers ::registerRouteForTarget + */ + public function testDispatchesPatternRoute() + { + $target = "/"; + + $this->request->getRequestTarget()->willReturn($target); + $this->route->getTarget()->willReturn($target); + $this->route->getType()->willReturn(RouteInterface::TYPE_PATTERN); + + $this->routeMap->add("GET", $target, "middleware"); + + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $this->routeMap->dispatch($request, $response); + + $this->route->dispatch(Argument::cetera())->shouldHaveBeenCalled(); + } + + /** + * @covers ::dispatch + * @covers ::getStaticRoute + * @covers ::getPrefixRoute + */ + public function testResponds404WhenNoRouteMatches() + { + $this->response->withStatus(Argument::any())->willReturn($this->response->reveal()); + + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $this->routeMap->dispatch($request, $response); + + $this->response->withStatus(404)->shouldHaveBeenCalled(); + } }