Add test for Router, and making some minor fixes on Router

This commit is contained in:
ohartl 2016-03-05 15:58:09 +01:00
parent a0c88ff757
commit 800777a4e4
3 changed files with 204 additions and 12 deletions

View file

@ -21,20 +21,55 @@ class Router
);
/**
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* @codeCoverageIgnore
*/
private function __clone()
{
}
/**
* @param array $routes
*/
public static function init($routes)
{
static::$routes = $routes;
}
/**
* @param string $method
*
* @return bool
*/
protected static function isValidMethod($method)
{
return in_array(
$method,
array(
static::METHOD_GET,
static::METHOD_POST
)
);
}
/**
* @param string|array $methods
* @param string $pattern
* @param callable|array|string $routeConfig
* @param array $permission
*
* @throws Exception
*/
public static function addRoute($methods, $pattern, $routeConfig, $permission = null)
{
@ -51,6 +86,10 @@ class Router
foreach($methods as $method){
$method = strtoupper($method);
if(!static::isValidMethod($method)){
throw new Exception('Unsupported HTTP method "'.$method.'".');
}
if(!isset(static::$routes[$method])){
static::$routes[$method] = array();
}
@ -98,24 +137,28 @@ class Router
* @param string $method
*
* @return string
*
* @throws Exception
*/
public static function execute($url, $method = self::METHOD_GET)
{
$method = strtoupper($method);
if(!in_array($method, array(static::METHOD_GET, static::METHOD_POST)) && !isset(self::$routes[$method])){
return 'Unsupported HTTP method.';
if(!static::isValidMethod($method) && !isset(self::$routes[$method])){
throw new Exception('Unsupported HTTP method "'.$method.'".');
}
foreach(self::$routes[$method] as $route){
if(rtrim($route['pattern'], '/') === rtrim($url, '/')){
if(!is_null($route['permission'])){
if(!Auth::isLoggedIn() || !Auth::hasPermission($route['permission'])){
return static::loadAndBufferOutput(static::$errorPages[403]);
if(isset(self::$routes[$method])){
foreach(self::$routes[$method] as $route){
if(rtrim($route['pattern'], '/') === rtrim($url, '/')){
if(!is_null($route['permission'])){
if(!Auth::isLoggedIn() || !Auth::hasPermission($route['permission'])){
return static::loadAndBufferOutput(static::$errorPages[403]);
}
}
}
return static::resolveRouteConfig($route['config']);
return static::resolveRouteConfig($route['config']);
}
}
}
@ -136,7 +179,10 @@ class Router
/**
* @param int $errorNumber
*
* @return string|null
*
* @codeCoverageIgnore
*/
public static function displayError($errorNumber)
{
@ -242,6 +288,8 @@ class Router
* Redirect user to an url
*
* @param string $url
*
* @codeCoverageIgnore
*/
public static function redirect($url)
{

View file

@ -1,7 +1,4 @@
<phpunit bootstrap="include/php/default.inc.php">
<php>
<env name="TEST_CONFIG" value="tests/config/"/>
</php>
<testsuites>
<testsuite name="webmum">
<directory>tests</directory>

147
tests/RouterTest.php Normal file
View file

@ -0,0 +1,147 @@
<?php
/**
* @covers Router
*/
class RouterTest extends TestCase
{
const BASE_URL = 'http://test.tld/somedir';
public function setUp()
{
Config::set('base_url', self::BASE_URL);
Router::init(array());
}
public function testUrl()
{
$this->assertEquals(self::BASE_URL, Router::url());
$this->assertEquals(self::BASE_URL, Router::url('/'));
$this->assertEquals(self::BASE_URL.'/this/sub/dir?get=123', Router::url('this/sub/dir?get=123'));
}
public function testAdd()
{
Router::addRoute(Router::METHOD_GET, 'test-get', 'test-get-file');
Router::execute('test-get', Router::METHOD_GET);
Router::addRoute(Router::METHOD_POST, 'test-post', 'test-post-file');
Router::execute('test-post', Router::METHOD_POST);
Router::addRoute(array(Router::METHOD_GET, Router::METHOD_POST), 'test-mixed', 'test-mixed-file');
Router::execute('test-mixed', Router::METHOD_GET);
Router::execute('test-mixed', Router::METHOD_POST);
}
public function testAddCallback()
{
$reachedCallback = false;
Router::addRoute(Router::METHOD_GET, 'test-callback', function() use(&$reachedCallback) {
$reachedCallback = true;
});
Router::execute('test-callback', Router::METHOD_GET);
$this->assertTrue($reachedCallback);
}
/**
* @expectedException Exception
* @expectedExceptionMessageRegExp /unsupported/i
*/
public function testAddMethodUnsupported()
{
Router::addRoute('not-a-method', 'test-fail', 'test-fail-file');
}
public function testAddShortcuts()
{
Router::addGet('test-get', 'test-get-file');
Router::execute('test-get', Router::METHOD_GET);
Router::addPost('test-post', 'test-post-file');
Router::execute('test-post', Router::METHOD_POST);
Router::addMixed('test-mixed', 'test-mixed-file');
Router::execute('test-mixed', Router::METHOD_GET);
Router::execute('test-mixed', Router::METHOD_POST);
}
/**
* @expectedException Exception
* @expectedExceptionMessageRegExp /unsupported/i
*/
public function testExecuteMethodUnsupported()
{
Router::execute('test-fail', 'not-a-method');
}
public function testExecuteCurrentRequest()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/somedir/test-get';
Router::executeCurrentRequest();
}
public function testRouteWithPermission()
{
$this->assertFalse(Auth::isLoggedIn());
$reachedCallback = false;
Router::addRoute(Router::METHOD_GET, 'test-perm-admin', function() use(&$reachedCallback) {
$reachedCallback = true;
}, User::ROLE_ADMIN);
Router::addRoute(Router::METHOD_GET, 'test-perm-user', function() use(&$reachedCallback) {
$reachedCallback = true;
}, User::ROLE_USER);
$reachedCallback = false;
Router::execute('test-perm-admin', Router::METHOD_GET);
$this->assertFalse($reachedCallback);
$reachedCallback = false;
Router::execute('test-perm-user', Router::METHOD_GET);
$this->assertFalse($reachedCallback);
// Now auth as admin and try again
Auth::login('admin@domain.tld', 'testtest');
$reachedCallback = false;
Router::execute('test-perm-admin', Router::METHOD_GET);
$this->assertTrue($reachedCallback);
$reachedCallback = false;
Router::execute('test-perm-user', Router::METHOD_GET);
$this->assertTrue($reachedCallback);
Auth::logout();
// Now auth as user and try again
Auth::login('user@domain.tld', 'testtest');
$reachedCallback = false;
Router::execute('test-perm-admin', Router::METHOD_GET);
$this->assertFalse($reachedCallback);
$reachedCallback = false;
Router::execute('test-perm-user', Router::METHOD_GET);
$this->assertTrue($reachedCallback);
Auth::logout();
}
}