Refactor Router (remove getUri method. All logic moved to getRoute method). Modified RouterTest.

This commit is contained in:
Alexander Demidov
2012-05-25 19:03:42 +04:00
parent 6e4a9ef23a
commit 9ef37d003b
2 changed files with 38 additions and 65 deletions

View File

@ -62,66 +62,48 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->assertAttributeEquals('userLayout', 'default_layout', $router);
}
public function testGetRoute()
public function testGetRouteWithNameIsNull()
{
$name = null;
$route = 'route object.';
$router = new Router();
$reflection = new ReflectionProperty('Router', 'route');
$reflection->setAccessible(true);
$reflection->setValue($router, $route);
$this->assertEquals($route, $router->getRoute());
$this->assertEquals($route, $router->getRoute($name));
}
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 = new Router();
$reflection = new ReflectionProperty('Router', 'route');
$reflection->setAccessible(true);
$reflection->setValue($router, $route_mock);
$this->assertEquals($uri, $router->getUri($name));
}
public function testGetUriWithNamed()
public function testGetRouteWithNamed()
{
$name = 'nameofroute';
$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));
$route = 'route object.';
$router = new Router();
$reflection = new ReflectionProperty('Router', 'routes');
$reflection->setAccessible(true);
$reflection->setValue($router, array($name => $route_mock));
$this->assertEquals($uri, $router->getUri($name));
$reflection->setValue($router, array($name => $route));
$this->assertEquals($route, $router->getRoute($name));
}
public function testGetUriWithNamedWithError()
public function testGetRouteWithNamedWithError()
{
$name = 'name of route';
$router_mock = $this->getMockBuilder('Router')
->disableOriginalConstructor()
->setMethods(array('getRoute', 'getRouteByName'))
->getMock();
$router_mock->expects($this->never())
->method('getRoute');
$router_mock->expects($this->never())
->method('getRouteByName');
$router = new Router();
$this->setExpectedException('ErrorException');
$router_mock->getUri($name);
$router->getRoute($name);
}
public function testRouteIsExists()
{
$route = 'route object.';
$name = 'nameofroute';
$name_is_not_exists = 'nameofroutenotexists';
$routes = array($name => $route);
$router = new Router();
$reflection = new ReflectionProperty('Router', 'routes');
$reflection->setAccessible(true);
$reflection->setValue($router, $routes);
$this->assertTrue($router->routeIsExists($name));
$this->assertFalse($router->routeIsExists($name_is_not_exists));
}
}