diff --git a/src/pjdietz/WellRESTed/Routes/TemplateRoute.php b/src/pjdietz/WellRESTed/Routes/TemplateRoute.php index 5023bb1..f6e2358 100644 --- a/src/pjdietz/WellRESTed/Routes/TemplateRoute.php +++ b/src/pjdietz/WellRESTed/Routes/TemplateRoute.php @@ -110,7 +110,14 @@ class TemplateRoute extends RegexRoute } - $pattern = '/^' . $pattern . '$/'; + $pattern = '/^' . $pattern; + if (substr($pattern, -1) === "*") { + // Allow path to include characters passed the pattern. + $pattern = rtrim($pattern, "*") . '/'; + } else { + // Path must end at the end of the pattern. + $pattern .= "$/"; + } return $pattern; } diff --git a/test/RouterTest.php b/test/RouterTest.php index f568efa..a2bd809 100644 --- a/test/RouterTest.php +++ b/test/RouterTest.php @@ -4,9 +4,11 @@ namespace pjdietz\WellRESTed\Test; use pjdietz\WellRESTed\Interfaces\HandlerInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface; +use pjdietz\WellRESTed\Interfaces\ResponseInterface; use pjdietz\WellRESTed\Response; use pjdietz\WellRESTed\Router; use pjdietz\WellRESTed\Routes\StaticRoute; +use pjdietz\WellRESTed\Routes\TemplateRoute; class RouterTest extends \PHPUnit_Framework_TestCase { @@ -101,6 +103,37 @@ class RouterTest extends \PHPUnit_Framework_TestCase $this->assertEquals("No resource at /cats/", $captured); } + /** + * @dataProvider nestedRouterRoutesProvider + */ + public function testNestedRouterFromWithRoutes($path, $expectedBody) + { + $router = new Router(); + $router->addRoutes(array( + new TemplateRoute("/cats/*", __NAMESPACE__ . "\\CatRouter"), + new TemplateRoute("/dogs/*", __NAMESPACE__ . "\\DogRouter"), + new NotFoundHandler() + )); + + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue($path)); + + $resp = $router->getResponse($mockRequest); + $this->assertEquals($expectedBody, $resp->getBody()); + } + + public function nestedRouterRoutesProvider() + { + return [ + ["/cats/", "/cats/"], + ["/cats/molly", "/cats/molly"], + ["/dogs/", "/dogs/"], + ["/birds/", "No resource found at /birds/"] + ]; + } + } /** @@ -112,6 +145,42 @@ class RouterTestHandler implements HandlerInterface { $resp = new Response(); $resp->setStatusCode(200); + $resp->setBody($request->getPath()); return $resp; } } + +class CatRouter extends Router +{ + public function __construct() + { + parent::__construct(); + $this->addRoutes([ + new StaticRoute("/cats/", __NAMESPACE__ . "\\RouterTestHandler"), + new StaticRoute("/cats/molly", __NAMESPACE__ . "\\RouterTestHandler"), + new StaticRoute("/cats/oscar", __NAMESPACE__ . "\\RouterTestHandler") + ]); + } +} + +class DogRouter extends Router +{ + public function __construct() + { + parent::__construct(); + $this->addRoutes([ + new StaticRoute("/dogs/", __NAMESPACE__ . "\\RouterTestHandler"), + new StaticRoute("/dogs/bear", __NAMESPACE__ . "\\RouterTestHandler") + ]); + } +} + +class NotFoundHandler implements HandlerInterface +{ + public function getResponse(RequestInterface $request, array $args = null) + { + $response = new Response(404); + $response->setBody("No resource found at " . $request->getPath()); + return $response; + } +} diff --git a/test/Routes/TemplateRouteTest.php b/test/Routes/TemplateRouteTest.php index 54be96f..7feac4b 100644 --- a/test/Routes/TemplateRouteTest.php +++ b/test/Routes/TemplateRouteTest.php @@ -27,18 +27,19 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase public function matchingTemplateProvider() { - return array( - array("/cat/{id}", TemplateRoute::RE_NUM, null, "/cat/12", "id", "12"), - array("/cat/{catId}/{dogId}", TemplateRoute::RE_SLUG, null, "/cat/molly/bear", "dogId", "bear"), - array("/cat/{catId}/{dogId}", TemplateRoute::RE_NUM, array( + return [ + ["/cat/{id}", TemplateRoute::RE_NUM, null, "/cat/12", "id", "12"], + ["/cat/{catId}/{dogId}", TemplateRoute::RE_SLUG, null, "/cat/molly/bear", "dogId", "bear"], + ["/cat/{catId}/{dogId}", TemplateRoute::RE_NUM, [ "catId" => TemplateRoute::RE_SLUG, - "dogId" => TemplateRoute::RE_SLUG), - "/cat/molly/bear", "dogId", "bear"), - array("cat/{catId}/{dogId}", TemplateRoute::RE_NUM, (object) array( + "dogId" => TemplateRoute::RE_SLUG], + "/cat/molly/bear", "dogId", "bear"], + ["cat/{catId}/{dogId}", TemplateRoute::RE_NUM, (object) [ "catId" => TemplateRoute::RE_SLUG, - "dogId" => TemplateRoute::RE_SLUG), - "/cat/molly/bear", "dogId", "bear") - ); + "dogId" => TemplateRoute::RE_SLUG], + "/cat/molly/bear", "dogId", "bear"], + ["/cat/{id}/*", null, null, "/cat/12/molly", "id", "12"] + ]; } /**