Browse Source

Merge branch 'route_modified'

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

5
app/router/Route.php

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

32
app/router/Router.php

@ -57,10 +57,38 @@ class Router
} }
/** /**
* @param null|string $name
* @return Route * @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 {
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
*/
protected function getRouteByName($name)
{
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'); $this->setExpectedException('PHPUnit_Framework_Error');
$route->match(''); $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'); $router->setDefaultLayout('userLayout');
$this->assertAttributeEquals('userLayout', 'default_layout', $router); $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