add requirements to routing

This commit is contained in:
Bernhard Posselt 2014-04-09 21:57:32 +02:00
parent 5b8c7a01e9
commit 70c88027db
2 changed files with 31 additions and 5 deletions

View File

@ -84,7 +84,15 @@ class RouteConfig {
// register the route
$handler = new RouteActionHandler($this->container, $controllerName, $actionName);
$this->router->create($this->appName.'.'.$controller.'.'.$action, $url)->method($verb)->action($handler);
$router = $this->router->create($this->appName.'.'.$controller.'.'.$action, $url)
->method($verb)
->action($handler);
// optionally register requirements for route. This is used to
// tell the route parser how url parameters should be matched
if(array_key_exists('requirements', $simpleRoute)) {
$router->requirements($simpleRoute['requirements']);
}
}
}

View File

@ -36,6 +36,16 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open');
}
public function testSimpleRouteWithRequirements()
{
$routes = array('routes' => array(
array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => array('something'))
));
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array('something'));
}
/**
* @expectedException \UnexpectedValueException
*/
@ -85,10 +95,10 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
* @param string $controllerName
* @param string $actionName
*/
private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName)
private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements=array())
{
// route mocks
$route = $this->mockRoute($verb, $controllerName, $actionName);
$route = $this->mockRoute($verb, $controllerName, $actionName, $requirements);
// router mock
$router = $this->getMock("\OC\Route\Router", array('create'));
@ -171,10 +181,10 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
* @param string $actionName
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function mockRoute($verb, $controllerName, $actionName)
private function mockRoute($verb, $controllerName, $actionName, array $requirements=array())
{
$container = new DIContainer('app1');
$route = $this->getMock("\OC\Route\Route", array('method', 'action'), array(), '', false);
$route = $this->getMock("\OC\Route\Route", array('method', 'action', 'requirements'), array(), '', false);
$route
->expects($this->exactly(1))
->method('method')
@ -186,6 +196,14 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
->method('action')
->with($this->equalTo(new RouteActionHandler($container, $controllerName, $actionName)))
->will($this->returnValue($route));
if(count($requirements) > 0) {
$route
->expects($this->exactly(1))
->method('requirements')
->with($this->equalTo($requirements))
->will($this->returnValue($route));
}
return $route;
}