Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

XmlFileLoaderTest.php 20KB

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