diff --git a/app/router/Route.php b/app/router/Route.php index 5f60df4..7f42f31 100644 --- a/app/router/Route.php +++ b/app/router/Route.php @@ -54,6 +54,11 @@ class Route return true; } + + public function getUri() + { + return '/' . $this->route; + } public function getAction() { diff --git a/app/router/Router.php b/app/router/Router.php index eb44424..5eca53d 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -13,16 +13,16 @@ class Router { protected $routes = array(); - + protected $route_name; - + protected $default_layout = 'Default'; - + /** * @var Route */ protected $route; - + public function add($name, $route, $action, $params = array(), $layout = null) { if (!$layout) { @@ -30,11 +30,11 @@ class Router } $this->routes[$name] = new Route($route, $action, $params, $layout); } - + public function route($request) { $req = explode('/', trim($request, '/')); - + foreach ($this->routes as $name => $route) { if ($route->match($req)) { $this->route_name = $name; @@ -45,22 +45,50 @@ class Router } return false; } - + public function setDefaultLayout($layout = 'Default') { $this->default_layout = $layout; } - + public function getRouteName() { return $this->route_name; } - + + /** + * @param null|string $name + * @return Route + * @throws ErrorException + */ + public function getRoute($name = null) + { + if (is_null($name)) { + return $this->route; + } else { + if ($this->routeIsExists($name)) { + return $this->getRouteByName($name); + } else { + throw new ErrorException('Unknown route name: "' . $name . '".'); + } + } + } + + /** + * @param string $name + * @return bool + */ + public function routeIsExists($name) + { + return array_key_exists($name, $this->routes); + } + /** + * @param string $name * @return Route */ - public function getRoute() + protected function getRouteByName($name) { - return $this->route; + return $this->routes[$name]; } } \ No newline at end of file diff --git a/tests/app/router/RouteTest.php b/tests/app/router/RouteTest.php index 9aaea7c..778a43c 100644 --- a/tests/app/router/RouteTest.php +++ b/tests/app/router/RouteTest.php @@ -77,4 +77,17 @@ class RouteTest extends PHPUnit_Framework_TestCase $this->setExpectedException('PHPUnit_Framework_Error'); $route->match(''); } + + public function testGetUri() + { + $route = 'myroute'; + $route_mock = $this->getMockBuilder('Route') + ->disableOriginalConstructor() + ->setMethods(array('__construct')) + ->getMock(); + $reflection = new ReflectionProperty('Route', 'route'); + $reflection->setAccessible(true); + $reflection->setValue($route_mock, $route); + $this->assertEquals('/' . $route, $route_mock->getUri()); + } } \ No newline at end of file diff --git a/tests/app/router/RouterTest.php b/tests/app/router/RouterTest.php index 2584961..d2c831f 100644 --- a/tests/app/router/RouterTest.php +++ b/tests/app/router/RouterTest.php @@ -61,4 +61,49 @@ class RouterTest extends PHPUnit_Framework_TestCase $router->setDefaultLayout('userLayout'); $this->assertAttributeEquals('userLayout', 'default_layout', $router); } + + 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($name)); + } + + public function testGetRouteWithNamed() + { + $name = 'nameofroute'; + $uri = 'uri from route.'; + $route = 'route object.'; + $router = new Router(); + $reflection = new ReflectionProperty('Router', 'routes'); + $reflection->setAccessible(true); + $reflection->setValue($router, array($name => $route)); + $this->assertEquals($route, $router->getRoute($name)); + } + + public function testGetRouteWithNamedWithError() + { + $name = 'name of route'; + $router = new Router(); + $this->setExpectedException('ErrorException'); + $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)); + } }