YamlFileLoaderTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\YamlFileLoader;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected function setUp()
  17. {
  18. if (!class_exists('Symfony\Component\Config\FileLocator')) {
  19. $this->markTestSkipped('The "Config" component is not available');
  20. }
  21. if (!class_exists('Symfony\Component\Yaml\Yaml')) {
  22. $this->markTestSkipped('The "Yaml" component is not available');
  23. }
  24. }
  25. public function testSupports()
  26. {
  27. $loader = new YamlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
  28. $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
  29. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  30. $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
  31. $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
  32. }
  33. public function testLoadDoesNothingIfEmpty()
  34. {
  35. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  36. $collection = $loader->load('empty.yml');
  37. $this->assertEquals(array(), $collection->all());
  38. $this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
  39. }
  40. /**
  41. * @expectedException \InvalidArgumentException
  42. * @dataProvider getPathsToInvalidFiles
  43. */
  44. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  45. {
  46. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  47. $loader->load($filePath);
  48. }
  49. public function getPathsToInvalidFiles()
  50. {
  51. return array(array('nonvalid.yml'), array('nonvalid2.yml'), array('incomplete.yml'), array('nonvalidkeys.yml'), array('nonesense_resource_plus_path.yml'), array('nonesense_type_without_resource.yml'));
  52. }
  53. public function testLoadSpecialRouteName()
  54. {
  55. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  56. $routeCollection = $loader->load('special_route_name.yml');
  57. $route = $routeCollection->get('#$péß^a|');
  58. $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
  59. $this->assertSame('/true', $route->getPath());
  60. }
  61. public function testLoadWithRoute()
  62. {
  63. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  64. $routeCollection = $loader->load('validpattern.yml');
  65. $routes = $routeCollection->all();
  66. $this->assertCount(2, $routes, 'Two routes are loaded');
  67. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  68. foreach ($routes as $route) {
  69. $this->assertSame('/blog/{slug}', $route->getPath());
  70. $this->assertSame('{locale}.example.com', $route->getHost());
  71. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  72. $this->assertSame('\w+', $route->getRequirement('locale'));
  73. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  74. $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  75. $this->assertEquals(array('https'), $route->getSchemes());
  76. }
  77. }
  78. public function testLoadWithResource()
  79. {
  80. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  81. $routeCollection = $loader->load('validresource.yml');
  82. $routes = $routeCollection->all();
  83. $this->assertCount(2, $routes, 'Two routes are loaded');
  84. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  85. foreach ($routes as $route) {
  86. $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
  87. $this->assertSame('123', $route->getDefault('foo'));
  88. $this->assertSame('\d+', $route->getRequirement('foo'));
  89. $this->assertSame('bar', $route->getOption('foo'));
  90. $this->assertSame('', $route->getHost());
  91. }
  92. }
  93. }