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