@ -31,12 +31,11 @@ class RouterTest extends PHPUnit_Framework_TestCase
echo 'OK';
}
// Checks if a route was matched
function check($str = 'OK '){
// Checks if a route was matched with a given output
function check($str = ''){
$route = $this->router->route($this->request);
$params = array_values($route->params);
array_push($params, $route);
$this->assertTrue(is_callable($route->callback));
@ -50,7 +49,7 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->router->map('/', array($this, 'ok'));
$this->request->url = '/';
$this->check();
$this->check('OK' );
}
// Simple path
@ -58,7 +57,7 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->router->map('/path', array($this, 'ok'));
$this->request->url = '/path';
$this->check();
$this->check('OK' );
}
// POST route
@ -67,7 +66,7 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->request->url = '/';
$this->request->method = 'POST';
$this->check();
$this->check('OK' );
}
// Either GET or POST route
@ -76,7 +75,7 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->request->url = '/';
$this->request->method = 'GET';
$this->check();
$this->check('OK' );
}
// Test regular expression matching
@ -84,7 +83,7 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->router->map('/num/[0-9]+', array($this, 'ok'));
$this->request->url = '/num/1234';
$this->check();
$this->check('OK' );
}
// Passing URL parameters
@ -146,6 +145,23 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->router->map('/account/*', array($this, 'ok'));
$this->request->url = '/account/123/abc/xyz';
$this->check('OK');
}
// Check if route was passed
function testRoutePassing(){
$this->router->map('/yes_route', function($route){
$this->assertTrue(is_object($route));
},
true);
$this->request->url = '/yes_route';
$this->check();
$this->router->map('/no_route', function($route = null){
$this->assertTrue(is_null($route));
},
false);
$this->request->url = '/no_route';
$this->check();
}
@ -153,9 +169,11 @@ class RouterTest extends PHPUnit_Framework_TestCase
function testSplatWildcard(){
$this->router->map('/account/*', function($route){
echo $route->splat;
});
$this->request->url = '/account/123/abc/xyz';
},
true);
$this->request->url = '/account/456/def/xyz';
$this->check('123/abc/xyz');
$this->check('456/def /xyz');
}
}