PhpFileLoaderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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\PhpFileLoader;
  15. use Symfony\Component\Routing\Route;
  16. use Symfony\Component\Routing\RouteCollection;
  17. class PhpFileLoaderTest extends TestCase
  18. {
  19. public function testSupports()
  20. {
  21. $loader = new PhpFileLoader($this->createMock(FileLocator::class));
  22. $this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
  23. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  24. $this->assertTrue($loader->supports('foo.php', 'php'), '->supports() checks the resource type if specified');
  25. $this->assertFalse($loader->supports('foo.php', 'foo'), '->supports() checks the resource type if specified');
  26. }
  27. public function testLoadWithRoute()
  28. {
  29. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  30. $routeCollection = $loader->load('validpattern.php');
  31. $routes = $routeCollection->all();
  32. $this->assertCount(1, $routes, 'One route is loaded');
  33. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  34. foreach ($routes as $route) {
  35. $this->assertSame('/blog/{slug}', $route->getPath());
  36. $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
  37. $this->assertTrue($route->getDefault('_stateless'));
  38. $this->assertSame('{locale}.example.com', $route->getHost());
  39. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  40. $this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  41. $this->assertEquals(['https'], $route->getSchemes());
  42. }
  43. }
  44. public function testLoadWithImport()
  45. {
  46. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  47. $routeCollection = $loader->load('validresource.php');
  48. $routes = $routeCollection->all();
  49. $this->assertCount(1, $routes, 'One route is loaded');
  50. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  51. foreach ($routes as $route) {
  52. $this->assertSame('/prefix/blog/{slug}', $route->getPath());
  53. $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
  54. $this->assertSame('{locale}.example.com', $route->getHost());
  55. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  56. $this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  57. $this->assertEquals(['https'], $route->getSchemes());
  58. }
  59. }
  60. public function testThatDefiningVariableInConfigFileHasNoSideEffects()
  61. {
  62. $locator = new FileLocator([__DIR__.'/../Fixtures']);
  63. $loader = new PhpFileLoader($locator);
  64. $routeCollection = $loader->load('with_define_path_variable.php');
  65. $resources = $routeCollection->getResources();
  66. $this->assertCount(1, $resources);
  67. $this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
  68. $fileResource = reset($resources);
  69. $this->assertSame(
  70. realpath($locator->locate('with_define_path_variable.php')),
  71. (string) $fileResource
  72. );
  73. }
  74. public function testLoadingRouteWithDefaults()
  75. {
  76. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  77. $routes = $loader->load('defaults.php');
  78. $this->assertCount(1, $routes);
  79. $defaultsRoute = $routes->get('defaults');
  80. $this->assertSame('/defaults', $defaultsRoute->getPath());
  81. $this->assertSame('en', $defaultsRoute->getDefault('_locale'));
  82. $this->assertSame('html', $defaultsRoute->getDefault('_format'));
  83. }
  84. public function testLoadingImportedRoutesWithDefaults()
  85. {
  86. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  87. $routes = $loader->load('importer-with-defaults.php');
  88. $this->assertCount(2, $routes);
  89. $expectedRoutes = new RouteCollection();
  90. $expectedRoutes->add('one', $localeRoute = new Route('/defaults/one'));
  91. $localeRoute->setDefault('_locale', 'g_locale');
  92. $localeRoute->setDefault('_format', 'g_format');
  93. $localeRoute->setDefault('_stateless', true);
  94. $expectedRoutes->add('two', $formatRoute = new Route('/defaults/two'));
  95. $formatRoute->setDefault('_locale', 'g_locale');
  96. $formatRoute->setDefault('_format', 'g_format');
  97. $formatRoute->setDefault('_stateless', true);
  98. $formatRoute->setDefault('specific', 'imported');
  99. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/imported-with-defaults.php'));
  100. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/importer-with-defaults.php'));
  101. $this->assertEquals($expectedRoutes, $routes);
  102. }
  103. public function testLoadingUtf8Route()
  104. {
  105. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  106. $routes = $loader->load('utf8.php');
  107. $this->assertCount(2, $routes);
  108. $expectedRoutes = new RouteCollection();
  109. $expectedRoutes->add('some_route', new Route('/'));
  110. $expectedRoutes->add('some_utf8_route', $route = new Route('/utf8'));
  111. $route->setOption('utf8', true);
  112. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/utf8.php'));
  113. $this->assertEquals($expectedRoutes, $routes);
  114. }
  115. public function testLoadingUtf8ImportedRoutes()
  116. {
  117. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
  118. $routes = $loader->load('importer-with-utf8.php');
  119. $this->assertCount(2, $routes);
  120. $expectedRoutes = new RouteCollection();
  121. $expectedRoutes->add('utf8_one', $one = new Route('/one'));
  122. $one->setOption('utf8', true);
  123. $expectedRoutes->add('utf8_two', $two = new Route('/two'));
  124. $two->setOption('utf8', true);
  125. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/imported-with-utf8.php'));
  126. $expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/importer-with-utf8.php'));
  127. $this->assertEquals($expectedRoutes, $routes);
  128. }
  129. public function testRoutingConfigurator()
  130. {
  131. $locator = new FileLocator([__DIR__.'/../Fixtures']);
  132. $loader = new PhpFileLoader($locator);
  133. $routeCollectionClosure = $loader->load('php_dsl.php');
  134. $routeCollectionObject = $loader->load('php_object_dsl.php');
  135. $expectedCollection = new RouteCollection();
  136. $expectedCollection->add('foo', (new Route('/foo'))
  137. ->setOptions(['utf8' => true])
  138. ->setCondition('abc')
  139. );
  140. $expectedCollection->add('buz', (new Route('/zub'))
  141. ->setDefaults(['_controller' => 'foo:act', '_stateless' => true])
  142. );
  143. $expectedCollection->add('controller_class', (new Route('/controller'))
  144. ->setDefaults(['_controller' => ['Acme\MyApp\MyController', 'myAction']])
  145. );
  146. $expectedCollection->add('c_root', (new Route('/sub/pub/'))
  147. ->setRequirements(['id' => '\d+'])
  148. );
  149. $expectedCollection->add('c_bar', (new Route('/sub/pub/bar'))
  150. ->setRequirements(['id' => '\d+'])
  151. );
  152. $expectedCollection->add('c_pub_buz', (new Route('/sub/pub/buz'))
  153. ->setHost('host')
  154. ->setRequirements(['id' => '\d+'])
  155. );
  156. $expectedCollection->add('z_c_root', new Route('/zub/pub/'));
  157. $expectedCollection->add('z_c_bar', new Route('/zub/pub/bar'));
  158. $expectedCollection->add('z_c_pub_buz', (new Route('/zub/pub/buz'))->setHost('host'));
  159. $expectedCollection->add('r_root', new Route('/bus'));
  160. $expectedCollection->add('r_bar', new Route('/bus/bar/'));
  161. $expectedCollection->add('ouf', (new Route('/ouf'))
  162. ->setSchemes(['https'])
  163. ->setMethods(['GET'])
  164. ->setDefaults(['id' => 0])
  165. );
  166. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub.php')));
  167. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_root.php')));
  168. $expectedCollectionClosure = $expectedCollection;
  169. $expectedCollectionObject = clone $expectedCollection;
  170. $expectedCollectionClosure->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
  171. $expectedCollectionObject->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_object_dsl.php')));
  172. $this->assertEquals($expectedCollectionClosure, $routeCollectionClosure);
  173. $this->assertEquals($expectedCollectionObject, $routeCollectionObject);
  174. }
  175. public function testRoutingConfiguratorCanImportGlobPatterns()
  176. {
  177. $locator = new FileLocator([__DIR__.'/../Fixtures/glob']);
  178. $loader = new PhpFileLoader($locator);
  179. $routeCollection = $loader->load('php_dsl.php');
  180. $route = $routeCollection->get('bar_route');
  181. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  182. $route = $routeCollection->get('baz_route');
  183. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  184. }
  185. public function testRoutingI18nConfigurator()
  186. {
  187. $locator = new FileLocator([__DIR__.'/../Fixtures']);
  188. $loader = new PhpFileLoader($locator);
  189. $routeCollection = $loader->load('php_dsl_i18n.php');
  190. $expectedCollection = new RouteCollection();
  191. $expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo'])->setRequirement('_locale', 'en'));
  192. $expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar'])->setRequirement('_locale', 'en'));
  193. $expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz'])->setRequirement('_locale', 'en'));
  194. $expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo'])->setRequirement('_locale', 'fr'));
  195. $expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar'])->setRequirement('_locale', 'fr'));
  196. $expectedCollection->add('non_localized.fr', (new Route('/ench/non-localized'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'non_localized'])->setRequirement('_locale', 'fr'));
  197. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_i18n.php')));
  198. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php')));
  199. $this->assertEquals($expectedCollection, $routeCollection);
  200. }
  201. public function testImportingRoutesWithHostsInImporter()
  202. {
  203. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  204. $routes = $loader->load('importer-with-host.php');
  205. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php';
  206. $this->assertEquals($expectedRoutes('php'), $routes);
  207. }
  208. public function testImportingRoutesWithLocalesAndHostInImporter()
  209. {
  210. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  211. $routes = $loader->load('importer-with-locale-and-host.php');
  212. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php';
  213. $this->assertEquals($expectedRoutes('php'), $routes);
  214. }
  215. public function testImportingRoutesWithoutHostInImporter()
  216. {
  217. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  218. $routes = $loader->load('importer-without-host.php');
  219. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php';
  220. $this->assertEquals($expectedRoutes('php'), $routes);
  221. }
  222. public function testImportingRoutesWithSingleHostInImporter()
  223. {
  224. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  225. $routes = $loader->load('importer-with-single-host.php');
  226. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php';
  227. $this->assertEquals($expectedRoutes('php'), $routes);
  228. }
  229. public function testImportingAliases()
  230. {
  231. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/alias']));
  232. $routes = $loader->load('alias.php');
  233. $expectedRoutes = require __DIR__.'/../Fixtures/alias/expected.php';
  234. $this->assertEquals($expectedRoutes('php'), $routes);
  235. }
  236. }