<?php
namespace gnomephp\mvc\router;
class TestRouter extends \gnomephp\testsuite\TestController{
private $router;
public function __construct(){
parent::__construct();
$this->router = new Router('/test/url/12', $_SERVER);
$this->router->parse("
# comment
# comment
GET /test/url/{<\d+>index} Test.method(\$index)
* /test/two/? Test.index()
* /test/both/{var}/? Test.both(\$var)
* /testNs \gnomephp\mvc\TestRouter.index()
");
}
protected function testFirstRouterRule(){
// Test the first router rule...
$route = $this->router->route();
$this->test($route)->assertInstanceOf('\gnomephp\mvc\router\BackendObject');
// Correct controller ?
$this->test($route->getController())->assertEqual('Test');
// Correct method ?
$this->test($route->getMethod())->assertEqual('method');
// Correct arguments returned ?
$this->test($route->getArguments())->assertArray()->assertEqual(array(0 => 12));
// Test reverse engineer.
$link = $this->router->reverse('Test', 'method', array('index' => 12));
$this->test((string)$link)->assertEqual((string)new UrlString('test/url/12'));
// Set base namespace.
$route->setBaseNamespace('\test\controller');
$this->test($route->getController())->assertEqual('\test\controller\Test');
}
protected function test2thRouterRule(){
// Test 2th URL
$this->router->setUrl('/test/two');
$this->testRouteNr2();
// 2th with arguments since we got /? appended.
$this->router->setUrl('/test/two/1/2/3/4');
$route = $this->testRouteNr2();
// Correct number of args ?
$this->test(count($route->getArguments()))->assertEqual(4);
// Reverse this one.
$link = $this->router->reverse('Test', 'index', array(1,2,3,4));
$this->test((string)$link)->assertEqual((string)new UrlString('test/two/1/2/3/4'));
}
protected function test3rdRouterRule(){
// Test 3th URL
$this->router->setUrl('/test/both/varValue');
$this->testRouteNr3();
$this->router->setUrl('/test/both/varValue/2/3/4/5');
$route = $this->testRouteNr3();
$this->test(count($route->getArguments()))->assertEqual(5);
// Reverse this one.
$link = $this->router->reverse('Test', 'both', array('var' => 'something', 0 => 'k'));
$this->test((string)$link)->assertEqual((string)new UrlString('test/both/something/k'));
}
protected function test4rdRouterRule(){
// Test 3th URL
$this->router->setUrl('/testNs');
$route = $this->testRouteNr4();
// Reverse this one.
$link = $this->router->reverse('\gnomephp\mvc\TestRouter', 'index');
$this->test((string)$link)->assertEqual((string)new UrlString('testNs'));
}
/// Helpers.
private function testRouteNr2(){
$route = $this->router->route();
$this->test($route)->assertInstanceOf('\gnomephp\mvc\router\BackendObject');
$this->test($route->getMethod())->assertEqual('index');
return $route;
}
private function testRouteNr3(){
$route = $this->router->route();
$this->test($route)->assertInstanceOf('\gnomephp\mvc\router\BackendObject');
$this->test($route->getMethod())->assertEqual('both');
return $route;
}
private function testRouteNr4(){
$route = $this->router->route();
$this->test($route)->assertInstanceOf('\gnomephp\mvc\router\BackendObject');
$this->test($route->getController())->assertEqual('\gnomephp\mvc\TestRouter');
$this->test($route->getMethod())->assertEqual('index');
return $route;
}
}