From 6232f67b9cd6e80cb877b89d0bac1749d5ad56a3 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Wed, 13 May 2015 21:17:07 -0400 Subject: [PATCH] Update RouteInterface and routes --- src/Routing/Route/PrefixRoute.php | 8 +++++ src/Routing/Route/RegexRoute.php | 18 +++++----- src/Routing/Route/RouteInterface.php | 35 +++++++++++++++++++ src/Routing/Route/StaticRoute.php | 8 +++++ .../unit/Routing/Route/PrefixRouteTest.php | 10 ++++++ .../unit/Routing/Route/RegexRouteTest.php | 24 ++++++------- .../unit/Routing/Route/StaticRouteTest.php | 10 ++++++ 7 files changed, 92 insertions(+), 21 deletions(-) diff --git a/src/Routing/Route/PrefixRoute.php b/src/Routing/Route/PrefixRoute.php index 26b74f9..287529a 100644 --- a/src/Routing/Route/PrefixRoute.php +++ b/src/Routing/Route/PrefixRoute.php @@ -25,4 +25,12 @@ class PrefixRoute extends Route { return strrpos($requestTarget, $this->target, -strlen($requestTarget)) !== false; } + + /** + * Always returns an empty array. + */ + public function getPathVariables() + { + return []; + } } diff --git a/src/Routing/Route/RegexRoute.php b/src/Routing/Route/RegexRoute.php index f3d3ae2..2d39a99 100644 --- a/src/Routing/Route/RegexRoute.php +++ b/src/Routing/Route/RegexRoute.php @@ -2,12 +2,8 @@ namespace WellRESTed\Routing\Route; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; - class RegexRoute extends Route { - private $capturesAttribute = "uriVariables"; private $captures; public function getType() @@ -23,6 +19,7 @@ class RegexRoute extends Route */ public function matchesRequestTarget($requestTarget) { + $this->captures = []; $matched = @preg_match($this->getTarget(), $requestTarget, $captures); if ($matched) { $this->captures = $captures; @@ -33,11 +30,14 @@ class RegexRoute extends Route return false; } - public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next) + /** + * Returns an array of matches from the last call to matchesRequestTarget. + * + * @see \preg_match + * @return array + */ + public function getPathVariables() { - if ($this->captures) { - $request = $request->withAttribute($this->capturesAttribute, $this->captures); - } - return parent::dispatch($request, $response, $next); + return $this->captures; } } diff --git a/src/Routing/Route/RouteInterface.php b/src/Routing/Route/RouteInterface.php index 88e22a5..59fb98f 100644 --- a/src/Routing/Route/RouteInterface.php +++ b/src/Routing/Route/RouteInterface.php @@ -7,14 +7,47 @@ use WellRESTed\Routing\MethodMapInterface; interface RouteInterface extends MiddlewareInterface { + /** Matches when path is an exact match only */ const TYPE_STATIC = 0; + /** Matches when path has the expected beginning */ const TYPE_PREFIX = 1; + /** Matches by pattern. Use matchesRequestTarget to test for matches */ const TYPE_PATTERN = 2; + /** + * @return string + */ public function getTarget(); + /** + * Return the RouteInterface::TYPE_ contants that identifies the type. + * + * TYPE_STATIC indicates the route MUST match only when the path is an + * exact match to the route's target. This route type SHOULD NOT + * provide path variables. + * + * TYPE_PREFIX indicates the route MUST match when the route's target + * appears in its entirety at the beginning of the path. + * + * TYPE_PATTERN indicates that matchesRequestTarget MUST be used + * to determine a match against a given path. This route type SHOULD + * provide path variables. + * + * @return int One of the RouteInterface::TYPE_ constants. + */ public function getType(); + /** + * Return an array of variables extracted from the path most recently + * passed to matchesRequestTarget. + * + * If the path does not contain variables, or if matchesRequestTarget + * has not yet been called, this method MUST return an empty array. + * + * @return array + */ + public function getPathVariables(); + /** * Return the instance mapping methods to middleware for this route. * @@ -27,6 +60,8 @@ interface RouteInterface extends MiddlewareInterface * * @param string $requestTarget * @return boolean + * @throw \RuntimeException Error occured testing the target such as an + * invalid regular expression */ public function matchesRequestTarget($requestTarget); } diff --git a/src/Routing/Route/StaticRoute.php b/src/Routing/Route/StaticRoute.php index e9aeb20..d31b84c 100644 --- a/src/Routing/Route/StaticRoute.php +++ b/src/Routing/Route/StaticRoute.php @@ -19,4 +19,12 @@ class StaticRoute extends Route { return $requestTarget === $this->getTarget(); } + + /** + * Always returns an empty array. + */ + public function getPathVariables() + { + return []; + } } diff --git a/test/tests/unit/Routing/Route/PrefixRouteTest.php b/test/tests/unit/Routing/Route/PrefixRouteTest.php index 6721103..53f8c2a 100644 --- a/test/tests/unit/Routing/Route/PrefixRouteTest.php +++ b/test/tests/unit/Routing/Route/PrefixRouteTest.php @@ -35,6 +35,16 @@ class PrefixRouteTest extends \PHPUnit_Framework_TestCase $this->assertSame(RouteInterface::TYPE_PREFIX, $route->getType()); } + /** + * @covers ::getPathVariables + */ + public function testReturnsEmptyArrayForPathVariables() + { + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = new PrefixRoute("/*", $methodMap->reveal()); + $this->assertSame([], $route->getPathVariables()); + } + /** * @covers ::matchesRequestTarget */ diff --git a/test/tests/unit/Routing/Route/RegexRouteTest.php b/test/tests/unit/Routing/Route/RegexRouteTest.php index c890d8b..80268a4 100644 --- a/test/tests/unit/Routing/Route/RegexRouteTest.php +++ b/test/tests/unit/Routing/Route/RegexRouteTest.php @@ -43,23 +43,23 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase /** * @covers ::matchesRequestTarget - * @covers ::dispatch * @dataProvider matchingRouteProvider */ - public function testProvidesCapturesAsRequestAttributes($pattern, $path, $expectedCaptures) + public function testMatchesTargetByRegex($pattern, $target) { - $request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); - $request->withAttribute(Argument::cetera())->willReturn($request->reveal()); - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - $next = function ($request, $response) { - return $response; - }; - $route = new RegexRoute($pattern, $this->methodMap->reveal()); - $route->matchesRequestTarget($path); - $route->dispatch($request->reveal(), $response->reveal(), $next); + $this->assertTrue($route->matchesRequestTarget($target)); + } - $request->withAttribute("uriVariables", $expectedCaptures)->shouldHaveBeenCalled(); + /** + * @covers ::getPathVariables + * @dataProvider matchingRouteProvider + */ + public function testExtractsPathVariablesByRegex($pattern, $target, $expectedCaptures) + { + $route = new RegexRoute($pattern, $this->methodMap->reveal()); + $route->matchesRequestTarget($target); + $this->assertEquals($expectedCaptures, $route->getPathVariables()); } public function matchingRouteProvider() diff --git a/test/tests/unit/Routing/Route/StaticRouteTest.php b/test/tests/unit/Routing/Route/StaticRouteTest.php index bcc647f..541285f 100644 --- a/test/tests/unit/Routing/Route/StaticRouteTest.php +++ b/test/tests/unit/Routing/Route/StaticRouteTest.php @@ -35,6 +35,16 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase $this->assertTrue($route->matchesRequestTarget("/")); } + /** + * @covers ::getPathVariables + */ + public function testReturnsEmptyArrayForPathVariables() + { + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = new StaticRoute("/", $methodMap->reveal()); + $this->assertSame([], $route->getPathVariables()); + } + /** * @covers ::matchesRequestTarget */