From cfcc3b969027ec77fb6d61ef1c5131496a721f76 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 7 May 2015 23:30:42 -0400 Subject: [PATCH] Revise PrefixRoute --- src/Routing/Route/PrefixRoute.php | 25 ++++---- src/Routing/Route/PrefixRouteInterface.php | 11 ---- .../unit/Routing/Route/PrefixRouteTest.php | 59 +++++++++++++------ 3 files changed, 51 insertions(+), 44 deletions(-) delete mode 100644 src/Routing/Route/PrefixRouteInterface.php diff --git a/src/Routing/Route/PrefixRoute.php b/src/Routing/Route/PrefixRoute.php index 7b424da..516e85c 100644 --- a/src/Routing/Route/PrefixRoute.php +++ b/src/Routing/Route/PrefixRoute.php @@ -2,14 +2,17 @@ namespace WellRESTed\Routing\Route; -class PrefixRoute extends Route implements PrefixRouteInterface +class PrefixRoute extends Route { - private $prefix; - - public function __construct($prefix, $middleware) + public function __construct($target, $methodMap) { - parent::__construct($middleware); - $this->prefix = $prefix; + $this->target = rtrim($target, "*"); + $this->methodMap = $methodMap; + } + + public function getType() + { + return RouteInterface::TYPE_PREFIX; } /** @@ -19,14 +22,6 @@ class PrefixRoute extends Route implements PrefixRouteInterface */ public function matchesRequestTarget($requestTarget, &$captures = null) { - return strrpos($requestTarget, $this->prefix, -strlen($requestTarget)) !== false; - } - - /** - * @return string - */ - public function getPrefix() - { - return $this->prefix; + return strrpos($requestTarget, $this->target, -strlen($requestTarget)) !== false; } } diff --git a/src/Routing/Route/PrefixRouteInterface.php b/src/Routing/Route/PrefixRouteInterface.php deleted file mode 100644 index 00c68db..0000000 --- a/src/Routing/Route/PrefixRouteInterface.php +++ /dev/null @@ -1,11 +0,0 @@ -request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); - $this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); - $this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = new PrefixRoute("/cats/*", $methodMap->reveal()); + $this->assertEquals("/cats/", $route->getTarget()); } - public function testMatchesPrefix() + /** + * @covers ::getType + */ + public function testReturnsPrefixType() { - $route = new PrefixRoute("/cats/", $this->middleware->reveal()); - $this->assertTrue($route->matchesRequestTarget("/cats/molly")); + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = new PrefixRoute("/*", $methodMap->reveal()); + $this->assertSame(RouteInterface::TYPE_PREFIX, $route->getType()); } - public function testFailsToMatchWrongPath() + /** + * @covers ::matchesRequestTarget + */ + public function testMatchesExactRequestTarget() { - $route = new PrefixRoute("/dogs/", $this->middleware->reveal()); - $this->assertFalse($route->matchesRequestTarget("/cats/")); + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = new PrefixRoute("/*", $methodMap->reveal()); + $this->assertTrue($route->matchesRequestTarget("/")); } - public function testReturnsPrefix() + /** + * @covers ::matchesRequestTarget + */ + public function testMatchesRequestTargetWithSamePrefix() { - $route = new PrefixRoute("/cats/", $this->middleware->reveal()); - $this->assertEquals("/cats/", $route->getPrefix()); + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = new PrefixRoute("/*", $methodMap->reveal()); + $this->assertTrue($route->matchesRequestTarget("/cats/")); + } + + /** + * @covers ::matchesRequestTarget + */ + public function testDoesNotMatchNonmatchingRequestTarget() + { + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = new PrefixRoute("/animals/cats/", $methodMap->reveal()); + $this->assertFalse($route->matchesRequestTarget("/animals/dogs/")); } }