YamlFileLoaderTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\YamlFileLoader;
  14. use Symfony\Component\Routing\Route;
  15. use Symfony\Component\Routing\RouteCollection;
  16. use Symfony\Component\Config\Resource\FileResource;
  17. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @covers Symfony\Component\Routing\Loader\YamlFileLoader::supports
  21. */
  22. public function testSupports()
  23. {
  24. $loader = new YamlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
  25. $this->assertTrue($loader->supports('foo.yml'), '->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.yml', 'yaml'), '->supports() checks the resource type if specified');
  28. $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
  29. }
  30. public function testLoadDoesNothingIfEmpty()
  31. {
  32. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  33. $collection = $loader->load('empty.yml');
  34. $this->assertEquals(array(), $collection->all());
  35. $this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
  36. }
  37. /**
  38. * @expectedException \InvalidArgumentException
  39. */
  40. public function testLoadThrowsExceptionIfNotAnArray()
  41. {
  42. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  43. $loader->load('nonvalid.yml');
  44. }
  45. /**
  46. * @expectedException \InvalidArgumentException
  47. */
  48. public function testLoadThrowsExceptionIfArrayHasUnsupportedKeys()
  49. {
  50. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  51. $loader->load('nonvalidkeys.yml');
  52. }
  53. /**
  54. * @expectedException \InvalidArgumentException
  55. */
  56. public function testLoadThrowsExceptionWhenIncomplete()
  57. {
  58. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  59. $loader->load('incomplete.yml');
  60. }
  61. public function testLoadWithPattern()
  62. {
  63. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  64. $routeCollection = $loader->load('validpattern.yml');
  65. $routes = $routeCollection->all();
  66. $this->assertEquals(1, count($routes), 'One route is loaded');
  67. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  68. }
  69. public function testLoadWithResource()
  70. {
  71. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  72. $routeCollection = $loader->load('validresource.yml');
  73. $routes = $routeCollection->all();
  74. $this->assertEquals(1, count($routes), 'One route is loaded');
  75. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  76. }
  77. /**
  78. * @expectedException \InvalidArgumentException
  79. */
  80. public function testParseRouteThrowsExceptionWithMissingPattern()
  81. {
  82. $loader = new CustomYamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  83. $loader->load('incomplete.yml');
  84. }
  85. }
  86. class CustomYamlFileLoader extends YamlFileLoader
  87. {
  88. public function load($file, $type = null)
  89. {
  90. $path = $this->locator->locate($file);
  91. $config = $this->loadFile($path);
  92. $collection = new RouteCollection();
  93. $collection->addResource(new FileResource($path));
  94. foreach ($config as $name => $config) {
  95. $this->parseRoute($collection, $name, $config, $path);
  96. }
  97. return $collection;
  98. }
  99. }