RouteCollectionBuilderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. use Symfony\Component\Routing\RouteCollectionBuilder;
  16. class RouteCollectionBuilderTest extends TestCase
  17. {
  18. public function testImport()
  19. {
  20. $resolvedLoader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  21. $resolver = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderResolverInterface')->getMock();
  22. $resolver->expects($this->once())
  23. ->method('resolve')
  24. ->with('admin_routing.yml', 'yaml')
  25. ->will($this->returnValue($resolvedLoader));
  26. $originalRoute = new Route('/foo/path');
  27. $expectedCollection = new RouteCollection();
  28. $expectedCollection->add('one_test_route', $originalRoute);
  29. $expectedCollection->addResource(new FileResource(__DIR__.'/Fixtures/file_resource.yml'));
  30. $resolvedLoader
  31. ->expects($this->once())
  32. ->method('load')
  33. ->with('admin_routing.yml', 'yaml')
  34. ->will($this->returnValue($expectedCollection));
  35. $loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  36. $loader->expects($this->any())
  37. ->method('getResolver')
  38. ->will($this->returnValue($resolver));
  39. // import the file!
  40. $routes = new RouteCollectionBuilder($loader);
  41. $importedRoutes = $routes->import('admin_routing.yml', '/', 'yaml');
  42. // we should get back a RouteCollectionBuilder
  43. $this->assertInstanceOf('Symfony\Component\Routing\RouteCollectionBuilder', $importedRoutes);
  44. // get the collection back so we can look at it
  45. $addedCollection = $importedRoutes->build();
  46. $route = $addedCollection->get('one_test_route');
  47. $this->assertSame($originalRoute, $route);
  48. // should return file_resource.yml, which is in the original collection
  49. $this->assertCount(1, $addedCollection->getResources());
  50. // make sure the routes were imported into the top-level builder
  51. $this->assertCount(1, $routes->build());
  52. }
  53. /**
  54. * @expectedException \BadMethodCallException
  55. */
  56. public function testImportWithoutLoaderThrowsException()
  57. {
  58. $collectionBuilder = new RouteCollectionBuilder();
  59. $collectionBuilder->import('routing.yml');
  60. }
  61. public function testAdd()
  62. {
  63. $collectionBuilder = new RouteCollectionBuilder();
  64. $addedRoute = $collectionBuilder->add('/checkout', 'AppBundle:Order:checkout');
  65. $addedRoute2 = $collectionBuilder->add('/blogs', 'AppBundle:Blog:list', 'blog_list');
  66. $this->assertInstanceOf('Symfony\Component\Routing\Route', $addedRoute);
  67. $this->assertEquals('AppBundle:Order:checkout', $addedRoute->getDefault('_controller'));
  68. $finalCollection = $collectionBuilder->build();
  69. $this->assertSame($addedRoute2, $finalCollection->get('blog_list'));
  70. }
  71. public function testFlushOrdering()
  72. {
  73. $importedCollection = new RouteCollection();
  74. $importedCollection->add('imported_route1', new Route('/imported/foo1'));
  75. $importedCollection->add('imported_route2', new Route('/imported/foo2'));
  76. $loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  77. // make this loader able to do the import - keeps mocking simple
  78. $loader->expects($this->any())
  79. ->method('supports')
  80. ->will($this->returnValue(true));
  81. $loader
  82. ->expects($this->once())
  83. ->method('load')
  84. ->will($this->returnValue($importedCollection));
  85. $routes = new RouteCollectionBuilder($loader);
  86. // 1) Add a route
  87. $routes->add('/checkout', 'AppBundle:Order:checkout', 'checkout_route');
  88. // 2) Import from a file
  89. $routes->mount('/', $routes->import('admin_routing.yml'));
  90. // 3) Add another route
  91. $routes->add('/', 'AppBundle:Default:homepage', 'homepage');
  92. // 4) Add another route
  93. $routes->add('/admin', 'AppBundle:Admin:dashboard', 'admin_dashboard');
  94. // set a default value
  95. $routes->setDefault('_locale', 'fr');
  96. $actualCollection = $routes->build();
  97. $this->assertCount(5, $actualCollection);
  98. $actualRouteNames = array_keys($actualCollection->all());
  99. $this->assertEquals(array(
  100. 'checkout_route',
  101. 'imported_route1',
  102. 'imported_route2',
  103. 'homepage',
  104. 'admin_dashboard',
  105. ), $actualRouteNames);
  106. // make sure the defaults were set
  107. $checkoutRoute = $actualCollection->get('checkout_route');
  108. $defaults = $checkoutRoute->getDefaults();
  109. $this->assertArrayHasKey('_locale', $defaults);
  110. $this->assertEquals('fr', $defaults['_locale']);
  111. }
  112. public function testFlushSetsRouteNames()
  113. {
  114. $collectionBuilder = new RouteCollectionBuilder();
  115. // add a "named" route
  116. $collectionBuilder->add('/admin', 'AppBundle:Admin:dashboard', 'admin_dashboard');
  117. // add an unnamed route
  118. $collectionBuilder->add('/blogs', 'AppBundle:Blog:list')
  119. ->setMethods(array('GET'));
  120. // integer route names are allowed - they don't confuse things
  121. $collectionBuilder->add('/products', 'AppBundle:Product:list', 100);
  122. $actualCollection = $collectionBuilder->build();
  123. $actualRouteNames = array_keys($actualCollection->all());
  124. $this->assertEquals(array(
  125. 'admin_dashboard',
  126. 'GET_blogs',
  127. '100',
  128. ), $actualRouteNames);
  129. }
  130. public function testFlushSetsDetailsOnChildrenRoutes()
  131. {
  132. $routes = new RouteCollectionBuilder();
  133. $routes->add('/blogs/{page}', 'listAction', 'blog_list')
  134. // unique things for the route
  135. ->setDefault('page', 1)
  136. ->setRequirement('id', '\d+')
  137. ->setOption('expose', true)
  138. // things that the collection will try to override (but won't)
  139. ->setDefault('_format', 'html')
  140. ->setRequirement('_format', 'json|xml')
  141. ->setOption('fooBar', true)
  142. ->setHost('example.com')
  143. ->setCondition('request.isSecure()')
  144. ->setSchemes(array('https'))
  145. ->setMethods(array('POST'));
  146. // a simple route, nothing added to it
  147. $routes->add('/blogs/{id}', 'editAction', 'blog_edit');
  148. // configure the collection itself
  149. $routes
  150. // things that will not override the child route
  151. ->setDefault('_format', 'json')
  152. ->setRequirement('_format', 'xml')
  153. ->setOption('fooBar', false)
  154. ->setHost('symfony.com')
  155. ->setCondition('request.query.get("page")==1')
  156. // some unique things that should be set on the child
  157. ->setDefault('_locale', 'fr')
  158. ->setRequirement('_locale', 'fr|en')
  159. ->setOption('niceRoute', true)
  160. ->setSchemes(array('http'))
  161. ->setMethods(array('GET', 'POST'));
  162. $collection = $routes->build();
  163. $actualListRoute = $collection->get('blog_list');
  164. $this->assertEquals(1, $actualListRoute->getDefault('page'));
  165. $this->assertEquals('\d+', $actualListRoute->getRequirement('id'));
  166. $this->assertTrue($actualListRoute->getOption('expose'));
  167. // none of these should be overridden
  168. $this->assertEquals('html', $actualListRoute->getDefault('_format'));
  169. $this->assertEquals('json|xml', $actualListRoute->getRequirement('_format'));
  170. $this->assertTrue($actualListRoute->getOption('fooBar'));
  171. $this->assertEquals('example.com', $actualListRoute->getHost());
  172. $this->assertEquals('request.isSecure()', $actualListRoute->getCondition());
  173. $this->assertEquals(array('https'), $actualListRoute->getSchemes());
  174. $this->assertEquals(array('POST'), $actualListRoute->getMethods());
  175. // inherited from the main collection
  176. $this->assertEquals('fr', $actualListRoute->getDefault('_locale'));
  177. $this->assertEquals('fr|en', $actualListRoute->getRequirement('_locale'));
  178. $this->assertTrue($actualListRoute->getOption('niceRoute'));
  179. $actualEditRoute = $collection->get('blog_edit');
  180. // inherited from the collection
  181. $this->assertEquals('symfony.com', $actualEditRoute->getHost());
  182. $this->assertEquals('request.query.get("page")==1', $actualEditRoute->getCondition());
  183. $this->assertEquals(array('http'), $actualEditRoute->getSchemes());
  184. $this->assertEquals(array('GET', 'POST'), $actualEditRoute->getMethods());
  185. }
  186. /**
  187. * @dataProvider providePrefixTests
  188. */
  189. public function testFlushPrefixesPaths($collectionPrefix, $routePath, $expectedPath)
  190. {
  191. $routes = new RouteCollectionBuilder();
  192. $routes->add($routePath, 'someController', 'test_route');
  193. $outerRoutes = new RouteCollectionBuilder();
  194. $outerRoutes->mount($collectionPrefix, $routes);
  195. $collection = $outerRoutes->build();
  196. $this->assertEquals($expectedPath, $collection->get('test_route')->getPath());
  197. }
  198. public function providePrefixTests()
  199. {
  200. $tests = array();
  201. // empty prefix is of course ok
  202. $tests[] = array('', '/foo', '/foo');
  203. // normal prefix - does not matter if it's a wildcard
  204. $tests[] = array('/{admin}', '/foo', '/{admin}/foo');
  205. // shows that a prefix will always be given the starting slash
  206. $tests[] = array('0', '/foo', '/0/foo');
  207. // spaces are ok, and double slahses at the end are cleaned
  208. $tests[] = array('/ /', '/foo', '/ /foo');
  209. return $tests;
  210. }
  211. public function testFlushSetsPrefixedWithMultipleLevels()
  212. {
  213. $loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  214. $routes = new RouteCollectionBuilder($loader);
  215. $routes->add('homepage', 'MainController::homepageAction', 'homepage');
  216. $adminRoutes = $routes->createBuilder();
  217. $adminRoutes->add('/dashboard', 'AdminController::dashboardAction', 'admin_dashboard');
  218. // embedded collection under /admin
  219. $adminBlogRoutes = $routes->createBuilder();
  220. $adminBlogRoutes->add('/new', 'BlogController::newAction', 'admin_blog_new');
  221. // mount into admin, but before the parent collection has been mounted
  222. $adminRoutes->mount('/blog', $adminBlogRoutes);
  223. // now mount the /admin routes, above should all still be /blog/admin
  224. $routes->mount('/admin', $adminRoutes);
  225. // add a route after mounting
  226. $adminRoutes->add('/users', 'AdminController::userAction', 'admin_users');
  227. // add another sub-collection after the mount
  228. $otherAdminRoutes = $routes->createBuilder();
  229. $otherAdminRoutes->add('/sales', 'StatsController::indexAction', 'admin_stats_sales');
  230. $adminRoutes->mount('/stats', $otherAdminRoutes);
  231. // add a normal collection and see that it is also prefixed
  232. $importedCollection = new RouteCollection();
  233. $importedCollection->add('imported_route', new Route('/foo'));
  234. // make this loader able to do the import - keeps mocking simple
  235. $loader->expects($this->any())
  236. ->method('supports')
  237. ->will($this->returnValue(true));
  238. $loader
  239. ->expects($this->any())
  240. ->method('load')
  241. ->will($this->returnValue($importedCollection));
  242. // import this from the /admin route builder
  243. $adminRoutes->import('admin.yml', '/imported');
  244. $collection = $routes->build();
  245. $this->assertEquals('/admin/dashboard', $collection->get('admin_dashboard')->getPath(), 'Routes before mounting have the prefix');
  246. $this->assertEquals('/admin/users', $collection->get('admin_users')->getPath(), 'Routes after mounting have the prefix');
  247. $this->assertEquals('/admin/blog/new', $collection->get('admin_blog_new')->getPath(), 'Sub-collections receive prefix even if mounted before parent prefix');
  248. $this->assertEquals('/admin/stats/sales', $collection->get('admin_stats_sales')->getPath(), 'Sub-collections receive prefix if mounted after parent prefix');
  249. $this->assertEquals('/admin/imported/foo', $collection->get('imported_route')->getPath(), 'Normal RouteCollections are also prefixed properly');
  250. }
  251. public function testAutomaticRouteNamesDoNotConflict()
  252. {
  253. $routes = new RouteCollectionBuilder();
  254. $adminRoutes = $routes->createBuilder();
  255. // route 1
  256. $adminRoutes->add('/dashboard', '');
  257. $accountRoutes = $routes->createBuilder();
  258. // route 2
  259. $accountRoutes->add('/dashboard', '')
  260. ->setMethods(array('GET'));
  261. // route 3
  262. $accountRoutes->add('/dashboard', '')
  263. ->setMethods(array('POST'));
  264. $routes->mount('/admin', $adminRoutes);
  265. $routes->mount('/account', $accountRoutes);
  266. $collection = $routes->build();
  267. // there are 2 routes (i.e. with non-conflicting names)
  268. $this->assertCount(3, $collection->all());
  269. }
  270. }