XmlFileLoaderTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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(array(__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(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  37. $this->assertEquals(array('https'), $route->getSchemes());
  38. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  39. }
  40. public function testLoadWithNamespacePrefix()
  41. {
  42. $loader = new XmlFileLoader(new FileLocator(array(__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(array(__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. /**
  72. * @expectedException \InvalidArgumentException
  73. * @dataProvider getPathsToInvalidFiles
  74. */
  75. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  76. {
  77. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  78. $loader->load($filePath);
  79. }
  80. /**
  81. * @expectedException \InvalidArgumentException
  82. * @dataProvider getPathsToInvalidFiles
  83. */
  84. public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
  85. {
  86. $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  87. $loader->load($filePath);
  88. }
  89. public function getPathsToInvalidFiles()
  90. {
  91. return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
  92. }
  93. /**
  94. * @expectedException \InvalidArgumentException
  95. * @expectedExceptionMessage Document types are not allowed.
  96. */
  97. public function testDocTypeIsNotAllowed()
  98. {
  99. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  100. $loader->load('withdoctype.xml');
  101. }
  102. public function testNullValues()
  103. {
  104. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  105. $routeCollection = $loader->load('null_values.xml');
  106. $route = $routeCollection->get('blog_show');
  107. $this->assertTrue($route->hasDefault('foo'));
  108. $this->assertNull($route->getDefault('foo'));
  109. $this->assertTrue($route->hasDefault('bar'));
  110. $this->assertNull($route->getDefault('bar'));
  111. $this->assertEquals('foo', $route->getDefault('foobar'));
  112. $this->assertEquals('bar', $route->getDefault('baz'));
  113. }
  114. public function testScalarDataTypeDefaults()
  115. {
  116. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  117. $routeCollection = $loader->load('scalar_defaults.xml');
  118. $route = $routeCollection->get('blog');
  119. $this->assertSame(
  120. array(
  121. '_controller' => 'AcmeBlogBundle:Blog:index',
  122. 'slug' => null,
  123. 'published' => true,
  124. 'page' => 1,
  125. 'price' => 3.5,
  126. 'archived' => false,
  127. 'free' => true,
  128. 'locked' => false,
  129. 'foo' => null,
  130. 'bar' => null,
  131. ),
  132. $route->getDefaults()
  133. );
  134. }
  135. public function testListDefaults()
  136. {
  137. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  138. $routeCollection = $loader->load('list_defaults.xml');
  139. $route = $routeCollection->get('blog');
  140. $this->assertSame(
  141. array(
  142. '_controller' => 'AcmeBlogBundle:Blog:index',
  143. 'values' => array(true, 1, 3.5, 'foo'),
  144. ),
  145. $route->getDefaults()
  146. );
  147. }
  148. public function testListInListDefaults()
  149. {
  150. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  151. $routeCollection = $loader->load('list_in_list_defaults.xml');
  152. $route = $routeCollection->get('blog');
  153. $this->assertSame(
  154. array(
  155. '_controller' => 'AcmeBlogBundle:Blog:index',
  156. 'values' => array(array(true, 1, 3.5, 'foo')),
  157. ),
  158. $route->getDefaults()
  159. );
  160. }
  161. public function testListInMapDefaults()
  162. {
  163. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  164. $routeCollection = $loader->load('list_in_map_defaults.xml');
  165. $route = $routeCollection->get('blog');
  166. $this->assertSame(
  167. array(
  168. '_controller' => 'AcmeBlogBundle:Blog:index',
  169. 'values' => array('list' => array(true, 1, 3.5, 'foo')),
  170. ),
  171. $route->getDefaults()
  172. );
  173. }
  174. public function testMapDefaults()
  175. {
  176. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  177. $routeCollection = $loader->load('map_defaults.xml');
  178. $route = $routeCollection->get('blog');
  179. $this->assertSame(
  180. array(
  181. '_controller' => 'AcmeBlogBundle:Blog:index',
  182. 'values' => array(
  183. 'public' => true,
  184. 'page' => 1,
  185. 'price' => 3.5,
  186. 'title' => 'foo',
  187. ),
  188. ),
  189. $route->getDefaults()
  190. );
  191. }
  192. public function testMapInListDefaults()
  193. {
  194. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  195. $routeCollection = $loader->load('map_in_list_defaults.xml');
  196. $route = $routeCollection->get('blog');
  197. $this->assertSame(
  198. array(
  199. '_controller' => 'AcmeBlogBundle:Blog:index',
  200. 'values' => array(array(
  201. 'public' => true,
  202. 'page' => 1,
  203. 'price' => 3.5,
  204. 'title' => 'foo',
  205. )),
  206. ),
  207. $route->getDefaults()
  208. );
  209. }
  210. public function testMapInMapDefaults()
  211. {
  212. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  213. $routeCollection = $loader->load('map_in_map_defaults.xml');
  214. $route = $routeCollection->get('blog');
  215. $this->assertSame(
  216. array(
  217. '_controller' => 'AcmeBlogBundle:Blog:index',
  218. 'values' => array('map' => array(
  219. 'public' => true,
  220. 'page' => 1,
  221. 'price' => 3.5,
  222. 'title' => 'foo',
  223. )),
  224. ),
  225. $route->getDefaults()
  226. );
  227. }
  228. public function testNullValuesInList()
  229. {
  230. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  231. $routeCollection = $loader->load('list_null_values.xml');
  232. $route = $routeCollection->get('blog');
  233. $this->assertSame(array(null, null, null, null, null, null), $route->getDefault('list'));
  234. }
  235. public function testNullValuesInMap()
  236. {
  237. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  238. $routeCollection = $loader->load('map_null_values.xml');
  239. $route = $routeCollection->get('blog');
  240. $this->assertSame(
  241. array(
  242. 'boolean' => null,
  243. 'integer' => null,
  244. 'float' => null,
  245. 'string' => null,
  246. 'list' => null,
  247. 'map' => null,
  248. ),
  249. $route->getDefault('map')
  250. );
  251. }
  252. }