Overview

Namespaces

  • Evenement
  • None
  • PHP
  • Psr
    • Http
      • Message
  • Ratchet
    • Http
    • RFC6455
      • Handshake
      • Messaging
    • Server
    • Session
      • Serialize
      • Storage
        • Proxy
    • Wamp
    • WebSocket
  • React
    • EventLoop
      • Tick
      • Timer
    • Socket
    • Stream
  • Symfony
    • Component
      • HttpFoundation
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
      • Routing
        • Annotation
        • Exception
        • Generator
          • Dumper
        • Loader
          • DependencyInjection
        • Matcher
          • Dumper
        • Tests
          • Annotation
          • Fixtures
            • AnnotatedClasses
            • OtherAnnotatedClasses
          • Generator
            • Dumper
          • Loader
          • Matcher
            • Dumper

Classes

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