YamlFileLoaderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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\Loader;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. use Symfony\Component\Routing\Loader\YamlFileLoader;
  15. class YamlFileLoaderTest extends TestCase
  16. {
  17. public function testSupports()
  18. {
  19. $loader = new YamlFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
  20. $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
  21. $this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
  22. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  23. $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
  24. $this->assertTrue($loader->supports('foo.yaml', 'yaml'), '->supports() checks the resource type if specified');
  25. $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
  26. }
  27. public function testLoadDoesNothingIfEmpty()
  28. {
  29. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  30. $collection = $loader->load('empty.yml');
  31. $this->assertEquals([], $collection->all());
  32. $this->assertEquals([new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))], $collection->getResources());
  33. }
  34. /**
  35. * @expectedException \InvalidArgumentException
  36. * @dataProvider getPathsToInvalidFiles
  37. */
  38. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  39. {
  40. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  41. $loader->load($filePath);
  42. }
  43. public function getPathsToInvalidFiles()
  44. {
  45. return [
  46. ['nonvalid.yml'],
  47. ['nonvalid2.yml'],
  48. ['incomplete.yml'],
  49. ['nonvalidkeys.yml'],
  50. ['nonesense_resource_plus_path.yml'],
  51. ['nonesense_type_without_resource.yml'],
  52. ['bad_format.yml'],
  53. ];
  54. }
  55. public function testLoadSpecialRouteName()
  56. {
  57. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  58. $routeCollection = $loader->load('special_route_name.yml');
  59. $route = $routeCollection->get('#$péß^a|');
  60. $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
  61. $this->assertSame('/true', $route->getPath());
  62. }
  63. public function testLoadWithRoute()
  64. {
  65. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  66. $routeCollection = $loader->load('validpattern.yml');
  67. $route = $routeCollection->get('blog_show');
  68. $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
  69. $this->assertSame('/blog/{slug}', $route->getPath());
  70. $this->assertSame('{locale}.example.com', $route->getHost());
  71. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  72. $this->assertSame('\w+', $route->getRequirement('locale'));
  73. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  74. $this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  75. $this->assertEquals(['https'], $route->getSchemes());
  76. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  77. }
  78. public function testLoadWithResource()
  79. {
  80. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  81. $routeCollection = $loader->load('validresource.yml');
  82. $routes = $routeCollection->all();
  83. $this->assertCount(2, $routes, 'Two routes are loaded');
  84. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  85. foreach ($routes as $route) {
  86. $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
  87. $this->assertSame('123', $route->getDefault('foo'));
  88. $this->assertSame('\d+', $route->getRequirement('foo'));
  89. $this->assertSame('bar', $route->getOption('foo'));
  90. $this->assertSame('', $route->getHost());
  91. $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
  92. }
  93. }
  94. public function testLoadRouteWithControllerAttribute()
  95. {
  96. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  97. $routeCollection = $loader->load('routing.yml');
  98. $route = $routeCollection->get('app_homepage');
  99. $this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
  100. }
  101. public function testLoadRouteWithoutControllerAttribute()
  102. {
  103. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  104. $routeCollection = $loader->load('routing.yml');
  105. $route = $routeCollection->get('app_logout');
  106. $this->assertNull($route->getDefault('_controller'));
  107. }
  108. public function testLoadRouteWithControllerSetInDefaults()
  109. {
  110. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  111. $routeCollection = $loader->load('routing.yml');
  112. $route = $routeCollection->get('app_blog');
  113. $this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
  114. }
  115. /**
  116. * @expectedException \InvalidArgumentException
  117. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "app_blog"/
  118. */
  119. public function testOverrideControllerInDefaults()
  120. {
  121. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  122. $loader->load('override_defaults.yml');
  123. }
  124. /**
  125. * @dataProvider provideFilesImportingRoutesWithControllers
  126. */
  127. public function testImportRouteWithController($file)
  128. {
  129. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  130. $routeCollection = $loader->load($file);
  131. $route = $routeCollection->get('app_homepage');
  132. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  133. $route = $routeCollection->get('app_blog');
  134. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  135. $route = $routeCollection->get('app_logout');
  136. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  137. }
  138. public function provideFilesImportingRoutesWithControllers()
  139. {
  140. yield ['import_controller.yml'];
  141. yield ['import__controller.yml'];
  142. }
  143. /**
  144. * @expectedException \InvalidArgumentException
  145. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "_static"/
  146. */
  147. public function testImportWithOverriddenController()
  148. {
  149. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  150. $loader->load('import_override_defaults.yml');
  151. }
  152. public function testImportRouteWithGlobMatchingSingleFile()
  153. {
  154. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  155. $routeCollection = $loader->load('import_single.yml');
  156. $route = $routeCollection->get('bar_route');
  157. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  158. }
  159. public function testImportRouteWithGlobMatchingMultipleFiles()
  160. {
  161. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  162. $routeCollection = $loader->load('import_multiple.yml');
  163. $route = $routeCollection->get('bar_route');
  164. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  165. $route = $routeCollection->get('baz_route');
  166. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  167. }
  168. public function testImportRouteWithNamePrefix()
  169. {
  170. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_name_prefix']));
  171. $routeCollection = $loader->load('routing.yml');
  172. $this->assertNotNull($routeCollection->get('app_blog'));
  173. $this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
  174. $this->assertNotNull($routeCollection->get('api_app_blog'));
  175. $this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
  176. }
  177. public function testRemoteSourcesAreNotAccepted()
  178. {
  179. $loader = new YamlFileLoader(new FileLocatorStub());
  180. $this->expectException(\InvalidArgumentException::class);
  181. $loader->load('http://remote.com/here.yml');
  182. }
  183. public function testLoadingLocalizedRoute()
  184. {
  185. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  186. $routes = $loader->load('localized-route.yml');
  187. $this->assertCount(3, $routes);
  188. }
  189. public function testImportingRoutesFromDefinition()
  190. {
  191. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  192. $routes = $loader->load('importing-localized-route.yml');
  193. $this->assertCount(3, $routes);
  194. $this->assertEquals('/nl', $routes->get('home.nl')->getPath());
  195. $this->assertEquals('/en', $routes->get('home.en')->getPath());
  196. $this->assertEquals('/here', $routes->get('not_localized')->getPath());
  197. }
  198. public function testImportingRoutesWithLocales()
  199. {
  200. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  201. $routes = $loader->load('importer-with-locale.yml');
  202. $this->assertCount(2, $routes);
  203. $this->assertEquals('/nl/voorbeeld', $routes->get('imported.nl')->getPath());
  204. $this->assertEquals('/en/example', $routes->get('imported.en')->getPath());
  205. }
  206. public function testImportingNonLocalizedRoutesWithLocales()
  207. {
  208. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  209. $routes = $loader->load('importer-with-locale-imports-non-localized-route.yml');
  210. $this->assertCount(2, $routes);
  211. $this->assertEquals('/nl/imported', $routes->get('imported.nl')->getPath());
  212. $this->assertEquals('/en/imported', $routes->get('imported.en')->getPath());
  213. }
  214. public function testImportingRoutesWithOfficialLocales()
  215. {
  216. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  217. $routes = $loader->load('officially_formatted_locales.yml');
  218. $this->assertCount(3, $routes);
  219. $this->assertEquals('/omelette-au-fromage', $routes->get('official.fr.UTF-8')->getPath());
  220. $this->assertEquals('/eu-não-sou-espanhol', $routes->get('official.pt-PT')->getPath());
  221. $this->assertEquals('/churrasco', $routes->get('official.pt_BR')->getPath());
  222. }
  223. public function testImportingRoutesFromDefinitionMissingLocalePrefix()
  224. {
  225. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  226. $this->expectException(\InvalidArgumentException::class);
  227. $loader->load('missing-locale-in-importer.yml');
  228. }
  229. public function testImportingRouteWithoutPathOrLocales()
  230. {
  231. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  232. $this->expectException(\InvalidArgumentException::class);
  233. $loader->load('route-without-path-or-locales.yml');
  234. }
  235. public function testImportingWithControllerDefault()
  236. {
  237. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  238. $routes = $loader->load('importer-with-controller-default.yml');
  239. $this->assertCount(3, $routes);
  240. $this->assertEquals('DefaultController::defaultAction', $routes->get('home.en')->getDefault('_controller'));
  241. $this->assertEquals('DefaultController::defaultAction', $routes->get('home.nl')->getDefault('_controller'));
  242. $this->assertEquals('DefaultController::defaultAction', $routes->get('not_localized')->getDefault('_controller'));
  243. }
  244. public function testImportRouteWithNoTrailingSlash()
  245. {
  246. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_no_trailing_slash']));
  247. $routeCollection = $loader->load('routing.yml');
  248. $this->assertEquals('/slash/', $routeCollection->get('a_app_homepage')->getPath());
  249. $this->assertEquals('/no-slash', $routeCollection->get('b_app_homepage')->getPath());
  250. }
  251. /**
  252. * @group legacy
  253. * @expectedDeprecation A placeholder name must be a string (0 given). Did you forget to specify the placeholder key for the requirement "\d+" of route "foo" in "%srequirements_without_placeholder_name.yml"?
  254. */
  255. public function testRequirementsWithoutPlaceholderName()
  256. {
  257. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  258. $loader->load('requirements_without_placeholder_name.yml');
  259. }
  260. }