PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

http://github.com/symfony/symfony
PHP | 287 lines | 218 code | 61 blank | 8 comment | 0 complexity | 8fa3fc33ad561c80d71ede9020339ceb MD5 | raw file
  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->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
  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('c_root', (new Route('/sub/pub/'))
  144. ->setRequirements(['id' => '\d+'])
  145. );
  146. $expectedCollection->add('c_bar', (new Route('/sub/pub/bar'))
  147. ->setRequirements(['id' => '\d+'])
  148. );
  149. $expectedCollection->add('c_pub_buz', (new Route('/sub/pub/buz'))
  150. ->setHost('host')
  151. ->setRequirements(['id' => '\d+'])
  152. );
  153. $expectedCollection->add('z_c_root', new Route('/zub/pub/'));
  154. $expectedCollection->add('z_c_bar', new Route('/zub/pub/bar'));
  155. $expectedCollection->add('z_c_pub_buz', (new Route('/zub/pub/buz'))->setHost('host'));
  156. $expectedCollection->add('r_root', new Route('/bus'));
  157. $expectedCollection->add('r_bar', new Route('/bus/bar/'));
  158. $expectedCollection->add('ouf', (new Route('/ouf'))
  159. ->setSchemes(['https'])
  160. ->setMethods(['GET'])
  161. ->setDefaults(['id' => 0])
  162. );
  163. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub.php')));
  164. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_root.php')));
  165. $expectedCollectionClosure = $expectedCollection;
  166. $expectedCollectionObject = clone $expectedCollection;
  167. $expectedCollectionClosure->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
  168. $expectedCollectionObject->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_object_dsl.php')));
  169. $this->assertEquals($expectedCollectionClosure, $routeCollectionClosure);
  170. $this->assertEquals($expectedCollectionObject, $routeCollectionObject);
  171. }
  172. public function testRoutingConfiguratorCanImportGlobPatterns()
  173. {
  174. $locator = new FileLocator([__DIR__.'/../Fixtures/glob']);
  175. $loader = new PhpFileLoader($locator);
  176. $routeCollection = $loader->load('php_dsl.php');
  177. $route = $routeCollection->get('bar_route');
  178. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  179. $route = $routeCollection->get('baz_route');
  180. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  181. }
  182. public function testRoutingI18nConfigurator()
  183. {
  184. $locator = new FileLocator([__DIR__.'/../Fixtures']);
  185. $loader = new PhpFileLoader($locator);
  186. $routeCollection = $loader->load('php_dsl_i18n.php');
  187. $expectedCollection = new RouteCollection();
  188. $expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo'])->setRequirement('_locale', 'en'));
  189. $expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar'])->setRequirement('_locale', 'en'));
  190. $expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz'])->setRequirement('_locale', 'en'));
  191. $expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo'])->setRequirement('_locale', 'fr'));
  192. $expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar'])->setRequirement('_locale', 'fr'));
  193. $expectedCollection->add('non_localized.fr', (new Route('/ench/non-localized'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'non_localized'])->setRequirement('_locale', 'fr'));
  194. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_i18n.php')));
  195. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php')));
  196. $this->assertEquals($expectedCollection, $routeCollection);
  197. }
  198. public function testImportingRoutesWithHostsInImporter()
  199. {
  200. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  201. $routes = $loader->load('importer-with-host.php');
  202. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php';
  203. $this->assertEquals($expectedRoutes('php'), $routes);
  204. }
  205. public function testImportingRoutesWithLocalesAndHostInImporter()
  206. {
  207. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  208. $routes = $loader->load('importer-with-locale-and-host.php');
  209. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php';
  210. $this->assertEquals($expectedRoutes('php'), $routes);
  211. }
  212. public function testImportingRoutesWithoutHostInImporter()
  213. {
  214. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  215. $routes = $loader->load('importer-without-host.php');
  216. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php';
  217. $this->assertEquals($expectedRoutes('php'), $routes);
  218. }
  219. public function testImportingRoutesWithSingleHostInImporter()
  220. {
  221. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
  222. $routes = $loader->load('importer-with-single-host.php');
  223. $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php';
  224. $this->assertEquals($expectedRoutes('php'), $routes);
  225. }
  226. }