YamlFileLoaderTest.php 4.5 KB

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