XmlFileLoaderTest.php 3.0 KB

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