Skip to content

Commit

Permalink
Add more tests for brace style routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 12, 2018
1 parent f018dbf commit 7968bbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Routing/Route/Route.php
Expand Up @@ -323,7 +323,7 @@ protected function _writeRoute()
$parsed = preg_quote($this->template, '#');

if (strpos($route, '{') !== false && strpos($route, '}') !== false) {
preg_match_all('/\{([a-z0-9-_]+)\}/i', $route, $namedElements);
preg_match_all('/\{([a-z][a-z0-9-_]*)\}/i', $route, $namedElements);
$this->braceKeys = true;
} else {
preg_match_all('/:([a-z0-9-_]+(?<![-_]))/i', $route, $namedElements);
Expand Down
50 changes: 46 additions & 4 deletions tests/TestCase/Routing/Route/RouteTest.php
Expand Up @@ -154,8 +154,7 @@ public function testRouteCompileBraces()
['controller' => 'Fighters', 'action' => 'move'],
['id' => '\d+', 'x' => '\d+', 'y' => '\d+', 'pass' => ['id', 'x', 'y']]
);
$pattern = $route->compile();
$this->assertRegExp($pattern, '/fighters/123/move/8/42');
$this->assertRegExp($route->compile(), '/fighters/123/move/8/42');

$result = $route->match([
'controller' => 'Fighters',
Expand All @@ -170,8 +169,7 @@ public function testRouteCompileBraces()
'/images/{id}/{x}x{y}',
['controller' => 'Images', 'action' => 'view']
);
$pattern = $route->compile();
$this->assertRegExp($pattern, '/images/123/640x480');
$this->assertRegExp($route->compile(), '/images/123/640x480');

$result = $route->match([
'controller' => 'Images',
Expand All @@ -183,6 +181,50 @@ public function testRouteCompileBraces()
$this->assertEquals('/images/123/8x42', $result);
}

/**
* Test route compile with brace format.
*
* @return void
*/
public function testRouteCompileBracesVariableName()
{
$route = new Route(
'/fighters/{0id}',
['controller' => 'Fighters', 'action' => 'move']
);
$pattern = $route->compile();
$this->assertNotRegExp($route->compile(), '/fighters/123', 'Placeholders must start with letter');

$route = new Route('/fighters/{Id}', ['controller' => 'Fighters', 'action' => 'move']);
$this->assertRegExp($route->compile(), '/fighters/123');

$route = new Route('/fighters/{i_d}', ['controller' => 'Fighters', 'action' => 'move']);
$this->assertRegExp($route->compile(), '/fighters/123');

$route = new Route('/fighters/{id99}', ['controller' => 'Fighters', 'action' => 'move']);
$this->assertRegExp($route->compile(), '/fighters/123');
}

/**
* Test route compile with brace format.
*
* @return void
*/
public function testRouteCompileBracesInvalid()
{
$route = new Route(
'/fighters/{ id }',
['controller' => 'Fighters', 'action' => 'move']
);
$this->assertNotRegExp($route->compile(), '/fighters/123', 'no spaces in placeholder');

$route = new Route(
'/fighters/{i d}',
['controller' => 'Fighters', 'action' => 'move']
);
$this->assertNotRegExp($route->compile(), '/fighters/123', 'no spaces in placeholder');
}

/**
* Test route compile with mixed placeholder types brace format.
*
Expand Down

0 comments on commit 7968bbe

Please sign in to comment.