2015-05-03 20:50:36 +00:00
|
|
|
<?php
|
2015-10-11 18:14:49 +00:00
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* FuzeWorks.
|
2015-10-11 18:14:49 +00:00
|
|
|
*
|
|
|
|
* The FuzeWorks MVC PHP FrameWork
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 TechFuze
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author TechFuze
|
2016-05-07 17:22:09 +00:00
|
|
|
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
2015-10-11 18:14:49 +00:00
|
|
|
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2015-10-11 18:14:49 +00:00
|
|
|
* @link http://fuzeworks.techfuze.net
|
|
|
|
* @since Version 0.0.1
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2015-10-11 18:14:49 +00:00
|
|
|
* @version Version 0.0.1
|
|
|
|
*/
|
2016-05-25 15:01:59 +00:00
|
|
|
use FuzeWorks\Router;
|
2015-09-06 13:42:57 +00:00
|
|
|
|
2015-05-03 20:50:36 +00:00
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* Class RouterTest.
|
2015-05-03 20:50:36 +00:00
|
|
|
*
|
|
|
|
* This test will test the router
|
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
class routerTest extends CoreTestAbstract
|
2015-05-03 20:50:36 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testParsePath()
|
|
|
|
{
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Act and assert
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath('a/b/c/d/');
|
|
|
|
$this->assertEquals('a/b/c/d', Router::getPath());
|
2015-05-03 20:50:36 +00:00
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath('//a//b//c');
|
|
|
|
$this->assertEquals('a/b/c', Router::getPath());
|
2015-05-03 20:50:36 +00:00
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath('/');
|
|
|
|
$this->assertEquals('', Router::getPath());
|
2015-05-03 20:50:36 +00:00
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath('');
|
|
|
|
$this->assertEquals('', Router::getPath());
|
2015-05-03 20:50:36 +00:00
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath(false);
|
|
|
|
$this->assertEquals('', Router::getPath());
|
2015-05-03 20:50:36 +00:00
|
|
|
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath(null);
|
|
|
|
$this->assertEquals('', Router::getPath());
|
2015-05-03 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testParsePath
|
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testDoRoute()
|
|
|
|
{
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Act
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath('a/b/c/d/');
|
|
|
|
Router::route(false);
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
// Whole route
|
2016-05-07 17:22:09 +00:00
|
|
|
$this->assertEquals(array('a', 'b', 'c/d'), array(Router::getMatches()['controller'], Router::getMatches()['function'], Router::getMatches()['parameters']));
|
2015-10-11 18:14:49 +00:00
|
|
|
$this->assertEquals('a', Router::getMatches()['controller']);
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Parameters
|
2015-10-11 18:14:49 +00:00
|
|
|
$this->assertEquals('c/d', Router::getMatches()['parameters']);
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Function and controller
|
2015-10-11 18:14:49 +00:00
|
|
|
$this->assertEquals('a', Router::getMatches()['controller']);
|
|
|
|
$this->assertEquals('b', Router::getMatches()['function']);
|
2015-05-03 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testDoRoute
|
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testOddRoutes()
|
|
|
|
{
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Empty path
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath(null);
|
|
|
|
Router::route(false);
|
2015-10-11 18:14:49 +00:00
|
|
|
$this->assertEquals(null, Router::getMatches()['controller']);
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Double slashes
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath('a///b');
|
|
|
|
Router::route(false);
|
2016-05-07 17:22:09 +00:00
|
|
|
$this->assertEquals(array('a', 'b'), array(Router::getMatches()['controller'], Router::getMatches()['function']));
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Escaped path path
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath('/a\/b\/c/');
|
|
|
|
Router::route(false);
|
2016-05-07 17:22:09 +00:00
|
|
|
$this->assertEquals(array('a\\', 'b\\', 'c'), array(Router::getMatches()['controller'], Router::getMatches()['function'], Router::getMatches()['parameters']));
|
2015-10-11 18:14:49 +00:00
|
|
|
$this->assertNotEquals('a', Router::getMatches()['controller']);
|
2015-05-03 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testCustomRoute()
|
|
|
|
{
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::addRoute('/test1\/test2/', 'callable');
|
|
|
|
$this->assertArraySubset(array('/test1\/test2/' => 'callable'), Router::getRoutes());
|
2015-05-03 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
public function testCustomRouteWithParameters()
|
|
|
|
{
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::addRoute('/^b\/(?P<controller>[^\/]+)\/?(?P<function>.+?)$/', 'callable');
|
|
|
|
Router::addRoute('/e\/(?P<function>[^\/]+)/', 'callable');
|
|
|
|
Router::addRoute('/b\/b$/', 'callable');
|
|
|
|
|
|
|
|
Router::setPath('b/controller_a/function_a');
|
|
|
|
Router::route(false);
|
2015-10-11 18:14:49 +00:00
|
|
|
$this->assertEquals('controller_a', Router::getMatches()['controller']);
|
|
|
|
$this->assertEquals('function_a', Router::getMatches()['function']);
|
2015-09-06 13:42:57 +00:00
|
|
|
|
|
|
|
Router::setPath('e/function_b/c');
|
|
|
|
Router::route(false);
|
2016-05-25 12:28:57 +00:00
|
|
|
$this->assertFalse(isset(Router::getMatches()['controller']));
|
2015-10-11 18:14:49 +00:00
|
|
|
$this->assertEquals('function_b', Router::getMatches()['function']);
|
2015-09-06 13:42:57 +00:00
|
|
|
|
|
|
|
Router::setPath('b/b');
|
|
|
|
Router::route(false);
|
2016-05-25 12:28:57 +00:00
|
|
|
$this->assertFalse(isset(Router::getMatches()['controller']));
|
|
|
|
$this->assertFalse(isset(Router::getMatches()['function']));
|
2015-09-06 13:42:57 +00:00
|
|
|
|
|
|
|
Router::setPath('a/b');
|
|
|
|
Router::route(false);
|
2015-10-11 18:14:49 +00:00
|
|
|
$this->assertEquals('a', Router::getMatches()['controller']);
|
|
|
|
$this->assertEquals('b', Router::getMatches()['function']);
|
2015-05-03 20:50:36 +00:00
|
|
|
}
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|