XmlFileLoaderTest.php 23 KB

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