XmlFileLoaderTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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\Routing\Loader\XmlFileLoader;
  14. use Symfony\Component\Routing\Tests\Fixtures\CustomXmlFileLoader;
  15. class XmlFileLoaderTest extends TestCase
  16. {
  17. public function testSupports()
  18. {
  19. $loader = new XmlFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
  20. $this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
  21. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  22. $this->assertTrue($loader->supports('foo.xml', 'xml'), '->supports() checks the resource type if specified');
  23. $this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
  24. }
  25. public function testLoadWithRoute()
  26. {
  27. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  28. $routeCollection = $loader->load('validpattern.xml');
  29. $route = $routeCollection->get('blog_show');
  30. $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
  31. $this->assertSame('/blog/{slug}', $route->getPath());
  32. $this->assertSame('{locale}.example.com', $route->getHost());
  33. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  34. $this->assertSame('\w+', $route->getRequirement('locale'));
  35. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  36. $this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  37. $this->assertEquals(['https'], $route->getSchemes());
  38. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  39. }
  40. public function testLoadWithNamespacePrefix()
  41. {
  42. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  43. $routeCollection = $loader->load('namespaceprefix.xml');
  44. $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
  45. $route = $routeCollection->get('blog_show');
  46. $this->assertSame('/blog/{slug}', $route->getPath());
  47. $this->assertSame('{_locale}.example.com', $route->getHost());
  48. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  49. $this->assertSame('\w+', $route->getRequirement('slug'));
  50. $this->assertSame('en|fr|de', $route->getRequirement('_locale'));
  51. $this->assertNull($route->getDefault('slug'));
  52. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  53. $this->assertSame(1, $route->getDefault('page'));
  54. }
  55. public function testLoadWithImport()
  56. {
  57. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  58. $routeCollection = $loader->load('validresource.xml');
  59. $routes = $routeCollection->all();
  60. $this->assertCount(2, $routes, 'Two routes are loaded');
  61. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  62. foreach ($routes as $route) {
  63. $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
  64. $this->assertSame('123', $route->getDefault('foo'));
  65. $this->assertSame('\d+', $route->getRequirement('foo'));
  66. $this->assertSame('bar', $route->getOption('foo'));
  67. $this->assertSame('', $route->getHost());
  68. $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
  69. }
  70. }
  71. public function testLoadLocalized()
  72. {
  73. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  74. $routeCollection = $loader->load('localized.xml');
  75. $routes = $routeCollection->all();
  76. $this->assertCount(2, $routes, 'Two routes are loaded');
  77. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  78. $this->assertEquals('/route', $routeCollection->get('localized.fr')->getPath());
  79. $this->assertEquals('/path', $routeCollection->get('localized.en')->getPath());
  80. }
  81. public function testLocalizedImports()
  82. {
  83. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  84. $routeCollection = $loader->load('importer-with-locale.xml');
  85. $routes = $routeCollection->all();
  86. $this->assertCount(2, $routes, 'Two routes are loaded');
  87. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  88. $this->assertEquals('/le-prefix/le-suffix', $routeCollection->get('imported.fr')->getPath());
  89. $this->assertEquals('/the-prefix/suffix', $routeCollection->get('imported.en')->getPath());
  90. }
  91. public function testLocalizedImportsOfNotLocalizedRoutes()
  92. {
  93. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  94. $routeCollection = $loader->load('importer-with-locale-imports-non-localized-route.xml');
  95. $routes = $routeCollection->all();
  96. $this->assertCount(2, $routes, 'Two routes are loaded');
  97. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  98. $this->assertEquals('/le-prefix/suffix', $routeCollection->get('imported.fr')->getPath());
  99. $this->assertEquals('/the-prefix/suffix', $routeCollection->get('imported.en')->getPath());
  100. }
  101. /**
  102. * @expectedException \InvalidArgumentException
  103. * @dataProvider getPathsToInvalidFiles
  104. */
  105. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  106. {
  107. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  108. $loader->load($filePath);
  109. }
  110. /**
  111. * @expectedException \InvalidArgumentException
  112. * @dataProvider getPathsToInvalidFiles
  113. */
  114. public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
  115. {
  116. $loader = new CustomXmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  117. $loader->load($filePath);
  118. }
  119. public function getPathsToInvalidFiles()
  120. {
  121. return [['nonvalidnode.xml'], ['nonvalidroute.xml'], ['nonvalid.xml'], ['missing_id.xml'], ['missing_path.xml']];
  122. }
  123. /**
  124. * @expectedException \InvalidArgumentException
  125. * @expectedExceptionMessage Document types are not allowed.
  126. */
  127. public function testDocTypeIsNotAllowed()
  128. {
  129. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  130. $loader->load('withdoctype.xml');
  131. }
  132. public function testNullValues()
  133. {
  134. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  135. $routeCollection = $loader->load('null_values.xml');
  136. $route = $routeCollection->get('blog_show');
  137. $this->assertTrue($route->hasDefault('foo'));
  138. $this->assertNull($route->getDefault('foo'));
  139. $this->assertTrue($route->hasDefault('bar'));
  140. $this->assertNull($route->getDefault('bar'));
  141. $this->assertEquals('foo', $route->getDefault('foobar'));
  142. $this->assertEquals('bar', $route->getDefault('baz'));
  143. }
  144. public function testScalarDataTypeDefaults()
  145. {
  146. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  147. $routeCollection = $loader->load('scalar_defaults.xml');
  148. $route = $routeCollection->get('blog');
  149. $this->assertSame(
  150. [
  151. '_controller' => 'AcmeBlogBundle:Blog:index',
  152. 'slug' => null,
  153. 'published' => true,
  154. 'page' => 1,
  155. 'price' => 3.5,
  156. 'archived' => false,
  157. 'free' => true,
  158. 'locked' => false,
  159. 'foo' => null,
  160. 'bar' => null,
  161. ],
  162. $route->getDefaults()
  163. );
  164. }
  165. public function testListDefaults()
  166. {
  167. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  168. $routeCollection = $loader->load('list_defaults.xml');
  169. $route = $routeCollection->get('blog');
  170. $this->assertSame(
  171. [
  172. '_controller' => 'AcmeBlogBundle:Blog:index',
  173. 'values' => [true, 1, 3.5, 'foo'],
  174. ],
  175. $route->getDefaults()
  176. );
  177. }
  178. public function testListInListDefaults()
  179. {
  180. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  181. $routeCollection = $loader->load('list_in_list_defaults.xml');
  182. $route = $routeCollection->get('blog');
  183. $this->assertSame(
  184. [
  185. '_controller' => 'AcmeBlogBundle:Blog:index',
  186. 'values' => [[true, 1, 3.5, 'foo']],
  187. ],
  188. $route->getDefaults()
  189. );
  190. }
  191. public function testListInMapDefaults()
  192. {
  193. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  194. $routeCollection = $loader->load('list_in_map_defaults.xml');
  195. $route = $routeCollection->get('blog');
  196. $this->assertSame(
  197. [
  198. '_controller' => 'AcmeBlogBundle:Blog:index',
  199. 'values' => ['list' => [true, 1, 3.5, 'foo']],
  200. ],
  201. $route->getDefaults()
  202. );
  203. }
  204. public function testMapDefaults()
  205. {
  206. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  207. $routeCollection = $loader->load('map_defaults.xml');
  208. $route = $routeCollection->get('blog');
  209. $this->assertSame(
  210. [
  211. '_controller' => 'AcmeBlogBundle:Blog:index',
  212. 'values' => [
  213. 'public' => true,
  214. 'page' => 1,
  215. 'price' => 3.5,
  216. 'title' => 'foo',
  217. ],
  218. ],
  219. $route->getDefaults()
  220. );
  221. }
  222. public function testMapInListDefaults()
  223. {
  224. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  225. $routeCollection = $loader->load('map_in_list_defaults.xml');
  226. $route = $routeCollection->get('blog');
  227. $this->assertSame(
  228. [
  229. '_controller' => 'AcmeBlogBundle:Blog:index',
  230. 'values' => [[
  231. 'public' => true,
  232. 'page' => 1,
  233. 'price' => 3.5,
  234. 'title' => 'foo',
  235. ]],
  236. ],
  237. $route->getDefaults()
  238. );
  239. }
  240. public function testMapInMapDefaults()
  241. {
  242. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  243. $routeCollection = $loader->load('map_in_map_defaults.xml');
  244. $route = $routeCollection->get('blog');
  245. $this->assertSame(
  246. [
  247. '_controller' => 'AcmeBlogBundle:Blog:index',
  248. 'values' => ['map' => [
  249. 'public' => true,
  250. 'page' => 1,
  251. 'price' => 3.5,
  252. 'title' => 'foo',
  253. ]],
  254. ],
  255. $route->getDefaults()
  256. );
  257. }
  258. public function testNullValuesInList()
  259. {
  260. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  261. $routeCollection = $loader->load('list_null_values.xml');
  262. $route = $routeCollection->get('blog');
  263. $this->assertSame([null, null, null, null, null, null], $route->getDefault('list'));
  264. }
  265. public function testNullValuesInMap()
  266. {
  267. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  268. $routeCollection = $loader->load('map_null_values.xml');
  269. $route = $routeCollection->get('blog');
  270. $this->assertSame(
  271. [
  272. 'boolean' => null,
  273. 'integer' => null,
  274. 'float' => null,
  275. 'string' => null,
  276. 'list' => null,
  277. 'map' => null,
  278. ],
  279. $route->getDefault('map')
  280. );
  281. }
  282. public function testLoadRouteWithControllerAttribute()
  283. {
  284. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  285. $routeCollection = $loader->load('routing.xml');
  286. $route = $routeCollection->get('app_homepage');
  287. $this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
  288. }
  289. public function testLoadRouteWithoutControllerAttribute()
  290. {
  291. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  292. $routeCollection = $loader->load('routing.xml');
  293. $route = $routeCollection->get('app_logout');
  294. $this->assertNull($route->getDefault('_controller'));
  295. }
  296. public function testLoadRouteWithControllerSetInDefaults()
  297. {
  298. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  299. $routeCollection = $loader->load('routing.xml');
  300. $route = $routeCollection->get('app_blog');
  301. $this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
  302. }
  303. /**
  304. * @expectedException \InvalidArgumentException
  305. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" attribute and the defaults key "_controller" for "app_blog"/
  306. */
  307. public function testOverrideControllerInDefaults()
  308. {
  309. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  310. $loader->load('override_defaults.xml');
  311. }
  312. /**
  313. * @dataProvider provideFilesImportingRoutesWithControllers
  314. */
  315. public function testImportRouteWithController($file)
  316. {
  317. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  318. $routeCollection = $loader->load($file);
  319. $route = $routeCollection->get('app_homepage');
  320. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  321. $route = $routeCollection->get('app_blog');
  322. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  323. $route = $routeCollection->get('app_logout');
  324. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  325. }
  326. public function provideFilesImportingRoutesWithControllers()
  327. {
  328. yield ['import_controller.xml'];
  329. yield ['import__controller.xml'];
  330. }
  331. /**
  332. * @expectedException \InvalidArgumentException
  333. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" attribute and the defaults key "_controller" for the "import" tag/
  334. */
  335. public function testImportWithOverriddenController()
  336. {
  337. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  338. $loader->load('import_override_defaults.xml');
  339. }
  340. public function testImportRouteWithGlobMatchingSingleFile()
  341. {
  342. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  343. $routeCollection = $loader->load('import_single.xml');
  344. $route = $routeCollection->get('bar_route');
  345. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  346. }
  347. public function testImportRouteWithGlobMatchingMultipleFiles()
  348. {
  349. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  350. $routeCollection = $loader->load('import_multiple.xml');
  351. $route = $routeCollection->get('bar_route');
  352. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  353. $route = $routeCollection->get('baz_route');
  354. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  355. }
  356. public function testImportRouteWithNamePrefix()
  357. {
  358. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_name_prefix']));
  359. $routeCollection = $loader->load('routing.xml');
  360. $this->assertNotNull($routeCollection->get('app_blog'));
  361. $this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
  362. $this->assertNotNull($routeCollection->get('api_app_blog'));
  363. $this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
  364. }
  365. public function testImportRouteWithNoTrailingSlash()
  366. {
  367. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_no_trailing_slash']));
  368. $routeCollection = $loader->load('routing.xml');
  369. $this->assertEquals('/slash/', $routeCollection->get('a_app_homepage')->getPath());
  370. $this->assertEquals('/no-slash', $routeCollection->get('b_app_homepage')->getPath());
  371. }
  372. }