From 800777a4e49c67b2664f4bb1a2c9f952c0fe1ed9 Mon Sep 17 00:00:00 2001 From: ohartl Date: Sat, 5 Mar 2016 15:58:09 +0100 Subject: [PATCH] Add test for Router, and making some minor fixes on Router --- include/php/classes/Router.php | 66 +++++++++++++-- phpunit.xml | 3 - tests/RouterTest.php | 147 +++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+), 12 deletions(-) create mode 100644 tests/RouterTest.php diff --git a/include/php/classes/Router.php b/include/php/classes/Router.php index be5eace..6bca44e 100644 --- a/include/php/classes/Router.php +++ b/include/php/classes/Router.php @@ -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 @@ 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(); } @@ -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']); + } } } @@ -136,7 +179,10 @@ public static function executeCurrentRequest() /** * @param int $errorNumber + * * @return string|null + * + * @codeCoverageIgnore */ public static function displayError($errorNumber) { @@ -242,6 +288,8 @@ public static function url($url = '') * Redirect user to an url * * @param string $url + * + * @codeCoverageIgnore */ public static function redirect($url) { diff --git a/phpunit.xml b/phpunit.xml index d3bca7e..46871ee 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,4 @@ - - - tests diff --git a/tests/RouterTest.php b/tests/RouterTest.php new file mode 100644 index 0000000..5c6dbf0 --- /dev/null +++ b/tests/RouterTest.php @@ -0,0 +1,147 @@ +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(); + } +} \ No newline at end of file