]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add route tests
authorRoeland Jago Douma <roeland@famdouma.nl>
Thu, 14 Jul 2016 18:45:19 +0000 (20:45 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Mon, 18 Jul 2016 09:09:49 +0000 (11:09 +0200)
lib/private/AppFramework/Routing/RouteConfig.php
tests/lib/AppFramework/Routing/RoutingTest.php

index d4b4ec038ad9c8a2f4a687e313a7bf5841608a83..eca0051691fc7817ffe18f1344696b90d9078c03 100644 (file)
@@ -79,7 +79,7 @@ class RouteConfig {
                $ocsRoutes = isset($routes['ocs']) ? $routes['ocs'] : [];
                foreach ($ocsRoutes as $ocsRoute) {
                        $name = $ocsRoute['name'];
-                       $postFix = '';
+                       $postfix = '';
 
                        if (isset($ocsRoute['postfix'])) {
                                $postfix = $ocsRoute['postfix'];
index 52a5eb33ba9e661999926d55b4913e5f7f6ec516..6c8b0f40133b3f20aa9a0c7953a344bc08617e0c 100644 (file)
@@ -18,6 +18,15 @@ class RoutingTest extends \Test\TestCase
                $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
        }
 
+       public function testSimpleOCSRoute() {
+               $routes = ['ocs' => [
+                               ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
+                       ]
+               ];
+
+               $this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
+       }
+
        public function testSimpleRouteWithMissingVerb()
        {
                $routes = array('routes' => array(
@@ -27,6 +36,15 @@ class RoutingTest extends \Test\TestCase
                $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
        }
 
+       public function testSimpleOCSRouteWithMissingVerb() {
+               $routes = ['ocs' => [
+                               ['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
+                       ]
+               ];
+
+               $this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
+       }
+
        public function testSimpleRouteWithLowercaseVerb()
        {
                $routes = array('routes' => array(
@@ -36,6 +54,15 @@ class RoutingTest extends \Test\TestCase
                $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open');
        }
 
+       public function testSimpleOCSRouteWithLowercaseVerb() {
+               $routes = ['ocs' => [
+                               ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
+                       ]
+               ];
+
+               $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open');
+       }
+
        public function testSimpleRouteWithRequirements()
        {
                $routes = array('routes' => array(
@@ -45,6 +72,15 @@ class RoutingTest extends \Test\TestCase
                $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array('something'));
        }
 
+       public function testSimpleOCSRouteWithRequirements() {
+               $routes = ['ocs' => [
+                               ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
+                       ]
+               ];
+
+               $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
+       }
+
        public function testSimpleRouteWithDefaults()
        {
                $routes = array('routes' => array(
@@ -54,6 +90,16 @@ class RoutingTest extends \Test\TestCase
                $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array(), array('param' => 'foobar'));
        }
 
+
+       public function testSimpleOCSRouteWithDefaults() {
+               $routes = ['ocs' => [
+                               ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'defaults' => ['param' => 'foobar']]
+                       ]
+               ];
+
+               $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
+       }
+
        public function testSimpleRouteWithPostfix()
        {
                $routes = array('routes' => array(
@@ -63,6 +109,14 @@ class RoutingTest extends \Test\TestCase
                $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array(), array(), '_something');
        }
 
+       public function testSimpleOCSRouteWithPostfix() {
+               $routes = ['ocs' => [
+                               ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
+                       ]
+               ];
+
+               $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
+       }
 
        /**
         * @expectedException \UnexpectedValueException
@@ -86,6 +140,27 @@ class RoutingTest extends \Test\TestCase
                $config->register();
        }
 
+       /**
+        * @expectedException \UnexpectedValueException
+        */
+       public function testSimpleOCSRouteWithBrokenName() {
+               $routes = ['ocs' => [
+                       ['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
+               ]];
+
+               // router mock
+               $router = $this->getMockBuilder('\OC\Route\Router')
+                       ->setMethods(['create'])
+                       ->setConstructorArgs([$this->getMockBuilder('\OCP\ILogger')->getMock()])
+                       ->getMock();
+
+               // load route configuration
+               $container = new DIContainer('app1');
+               $config = new RouteConfig($container, $router, $routes);
+
+               $config->register();
+       }
+
        public function testSimpleRouteWithUnderScoreNames()
        {
                $routes = array('routes' => array(
@@ -95,6 +170,14 @@ class RoutingTest extends \Test\TestCase
                $this->assertSimpleRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
        }
 
+       public function testSimpleOCSRouteWithUnderScoreNames() {
+               $routes = ['ocs' => [
+                       ['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
+               ]];
+
+               $this->assertSimpleOCSRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
+       }
+
        public function testResource()
        {
                $routes = array('resources' => array('account' => array('url' => '/accounts')));
@@ -145,6 +228,54 @@ class RoutingTest extends \Test\TestCase
                $config->register();
        }
 
+       /**
+        * @param $routes
+        * @param string $name
+        * @param string $verb
+        * @param string $url
+        * @param string $controllerName
+        * @param string $actionName
+        * @param array $requirements
+        * @param array $defaults
+        * @param string $postfix
+        */
+       private function assertSimpleOCSRoute($routes,
+                                                                                 $name,
+                                                                                 $verb,
+                                                                                 $url,
+                                                                                 $controllerName,
+                                                                                 $actionName,
+                                                                                 array $requirements=array(),
+                                                                                 array $defaults=array(),
+                                                                                 $postfix='')
+       {
+               if ($postfix) {
+                       $name .= $postfix;
+               }
+
+               // route mocks
+               $container = new DIContainer('app1');
+               $route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
+
+               // router mock
+               $router = $this->getMockBuilder('\OC\Route\Router')
+                       ->setMethods(['create'])
+                       ->setConstructorArgs([$this->getMockBuilder('\OCP\ILogger')->getMock()])
+                       ->getMock();
+
+               // we expect create to be called once:
+               $router
+                       ->expects($this->once())
+                       ->method('create')
+                       ->with($this->equalTo('ocs.app1.' . $name), $this->equalTo($url))
+                       ->will($this->returnValue($route));
+
+               // load route configuration
+               $config = new RouteConfig($container, $router, $routes);
+
+               $config->register();
+       }
+
        /**
         * @param string $resourceName
         * @param string $url