XmlFileLoaderTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  41. * @group legacy
  42. */
  43. public function testLegacyRouteDefinitionLoading()
  44. {
  45. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  46. $routeCollection = $loader->load('legacy_validpattern.xml');
  47. $route = $routeCollection->get('blog_show_legacy');
  48. $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
  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('locale'));
  53. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  54. $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  55. $this->assertEquals(array('https'), $route->getSchemes());
  56. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  57. }
  58. public function testLoadWithNamespacePrefix()
  59. {
  60. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  61. $routeCollection = $loader->load('namespaceprefix.xml');
  62. $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
  63. $route = $routeCollection->get('blog_show');
  64. $this->assertSame('/blog/{slug}', $route->getPath());
  65. $this->assertSame('{_locale}.example.com', $route->getHost());
  66. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  67. $this->assertSame('\w+', $route->getRequirement('slug'));
  68. $this->assertSame('en|fr|de', $route->getRequirement('_locale'));
  69. $this->assertNull($route->getDefault('slug'));
  70. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  71. }
  72. public function testLoadWithImport()
  73. {
  74. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  75. $routeCollection = $loader->load('validresource.xml');
  76. $routes = $routeCollection->all();
  77. $this->assertCount(2, $routes, 'Two routes are loaded');
  78. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  79. foreach ($routes as $route) {
  80. $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
  81. $this->assertSame('123', $route->getDefault('foo'));
  82. $this->assertSame('\d+', $route->getRequirement('foo'));
  83. $this->assertSame('bar', $route->getOption('foo'));
  84. $this->assertSame('', $route->getHost());
  85. $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
  86. }
  87. }
  88. /**
  89. * @expectedException \InvalidArgumentException
  90. * @dataProvider getPathsToInvalidFiles
  91. */
  92. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  93. {
  94. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  95. $loader->load($filePath);
  96. }
  97. /**
  98. * @expectedException \InvalidArgumentException
  99. * @dataProvider getPathsToInvalidFiles
  100. */
  101. public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
  102. {
  103. $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  104. $loader->load($filePath);
  105. }
  106. public function getPathsToInvalidFiles()
  107. {
  108. return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
  109. }
  110. /**
  111. * @expectedException \InvalidArgumentException
  112. * @expectedExceptionMessage Document types are not allowed.
  113. */
  114. public function testDocTypeIsNotAllowed()
  115. {
  116. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  117. $loader->load('withdoctype.xml');
  118. }
  119. public function testNullValues()
  120. {
  121. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  122. $routeCollection = $loader->load('null_values.xml');
  123. $route = $routeCollection->get('blog_show');
  124. $this->assertTrue($route->hasDefault('foo'));
  125. $this->assertNull($route->getDefault('foo'));
  126. $this->assertTrue($route->hasDefault('bar'));
  127. $this->assertNull($route->getDefault('bar'));
  128. $this->assertEquals('foo', $route->getDefault('foobar'));
  129. $this->assertEquals('bar', $route->getDefault('baz'));
  130. }
  131. }