]> source.dussan.org Git - nextcloud-server.git/commitdiff
Allow default values for route parameters.
authorPatrick Paysant <ppaysant@linagora.com>
Sun, 7 Sep 2014 10:20:48 +0000 (12:20 +0200)
committerPatrick Paysant <ppaysant@linagora.com>
Tue, 9 Sep 2014 21:00:29 +0000 (23:00 +0200)
lib/private/appframework/routing/routeconfig.php
tests/lib/appframework/routing/RoutingTest.php

index 5b4d411a3553243bb36064c0181bc6ed58874cb5..91687b9c83c8cd0c8bc60c6e82714949b87d7cc1 100644 (file)
@@ -93,6 +93,12 @@ class RouteConfig {
                        if(array_key_exists('requirements', $simpleRoute)) {
                                $router->requirements($simpleRoute['requirements']);
                        }
+
+                       // optionally register defaults for route. This is used to
+                       // tell the route parser how url parameters should be default valued
+                       if(array_key_exists('defaults', $simpleRoute)) {
+                               $router->defaults($simpleRoute['defaults']);
+                       }
                }
        }
 
index 7cd07db6ce1ef0a03badb798a2b9c529a3a1d913..a1d9a51a3c8b2e1fd3d94c5367856171aa5fc5f6 100644 (file)
@@ -45,6 +45,15 @@ class RoutingTest extends \PHPUnit_Framework_TestCase
                $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array('something'));
        }
 
+       public function testSimpleRouteWithDefaults()
+       {
+               $routes = array('routes' => array(
+                       array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', array(), 'defaults' => array('param' => 'foobar'))
+               ));
+
+               $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array(), array('param' => 'foobar'));
+       }
+
 
        /**
         * @expectedException \UnexpectedValueException
@@ -95,10 +104,10 @@ class RoutingTest extends \PHPUnit_Framework_TestCase
         * @param string $controllerName
         * @param string $actionName
         */
-       private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements=array())
+       private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements=array(), array $defaults=array())
        {
                // route mocks
-               $route = $this->mockRoute($verb, $controllerName, $actionName, $requirements);
+               $route = $this->mockRoute($verb, $controllerName, $actionName, $requirements, $defaults);
 
                // router mock
                $router = $this->getMock("\OC\Route\Router", array('create'));
@@ -181,10 +190,10 @@ class RoutingTest extends \PHPUnit_Framework_TestCase
         * @param string $actionName
         * @return \PHPUnit_Framework_MockObject_MockObject
         */
-       private function mockRoute($verb, $controllerName, $actionName, array $requirements=array())
+       private function mockRoute($verb, $controllerName, $actionName, array $requirements=array(), array $defaults=array())
        {
                $container = new DIContainer('app1');
-               $route = $this->getMock("\OC\Route\Route", array('method', 'action', 'requirements'), array(), '', false);
+               $route = $this->getMock("\OC\Route\Route", array('method', 'action', 'requirements', 'defaults'), array(), '', false);
                $route
                        ->expects($this->exactly(1))
                        ->method('method')
@@ -204,6 +213,15 @@ class RoutingTest extends \PHPUnit_Framework_TestCase
                                ->with($this->equalTo($requirements))
                                ->will($this->returnValue($route));
                }
+
+               if (count($defaults) > 0) {
+                       $route
+                               ->expects($this->exactly(1))
+                               ->method('defaults')
+                               ->with($this->equalTo($defaults))
+                               ->will($this->returnValue($route));
+               }
+
                return $route;
        }