diff --git a/library/Zepto/Router.php b/library/Zepto/Router.php index 0ede88f..d44c006 100644 --- a/library/Zepto/Router.php +++ b/library/Zepto/Router.php @@ -10,6 +10,7 @@ * @package Zepto * @subpackage Router * @author Brandon Wamboldt + * @author Hassan Khan * @license MIT */ @@ -265,7 +266,6 @@ public function dispatch() { if ($this->callback == null || $this->params == null) { throw new \Exception('No callback or parameters found, please run $router->run() before $router->dispatch(). Please make sure you\'ve set a default route.'); - return false; } call_user_func_array($this->callback, $this->params); @@ -346,10 +346,7 @@ public function route($route, $callback, $request_method = 'GET') // Does this URL routing rule already exist in the routing table? if (isset($this->routes[$request_method][$route])) { // Trigger a new error and exception if errors are on - if ($this->show_errors) { - throw new \Exception('The URI "' . htmlspecialchars($route) . '" already exists in the routing table'); - } - return false; + throw new \Exception('The URI "' . htmlspecialchars($route) . '" already exists in the routing table'); } // Add the route to our routing array diff --git a/tests/Zepto/RouterTest.php b/tests/Zepto/RouterTest.php index 83f6078..38b7499 100644 --- a/tests/Zepto/RouterTest.php +++ b/tests/Zepto/RouterTest.php @@ -22,6 +22,7 @@ protected function setUp() $_SERVER['SERVER_NAME'] = 'zepto'; $_SERVER['SERVER_PORT'] = '80'; $_SERVER['SCRIPT_NAME'] = '/zepto/index.php'; + $_SERVER['REQUEST_URL'] = '/zepto/index.php/bar/xyz'; $_SERVER['REQUEST_URI'] = '/zepto/index.php/bar/xyz'; $_SERVER['PATH_INFO'] = '/bar/xyz'; $_SERVER['REQUEST_METHOD'] = 'GET'; @@ -95,7 +96,26 @@ public function testRun() */ public function testDispatchOnNonExistentRoute() { - $this->assertFalse($this->object->dispatch()); + $this->object->dispatch(); + } + + /** + * @covers Zepto\Router::dispatch + */ + public function testDispatch() + { + $_SERVER['REQUEST_URL'] = '/zepto/index.php/get'; + $_SERVER['REQUEST_URI'] = '/zepto/index.php/get'; + + $router = new Router; + + $callback = function() { + echo '1'; + }; + $router->get('/get', $callback); + $router->run(); + + $this->assertTrue($router->dispatch()); } /** @@ -134,7 +154,7 @@ public function testRouteAdd() $this->object->route('/hello', $callback); $expected = array( - 10 => array( + 'GET' => array( '#^/hello/$#' => $callback ) ); @@ -144,4 +164,46 @@ public function testRouteAdd() $this->assertEquals($expected, $actual); } + /** + * @covers Zepto\Router::get + */ + public function testGetRouteAdd() + { + $callback = function() { + echo '1'; + }; + $this->object->get('/get', $callback); + + $expected = array( + 'GET' => array( + '#^/get/$#' => $callback + ) + ); + + $actual = $this->object->get_routes(); + + $this->assertEquals($expected, $actual); + } + + /** + * @covers Zepto\Router::post + */ + public function testPostRouteAdd() + { + $callback = function() { + echo '1'; + }; + $this->object->post('/post', $callback); + + $expected = array( + 'POST' => array( + '#^/post/$#' => $callback + ) + ); + + $actual = $this->object->get_routes(); + + $this->assertEquals($expected, $actual); + } + }