YamlFileLoaderTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. use Symfony\Component\Routing\Route;
  16. use Symfony\Component\Routing\RouteCollection;
  17. class YamlFileLoaderTest extends TestCase
  18. {
  19. public function testSupports()
  20. {
  21. $loader = new YamlFileLoader($this->createMock(FileLocator::class));
  22. $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
  23. $this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
  24. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  25. $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
  26. $this->assertTrue($loader->supports('foo.yaml', 'yaml'), '->supports() checks the resource type if specified');
  27. $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
  28. }
  29. public function testLoadDoesNothingIfEmpty()
  30. {
  31. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  32. $collection = $loader->load('empty.yml');
  33. $this->assertEquals([], $collection->all());
  34. $this->assertEquals([new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))], $collection->getResources());
  35. }
  36. /**
  37. * @dataProvider getPathsToInvalidFiles
  38. */
  39. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  40. {
  41. $this->expectException(\InvalidArgumentException::class);
  42. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  43. $loader->load($filePath);
  44. }
  45. public static function getPathsToInvalidFiles()
  46. {
  47. return [
  48. ['nonvalid.yml'],
  49. ['nonvalid2.yml'],
  50. ['incomplete.yml'],
  51. ['nonvalidkeys.yml'],
  52. ['nonesense_resource_plus_path.yml'],
  53. ['nonesense_type_without_resource.yml'],
  54. ['bad_format.yml'],
  55. ['alias/invalid-alias.yaml'],
  56. ['alias/invalid-deprecated-no-package.yaml'],
  57. ['alias/invalid-deprecated-no-version.yaml'],
  58. ];
  59. }
  60. public function testLoadSpecialRouteName()
  61. {
  62. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  63. $routeCollection = $loader->load('special_route_name.yml');
  64. $route = $routeCollection->get('#$péß^a|');
  65. $this->assertInstanceOf(Route::class, $route);
  66. $this->assertSame('/true', $route->getPath());
  67. }
  68. public function testLoadWithRoute()
  69. {
  70. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  71. $routeCollection = $loader->load('validpattern.yml');
  72. $route = $routeCollection->get('blog_show');
  73. $this->assertInstanceOf(Route::class, $route);
  74. $this->assertSame('/blog/{slug}', $route->getPath());
  75. $this->assertSame('{locale}.example.com', $route->getHost());
  76. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  77. $this->assertSame('\w+', $route->getRequirement('locale'));
  78. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  79. $this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  80. $this->assertEquals(['https'], $route->getSchemes());
  81. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  82. $this->assertTrue($route->getDefault('_stateless'));
  83. }
  84. public function testLoadWithResource()
  85. {
  86. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  87. $routeCollection = $loader->load('validresource.yml');
  88. $routes = $routeCollection->all();
  89. $this->assertCount(2, $routes, 'Two routes are loaded');
  90. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  91. foreach ($routes as $route) {
  92. $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
  93. $this->assertSame('123', $route->getDefault('foo'));
  94. $this->assertSame('\d+', $route->getRequirement('foo'));
  95. $this->assertSame('bar', $route->getOption('foo'));
  96. $this->assertSame('', $route->getHost());
  97. $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
  98. }
  99. }
  100. public function testLoadRouteWithControllerAttribute()
  101. {
  102. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  103. $routeCollection = $loader->load('routing.yml');
  104. $route = $routeCollection->get('app_homepage');
  105. $this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
  106. }
  107. public function testLoadRouteWithoutControllerAttribute()
  108. {
  109. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  110. $routeCollection = $loader->load('routing.yml');
  111. $route = $routeCollection->get('app_logout');
  112. $this->assertNull($route->getDefault('_controller'));
  113. }
  114. public function testLoadRouteWithControllerSetInDefaults()
  115. {
  116. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  117. $routeCollection = $loader->load('routing.yml');
  118. $route = $routeCollection->get('app_blog');
  119. $this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
  120. }
  121. public function testOverrideControllerInDefaults()
  122. {
  123. $this->expectException(\InvalidArgumentException::class);
  124. $this->expectExceptionMessageMatches('/The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "app_blog"/');
  125. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  126. $loader->load('override_defaults.yml');
  127. }
  128. /**
  129. * @dataProvider provideFilesImportingRoutesWithControllers
  130. */
  131. public function testImportRouteWithController($file)
  132. {
  133. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  134. $routeCollection = $loader->load($file);
  135. $route = $routeCollection->get('app_homepage');
  136. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  137. $route = $routeCollection->get('app_blog');
  138. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  139. $route = $routeCollection->get('app_logout');
  140. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  141. }
  142. public static function provideFilesImportingRoutesWithControllers()
  143. {
  144. yield ['import_controller.yml'];
  145. yield ['import__controller.yml'];
  146. }
  147. public function testImportWithOverriddenController()
  148. {
  149. $this->expectException(\InvalidArgumentException::class);
  150. $this->expectExceptionMessageMatches('/The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "_static"/');
  151. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  152. $loader->load('import_override_defaults.yml');
  153. }
  154. public function testImportRouteWithGlobMatchingSingleFile()
  155. {
  156. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  157. $routeCollection = $loader->load('import_single.yml');
  158. $route = $routeCollection->get('bar_route');
  159. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  160. }
  161. public function testImportRouteWithGlobMatchingMultipleFiles()
  162. {
  163. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  164. $routeCollection = $loader->load('import_multiple.yml');
  165. $route = $routeCollection->get('bar_route');
  166. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  167. $route = $routeCollection->get('baz_route');
  168. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  169. }
  170. public function testImportRouteWithNamePrefix()
  171. {
  172. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_name_prefix']));
  173. $routeCollection = $loader->load('routing.yml');
  174. $this->assertNotNull($routeCollection->get('app_blog'));
  175. $this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
  176. $this->assertNotNull($routeCollection->get('api_app_blog'));
  177. $this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
  178. }
  179. public function testRemoteSourcesAreNotAccepted()
  180. {
  181. $loader = new YamlFileLoader(new FileLocatorStub());
  182. $this->expectException(\InvalidArgumentException::class);
  183. $loader->load('http://remote.com/here.yml');
  184. }
  185. public function testLoadingRouteWithDefaults()
  186. {
  187. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  188. $routes = $loader->load('defaults.yml');
  189. $this->assertCount(1, $routes);
  190. $defaultsRoute = $routes->get('defaults');
  191. $this->assertSame('/defaults', $defaultsRoute->getPath());
  192. $this->assertSame('en', $defaultsRoute->getDefault('_locale'));
  193. $this->assertSame('html', $defaultsRoute->getDefault('_format'));
  194. $this->assertTrue($defaultsRoute->getDefault('_stateless'));
  195. }
  196. public function testLoadingImportedRoutesWithDefaults()
  197. {
  198. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  199. $routes = $loader->load('importer-with-defaults.yml');
  200. $this->assertCount(2, $routes);
  201. $expectedRoutes = new RouteCollection();
  202. $expectedRoutes->add('one', $localeRoute = new Route('/defaults/one'));
  203. $localeRoute->setDefault('_locale', 'g_locale');
  204. $localeRoute->setDefault('_format', 'g_format');
  205. $localeRoute->setDefault('_stateless', true);
  206. $expectedRoutes->add('two', $formatRoute = new Route('/defaults/two'));
  207. $formatRoute->setDefault('_locale', 'g_locale');
  208. $formatRoute->setDefault('_format', 'g_format');
  209. $formatRoute->setDefault('_stateless', true);
  210. $formatRoute->setDefault('specific', 'imported');
  211. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/imported-with-defaults.yml'));
  212. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/importer-with-defaults.yml'));
  213. $this->assertEquals($expectedRoutes, $routes);
  214. }
  215. public function testLoadingUtf8Route()
  216. {
  217. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  218. $routes = $loader->load('utf8.yml');
  219. $this->assertCount(2, $routes);
  220. $expectedRoutes = new RouteCollection();
  221. $expectedRoutes->add('some_route', new Route('/'));
  222. $expectedRoutes->add('some_utf8_route', $route = new Route('/utf8'));
  223. $route->setOption('utf8', true);
  224. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/utf8.yml'));
  225. $this->assertEquals($expectedRoutes, $routes);
  226. }
  227. public function testLoadingUtf8ImportedRoutes()
  228. {
  229. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  230. $routes = $loader->load('importer-with-utf8.yml');
  231. $this->assertCount(2, $routes);
  232. $expectedRoutes = new RouteCollection();
  233. $expectedRoutes->add('utf8_one', $one = new Route('/one'));
  234. $one->setOption('utf8', true);
  235. $expectedRoutes->add('utf8_two', $two = new Route('/two'));
  236. $two->setOption('utf8', true);
  237. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/imported-with-utf8.yml'));
  238. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/importer-with-utf8.yml'));
  239. $this->assertEquals($expectedRoutes, $routes);
  240. }
  241. public function testLoadingLocalizedRoute()
  242. {
  243. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  244. $routes = $loader->load('localized-route.yml');
  245. $this->assertCount(3, $routes);
  246. }
  247. public function testImportingRoutesFromDefinition()
  248. {
  249. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  250. $routes = $loader->load('importing-localized-route.yml');
  251. $this->assertCount(3, $routes);
  252. $this->assertEquals('/nl', $routes->get('home.nl')->getPath());
  253. $this->assertEquals('/en', $routes->get('home.en')->getPath());
  254. $this->assertEquals('/here', $routes->get('not_localized')->getPath());
  255. }
  256. public function testImportingRoutesWithLocales()
  257. {
  258. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  259. $routes = $loader->load('importer-with-locale.yml');
  260. $this->assertCount(2, $routes);
  261. $this->assertEquals('/nl/voorbeeld', $routes->get('imported.nl')->getPath());
  262. $this->assertEquals('/en/example', $routes->get('imported.en')->getPath());
  263. $this->assertEquals('nl', $routes->get('imported.nl')->getRequirement('_locale'));
  264. $this->assertEquals('en', $routes->get('imported.en')->getRequirement('_locale'));
  265. }
  266. public function testImportingNonLocalizedRoutesWithLocales()
  267. {
  268. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  269. $routes = $loader->load('importer-with-locale-imports-non-localized-route.yml');
  270. $this->assertCount(2, $routes);
  271. $this->assertEquals('/nl/imported', $routes->get('imported.nl')->getPath());
  272. $this->assertEquals('/en/imported', $routes->get('imported.en')->getPath());
  273. $this->assertSame('nl', $routes->get('imported.nl')->getRequirement('_locale'));
  274. $this->assertSame('en', $routes->get('imported.en')->getRequirement('_locale'));
  275. }
  276. public function testImportingRoutesWithOfficialLocales()
  277. {
  278. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  279. $routes = $loader->load('officially_formatted_locales.yml');
  280. $this->assertCount(3, $routes);
  281. $this->assertEquals('/omelette-au-fromage', $routes->get('official.fr.UTF-8')->getPath());
  282. $this->assertEquals('/eu-não-sou-espanhol', $routes->get('official.pt-PT')->getPath());
  283. $this->assertEquals('/churrasco', $routes->get('official.pt_BR')->getPath());
  284. }
  285. public function testImportingRoutesFromDefinitionMissingLocalePrefix()
  286. {
  287. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  288. $this->expectException(\InvalidArgumentException::class);
  289. $loader->load('missing-locale-in-importer.yml');
  290. }
  291. public function testImportingRouteWithoutPathOrLocales()
  292. {
  293. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  294. $this->expectException(\InvalidArgumentException::class);
  295. $loader->load('route-without-path-or-locales.yml');
  296. }
  297. public function testImportingWithControllerDefault()
  298. {
  299. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  300. $routes = $loader->load('importer-with-controller-default.yml');
  301. $this->assertCount(3, $routes);
  302. $this->assertEquals('DefaultController::defaultAction', $routes->get('home.en')->getDefault('_controller'));
  303. $this->assertEquals('DefaultController::defaultAction', $routes->get('home.nl')->getDefault('_controller'));
  304. $this->assertEquals('DefaultController::defaultAction', $routes->get('not_localized')->getDefault('_controller'));
  305. }
  306. public function testImportRouteWithNoTrailingSlash()
  307. {
  308. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_no_trailing_slash']));
  309. $routeCollection = $loader->load('routing.yml');
  310. $this->assertEquals('/slash/', $routeCollection->get('a_app_homepage')->getPath());
  311. $this->assertEquals('/no-slash', $routeCollection->get('b_app_homepage')->getPath());
  312. }
  313. public function testRequirementsWithoutPlaceholderName()
  314. {
  315. $this->expectException(\InvalidArgumentException::class);
  316. $this->expectExceptionMessage('A placeholder name must be a string (0 given). Did you forget to specify the placeholder key for the requirement "\\d+" of route "foo"');
  317. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  318. $loader->load('requirements_without_placeholder_name.yml');
  319. }
  320. public function testImportingRoutesWithHostsInImporter()
  321. {
  322. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  323. $routes = $loader->load('importer-with-host.yml');
  324. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php';
  325. $this->assertEquals($expectedRoutes('yml'), $routes);
  326. }
  327. public function testImportingRoutesWithLocalesAndHostInImporter()
  328. {
  329. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  330. $routes = $loader->load('importer-with-locale-and-host.yml');
  331. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php';
  332. $this->assertEquals($expectedRoutes('yml'), $routes);
  333. }
  334. public function testImportingRoutesWithoutHostInImporter()
  335. {
  336. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  337. $routes = $loader->load('importer-without-host.yml');
  338. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php';
  339. $this->assertEquals($expectedRoutes('yml'), $routes);
  340. }
  341. public function testImportingRoutesWithSingleHostInImporter()
  342. {
  343. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  344. $routes = $loader->load('importer-with-single-host.yml');
  345. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php';
  346. $this->assertEquals($expectedRoutes('yml'), $routes);
  347. }
  348. public function testWhenEnv()
  349. {
  350. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'some-env');
  351. $routes = $loader->load('when-env.yml');
  352. $this->assertSame(['b', 'a'], array_keys($routes->all()));
  353. $this->assertSame('/b', $routes->get('b')->getPath());
  354. $this->assertSame('/a1', $routes->get('a')->getPath());
  355. }
  356. public function testImportingAliases()
  357. {
  358. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/alias']));
  359. $routes = $loader->load('alias.yaml');
  360. $expectedRoutes = require __DIR__.'/../Fixtures/alias/expected.php';
  361. $this->assertEquals($expectedRoutes('yaml'), $routes);
  362. }
  363. }