No Description

YamlFileLoaderTest.php 4.4KB

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