YamlFileLoaderTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\Routing\Loader\YamlFileLoader;
  14. use Symfony\Component\Config\Resource\FileResource;
  15. class YamlFileLoaderTest extends TestCase
  16. {
  17. public function testSupports()
  18. {
  19. $loader = new YamlFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
  20. $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
  21. $this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
  22. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  23. $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
  24. $this->assertTrue($loader->supports('foo.yaml', 'yaml'), '->supports() checks the resource type if specified');
  25. $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
  26. }
  27. public function testLoadDoesNothingIfEmpty()
  28. {
  29. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  30. $collection = $loader->load('empty.yml');
  31. $this->assertEquals(array(), $collection->all());
  32. $this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
  33. }
  34. /**
  35. * @expectedException \InvalidArgumentException
  36. * @dataProvider getPathsToInvalidFiles
  37. */
  38. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  39. {
  40. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  41. $loader->load($filePath);
  42. }
  43. public function getPathsToInvalidFiles()
  44. {
  45. return array(
  46. array('nonvalid.yml'),
  47. array('nonvalid2.yml'),
  48. array('incomplete.yml'),
  49. array('nonvalidkeys.yml'),
  50. array('nonesense_resource_plus_path.yml'),
  51. array('nonesense_type_without_resource.yml'),
  52. array('bad_format.yml'),
  53. );
  54. }
  55. public function testLoadSpecialRouteName()
  56. {
  57. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  58. $routeCollection = $loader->load('special_route_name.yml');
  59. $route = $routeCollection->get('#$péß^a|');
  60. $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
  61. $this->assertSame('/true', $route->getPath());
  62. }
  63. public function testLoadWithRoute()
  64. {
  65. $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  66. $routeCollection = $loader->load('validpattern.yml');
  67. $route = $routeCollection->get('blog_show');
  68. $this->assertInstanceOf('Symfony\Component\Routing\Route', $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. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  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. $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
  92. }
  93. }
  94. }