From f08691fff195195209dc7ae42d02735f296a0843 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sun, 8 Mar 2015 14:58:56 -0400 Subject: [PATCH] Allow template variables to be named as alpha followed by alphanumeric and underscore. --- .../WellRESTed/Routes/TemplateRoute.php | 2 +- test/tests/unit/Routes/TemplateRouteTest.php | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/pjdietz/WellRESTed/Routes/TemplateRoute.php b/src/pjdietz/WellRESTed/Routes/TemplateRoute.php index b39ed74..f583ab7 100644 --- a/src/pjdietz/WellRESTed/Routes/TemplateRoute.php +++ b/src/pjdietz/WellRESTed/Routes/TemplateRoute.php @@ -27,7 +27,7 @@ class TemplateRoute extends RegexRoute /** Regular expression matching letters and digits */ const RE_ALPHANUM = '[0-9a-zA-Z]+'; /** Regular expression matching a URI template variable (e.g., {id}) */ - const URI_TEMPLATE_EXPRESSION_RE = '/{([a-zA-Z]+)}/'; + const URI_TEMPLATE_EXPRESSION_RE = '/{([[a-zA-Z][a-zA-Z0-_]*)}/'; /** * Create a new route that matches a URI template to a Handler. diff --git a/test/tests/unit/Routes/TemplateRouteTest.php b/test/tests/unit/Routes/TemplateRouteTest.php index f79b995..55d876d 100644 --- a/test/tests/unit/Routes/TemplateRouteTest.php +++ b/test/tests/unit/Routes/TemplateRouteTest.php @@ -99,6 +99,56 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase ]; } + /** + * @dataProvider allowedVariableNamesProvider + */ + public function testMatchesAllowedVariablesNames($template, $path, $expectedCaptures) + { + $this->request->getPath()->willReturn($path); + $route = new TemplateRoute($template, $this->handler->reveal(), null, null); + $route->getResponse($this->request->reveal()); + $this->handler->getResponse( + Argument::any(), + Argument::that( + function ($args) use ($expectedCaptures) { + return count(array_diff_assoc($expectedCaptures, $args)) === 0; + } + ) + )->shouldHaveBeenCalled(); + } + + public function allowedVariableNamesProvider() + { + return [ + ["/{n}", "/lower", ["n" => "lower"]], + ["/{N}", "/UPPER", ["N" => "UPPER"]], + ["/{var1024}", "/digits", ["var1024" => "digits"]], + ["/{variable_name}", "/underscore", ["variable_name" => "underscore"]], + ]; + } + + /** + * @dataProvider illegalVariableNamesProvider + */ + public function testFailsToMatchIllegalVariablesNames($template, $path) + { + $this->request->getPath()->willReturn($path); + $route = new TemplateRoute($template, $this->handler->reveal(), null, null); + $route->getResponse($this->request->reveal()); + $this->handler->getResponse(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + public function illegalVariableNamesProvider() + { + return [ + ["/{not-legal}", "/hyphen"], + ["/{1digitfirst}", "/digitfirst"], + ["/{%2f}", "/percent-encoded"], + ["/{}", "/empty"], + ["/{{nested}}", "/nested"] + ]; + } + public function setUp() { $this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");