From 73ff07ce7dab1ae0755b471c35de8ea36016c8d4 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Thu, 24 May 2012 17:30:17 +0400 Subject: [PATCH 1/9] Add method Router::getUri($route_name = null) - Give me this uri on route_name is null, other another route uri from route_name. --- app/router/Route.php | 5 +++++ app/router/Router.php | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 9 deletions(-) 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..1a3b424 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,17 +45,17 @@ class Router } return false; } - + public function setDefaultLayout($layout = 'Default') { $this->default_layout = $layout; } - + public function getRouteName() { return $this->route_name; } - + /** * @return Route */ @@ -63,4 +63,30 @@ class Router { return $this->route; } + + public function routeIsExists($name) + { + return array_key_exists($name, $this->routes); + } + + public function getRouteByName($name) + { + return $this->routes[$name]; + } + + static public function getUri($route_name = null) + { + $router = FrontController::getInstance()->getRouter(); + if (is_null($route_name)) { + $route = $router->getRoute(); + } else { + if ($router->routeIsExists($route_name)) { + $route = $router->getRouteByName($route_name); + } else { + $btrace = debug_backtrace(); + throw new ErrorException('Unknown route handler: "' . $route_name . '". ' . 'Call from "' . $btrace[0]['file'] . '" on line ' . $btrace[0]['line'] . '.'); + } + } + return $route->getUri(); + } } \ No newline at end of file From 8efaf2a0a77acdd91bc4625739739c0e1b691bd4 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 25 May 2012 14:18:05 +0400 Subject: [PATCH 2/9] Change static to dynamic method getUri. --- app/router/Router.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/router/Router.php b/app/router/Router.php index 1a3b424..1f77dac 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -64,27 +64,26 @@ class Router return $this->route; } - public function routeIsExists($name) + protected function routeIsExists($name) { return array_key_exists($name, $this->routes); } - public function getRouteByName($name) + protected function getRouteByName($name) { return $this->routes[$name]; } - static public function getUri($route_name = null) + public function getUri($route_name = null) { - $router = FrontController::getInstance()->getRouter(); if (is_null($route_name)) { - $route = $router->getRoute(); + $route = $this->getRoute(); } else { - if ($router->routeIsExists($route_name)) { - $route = $router->getRouteByName($route_name); + if ($this->routeIsExists($route_name)) { + $route = $this->getRouteByName($route_name); } else { $btrace = debug_backtrace(); - throw new ErrorException('Unknown route handler: "' . $route_name . '". ' . 'Call from "' . $btrace[0]['file'] . '" on line ' . $btrace[0]['line'] . '.'); + throw new ErrorException('Unknown route name: "' . $route_name . '". ' . 'Call from "' . $btrace[0]['file'] . '" on line ' . $btrace[0]['line'] . '.'); } } return $route->getUri(); From 1cf82d74420b7fc4a643f06191727fb110aee4f3 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 25 May 2012 16:30:45 +0400 Subject: [PATCH 3/9] Add PHP-doc comments in new methods (routeIsExists, getRouteByName, getUri). --- app/router/Router.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/router/Router.php b/app/router/Router.php index 1f77dac..913769a 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -64,16 +64,29 @@ class Router return $this->route; } + /** + * @param $name + * @return bool + */ protected function routeIsExists($name) { return array_key_exists($name, $this->routes); } + /** + * @param $name + * @return Route + */ protected function getRouteByName($name) { return $this->routes[$name]; } + /** + * @param null $route_name + * @return string + * @throws ErrorException + */ public function getUri($route_name = null) { if (is_null($route_name)) { From c7815a21124a2ce09b0e4a898da14ce6ed3927fd Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 25 May 2012 16:31:08 +0400 Subject: [PATCH 4/9] Add tests app/router for new methods. --- tests/app/router/RouteTest.php | 13 +++++++ tests/app/router/RouterTest.php | 81 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) 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..436e2fe 100644 --- a/tests/app/router/RouterTest.php +++ b/tests/app/router/RouterTest.php @@ -61,4 +61,85 @@ class RouterTest extends PHPUnit_Framework_TestCase $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); + } } From 6e4a9ef23ac128969c3821b6bb5079adce93131f Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 25 May 2012 18:41:25 +0400 Subject: [PATCH 5/9] Modify RouterTest (unmocked simply methods). --- tests/app/router/RouterTest.php | 42 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/tests/app/router/RouterTest.php b/tests/app/router/RouterTest.php index 436e2fe..fa5c6e6 100644 --- a/tests/app/router/RouterTest.php +++ b/tests/app/router/RouterTest.php @@ -84,20 +84,16 @@ class RouterTest extends PHPUnit_Framework_TestCase ->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)); + $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() { - $name = 'name of route'; + $name = 'nameofroute'; $uri = 'uri from route.'; $route_mock = $this->getMockBuilder('Route') ->disableOriginalConstructor() @@ -107,21 +103,11 @@ class RouterTest extends PHPUnit_Framework_TestCase ->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)); + $router = new Router(); + $reflection = new ReflectionProperty('Router', 'routes'); + $reflection->setAccessible(true); + $reflection->setValue($router, array($name => $route_mock)); + $this->assertEquals($uri, $router->getUri($name)); } public function testGetUriWithNamedWithError() @@ -129,14 +115,10 @@ class RouterTest extends PHPUnit_Framework_TestCase $name = 'name of route'; $router_mock = $this->getMockBuilder('Router') ->disableOriginalConstructor() - ->setMethods(array('getRoute', 'routeIsExists', 'getRouteByName')) + ->setMethods(array('getRoute', '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'); From 9ef37d003b7f0da8396a3ea9d51e5be59fc6070b Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 25 May 2012 19:03:42 +0400 Subject: [PATCH 6/9] Refactor Router (remove getUri method. All logic moved to getRoute method). Modified RouterTest. --- app/router/Router.php | 37 +++++++++-------------- tests/app/router/RouterTest.php | 66 +++++++++++++++-------------------------- 2 files changed, 38 insertions(+), 65 deletions(-) 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)); } } From 119bba5e23bd8fb95fe65a85132a6b2a46cb1a1f Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 25 May 2012 19:21:05 +0400 Subject: [PATCH 7/9] Modified PHP-doc in Router. --- app/router/Router.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/router/Router.php b/app/router/Router.php index 433f9ad..7cf1889 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -76,7 +76,7 @@ class Router } /** - * @param $name + * @param string $name * @return bool */ public function routeIsExists($name) @@ -85,7 +85,7 @@ class Router } /** - * @param $name + * @param string $name * @return Route */ protected function getRouteByName($name) From f207e804097d2dd4c2b20efb5f88eb9e16a87610 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 25 May 2012 19:22:14 +0400 Subject: [PATCH 8/9] Modified PHP-doc var types in Router. --- app/router/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/router/Router.php b/app/router/Router.php index 7cf1889..0c2d199 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -57,7 +57,7 @@ class Router } /** - * @param null $name + * @param null|string $name * @return Route * @throws ErrorException */ From 444e5ba36996f0c6d33591547c0840ead2f1af55 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 25 May 2012 19:26:21 +0400 Subject: [PATCH 9/9] Remove backtrace function from getRoute method. --- app/router/Router.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/router/Router.php b/app/router/Router.php index 0c2d199..5eca53d 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -69,8 +69,7 @@ class Router 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'] . '.'); + throw new ErrorException('Unknown route name: "' . $name . '".'); } } }