YamlFileLoaderTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Config\Resource\FileResource;
  14. use Symfony\Component\Routing\Loader\YamlFileLoader;
  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([__DIR__.'/../Fixtures']));
  30. $collection = $loader->load('empty.yml');
  31. $this->assertEquals([], $collection->all());
  32. $this->assertEquals([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([__DIR__.'/../Fixtures']));
  41. $loader->load($filePath);
  42. }
  43. public function getPathsToInvalidFiles()
  44. {
  45. return [
  46. ['nonvalid.yml'],
  47. ['nonvalid2.yml'],
  48. ['incomplete.yml'],
  49. ['nonvalidkeys.yml'],
  50. ['nonesense_resource_plus_path.yml'],
  51. ['nonesense_type_without_resource.yml'],
  52. ['bad_format.yml'],
  53. ];
  54. }
  55. public function testLoadSpecialRouteName()
  56. {
  57. $loader = new YamlFileLoader(new FileLocator([__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([__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(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  75. $this->assertEquals(['https'], $route->getSchemes());
  76. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  77. }
  78. public function testLoadWithResource()
  79. {
  80. $loader = new YamlFileLoader(new FileLocator([__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. public function testLoadRouteWithControllerAttribute()
  95. {
  96. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  97. $routeCollection = $loader->load('routing.yml');
  98. $route = $routeCollection->get('app_homepage');
  99. $this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
  100. }
  101. public function testLoadRouteWithoutControllerAttribute()
  102. {
  103. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  104. $routeCollection = $loader->load('routing.yml');
  105. $route = $routeCollection->get('app_logout');
  106. $this->assertNull($route->getDefault('_controller'));
  107. }
  108. public function testLoadRouteWithControllerSetInDefaults()
  109. {
  110. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  111. $routeCollection = $loader->load('routing.yml');
  112. $route = $routeCollection->get('app_blog');
  113. $this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
  114. }
  115. /**
  116. * @expectedException \InvalidArgumentException
  117. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "app_blog"/
  118. */
  119. public function testOverrideControllerInDefaults()
  120. {
  121. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  122. $loader->load('override_defaults.yml');
  123. }
  124. /**
  125. * @dataProvider provideFilesImportingRoutesWithControllers
  126. */
  127. public function testImportRouteWithController($file)
  128. {
  129. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  130. $routeCollection = $loader->load($file);
  131. $route = $routeCollection->get('app_homepage');
  132. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  133. $route = $routeCollection->get('app_blog');
  134. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  135. $route = $routeCollection->get('app_logout');
  136. $this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
  137. }
  138. public function provideFilesImportingRoutesWithControllers()
  139. {
  140. yield ['import_controller.yml'];
  141. yield ['import__controller.yml'];
  142. }
  143. /**
  144. * @expectedException \InvalidArgumentException
  145. * @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "_static"/
  146. */
  147. public function testImportWithOverriddenController()
  148. {
  149. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
  150. $loader->load('import_override_defaults.yml');
  151. }
  152. public function testImportRouteWithGlobMatchingSingleFile()
  153. {
  154. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  155. $routeCollection = $loader->load('import_single.yml');
  156. $route = $routeCollection->get('bar_route');
  157. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  158. }
  159. public function testImportRouteWithGlobMatchingMultipleFiles()
  160. {
  161. $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/glob']));
  162. $routeCollection = $loader->load('import_multiple.yml');
  163. $route = $routeCollection->get('bar_route');
  164. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  165. $route = $routeCollection->get('baz_route');
  166. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  167. }
  168. }