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

Commit

Permalink
Massively refactor Zepto\Router and updated tests.
Browse files Browse the repository at this point in the history
Removed ``Zepto\Router::dispatch()``
  • Loading branch information
hassankhan committed Nov 14, 2013
1 parent 2d82b48 commit 61e1747
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 71 deletions.
76 changes: 34 additions & 42 deletions library/Zepto/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,7 @@ class Router
*
* @var Callable
*/
protected $error_404 = null;

/**
* Contains the last route executed, used when chaining methods calls in
* the route() function (Such as for put(), post(), and delete()).
*
* @var pointer
*/
protected $last_route = null;
public $error_404 = null;

/**
* An array containing the parameters to pass to the callback function,
Expand All @@ -109,14 +101,6 @@ class Router
*/
protected $routes_original = array();

/**
* Whether or not to display errors for things like malformed routes or
* conflicting routes.
*
* @var boolean
*/
protected $show_errors = true;

/**
* A sanitized version of the URL, excluding the domain and base component
*
Expand Down Expand Up @@ -148,6 +132,8 @@ public function __construct($url = null)
}
}

$this->error_404 = $this->set_404_callback();

// Store the dirty version of the URL
$this->url_dirty = $url;

Expand All @@ -160,6 +146,7 @@ public function __construct($url = null)
* execute the default function and return false.
*
* @return array
* @todo See if there's any way of avoiding the triple for-if
*/
public function run()
{
Expand All @@ -168,7 +155,7 @@ public function run()

// 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');
throw new \Exception('No routes exist in the routing table. Add some');
}

// Sort the array by request method
Expand Down Expand Up @@ -204,41 +191,33 @@ public function run()
}
}
}

// Was a match found or should we execute the default callback?
if (!$matched_route && $this->error_404 !== null) {
return array('params' => $this->url_clean, 'callback' => $this->error_404, 'route' => false, 'original_route' => false);
}
}

/**
* Calls the appropriate callback function and passes the given parameters
* given by Router::run()
* Runs the router matching engine and then calls the matching route's callback.
* If no matching route is found, then returns false
*
* @return boolean Returns true on a successful dispatch, otherwise false on 404 errors
* @uses Router::run()
* @return mixed
*/
public function dispatch()
public function execute()
{
try{
$this->run();
}
catch (Exception $e) {
// Add logging stuff here - maybe?
}

// Removing this makes testing easier, how can I fix this?
$this->error_404 = $this->routes['GET']['#^/404/$#'];

if ($this->callback == null || $this->params == null) {
call_user_func($this->error_404);
return false;
}

call_user_func_array($this->callback, $this->params);
return true;
}

/**
* Runs the router matching engine and then calls the dispatcher
*
* @uses Router::run()
* @uses Router::dispatch()
*/
public function execute()
{
$this->error_404 = $this->routes['GET']['#^/404/$#'];
$this->run();
$this->dispatch();
return call_user_func_array($this->callback, $this->params);
}

/**
Expand Down Expand Up @@ -325,6 +304,19 @@ public function get_routes()
return $this->routes;
}

/**
* Sets the 404 callback
*/
public function set_404_callback()
{
if (func_num_args() === 1 && gettype(func_get_arg(0)) === 'Callable') {
return $callback;
}
return function() {
echo 'Page doesn\'t exist';
};
}

/**
* Retrieves the part of the URL after the base (Calculated from the location
* of the main application file, such as index.php), excluding the query
Expand Down
2 changes: 1 addition & 1 deletion library/Zepto/Zepto.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function ($container) {
public function run()
{
$router = $this->container['router'];
$router->execute();
return $router->execute();
}

protected function load_files()
Expand Down
81 changes: 53 additions & 28 deletions tests/Zepto/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ protected function tearDown()
*/
public function testRun()
{

$_SERVER['REQUEST_URL'] = '/zepto/index.php/get';
$_SERVER['REQUEST_URI'] = '/zepto/index.php/get';

Expand Down Expand Up @@ -73,7 +72,6 @@ public function testRun()
*/
public function testRunWithParameters()
{

$_SERVER['REQUEST_URL'] = '/zepto/index.php/get/random_value';
$_SERVER['REQUEST_URI'] = '/zepto/index.php/get/random_value';

Expand Down Expand Up @@ -102,7 +100,6 @@ public function testRunWithParameters()
*/
public function testRunOnNonexistentRoute()
{

$_SERVER['REQUEST_URL'] = '/zepto/index.php/get';
$_SERVER['REQUEST_URI'] = '/zepto/index.php/get';

Expand All @@ -119,51 +116,53 @@ public function testRunOnNonexistentRoute()
}

/**
* @covers Zepto\Router::dispatch
* @covers Zepto\Router::execute
*/
public function testDispatch()
public function testExecute()
{
$_SERVER['REQUEST_URL'] = '/zepto/index.php/get';
$_SERVER['REQUEST_URI'] = '/zepto/index.php/get';

$router = new Router;

$callback = function() {
echo 'Successful dispatch';
return 'Successful GET';
};
$router->get('/get', $callback);
$router->run();

$this->assertTrue($router->dispatch());
$callback = function() {
return 'Shit! A 404';
};
$router->get('/404', $callback);

$expected = 'Successful GET';
$actual = $router->execute();

$this->assertEquals($expected, $actual);
}

/**
* @covers Zepto\Router::dispatch
* @expectedException Exception
* @covers Zepto\Router::execute
*/
public function testDispatchToNonexistentRoute()
public function testExecute404()
{
$_SERVER['REQUEST_URL'] = '/zepto/index.php/get';
$_SERVER['REQUEST_URI'] = '/zepto/index.php/get';
$_SERVER['REQUEST_URL'] = '/zepto/index.php/unknown_url';
$_SERVER['REQUEST_URI'] = '/zepto/index.php/unknown_url';

$router = new Router;
$router->run();

$this->assertFalse($router->dispatch());
}
$callback = function() {
return 'Successful GET';
};
$router->get('/get', $callback);

/**
* @covers Zepto\Router::run()
* @covers Zepto\Router::dispatch()
* @covers Zepto\Router::execute
* @todo Implement testExecute().
*/
public function testExecute()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$callback = function() {
return 'Shit! A 404';
};
$router->get('/404', $callback);

$actual = $router->execute();
$this->assertFalse($actual);
}

/**
Expand Down Expand Up @@ -250,4 +249,30 @@ public function testPostRouteAdd()
$this->assertEquals($expected, $actual);
}

public function testGetRoutes()
{
$router = new Router;

$post_callback = function() {
echo '1';
};
$router->post('/post', $post_callback);

$get_callback = function() {
echo '1';
};
$router->get('/get', $get_callback);

$expected = array(
'GET' => array(
'#^/get/$#' => $get_callback
),
'POST' => array(
'#^/post/$#' => $post_callback
)
);

$actual = $router->get_routes();
$this->assertEquals($expected, $actual);
}
}

0 comments on commit 61e1747

Please sign in to comment.