From 2d82b48d25a091ebe6e66e66a7608dee9ef62193 Mon Sep 17 00:00:00 2001 From: Hassan Khan Date: Thu, 14 Nov 2013 01:49:27 +0000 Subject: [PATCH] Updated ``Zepto\Router`` and test --- library/Zepto/Router.php | 12 ++++++++---- tests/Zepto/RouterTest.php | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/library/Zepto/Router.php b/library/Zepto/Router.php index 21da24e..ad4c4a1 100644 --- a/library/Zepto/Router.php +++ b/library/Zepto/Router.php @@ -159,14 +159,19 @@ public function __construct($url = null) * Tries to match one of the URL routes to the current URL, otherwise * execute the default function and return false. * - * @return boolean + * @return array */ public function run() { // Whether or not we have matched the URL to a route $matched_route = false; - // Sort the array by request_method + // If no routes have been added, then throw an exception + if (!array_key_exists('GET', $this->routes) === true) { + throw new \Exception('No routes exist in the routing table'); + } + + // Sort the array by request method ksort($this->routes); // Loop through each request_method level @@ -202,7 +207,6 @@ public function run() // Was a match found or should we execute the default callback? if (!$matched_route && $this->error_404 !== null) { - // call_user_func($this->error_404); return array('params' => $this->url_clean, 'callback' => $this->error_404, 'route' => false, 'original_route' => false); } } @@ -211,7 +215,7 @@ public function run() * Calls the appropriate callback function and passes the given parameters * given by Router::run() * - * @return boolean Returns true on a successful match, otherwise false + * @return boolean Returns true on a successful dispatch, otherwise false on 404 errors */ public function dispatch() { diff --git a/tests/Zepto/RouterTest.php b/tests/Zepto/RouterTest.php index 3da666b..4a9c6b1 100644 --- a/tests/Zepto/RouterTest.php +++ b/tests/Zepto/RouterTest.php @@ -96,6 +96,28 @@ public function testRunWithParameters() $this->assertEquals($expected, $actual); } + /** + * @covers Zepto\Router::run + * @expectedException Exception + */ + public function testRunOnNonexistentRoute() + { + + $_SERVER['REQUEST_URL'] = '/zepto/index.php/get'; + $_SERVER['REQUEST_URI'] = '/zepto/index.php/get'; + + $router = new Router; + + $expected = array( + 'callback' => '', + 'params' => array('/get/'), + 'route' => '#^/get/$#', + 'original_route' => '/get' + ); + + $actual = $router->run(); + } + /** * @covers Zepto\Router::dispatch */ @@ -119,7 +141,7 @@ public function testDispatch() * @covers Zepto\Router::dispatch * @expectedException Exception */ - public function testDispatchOnNonExistentRoute() + public function testDispatchToNonexistentRoute() { $_SERVER['REQUEST_URL'] = '/zepto/index.php/get'; $_SERVER['REQUEST_URI'] = '/zepto/index.php/get'; @@ -131,6 +153,8 @@ public function testDispatchOnNonExistentRoute() } /** + * @covers Zepto\Router::run() + * @covers Zepto\Router::dispatch() * @covers Zepto\Router::execute * @todo Implement testExecute(). */