Merge pull request #8137 from owncloud/routeing-requirements

add requirements to routing
This commit is contained in:
Thomas Müller 2014-04-10 16:23:01 +02:00
commit 26a89e369e
2 changed files with 31 additions and 5 deletions

View File

@ -84,7 +84,15 @@ class RouteConfig {
// register the route // register the route
$handler = new RouteActionHandler($this->container, $controllerName, $actionName); $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'); $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 * @expectedException \UnexpectedValueException
*/ */
@ -85,10 +95,10 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
* @param string $controllerName * @param string $controllerName
* @param string $actionName * @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 mocks
$route = $this->mockRoute($verb, $controllerName, $actionName); $route = $this->mockRoute($verb, $controllerName, $actionName, $requirements);
// router mock // router mock
$router = $this->getMock("\OC\Route\Router", array('create')); $router = $this->getMock("\OC\Route\Router", array('create'));
@ -171,10 +181,10 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
* @param string $actionName * @param string $actionName
* @return \PHPUnit_Framework_MockObject_MockObject * @return \PHPUnit_Framework_MockObject_MockObject
*/ */
private function mockRoute($verb, $controllerName, $actionName) private function mockRoute($verb, $controllerName, $actionName, array $requirements=array())
{ {
$container = new DIContainer('app1'); $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 $route
->expects($this->exactly(1)) ->expects($this->exactly(1))
->method('method') ->method('method')
@ -186,6 +196,14 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
->method('action') ->method('action')
->with($this->equalTo(new RouteActionHandler($container, $controllerName, $actionName))) ->with($this->equalTo(new RouteActionHandler($container, $controllerName, $actionName)))
->will($this->returnValue($route)); ->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; return $route;
} }