Browse Source

Merge branch 'route_modified'

master
Alexander Demidov 13 years ago
parent
commit
29a255776d
  1. 5
      app/router/Route.php
  2. 50
      app/router/Router.php
  3. 13
      tests/app/router/RouteTest.php
  4. 45
      tests/app/router/RouterTest.php

5
app/router/Route.php

@ -54,6 +54,11 @@ class Route
return true;
}
public function getUri()
{
return '/' . $this->route;
}
public function getAction()
{

50
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];
}
}

13
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());
}
}

45
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));
}
}
Loading…
Cancel
Save