XmlFileLoaderTest.php 5.4 KB

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