RouteCollectionBuilderTest.php 15 KB

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