XmlFileLoaderTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Symfony\Component\Config\FileLocator;
  12. use Symfony\Component\Routing\Loader\XmlFileLoader;
  13. use Symfony\Component\Routing\Tests\Fixtures\CustomXmlFileLoader;
  14. class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected function setUp()
  17. {
  18. if (!class_exists('Symfony\Component\Config\FileLocator')) {
  19. $this->markTestSkipped('The "Config" component is not available');
  20. }
  21. }
  22. public function testSupports()
  23. {
  24. $loader = new XmlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
  25. $this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
  26. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  27. $this->assertTrue($loader->supports('foo.xml', 'xml'), '->supports() checks the resource type if specified');
  28. $this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
  29. }
  30. public function testLoadWithRoute()
  31. {
  32. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  33. $routeCollection = $loader->load('validpattern.xml');
  34. $routes = $routeCollection->all();
  35. $this->assertCount(2, $routes, 'Two routes are loaded');
  36. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  37. foreach ($routes as $route) {
  38. $this->assertSame('/blog/{slug}', $route->getPath());
  39. $this->assertSame('{locale}.example.com', $route->getHost());
  40. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  41. $this->assertSame('\w+', $route->getRequirement('locale'));
  42. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  43. $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  44. $this->assertEquals(array('https'), $route->getSchemes());
  45. }
  46. }
  47. public function testLoadWithNamespacePrefix()
  48. {
  49. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  50. $routeCollection = $loader->load('namespaceprefix.xml');
  51. $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
  52. $route = $routeCollection->get('blog_show');
  53. $this->assertSame('/blog/{slug}', $route->getPath());
  54. $this->assertSame('{_locale}.example.com', $route->getHost());
  55. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  56. $this->assertSame('\w+', $route->getRequirement('slug'));
  57. $this->assertSame('en|fr|de', $route->getRequirement('_locale'));
  58. $this->assertSame(null, $route->getDefault('slug'));
  59. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  60. }
  61. public function testLoadWithImport()
  62. {
  63. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  64. $routeCollection = $loader->load('validresource.xml');
  65. $routes = $routeCollection->all();
  66. $this->assertCount(2, $routes, 'Two routes are loaded');
  67. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  68. foreach ($routes as $route) {
  69. $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
  70. $this->assertSame('123', $route->getDefault('foo'));
  71. $this->assertSame('\d+', $route->getRequirement('foo'));
  72. $this->assertSame('bar', $route->getOption('foo'));
  73. $this->assertSame('', $route->getHost());
  74. }
  75. }
  76. /**
  77. * @expectedException \InvalidArgumentException
  78. * @dataProvider getPathsToInvalidFiles
  79. */
  80. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  81. {
  82. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  83. $loader->load($filePath);
  84. }
  85. /**
  86. * @expectedException \InvalidArgumentException
  87. * @dataProvider getPathsToInvalidFiles
  88. */
  89. public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
  90. {
  91. $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  92. $loader->load($filePath);
  93. }
  94. public function getPathsToInvalidFiles()
  95. {
  96. return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
  97. }
  98. /**
  99. * @expectedException \InvalidArgumentException
  100. * @expectedExceptionMessage Document types are not allowed.
  101. */
  102. public function testDocTypeIsNotAllowed()
  103. {
  104. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  105. $loader->load('withdoctype.xml');
  106. }
  107. }