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

Commit

Permalink
Add test for Router, and making some minor fixes on Router
Browse files Browse the repository at this point in the history
  • Loading branch information
ohartl committed Mar 5, 2016
1 parent a0c88ff commit 800777a
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 12 deletions.
66 changes: 57 additions & 9 deletions include/php/classes/Router.php
Expand Up @@ -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)
{
Expand All @@ -51,6 +86,10 @@ public static function addRoute($methods, $pattern, $routeConfig, $permission =
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();
}
Expand Down Expand Up @@ -98,24 +137,28 @@ public static function addMixed($pattern, $routeConfig, $permission = null)
* @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']);
}
}
}

Expand All @@ -136,7 +179,10 @@ public static function executeCurrentRequest()

/**
* @param int $errorNumber
*
* @return string|null
*
* @codeCoverageIgnore
*/
public static function displayError($errorNumber)
{
Expand Down Expand Up @@ -242,6 +288,8 @@ public static function url($url = '')
* Redirect user to an url
*
* @param string $url
*
* @codeCoverageIgnore
*/
public static function redirect($url)
{
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml
@@ -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>
Expand Down
147 changes: 147 additions & 0 deletions tests/RouterTest.php
@@ -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();
}
}

0 comments on commit 800777a

Please sign in to comment.