PhpFileLoaderTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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->assertSame('{locale}.example.com', $route->getHost());
  38. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  39. $this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  40. $this->assertEquals(['https'], $route->getSchemes());
  41. }
  42. }
  43. public function testLoadWithImport()
  44. {
  45. $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
  46. $routeCollection = $loader->load('validresource.php');
  47. $routes = $routeCollection->all();
  48. $this->assertCount(1, $routes, 'One route is loaded');
  49. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  50. foreach ($routes as $route) {
  51. $this->assertSame('/prefix/blog/{slug}', $route->getPath());
  52. $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
  53. $this->assertSame('{locale}.example.com', $route->getHost());
  54. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  55. $this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
  56. $this->assertEquals(['https'], $route->getSchemes());
  57. }
  58. }
  59. public function testThatDefiningVariableInConfigFileHasNoSideEffects()
  60. {
  61. $locator = new FileLocator([__DIR__.'/../Fixtures']);
  62. $loader = new PhpFileLoader($locator);
  63. $routeCollection = $loader->load('with_define_path_variable.php');
  64. $resources = $routeCollection->getResources();
  65. $this->assertCount(1, $resources);
  66. $this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
  67. $fileResource = reset($resources);
  68. $this->assertSame(
  69. realpath($locator->locate('with_define_path_variable.php')),
  70. (string) $fileResource
  71. );
  72. }
  73. public function testRoutingConfigurator()
  74. {
  75. $locator = new FileLocator([__DIR__.'/../Fixtures']);
  76. $loader = new PhpFileLoader($locator);
  77. $routeCollectionClosure = $loader->load('php_dsl.php');
  78. $routeCollectionObject = $loader->load('php_object_dsl.php');
  79. $expectedCollection = new RouteCollection();
  80. $expectedCollection->add('foo', (new Route('/foo'))
  81. ->setOptions(['utf8' => true])
  82. ->setCondition('abc')
  83. );
  84. $expectedCollection->add('buz', (new Route('/zub'))
  85. ->setDefaults(['_controller' => 'foo:act'])
  86. );
  87. $expectedCollection->add('c_root', (new Route('/sub/pub/'))
  88. ->setRequirements(['id' => '\d+'])
  89. );
  90. $expectedCollection->add('c_bar', (new Route('/sub/pub/bar'))
  91. ->setRequirements(['id' => '\d+'])
  92. );
  93. $expectedCollection->add('c_pub_buz', (new Route('/sub/pub/buz'))
  94. ->setHost('host')
  95. ->setRequirements(['id' => '\d+'])
  96. );
  97. $expectedCollection->add('z_c_root', new Route('/zub/pub/'));
  98. $expectedCollection->add('z_c_bar', new Route('/zub/pub/bar'));
  99. $expectedCollection->add('z_c_pub_buz', (new Route('/zub/pub/buz'))->setHost('host'));
  100. $expectedCollection->add('r_root', new Route('/bus'));
  101. $expectedCollection->add('r_bar', new Route('/bus/bar/'));
  102. $expectedCollection->add('ouf', (new Route('/ouf'))
  103. ->setSchemes(['https'])
  104. ->setMethods(['GET'])
  105. ->setDefaults(['id' => 0])
  106. );
  107. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub.php')));
  108. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_root.php')));
  109. $expectedCollectionClosure = $expectedCollection;
  110. $expectedCollectionObject = clone $expectedCollection;
  111. $expectedCollectionClosure->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
  112. $expectedCollectionObject->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_object_dsl.php')));
  113. $this->assertEquals($expectedCollectionClosure, $routeCollectionClosure);
  114. $this->assertEquals($expectedCollectionObject, $routeCollectionObject);
  115. }
  116. public function testRoutingConfiguratorCanImportGlobPatterns()
  117. {
  118. $locator = new FileLocator([__DIR__.'/../Fixtures/glob']);
  119. $loader = new PhpFileLoader($locator);
  120. $routeCollection = $loader->load('php_dsl.php');
  121. $route = $routeCollection->get('bar_route');
  122. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  123. $route = $routeCollection->get('baz_route');
  124. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  125. }
  126. public function testRoutingI18nConfigurator()
  127. {
  128. $locator = new FileLocator([__DIR__.'/../Fixtures']);
  129. $loader = new PhpFileLoader($locator);
  130. $routeCollection = $loader->load('php_dsl_i18n.php');
  131. $expectedCollection = new RouteCollection();
  132. $expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo']));
  133. $expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar']));
  134. $expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz']));
  135. $expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo']));
  136. $expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar']));
  137. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_i18n.php')));
  138. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php')));
  139. $this->assertEquals($expectedCollection, $routeCollection);
  140. }
  141. }