Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bringing Router coverage up to 94.45%, minor router refactorings

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8258 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
commit 9da7b6ef552ef1de1b1dfdb540c3ff8aeeb586f0 1 parent f53181b
@tim-kos tim-kos authored
Showing with 78 additions and 10 deletions.
  1. +9 −10 cake/libs/router.php
  2. +69 −0 cake/tests/cases/libs/router.test.php
View
19 cake/libs/router.php
@@ -311,9 +311,9 @@ function mapResources($controller, $options = array()) {
/**
* Builds a route regular expression
*
- * @param string $route An empty string, or a route string "/"
- * @param array $default NULL or an array describing the default route
- * @param array $params An array matching the named elements in the route to regular expressions which that element should match.
+ * @param string $route An empty string, or a route string "/"
+ * @param array $default NULL or an array describing the default route
+ * @param array $params An array matching the named elements in the route to regular expressions which that element should match.
* @return array
* @see routes
* @access public
@@ -533,10 +533,7 @@ function __matchRoute($route, $url) {
function compile($i) {
$route = $this->routes[$i];
- if (!list($pattern, $names) = $this->writeRoute($route[0], $route[1], $route[2])) {
- unset($this->routes[$i]);
- return array();
- }
+ list($pattern, $names) = $this->writeRoute($route[0], $route[1], $route[2]);
$this->routes[$i] = array(
$route[0], $pattern, $names,
array_merge(array('plugin' => null, 'controller' => null), (array)$route[1]),
@@ -1271,9 +1268,9 @@ function stripEscape($param) {
return $param;
}
- $return = preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $param);
- return $return;
+ return preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $param);
}
+
foreach ($param as $key => $value) {
if (is_string($value)) {
$return[$key] = preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $value);
@@ -1346,7 +1343,9 @@ function getArgs($args, $options = array()) {
continue;
}
$param = $_this->stripEscape($param);
- if ((!isset($options['named']) || !empty($options['named'])) && strpos($param, $_this->named['separator']) !== false) {
+
+ $separatorIsPresent = strpos($param, $_this->named['separator']) !== false;
+ if ((!isset($options['named']) || !empty($options['named'])) && $separatorIsPresent) {
list($key, $val) = explode($_this->named['separator'], $param, 2);
$hasRule = isset($rules[$key]);
$passIt = (!$hasRule && !$greedy) || ($hasRule && !Router::matchNamed($key, $val, $rules[$key], $context));
View
69 cake/tests/cases/libs/router.test.php
@@ -1670,5 +1670,74 @@ function testStripPlugin() {
$this->assertEqual(Router::stripPlugin($url), $url);
$this->assertEqual(Router::stripPlugin($url, null), $url);
}
+/**
+ * testCurentRoute
+ *
+ * This test needs some improvement and actual requestAction() usage
+ *
+ * @return void
+ * @access public
+ */
+ function testCurentRoute() {
+ $url = array('controller' => 'pages', 'action' => 'display', 'government');
+ Router::connect('/government', $url);
+ Router::parse('/government');
+ $route = Router::currentRoute();
+ $this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);
+ }
+/**
+ * testRequestRoute
+ *
+ * @return void
+ * @access public
+ */
+ function testRequestRoute() {
+ $url = array('controller' => 'products', 'action' => 'display', 5);
+ Router::connect('/government', $url);
+ Router::parse('/government');
+ $route = Router::requestRoute();
+ $this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);
+
+ // test that the first route is matched
+ $newUrl = array('controller' => 'products', 'action' => 'display', 6);
+ Router::connect('/government', $url);
+ Router::parse('/government');
+ $route = Router::requestRoute();
+ $this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);
+
+ // test that an unmatched route does not change the current route
+ $newUrl = array('controller' => 'products', 'action' => 'display', 6);
+ Router::connect('/actor', $url);
+ Router::parse('/government');
+ $route = Router::requestRoute();
+ $this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);
+ }
+/**
+ * testGetParams
+ *
+ * @return void
+ * @access public
+ */
+ function testGetParams() {
+ $paths = array('base' => '/', 'here' => '/products/display/5', 'webroot' => '/webroot');
+ $params = array('param1' => '1', 'param2' => '2');
+ Router::setRequestInfo(array($params, $paths));
+ $expected = array(
+ 'plugin' => false, 'controller' => false, 'action' => false,
+ 'param1' => '1', 'param2' => '2'
+ );
+ $this->assertEqual(Router::getparams(), $expected);
+ $this->assertEqual(Router::getparam('controller'), false);
+ $this->assertEqual(Router::getparam('param1'), '1');
+ $this->assertEqual(Router::getparam('param2'), '2');
+
+ Router::reload();
+
+ $params = array('controller' => 'pages', 'action' => 'display');
+ Router::setRequestInfo(array($params, $paths));
+ $expected = array('plugin' => false, 'controller' => 'pages', 'action' => 'display');
+ $this->assertEqual(Router::getparams(), $expected);
+ $this->assertEqual(Router::getparams(true), $expected);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.