PhpFileLoaderTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. $routeCollection = $loader->load('php_dsl.php');
  78. $expectedCollection = new RouteCollection();
  79. $expectedCollection->add('foo', (new Route('/foo'))
  80. ->setOptions(['utf8' => true])
  81. ->setCondition('abc')
  82. );
  83. $expectedCollection->add('buz', (new Route('/zub'))
  84. ->setDefaults(['_controller' => 'foo:act'])
  85. );
  86. $expectedCollection->add('c_bar', (new Route('/sub/pub/bar'))
  87. ->setRequirements(['id' => '\d+'])
  88. );
  89. $expectedCollection->add('c_pub_buz', (new Route('/sub/pub/buz'))
  90. ->setHost('host')
  91. ->setRequirements(['id' => '\d+'])
  92. );
  93. $expectedCollection->add('ouf', (new Route('/ouf'))
  94. ->setSchemes(['https'])
  95. ->setMethods(['GET'])
  96. ->setDefaults(['id' => 0])
  97. );
  98. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub.php')));
  99. $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
  100. $this->assertEquals($expectedCollection, $routeCollection);
  101. }
  102. public function testRoutingConfiguratorCanImportGlobPatterns()
  103. {
  104. $locator = new FileLocator([__DIR__.'/../Fixtures/glob']);
  105. $loader = new PhpFileLoader($locator);
  106. $routeCollection = $loader->load('php_dsl.php');
  107. $route = $routeCollection->get('bar_route');
  108. $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
  109. $route = $routeCollection->get('baz_route');
  110. $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
  111. }
  112. }