RouteCollectionBuilderTest.php 15 KB

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