4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\Config\Resource\FileResource;
17 use Symfony\Component\Routing\Loader\YamlFileLoader;
18 use Symfony\Component\Routing\Route;
19 use Symfony\Component\Routing\RouteCollection;
20 use Symfony\Component\Routing\RouteCollectionBuilder;
22 class RouteCollectionBuilderTest extends TestCase
24 public function testImport()
26 $resolvedLoader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
27 $resolver = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderResolverInterface')->getMock();
28 $resolver->expects($this->once())
30 ->with('admin_routing.yml', 'yaml')
31 ->will($this->returnValue($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'));
39 ->expects($this->once())
41 ->with('admin_routing.yml', 'yaml')
42 ->will($this->returnValue($expectedCollection));
44 $loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
45 $loader->expects($this->any())
46 ->method('getResolver')
47 ->will($this->returnValue($resolver));
50 $routes = new RouteCollectionBuilder($loader);
51 $importedRoutes = $routes->import('admin_routing.yml', '/', 'yaml');
53 // we should get back a RouteCollectionBuilder
54 $this->assertInstanceOf('Symfony\Component\Routing\RouteCollectionBuilder', $importedRoutes);
56 // get the collection back so we can look at it
57 $addedCollection = $importedRoutes->build();
58 $route = $addedCollection->get('one_test_route');
59 $this->assertSame($originalRoute, $route);
60 // should return file_resource.yml, which is in the original collection
61 $this->assertCount(1, $addedCollection->getResources());
63 // make sure the routes were imported into the top-level builder
64 $routeCollection = $routes->build();
65 $this->assertCount(1, $routes->build());
66 $this->assertCount(1, $routeCollection->getResources());
69 public function testImportAddResources()
71 $routeCollectionBuilder = new RouteCollectionBuilder(new YamlFileLoader(new FileLocator(array(__DIR__.'/Fixtures/'))));
72 $routeCollectionBuilder->import('file_resource.yml');
73 $routeCollection = $routeCollectionBuilder->build();
75 $this->assertCount(1, $routeCollection->getResources());
79 * @expectedException \BadMethodCallException
81 public function testImportWithoutLoaderThrowsException()
83 $collectionBuilder = new RouteCollectionBuilder();
84 $collectionBuilder->import('routing.yml');
87 public function testAdd()
89 $collectionBuilder = new RouteCollectionBuilder();
91 $addedRoute = $collectionBuilder->add('/checkout', 'AppBundle:Order:checkout');
92 $addedRoute2 = $collectionBuilder->add('/blogs', 'AppBundle:Blog:list', 'blog_list');
93 $this->assertInstanceOf('Symfony\Component\Routing\Route', $addedRoute);
94 $this->assertEquals('AppBundle:Order:checkout', $addedRoute->getDefault('_controller'));
96 $finalCollection = $collectionBuilder->build();
97 $this->assertSame($addedRoute2, $finalCollection->get('blog_list'));
100 public function testFlushOrdering()
102 $importedCollection = new RouteCollection();
103 $importedCollection->add('imported_route1', new Route('/imported/foo1'));
104 $importedCollection->add('imported_route2', new Route('/imported/foo2'));
106 $loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
107 // make this loader able to do the import - keeps mocking simple
108 $loader->expects($this->any())
110 ->will($this->returnValue(true));
112 ->expects($this->once())
114 ->will($this->returnValue($importedCollection));
116 $routes = new RouteCollectionBuilder($loader);
119 $routes->add('/checkout', 'AppBundle:Order:checkout', 'checkout_route');
120 // 2) Import from a file
121 $routes->mount('/', $routes->import('admin_routing.yml'));
122 // 3) Add another route
123 $routes->add('/', 'AppBundle:Default:homepage', 'homepage');
124 // 4) Add another route
125 $routes->add('/admin', 'AppBundle:Admin:dashboard', 'admin_dashboard');
127 // set a default value
128 $routes->setDefault('_locale', 'fr');
130 $actualCollection = $routes->build();
132 $this->assertCount(5, $actualCollection);
133 $actualRouteNames = array_keys($actualCollection->all());
134 $this->assertEquals(array(
140 ), $actualRouteNames);
142 // make sure the defaults were set
143 $checkoutRoute = $actualCollection->get('checkout_route');
144 $defaults = $checkoutRoute->getDefaults();
145 $this->assertArrayHasKey('_locale', $defaults);
146 $this->assertEquals('fr', $defaults['_locale']);
149 public function testFlushSetsRouteNames()
151 $collectionBuilder = new RouteCollectionBuilder();
153 // add a "named" route
154 $collectionBuilder->add('/admin', 'AppBundle:Admin:dashboard', 'admin_dashboard');
155 // add an unnamed route
156 $collectionBuilder->add('/blogs', 'AppBundle:Blog:list')
157 ->setMethods(array('GET'));
159 // integer route names are allowed - they don't confuse things
160 $collectionBuilder->add('/products', 'AppBundle:Product:list', 100);
162 $actualCollection = $collectionBuilder->build();
163 $actualRouteNames = array_keys($actualCollection->all());
164 $this->assertEquals(array(
168 ), $actualRouteNames);
171 public function testFlushSetsDetailsOnChildrenRoutes()
173 $routes = new RouteCollectionBuilder();
175 $routes->add('/blogs/{page}', 'listAction', 'blog_list')
176 // unique things for the route
177 ->setDefault('page', 1)
178 ->setRequirement('id', '\d+')
179 ->setOption('expose', true)
180 // things that the collection will try to override (but won't)
181 ->setDefault('_format', 'html')
182 ->setRequirement('_format', 'json|xml')
183 ->setOption('fooBar', true)
184 ->setHost('example.com')
185 ->setCondition('request.isSecure()')
186 ->setSchemes(array('https'))
187 ->setMethods(array('POST'));
189 // a simple route, nothing added to it
190 $routes->add('/blogs/{id}', 'editAction', 'blog_edit');
192 // configure the collection itself
194 // things that will not override the child route
195 ->setDefault('_format', 'json')
196 ->setRequirement('_format', 'xml')
197 ->setOption('fooBar', false)
198 ->setHost('symfony.com')
199 ->setCondition('request.query.get("page")==1')
200 // some unique things that should be set on the child
201 ->setDefault('_locale', 'fr')
202 ->setRequirement('_locale', 'fr|en')
203 ->setOption('niceRoute', true)
204 ->setSchemes(array('http'))
205 ->setMethods(array('GET', 'POST'));
207 $collection = $routes->build();
208 $actualListRoute = $collection->get('blog_list');
210 $this->assertEquals(1, $actualListRoute->getDefault('page'));
211 $this->assertEquals('\d+', $actualListRoute->getRequirement('id'));
212 $this->assertTrue($actualListRoute->getOption('expose'));
213 // none of these should be overridden
214 $this->assertEquals('html', $actualListRoute->getDefault('_format'));
215 $this->assertEquals('json|xml', $actualListRoute->getRequirement('_format'));
216 $this->assertTrue($actualListRoute->getOption('fooBar'));
217 $this->assertEquals('example.com', $actualListRoute->getHost());
218 $this->assertEquals('request.isSecure()', $actualListRoute->getCondition());
219 $this->assertEquals(array('https'), $actualListRoute->getSchemes());
220 $this->assertEquals(array('POST'), $actualListRoute->getMethods());
221 // inherited from the main collection
222 $this->assertEquals('fr', $actualListRoute->getDefault('_locale'));
223 $this->assertEquals('fr|en', $actualListRoute->getRequirement('_locale'));
224 $this->assertTrue($actualListRoute->getOption('niceRoute'));
226 $actualEditRoute = $collection->get('blog_edit');
227 // inherited from the collection
228 $this->assertEquals('symfony.com', $actualEditRoute->getHost());
229 $this->assertEquals('request.query.get("page")==1', $actualEditRoute->getCondition());
230 $this->assertEquals(array('http'), $actualEditRoute->getSchemes());
231 $this->assertEquals(array('GET', 'POST'), $actualEditRoute->getMethods());
235 * @dataProvider providePrefixTests
237 public function testFlushPrefixesPaths($collectionPrefix, $routePath, $expectedPath)
239 $routes = new RouteCollectionBuilder();
241 $routes->add($routePath, 'someController', 'test_route');
243 $outerRoutes = new RouteCollectionBuilder();
244 $outerRoutes->mount($collectionPrefix, $routes);
246 $collection = $outerRoutes->build();
248 $this->assertEquals($expectedPath, $collection->get('test_route')->getPath());
251 public function providePrefixTests()
254 // empty prefix is of course ok
255 $tests[] = array('', '/foo', '/foo');
256 // normal prefix - does not matter if it's a wildcard
257 $tests[] = array('/{admin}', '/foo', '/{admin}/foo');
258 // shows that a prefix will always be given the starting slash
259 $tests[] = array('0', '/foo', '/0/foo');
261 // spaces are ok, and double slahses at the end are cleaned
262 $tests[] = array('/ /', '/foo', '/ /foo');
267 public function testFlushSetsPrefixedWithMultipleLevels()
269 $loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
270 $routes = new RouteCollectionBuilder($loader);
272 $routes->add('homepage', 'MainController::homepageAction', 'homepage');
274 $adminRoutes = $routes->createBuilder();
275 $adminRoutes->add('/dashboard', 'AdminController::dashboardAction', 'admin_dashboard');
277 // embedded collection under /admin
278 $adminBlogRoutes = $routes->createBuilder();
279 $adminBlogRoutes->add('/new', 'BlogController::newAction', 'admin_blog_new');
280 // mount into admin, but before the parent collection has been mounted
281 $adminRoutes->mount('/blog', $adminBlogRoutes);
283 // now mount the /admin routes, above should all still be /blog/admin
284 $routes->mount('/admin', $adminRoutes);
285 // add a route after mounting
286 $adminRoutes->add('/users', 'AdminController::userAction', 'admin_users');
288 // add another sub-collection after the mount
289 $otherAdminRoutes = $routes->createBuilder();
290 $otherAdminRoutes->add('/sales', 'StatsController::indexAction', 'admin_stats_sales');
291 $adminRoutes->mount('/stats', $otherAdminRoutes);
293 // add a normal collection and see that it is also prefixed
294 $importedCollection = new RouteCollection();
295 $importedCollection->add('imported_route', new Route('/foo'));
296 // make this loader able to do the import - keeps mocking simple
297 $loader->expects($this->any())
299 ->will($this->returnValue(true));
301 ->expects($this->any())
303 ->will($this->returnValue($importedCollection));
304 // import this from the /admin route builder
305 $adminRoutes->import('admin.yml', '/imported');
307 $collection = $routes->build();
308 $this->assertEquals('/admin/dashboard', $collection->get('admin_dashboard')->getPath(), 'Routes before mounting have the prefix');
309 $this->assertEquals('/admin/users', $collection->get('admin_users')->getPath(), 'Routes after mounting have the prefix');
310 $this->assertEquals('/admin/blog/new', $collection->get('admin_blog_new')->getPath(), 'Sub-collections receive prefix even if mounted before parent prefix');
311 $this->assertEquals('/admin/stats/sales', $collection->get('admin_stats_sales')->getPath(), 'Sub-collections receive prefix if mounted after parent prefix');
312 $this->assertEquals('/admin/imported/foo', $collection->get('imported_route')->getPath(), 'Normal RouteCollections are also prefixed properly');
315 public function testAutomaticRouteNamesDoNotConflict()
317 $routes = new RouteCollectionBuilder();
319 $adminRoutes = $routes->createBuilder();
321 $adminRoutes->add('/dashboard', '');
323 $accountRoutes = $routes->createBuilder();
325 $accountRoutes->add('/dashboard', '')
326 ->setMethods(array('GET'));
328 $accountRoutes->add('/dashboard', '')
329 ->setMethods(array('POST'));
331 $routes->mount('/admin', $adminRoutes);
332 $routes->mount('/account', $accountRoutes);
334 $collection = $routes->build();
335 // there are 2 routes (i.e. with non-conflicting names)
336 $this->assertCount(3, $collection->all());
339 public function testAddsThePrefixOnlyOnceWhenLoadingMultipleCollections()
341 $firstCollection = new RouteCollection();
342 $firstCollection->add('a', new Route('/a'));
344 $secondCollection = new RouteCollection();
345 $secondCollection->add('b', new Route('/b'));
347 $loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
348 $loader->expects($this->any())
350 ->will($this->returnValue(true));
352 ->expects($this->any())
354 ->will($this->returnValue(array($firstCollection, $secondCollection)));
356 $routeCollectionBuilder = new RouteCollectionBuilder($loader);
357 $routeCollectionBuilder->import('/directory/recurse/*', '/other/', 'glob');
358 $routes = $routeCollectionBuilder->build()->all();
360 $this->assertCount(2, $routes);
361 $this->assertEquals('/other/a', $routes['a']->getPath());
362 $this->assertEquals('/other/b', $routes['b']->getPath());