RoutingTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace OC\AppFramework\Routing;
  3. use OC\AppFramework\DependencyInjection\DIContainer;
  4. use OC\AppFramework\routing\RouteConfig;
  5. class RouteConfigTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testSimpleRoute()
  8. {
  9. $routes = array('routes' => array(
  10. array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET')
  11. ));
  12. $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
  13. }
  14. public function testSimpleRouteWithMissingVerb()
  15. {
  16. $routes = array('routes' => array(
  17. array('name' => 'folders#open', 'url' => '/folders/{folderId}/open')
  18. ));
  19. $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
  20. }
  21. public function testSimpleRouteWithLowercaseVerb()
  22. {
  23. $routes = array('routes' => array(
  24. array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
  25. ));
  26. $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open');
  27. }
  28. /**
  29. * @expectedException \UnexpectedValueException
  30. */
  31. public function testSimpleRouteWithBrokenName()
  32. {
  33. $routes = array('routes' => array(
  34. array('name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
  35. ));
  36. // router mock
  37. $router = $this->getMock("\OC_Router", array('create'));
  38. // load route configuration
  39. $container = new DIContainer('app1');
  40. $config = new RouteConfig($container, $router, $routes);
  41. $config->register();
  42. }
  43. public function testSimpleRouteWithUnderScoreNames()
  44. {
  45. $routes = array('routes' => array(
  46. array('name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
  47. ));
  48. $this->assertSimpleRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
  49. }
  50. public function testResource()
  51. {
  52. $routes = array('resources' => array('accounts' => array('url' => '/accounts')));
  53. $this->assertResource($routes, 'accounts', '/accounts', 'AccountsController', 'accountId');
  54. }
  55. public function testResourceWithUnderScoreName()
  56. {
  57. $routes = array('resources' => array('admin_accounts' => array('url' => '/admin/accounts')));
  58. $this->assertResource($routes, 'admin_accounts', '/admin/accounts', 'AdminAccountsController', 'adminAccountId');
  59. }
  60. private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName)
  61. {
  62. // route mocks
  63. $route = $this->mockRoute($verb, $controllerName, $actionName);
  64. // router mock
  65. $router = $this->getMock("\OC_Router", array('create'));
  66. // we expect create to be called once:
  67. $router
  68. ->expects($this->once())
  69. ->method('create')
  70. ->with($this->equalTo('app1.' . $name), $this->equalTo($url))
  71. ->will($this->returnValue($route));
  72. // load route configuration
  73. $container = new DIContainer('app1');
  74. $config = new RouteConfig($container, $router, $routes);
  75. $config->register();
  76. }
  77. private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName)
  78. {
  79. // router mock
  80. $router = $this->getMock("\OC_Router", array('create'));
  81. // route mocks
  82. $indexRoute = $this->mockRoute('GET', $controllerName, 'index');
  83. $showRoute = $this->mockRoute('GET', $controllerName, 'show');
  84. $createRoute = $this->mockRoute('POST', $controllerName, 'create');
  85. $updateRoute = $this->mockRoute('PUT', $controllerName, 'update');
  86. $destroyRoute = $this->mockRoute('DELETE', $controllerName, 'destroy');
  87. $urlWithParam = $url . '/{' . $paramName . '}';
  88. // we expect create to be called once:
  89. $router
  90. ->expects($this->at(0))
  91. ->method('create')
  92. ->with($this->equalTo('app1.' . $resourceName . '.index'), $this->equalTo($url))
  93. ->will($this->returnValue($indexRoute));
  94. $router
  95. ->expects($this->at(1))
  96. ->method('create')
  97. ->with($this->equalTo('app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam))
  98. ->will($this->returnValue($showRoute));
  99. $router
  100. ->expects($this->at(2))
  101. ->method('create')
  102. ->with($this->equalTo('app1.' . $resourceName . '.create'), $this->equalTo($url))
  103. ->will($this->returnValue($createRoute));
  104. $router
  105. ->expects($this->at(3))
  106. ->method('create')
  107. ->with($this->equalTo('app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam))
  108. ->will($this->returnValue($updateRoute));
  109. $router
  110. ->expects($this->at(4))
  111. ->method('create')
  112. ->with($this->equalTo('app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam))
  113. ->will($this->returnValue($destroyRoute));
  114. // load route configuration
  115. $container = new DIContainer('app1');
  116. $config = new RouteConfig($container, $router, $yaml);
  117. $config->register();
  118. }
  119. /**
  120. * @param $verb
  121. * @param $controllerName
  122. * @param $actionName
  123. * @return \PHPUnit_Framework_MockObject_MockObject
  124. */
  125. private function mockRoute($verb, $controllerName, $actionName)
  126. {
  127. $container = new DIContainer('app1');
  128. $route = $this->getMock("\OC_Route", array('method', 'action'), array(), '', false);
  129. $route
  130. ->expects($this->exactly(1))
  131. ->method('method')
  132. ->with($this->equalTo($verb))
  133. ->will($this->returnValue($route));
  134. $route
  135. ->expects($this->exactly(1))
  136. ->method('action')
  137. ->with($this->equalTo(new RouteActionHandler($container, $controllerName, $actionName)))
  138. ->will($this->returnValue($route));
  139. return $route;
  140. }
  141. }
  142. /*
  143. #
  144. # sample routes.yaml for ownCloud
  145. #
  146. # the section simple describes one route
  147. routes:
  148. - name: folders#open
  149. url: /folders/{folderId}/open
  150. verb: GET
  151. # controller: name.split()[0]
  152. # action: name.split()[1]
  153. # for a resource following actions will be generated:
  154. # - index
  155. # - create
  156. # - show
  157. # - update
  158. # - destroy
  159. # - new
  160. resources:
  161. accounts:
  162. url: /accounts
  163. folders:
  164. url: /accounts/{accountId}/folders
  165. # actions can be used to define additional actions on the resource
  166. actions:
  167. - name: validate
  168. verb: GET
  169. on-collection: false
  170. * */