Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Updated Zepto\Router and test
Browse files Browse the repository at this point in the history
  • Loading branch information
hassankhan committed Nov 14, 2013
1 parent cd36412 commit 2d82b48
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
12 changes: 8 additions & 4 deletions library/Zepto/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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()
{
Expand Down
26 changes: 25 additions & 1 deletion tests/Zepto/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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';
Expand All @@ -131,6 +153,8 @@ public function testDispatchOnNonExistentRoute()
}

/**
* @covers Zepto\Router::run()
* @covers Zepto\Router::dispatch()
* @covers Zepto\Router::execute
* @todo Implement testExecute().
*/
Expand Down

0 comments on commit 2d82b48

Please sign in to comment.