YamlFileLoaderTest.php 3.4 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\YamlFileLoader;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Config\Resource\FileResource;
  15. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @covers Symfony\Component\Routing\Loader\YamlFileLoader::supports
  19. */
  20. public function testSupports()
  21. {
  22. $loader = new YamlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
  23. $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
  24. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  25. $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
  26. $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
  27. }
  28. public function testLoadDoesNothingIfEmpty()
  29. {
  30. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  31. $collection = $loader->load('empty.yml');
  32. $this->assertEquals(array(), $collection->all());
  33. $this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
  34. }
  35. /**
  36. * @expectedException \InvalidArgumentException
  37. */
  38. public function testLoadThrowsExceptionIfNotAnArray()
  39. {
  40. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  41. $loader->load('nonvalid.yml');
  42. }
  43. /**
  44. * @expectedException \InvalidArgumentException
  45. */
  46. public function testLoadThrowsExceptionIfArrayHasUnsupportedKeys()
  47. {
  48. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  49. $loader->load('nonvalidkeys.yml');
  50. }
  51. /**
  52. * @expectedException \InvalidArgumentException
  53. */
  54. public function testLoadThrowsExceptionWhenIncomplete()
  55. {
  56. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  57. $loader->load('incomplete.yml');
  58. }
  59. public function testLoadWithPattern()
  60. {
  61. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  62. $routeCollection = $loader->load('validpattern.yml');
  63. $routes = $routeCollection->all();
  64. $this->assertEquals(1, count($routes), 'One route is loaded');
  65. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  66. }
  67. public function testLoadWithResource()
  68. {
  69. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  70. $routeCollection = $loader->load('validresource.yml');
  71. $routes = $routeCollection->all();
  72. $this->assertEquals(1, count($routes), 'One route is loaded');
  73. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  74. }
  75. /**
  76. * @expectedException \InvalidArgumentException
  77. */
  78. public function testParseRouteThrowsExceptionWithMissingPattern()
  79. {
  80. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  81. $loader->load('incomplete.yml');
  82. }
  83. }