Permalink
Browse files

Close #671

  • Loading branch information...
1 parent 2020719 commit ac85d00bc6b6af1b3e01f6b5947611411a92b4d9 @jails jails committed Nov 23, 2012
Showing with 79 additions and 1 deletion.
  1. +8 −1 net/http/Route.php
  2. +71 −0 tests/cases/net/http/RouterTest.php
View
@@ -464,7 +464,14 @@ protected function _regex($regex, $param, $token, $prefix) {
} else {
$regex = '[^\/]+';
}
- $req = $param === 'args' || array_key_exists($param, $this->_params) ? '?' : '';
+
+ $optionnal = false;
+ if (isset($this->_params[$param])) {
+ $optionnal = preg_match("@{$regex}@", $this->_params[$param]);
+ } elseif (array_key_exists($param, $this->_params)) {
+ $optionnal = true;
+ }
+ $req = $regex === '.*' || $optionnal ? '?' : '';
if ($prefix === '/') {
$pattern = "(?:/(?P<{$param}>{$regex}){$req}){$req}";
@@ -142,6 +142,77 @@ public function testRouteMatchingWithDefaultParameters() {
$this->assertNull(Router::parse($this->request));
}
+ public function testRouteMatchingWithRegExAction() {
+ Router::connect(
+ '/products/{:action:add|edit|remove}/{:category}',
+ array('controller' => 'Products')
+ );
+ Router::connect('/products/{:category}', array('Products::category'));
+
+ $this->request = new Request();
+ $this->request->url = '/products/add/computer';
+ $result = Router::parse($this->request);
+ $expected = array(
+ 'controller' => 'Products',
+ 'action' => 'add',
+ 'category' => 'computer'
+ );
+ $this->assertEqual($expected, $result->params);
+
+ $this->request = new Request();
+ $this->request->url = '/products/add';
+ $result = Router::parse($this->request);
+ $expected = array(
+ 'controller' => 'Products',
+ 'action' => 'category',
+ 'category' => 'add'
+ );
+ $this->assertEqual($expected, $result->params);
+
+ $this->request = new Request();
+ $this->request->url = '/products/computer';
+ $result = Router::parse($this->request);
+ $expected = array(
+ 'controller' => 'Products',
+ 'action' => 'category',
+ 'category' => 'computer'
+ );
+ $this->assertEqual($expected, $result->params);
+
+ Router::reset();
+ Router::connect(
+ '/products/{:action:add|edit|remove}/{:category:.*}',
+ array('controller' => 'Products')
+ );
+ Router::connect('/products/{:category}', array('Products::category'));
+
+ $this->request = new Request();
+ $this->request->url = '/products/add';
+ $result = Router::parse($this->request);
+ $expected = array(
+ 'controller' => 'Products',
+ 'action' => 'add'
+ );
+ $this->assertEqual($expected, $result->params);
+
+ Router::reset();
+ Router::connect(
+ '/products/{:action:add|edit|remove}/{:category:.*}',
+ array('controller' => 'Products', 'category' => 'default')
+ );
+ Router::connect('/products/{:category}', array('Products::category'));
+
+ $this->request = new Request();
+ $this->request->url = '/products/add';
+ $result = Router::parse($this->request);
+ $expected = array(
+ 'controller' => 'Products',
+ 'action' => 'add',
+ 'category' => 'default'
+ );
+ $this->assertEqual($expected, $result->params);
+ }
+
/**
* Tests that URLs specified as "Controller::action" are interpreted properly.
*/

0 comments on commit ac85d00

Please sign in to comment.