PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/routing/Tests/RouteCollectionBuilderTest.php

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