Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

YamlFileLoaderTest.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
  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. * @expectedException \InvalidArgumentException
  38. * @dataProvider getPathsToInvalidFiles
  39. */
  40. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  41. {
  42. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  43. $loader->load($filePath);
  44. }
  45. public 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. ];
  56. }
  57. public function testLoadSpecialRouteName()
  58. {
  59. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  60. $routeCollection = $loader->load('special_route_name.yml');
  61. $route = $routeCollection->get('#$péß^a|');
  62. $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
  63. $this->assertSame('/true', $route->getPath());
  64. }
  65. public function testLoadWithRoute()
  66. {
  67. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  68. $routeCollection = $loader->load('validpattern.yml');
  69. $route = $routeCollection->get('blog_show');
  70. $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
  71. $this->assertSame('/blog/{slug}', $route->getPath());
  72. $this->assertSame('{locale}.example.com', $route->getHost());
  73. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  74. $this->assertSame('\w+', $route->getRequirement('locale'));
  75. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  76. $this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  77. $this->assertEquals(['https'], $route->getSchemes());
  78. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  79. }
  80. public function testLoadWithResource()
  81. {
  82. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  83. $routeCollection = $loader->load('validresource.yml');
  84. $routes = $routeCollection->all();
  85. $this->assertCount(2, $routes, 'Two routes are loaded');
  86. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  87. foreach ($routes as $route) {
  88. $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
  89. $this->assertSame('123', $route->getDefault('foo'));
  90. $this->assertSame('\d+', $route->getRequirement('foo'));
  91. $this->assertSame('bar', $route->getOption('foo'));
  92. $this->assertSame('', $route->getHost());
  93. $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
  94. }
  95. }
  96. public function testLoadRouteWithControllerAttribute()
  97. {
  98. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  99. $routeCollection = $loader->load('routing.yml');
  100. $route = $routeCollection->get('app_homepage');
  101. $this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
  102. }
  103. public function testLoadRouteWithoutControllerAttribute()
  104. {
  105. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  106. $routeCollection = $loader->load('routing.yml');
  107. $route = $routeCollection->get('app_logout');
  108. $this->assertNull($route->getDefault('_controller'));
  109. }
  110. public function testLoadRouteWithControllerSetInDefaults()
  111. {
  112. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  113. $routeCollection = $loader->load('routing.yml');
  114. $route = $routeCollection->get('app_blog');
  115. $this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
  116. }
  117. /**
  118. * @expectedException \InvalidArgumentException
  119. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "app_blog"/
  120. */
  121. public function testOverrideControllerInDefaults()
  122. {
  123. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  124. $loader->load('override_defaults.yml');
  125. }
  126. /**
  127. * @dataProvider provideFilesImportingRoutesWithControllers
  128. */
  129. public function testImportRouteWithController($file)
  130. {
  131. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  132. $routeCollection = $loader->load($file);
  133. $route = $routeCollection->get('app_homepage');
  134. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  135. $route = $routeCollection->get('app_blog');
  136. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  137. $route = $routeCollection->get('app_logout');
  138. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  139. }
  140. public function provideFilesImportingRoutesWithControllers()
  141. {
  142. yield ['import_controller.yml'];
  143. yield ['import__controller.yml'];
  144. }
  145. /**
  146. * @expectedException \InvalidArgumentException
  147. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "_static"/
  148. */
  149. public function testImportWithOverriddenController()
  150. {
  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. }
  195. public function testLoadingImportedRoutesWithDefaults()
  196. {
  197. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  198. $routes = $loader->load('importer-with-defaults.yml');
  199. $this->assertCount(2, $routes);
  200. $expectedRoutes = new RouteCollection();
  201. $expectedRoutes->add('one', $localeRoute = new Route('/defaults/one'));
  202. $localeRoute->setDefault('_locale', 'g_locale');
  203. $localeRoute->setDefault('_format', 'g_format');
  204. $expectedRoutes->add('two', $formatRoute = new Route('/defaults/two'));
  205. $formatRoute->setDefault('_locale', 'g_locale');
  206. $formatRoute->setDefault('_format', 'g_format');
  207. $formatRoute->setDefault('specific', 'imported');
  208. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/imported-with-defaults.yml'));
  209. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/importer-with-defaults.yml'));
  210. $this->assertEquals($expectedRoutes, $routes);
  211. }
  212. public function testLoadingUtf8Route()
  213. {
  214. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  215. $routes = $loader->load('utf8.yml');
  216. $this->assertCount(2, $routes);
  217. $expectedRoutes = new RouteCollection();
  218. $expectedRoutes->add('some_route', new Route('/'));
  219. $expectedRoutes->add('some_utf8_route', $route = new Route('/utf8'));
  220. $route->setOption('utf8', true);
  221. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/utf8.yml'));
  222. $this->assertEquals($expectedRoutes, $routes);
  223. }
  224. public function testLoadingUtf8ImportedRoutes()
  225. {
  226. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  227. $routes = $loader->load('importer-with-utf8.yml');
  228. $this->assertCount(2, $routes);
  229. $expectedRoutes = new RouteCollection();
  230. $expectedRoutes->add('utf8_one', $one = new Route('/one'));
  231. $one->setOption('utf8', true);
  232. $expectedRoutes->add('utf8_two', $two = new Route('/two'));
  233. $two->setOption('utf8', true);
  234. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/imported-with-utf8.yml'));
  235. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/importer-with-utf8.yml'));
  236. $this->assertEquals($expectedRoutes, $routes);
  237. }
  238. public function testLoadingLocalizedRoute()
  239. {
  240. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  241. $routes = $loader->load('localized-route.yml');
  242. $this->assertCount(3, $routes);
  243. }
  244. public function testImportingRoutesFromDefinition()
  245. {
  246. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  247. $routes = $loader->load('importing-localized-route.yml');
  248. $this->assertCount(3, $routes);
  249. $this->assertEquals('/nl', $routes->get('home.nl')->getPath());
  250. $this->assertEquals('/en', $routes->get('home.en')->getPath());
  251. $this->assertEquals('/here', $routes->get('not_localized')->getPath());
  252. }
  253. public function testImportingRoutesWithLocales()
  254. {
  255. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  256. $routes = $loader->load('importer-with-locale.yml');
  257. $this->assertCount(2, $routes);
  258. $this->assertEquals('/nl/voorbeeld', $routes->get('imported.nl')->getPath());
  259. $this->assertEquals('/en/example', $routes->get('imported.en')->getPath());
  260. }
  261. public function testImportingNonLocalizedRoutesWithLocales()
  262. {
  263. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  264. $routes = $loader->load('importer-with-locale-imports-non-localized-route.yml');
  265. $this->assertCount(2, $routes);
  266. $this->assertEquals('/nl/imported', $routes->get('imported.nl')->getPath());
  267. $this->assertEquals('/en/imported', $routes->get('imported.en')->getPath());
  268. }
  269. public function testImportingRoutesWithOfficialLocales()
  270. {
  271. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  272. $routes = $loader->load('officially_formatted_locales.yml');
  273. $this->assertCount(3, $routes);
  274. $this->assertEquals('/omelette-au-fromage', $routes->get('official.fr.UTF-8')->getPath());
  275. $this->assertEquals('/eu-não-sou-espanhol', $routes->get('official.pt-PT')->getPath());
  276. $this->assertEquals('/churrasco', $routes->get('official.pt_BR')->getPath());
  277. }
  278. public function testImportingRoutesFromDefinitionMissingLocalePrefix()
  279. {
  280. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  281. $this->expectException(\InvalidArgumentException::class);
  282. $loader->load('missing-locale-in-importer.yml');
  283. }
  284. public function testImportingRouteWithoutPathOrLocales()
  285. {
  286. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  287. $this->expectException(\InvalidArgumentException::class);
  288. $loader->load('route-without-path-or-locales.yml');
  289. }
  290. public function testImportingWithControllerDefault()
  291. {
  292. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  293. $routes = $loader->load('importer-with-controller-default.yml');
  294. $this->assertCount(3, $routes);
  295. $this->assertEquals('DefaultController::defaultAction', $routes->get('home.en')->getDefault('_controller'));
  296. $this->assertEquals('DefaultController::defaultAction', $routes->get('home.nl')->getDefault('_controller'));
  297. $this->assertEquals('DefaultController::defaultAction', $routes->get('not_localized')->getDefault('_controller'));
  298. }
  299. public function testImportRouteWithNoTrailingSlash()
  300. {
  301. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_no_trailing_slash']));
  302. $routeCollection = $loader->load('routing.yml');
  303. $this->assertEquals('/slash/', $routeCollection->get('a_app_homepage')->getPath());
  304. $this->assertEquals('/no-slash', $routeCollection->get('b_app_homepage')->getPath());
  305. }
  306. /**
  307. * @group legacy
  308. * @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"?
  309. */
  310. public function testRequirementsWithoutPlaceholderName()
  311. {
  312. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  313. $loader->load('requirements_without_placeholder_name.yml');
  314. }
  315. }