You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.9 KiB
145 lines
4.9 KiB
<?php
|
|
|
|
/*
|
|
* @copyright NetMonsters <team@netmonsters.ru>
|
|
* @link http://netmonsters.ru
|
|
* @package Majestic
|
|
* @subpackage UnitTests
|
|
* @since 2011-10-31
|
|
*
|
|
* Unit tests for Router class
|
|
*/
|
|
|
|
require_once dirname(__FILE__) . '/../../../classes/Env.class.php';
|
|
require_once dirname(__FILE__) . '/../../../app/router/Route.php';
|
|
require_once dirname(__FILE__) . '/../../../app/router/Router.php';
|
|
|
|
class RouterTest extends PHPUnit_Framework_TestCase
|
|
{
|
|
public function testGetRouteFirst()
|
|
{
|
|
$router = new Router();
|
|
$this->assertNull($router->getRoute());
|
|
$this->assertNull($router->getRouteName());
|
|
}
|
|
|
|
public function testRouterCycle()
|
|
{
|
|
$router = new Router();
|
|
$router->add('user', 'user/account/:id', 'user');
|
|
$route = $router->route('user/account/213');
|
|
$this->assertSame(1, count($route->getParams()));
|
|
$this->assertSame(array('id' => '213'), $route->getParams());
|
|
$this->assertSame('user', $router->getRouteName());
|
|
}
|
|
|
|
public function testRouterMultipleRoutes()
|
|
{
|
|
$router = new Router();
|
|
$router->add('user', 'user/account/:id', 'user');
|
|
$router->add('sale', 'sale/order/:id', 'user');
|
|
$route = $router->route('user/account/213');
|
|
$this->assertSame('user', $router->getRouteName());
|
|
$this->assertSame(1, count($route->getParams()));
|
|
$this->assertSame(array('id' => '213'), $route->getParams());
|
|
$route = $router->route('sale/order/22');
|
|
$this->assertSame('sale', $router->getRouteName());
|
|
$this->assertSame(array('id' => '22'), $route->getParams());
|
|
}
|
|
|
|
public function testRouteNotmatch()
|
|
{
|
|
$router = new Router();
|
|
$router->add('user', 'user/account/:id', 'user');
|
|
$this->assertFalse($router->route('user/info/213'));
|
|
|
|
}
|
|
|
|
public function testSetDefaultLayout()
|
|
{
|
|
$router = new Router();
|
|
$router->setDefaultLayout('userLayout');
|
|
$this->assertAttributeEquals('userLayout', 'default_layout', $router);
|
|
}
|
|
|
|
public function testGetRoute()
|
|
{
|
|
$route = 'route object.';
|
|
$router = new Router();
|
|
$reflection = new ReflectionProperty('Router', 'route');
|
|
$reflection->setAccessible(true);
|
|
$reflection->setValue($router, $route);
|
|
$this->assertEquals($route, $router->getRoute());
|
|
}
|
|
|
|
public function testGetUriWithNameIsNull()
|
|
{
|
|
$name = null;
|
|
$uri = 'uri from route.';
|
|
$route_mock = $this->getMockBuilder('Route')
|
|
->disableOriginalConstructor()
|
|
->setMethods(array('getUri'))
|
|
->getMock();
|
|
$route_mock->expects($this->once())
|
|
->method('getUri')
|
|
->with()
|
|
->will($this->returnValue($uri));
|
|
$router_mock = $this->getMockBuilder('Router')
|
|
->disableOriginalConstructor()
|
|
->setMethods(array('getRoute'))
|
|
->getMock();
|
|
$router_mock->expects($this->once())
|
|
->method('getRoute')
|
|
->with()
|
|
->will($this->returnValue($route_mock));
|
|
$this->assertEquals($uri, $router_mock->getUri($name));
|
|
}
|
|
|
|
public function testGetUriWithNamed()
|
|
{
|
|
$name = 'name of route';
|
|
$uri = 'uri from route.';
|
|
$route_mock = $this->getMockBuilder('Route')
|
|
->disableOriginalConstructor()
|
|
->setMethods(array('getUri'))
|
|
->getMock();
|
|
$route_mock->expects($this->once())
|
|
->method('getUri')
|
|
->with()
|
|
->will($this->returnValue($uri));
|
|
$router_mock = $this->getMockBuilder('Router')
|
|
->disableOriginalConstructor()
|
|
->setMethods(array('getRoute', 'routeIsExists', 'getRouteByName'))
|
|
->getMock();
|
|
$router_mock->expects($this->never())
|
|
->method('getRoute');
|
|
$router_mock->expects($this->at(0))
|
|
->method('routeIsExists')
|
|
->with($this->equalTo($name))
|
|
->will($this->returnValue(true));
|
|
$router_mock->expects($this->at(1))
|
|
->method('getRouteByName')
|
|
->with($this->equalTo($name))
|
|
->will($this->returnValue($route_mock));
|
|
$this->assertEquals($uri, $router_mock->getUri($name));
|
|
}
|
|
|
|
public function testGetUriWithNamedWithError()
|
|
{
|
|
$name = 'name of route';
|
|
$router_mock = $this->getMockBuilder('Router')
|
|
->disableOriginalConstructor()
|
|
->setMethods(array('getRoute', 'routeIsExists', 'getRouteByName'))
|
|
->getMock();
|
|
$router_mock->expects($this->never())
|
|
->method('getRoute');
|
|
$router_mock->expects($this->once())
|
|
->method('routeIsExists')
|
|
->with($this->equalTo($name))
|
|
->will($this->returnValue(false));
|
|
$router_mock->expects($this->never())
|
|
->method('getRouteByName');
|
|
$this->setExpectedException('ErrorException');
|
|
$router_mock->getUri($name);
|
|
}
|
|
}
|