Allow template variables to be named as alpha followed by alphanumeric and underscore.

This commit is contained in:
PJ Dietz 2015-03-08 14:58:56 -04:00
parent 81ce6dae9d
commit f08691fff1
2 changed files with 51 additions and 1 deletions

View File

@ -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.

View File

@ -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");