From 121a15bbcff39bb3ab0b8e8192dc087dc5c56b27 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 13 Jul 2011 23:24:22 -0500 Subject: [PATCH] refactored route parameter parsing tests. --- system/router.php | 2 +- tests/suite/RouterTest.php | 39 ++++++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/system/router.php b/system/router.php index dc9a870c..858753dd 100644 --- a/system/router.php +++ b/system/router.php @@ -107,7 +107,7 @@ private static function translate_wildcards($key) * @param string $route * @return array */ - public static function parameters($uri, $route) + private static function parameters($uri, $route) { return array_values(array_intersect_key(explode('/', $uri), preg_grep('/\(.+\)/', explode('/', $route)))); } diff --git a/tests/suite/RouterTest.php b/tests/suite/RouterTest.php index 680bab3b..cff55cbc 100644 --- a/tests/suite/RouterTest.php +++ b/tests/suite/RouterTest.php @@ -45,6 +45,16 @@ public function testRouterRoutesToProperRouteWhenSegmentsArePresent() $this->assertEquals(System\Router::route('POST', 'home')->callback['name'], 'post-home'); } + public function testRouterGivesRouteProperSegmentsWhenTheyArePresent() + { + $this->assertEquals(System\Router::route('GET', 'user/1')->parameters[0], 1); + $this->assertEquals(count(System\Router::route('GET', 'user/1')->parameters), 1); + + $this->assertEquals(System\Router::route('GET', 'user/taylor/25/edit')->parameters[0], 'taylor'); + $this->assertEquals(System\Router::route('GET', 'user/taylor/25/edit')->parameters[1], 25); + $this->assertEquals(count(System\Router::route('GET', 'user/taylor/25/edit')->parameters), 2); + } + public function testRouterRoutesToProperRouteWhenUsingOptionalSegments() { $this->assertEquals(System\Router::route('GET', 'cart')->callback['name'], 'cart'); @@ -54,6 +64,21 @@ public function testRouterRoutesToProperRouteWhenUsingOptionalSegments() $this->assertEquals(System\Router::route('GET', 'download/1/a')->callback['name'], 'download'); } + public function testRouterGivesRouteProperOptionalSegmentsWhenTheyArePresent() + { + $this->assertTrue(is_array(System\Router::route('GET', 'cart')->parameters)); + $this->assertEquals(count(System\Router::route('GET', 'cart')->parameters), 0); + $this->assertEquals(System\Router::route('GET', 'cart/1')->parameters[0], 1); + + $this->assertEquals(count(System\Router::route('GET', 'download')->parameters), 0); + $this->assertEquals(System\Router::route('GET', 'download/1')->parameters[0], 1); + $this->assertEquals(count(System\Router::route('GET', 'download/1')->parameters), 1); + + $this->assertEquals(System\Router::route('GET', 'download/1/a')->parameters[0], 1); + $this->assertEquals(System\Router::route('GET', 'download/1/a')->parameters[1], 'a'); + $this->assertEquals(count(System\Router::route('GET', 'download/1/a')->parameters), 2); + } + public function testRouterReturnsNullWhenRouteNotFound() { $this->assertNull(System\Router::route('GET', 'user/taylor/taylor/edit')); @@ -97,18 +122,4 @@ private function setupRoutesDirectory() file_put_contents(APP_PATH.'routes/cart.php', " function() {return '/cart/edit';}); ?>", LOCK_EX); } - public function testParameterMethodReturnsNoParametersWhenNoneArePresent() - { - $this->assertEmpty(System\Router::parameters('GET /test/route', 'GET /test/route')); - $this->assertEmpty(System\Router::parameters('GET /', 'GET /')); - } - - public function testParameterMethodReturnsParametersWhenTheyArePresent() - { - $this->assertEquals(System\Router::parameters('GET /user/1', 'GET /user/(:num)'), array(1)); - $this->assertEquals(System\Router::parameters('GET /user/1/2', 'GET /user/(:num)/(:num)'), array(1, 2)); - $this->assertEquals(System\Router::parameters('GET /user/1/test', 'GET /user/(:num)/(:any)'), array(1, 'test')); - $this->assertEquals(System\Router::parameters('GET /user/1/test/again', 'GET /user/(:num)/test/(:any)'), array(1, 'again')); - } - } \ No newline at end of file