diff --git a/app/router/Router.php b/app/router/Router.php index 913769a..433f9ad 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -57,18 +57,29 @@ class Router } /** + * @param null $name * @return Route + * @throws ErrorException */ - public function getRoute() + public function getRoute($name = null) { - return $this->route; + if (is_null($name)) { + return $this->route; + } else { + if ($this->routeIsExists($name)) { + return $this->getRouteByName($name); + } else { + $btrace = debug_backtrace(); + throw new ErrorException('Unknown route name: "' . $name . '". ' . 'Call from "' . $btrace[0]['file'] . '" on line ' . $btrace[0]['line'] . '.'); + } + } } /** * @param $name * @return bool */ - protected function routeIsExists($name) + public function routeIsExists($name) { return array_key_exists($name, $this->routes); } @@ -81,24 +92,4 @@ class Router { return $this->routes[$name]; } - - /** - * @param null $route_name - * @return string - * @throws ErrorException - */ - public function getUri($route_name = null) - { - if (is_null($route_name)) { - $route = $this->getRoute(); - } else { - if ($this->routeIsExists($route_name)) { - $route = $this->getRouteByName($route_name); - } else { - $btrace = debug_backtrace(); - throw new ErrorException('Unknown route name: "' . $route_name . '". ' . 'Call from "' . $btrace[0]['file'] . '" on line ' . $btrace[0]['line'] . '.'); - } - } - return $route->getUri(); - } } \ No newline at end of file diff --git a/tests/app/router/RouterTest.php b/tests/app/router/RouterTest.php index fa5c6e6..d2c831f 100644 --- a/tests/app/router/RouterTest.php +++ b/tests/app/router/RouterTest.php @@ -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()); - } - - 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)); + $this->assertEquals($route, $router->getRoute($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)); } }