XmlFileLoaderTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Tests\Component\Routing\Loader;
  11. use Symfony\Component\Config\FileLocator;
  12. use Symfony\Component\Routing\Loader\XmlFileLoader;
  13. use Symfony\Component\Routing\Route;
  14. class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @covers Symfony\Component\Routing\Loader\XmlFileLoader::supports
  18. */
  19. public function testSupports()
  20. {
  21. $loader = new XmlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
  22. $this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
  23. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  24. $this->assertTrue($loader->supports('foo.xml', 'xml'), '->supports() checks the resource type if specified');
  25. $this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
  26. }
  27. public function testLoadWithRoute()
  28. {
  29. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  30. $routeCollection = $loader->load('validpattern.xml');
  31. $routes = $routeCollection->all();
  32. $this->assertEquals(1, count($routes), 'One route is loaded');
  33. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  34. }
  35. public function testLoadWithImport()
  36. {
  37. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  38. $routeCollection = $loader->load('validresource.xml');
  39. $routes = $routeCollection->all();
  40. $this->assertEquals(1, count($routes), 'One route is loaded');
  41. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  42. }
  43. /**
  44. * @expectedException \InvalidArgumentException
  45. * @dataProvider getPathsToInvalidFiles
  46. */
  47. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  48. {
  49. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  50. $loader->load($filePath);
  51. }
  52. /**
  53. * @expectedException \InvalidArgumentException
  54. * @dataProvider getPathsToInvalidFiles
  55. */
  56. public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
  57. {
  58. $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  59. $loader->load($filePath);
  60. }
  61. public function getPathsToInvalidFiles()
  62. {
  63. return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'));
  64. }
  65. /**
  66. * @expectedException \InvalidArgumentException
  67. * @expectedExceptionMessage Document types are not allowed.
  68. */
  69. public function testDocTypeIsNotAllowed()
  70. {
  71. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  72. $loader->load('withdoctype.xml');
  73. }
  74. }
  75. /**
  76. * XmlFileLoader with schema validation turned off
  77. */
  78. class CustomXmlFileLoader extends XmlFileLoader
  79. {
  80. protected function validate(\DOMDocument $dom)
  81. {
  82. return true;
  83. }
  84. }