2015-05-03 20:50:36 +00:00
|
|
|
<?php
|
2015-09-06 13:42:57 +00:00
|
|
|
use \FuzeWorks\Core;
|
|
|
|
use \FuzeWorks\Router;
|
|
|
|
|
2015-05-03 20:50:36 +00:00
|
|
|
/**
|
|
|
|
* Class RouterTest
|
|
|
|
*
|
|
|
|
* This test will test the router
|
|
|
|
*/
|
|
|
|
class RouterTest extends CoreTestAbstract
|
|
|
|
{
|
|
|
|
public function testParsePath(){
|
|
|
|
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
public function testDoRoute(){
|
|
|
|
|
|
|
|
// 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
|
2015-09-06 13:42:57 +00:00
|
|
|
$this->assertEquals(array('a','b',array('c','d')), array(Router::getController(), Router::getFunction(), Router::getParameters()));
|
|
|
|
$this->assertEquals('a', Router::getController());
|
|
|
|
$this->assertEquals('d', Router::getParameter(-1));
|
|
|
|
$this->assertEquals(null, Router::getParameter(5));
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Parameters
|
2015-09-06 13:42:57 +00:00
|
|
|
$this->assertEquals(array('c','d'), Router::getParameters());
|
|
|
|
$this->assertEquals('c', Router::getParameter(0));
|
|
|
|
$this->assertEquals('d', Router::getParameter(-1));
|
2015-05-03 20:50:36 +00:00
|
|
|
|
|
|
|
// Function and controller
|
2015-09-06 13:42:57 +00:00
|
|
|
$this->assertEquals('a', Router::getController());
|
|
|
|
$this->assertEquals('b', Router::getFunction());
|
2015-05-03 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testDoRoute
|
|
|
|
*/
|
|
|
|
public function testOddRoutes(){
|
|
|
|
|
|
|
|
// Empty path
|
2015-09-06 13:42:57 +00:00
|
|
|
Router::setPath(null);
|
|
|
|
Router::route(false);
|
|
|
|
$this->assertEquals(null, Router::getController());
|
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);
|
|
|
|
$this->assertEquals(array('a','b'), array(Router::getController(), Router::getFunction()));
|
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);
|
|
|
|
$this->assertEquals(array('a\\','b\\','c'), array(Router::getController(), Router::getFunction(), Router::getParameter(0)));
|
|
|
|
$this->assertNotEquals('a', Router::getController());
|
2015-05-03 20:50:36 +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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
$this->assertEquals('controller_a', Router::getController());
|
|
|
|
$this->assertEquals('function_a', Router::getFunction());
|
|
|
|
|
|
|
|
Router::setPath('e/function_b/c');
|
|
|
|
Router::route(false);
|
|
|
|
$this->assertEquals(null, Router::getController());
|
|
|
|
$this->assertEquals('function_b', Router::getFunction());
|
|
|
|
|
|
|
|
Router::setPath('b/b');
|
|
|
|
Router::route(false);
|
|
|
|
$this->assertEquals(null, Router::getController());
|
|
|
|
$this->assertEquals(null, Router::getFunction());
|
|
|
|
|
|
|
|
Router::setPath('a/b');
|
|
|
|
Router::route(false);
|
|
|
|
$this->assertEquals('a', Router::getController());
|
|
|
|
$this->assertEquals('b', Router::getFunction());
|
2015-05-03 20:50:36 +00:00
|
|
|
}
|
|
|
|
}
|