XmlFileLoaderTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 testUtf8Route()
  72. {
  73. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  74. $routeCollection = $loader->load('utf8.xml');
  75. $routes = $routeCollection->all();
  76. $this->assertCount(2, $routes, 'Two routes are loaded');
  77. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  78. $utf8Route = $routeCollection->get('app_utf8');
  79. $this->assertSame('/utf8', $utf8Route->getPath());
  80. $this->assertTrue($utf8Route->getOption('utf8'), 'Must be utf8');
  81. $noUtf8Route = $routeCollection->get('app_no_utf8');
  82. $this->assertSame('/no-utf8', $noUtf8Route->getPath());
  83. $this->assertFalse($noUtf8Route->getOption('utf8'), 'Must not be utf8');
  84. }
  85. /**
  86. * @expectedException \InvalidArgumentException
  87. * @dataProvider getPathsToInvalidFiles
  88. */
  89. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  90. {
  91. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  92. $loader->load($filePath);
  93. }
  94. /**
  95. * @expectedException \InvalidArgumentException
  96. * @dataProvider getPathsToInvalidFiles
  97. */
  98. public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
  99. {
  100. $loader = new CustomXmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  101. $loader->load($filePath);
  102. }
  103. public function getPathsToInvalidFiles()
  104. {
  105. return [['nonvalidnode.xml'], ['nonvalidroute.xml'], ['nonvalid.xml'], ['missing_id.xml'], ['missing_path.xml']];
  106. }
  107. /**
  108. * @expectedException \InvalidArgumentException
  109. * @expectedExceptionMessage Document types are not allowed.
  110. */
  111. public function testDocTypeIsNotAllowed()
  112. {
  113. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  114. $loader->load('withdoctype.xml');
  115. }
  116. public function testNullValues()
  117. {
  118. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  119. $routeCollection = $loader->load('null_values.xml');
  120. $route = $routeCollection->get('blog_show');
  121. $this->assertTrue($route->hasDefault('foo'));
  122. $this->assertNull($route->getDefault('foo'));
  123. $this->assertTrue($route->hasDefault('bar'));
  124. $this->assertNull($route->getDefault('bar'));
  125. $this->assertEquals('foo', $route->getDefault('foobar'));
  126. $this->assertEquals('bar', $route->getDefault('baz'));
  127. }
  128. public function testScalarDataTypeDefaults()
  129. {
  130. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  131. $routeCollection = $loader->load('scalar_defaults.xml');
  132. $route = $routeCollection->get('blog');
  133. $this->assertSame(
  134. [
  135. '_controller' => 'AcmeBlogBundle:Blog:index',
  136. 'slug' => null,
  137. 'published' => true,
  138. 'page' => 1,
  139. 'price' => 3.5,
  140. 'archived' => false,
  141. 'free' => true,
  142. 'locked' => false,
  143. 'foo' => null,
  144. 'bar' => null,
  145. ],
  146. $route->getDefaults()
  147. );
  148. }
  149. public function testListDefaults()
  150. {
  151. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  152. $routeCollection = $loader->load('list_defaults.xml');
  153. $route = $routeCollection->get('blog');
  154. $this->assertSame(
  155. [
  156. '_controller' => 'AcmeBlogBundle:Blog:index',
  157. 'values' => [true, 1, 3.5, 'foo'],
  158. ],
  159. $route->getDefaults()
  160. );
  161. }
  162. public function testListInListDefaults()
  163. {
  164. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  165. $routeCollection = $loader->load('list_in_list_defaults.xml');
  166. $route = $routeCollection->get('blog');
  167. $this->assertSame(
  168. [
  169. '_controller' => 'AcmeBlogBundle:Blog:index',
  170. 'values' => [[true, 1, 3.5, 'foo']],
  171. ],
  172. $route->getDefaults()
  173. );
  174. }
  175. public function testListInMapDefaults()
  176. {
  177. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  178. $routeCollection = $loader->load('list_in_map_defaults.xml');
  179. $route = $routeCollection->get('blog');
  180. $this->assertSame(
  181. [
  182. '_controller' => 'AcmeBlogBundle:Blog:index',
  183. 'values' => ['list' => [true, 1, 3.5, 'foo']],
  184. ],
  185. $route->getDefaults()
  186. );
  187. }
  188. public function testMapDefaults()
  189. {
  190. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  191. $routeCollection = $loader->load('map_defaults.xml');
  192. $route = $routeCollection->get('blog');
  193. $this->assertSame(
  194. [
  195. '_controller' => 'AcmeBlogBundle:Blog:index',
  196. 'values' => [
  197. 'public' => true,
  198. 'page' => 1,
  199. 'price' => 3.5,
  200. 'title' => 'foo',
  201. ],
  202. ],
  203. $route->getDefaults()
  204. );
  205. }
  206. public function testMapInListDefaults()
  207. {
  208. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  209. $routeCollection = $loader->load('map_in_list_defaults.xml');
  210. $route = $routeCollection->get('blog');
  211. $this->assertSame(
  212. [
  213. '_controller' => 'AcmeBlogBundle:Blog:index',
  214. 'values' => [[
  215. 'public' => true,
  216. 'page' => 1,
  217. 'price' => 3.5,
  218. 'title' => 'foo',
  219. ]],
  220. ],
  221. $route->getDefaults()
  222. );
  223. }
  224. public function testMapInMapDefaults()
  225. {
  226. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  227. $routeCollection = $loader->load('map_in_map_defaults.xml');
  228. $route = $routeCollection->get('blog');
  229. $this->assertSame(
  230. [
  231. '_controller' => 'AcmeBlogBundle:Blog:index',
  232. 'values' => ['map' => [
  233. 'public' => true,
  234. 'page' => 1,
  235. 'price' => 3.5,
  236. 'title' => 'foo',
  237. ]],
  238. ],
  239. $route->getDefaults()
  240. );
  241. }
  242. public function testNullValuesInList()
  243. {
  244. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  245. $routeCollection = $loader->load('list_null_values.xml');
  246. $route = $routeCollection->get('blog');
  247. $this->assertSame([null, null, null, null, null, null], $route->getDefault('list'));
  248. }
  249. public function testNullValuesInMap()
  250. {
  251. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  252. $routeCollection = $loader->load('map_null_values.xml');
  253. $route = $routeCollection->get('blog');
  254. $this->assertSame(
  255. [
  256. 'boolean' => null,
  257. 'integer' => null,
  258. 'float' => null,
  259. 'string' => null,
  260. 'list' => null,
  261. 'map' => null,
  262. ],
  263. $route->getDefault('map')
  264. );
  265. }
  266. public function testLoadRouteWithControllerAttribute()
  267. {
  268. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  269. $routeCollection = $loader->load('routing.xml');
  270. $route = $routeCollection->get('app_homepage');
  271. $this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
  272. }
  273. public function testLoadRouteWithoutControllerAttribute()
  274. {
  275. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  276. $routeCollection = $loader->load('routing.xml');
  277. $route = $routeCollection->get('app_logout');
  278. $this->assertNull($route->getDefault('_controller'));
  279. }
  280. public function testLoadRouteWithControllerSetInDefaults()
  281. {
  282. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  283. $routeCollection = $loader->load('routing.xml');
  284. $route = $routeCollection->get('app_blog');
  285. $this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
  286. }
  287. /**
  288. * @expectedException \InvalidArgumentException
  289. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" attribute and the defaults key "_controller" for "app_blog"/
  290. */
  291. public function testOverrideControllerInDefaults()
  292. {
  293. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  294. $loader->load('override_defaults.xml');
  295. }
  296. /**
  297. * @dataProvider provideFilesImportingRoutesWithControllers
  298. */
  299. public function testImportRouteWithController($file)
  300. {
  301. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  302. $routeCollection = $loader->load($file);
  303. $route = $routeCollection->get('app_homepage');
  304. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  305. $route = $routeCollection->get('app_blog');
  306. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  307. $route = $routeCollection->get('app_logout');
  308. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  309. }
  310. public function provideFilesImportingRoutesWithControllers()
  311. {
  312. yield ['import_controller.xml'];
  313. yield ['import__controller.xml'];
  314. }
  315. /**
  316. * @expectedException \InvalidArgumentException
  317. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" attribute and the defaults key "_controller" for the "import" tag/
  318. */
  319. public function testImportWithOverriddenController()
  320. {
  321. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  322. $loader->load('import_override_defaults.xml');
  323. }
  324. public function testImportRouteWithGlobMatchingSingleFile()
  325. {
  326. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  327. $routeCollection = $loader->load('import_single.xml');
  328. $route = $routeCollection->get('bar_route');
  329. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  330. }
  331. public function testImportRouteWithGlobMatchingMultipleFiles()
  332. {
  333. $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  334. $routeCollection = $loader->load('import_multiple.xml');
  335. $route = $routeCollection->get('bar_route');
  336. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  337. $route = $routeCollection->get('baz_route');
  338. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  339. }
  340. }