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

Commit

Permalink
Updated test cases for Zepto\Router
Browse files Browse the repository at this point in the history
  • Loading branch information
hassankhan committed Nov 12, 2013
1 parent 668f6c1 commit 5f1ab43
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
7 changes: 2 additions & 5 deletions library/Zepto/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @package Zepto
* @subpackage Router
* @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
* @author Hassan Khan <contact@hassankhan.me>
* @license MIT
*/

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
66 changes: 64 additions & 2 deletions tests/Zepto/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -134,7 +154,7 @@ public function testRouteAdd()
$this->object->route('/hello', $callback);

$expected = array(
10 => array(
'GET' => array(
'#^/hello/$#' => $callback
)
);
Expand All @@ -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);
}

}

0 comments on commit 5f1ab43

Please sign in to comment.