RouterTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Loader\LoaderInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Generator\CompiledUrlGenerator;
  15. use Symfony\Component\Routing\Generator\UrlGenerator;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  18. use Symfony\Component\Routing\Matcher\UrlMatcher;
  19. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  20. use Symfony\Component\Routing\RouteCollection;
  21. use Symfony\Component\Routing\Router;
  22. class RouterTest extends TestCase
  23. {
  24. private $router = null;
  25. private $loader = null;
  26. private $cacheDir;
  27. protected function setUp(): void
  28. {
  29. $this->loader = $this->createMock(LoaderInterface::class);
  30. $this->router = new Router($this->loader, 'routing.yml');
  31. $this->cacheDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('router_', true);
  32. }
  33. protected function tearDown(): void
  34. {
  35. if (is_dir($this->cacheDir)) {
  36. array_map('unlink', glob($this->cacheDir.\DIRECTORY_SEPARATOR.'*'));
  37. @rmdir($this->cacheDir);
  38. }
  39. $this->loader = null;
  40. $this->router = null;
  41. $this->cacheDir = null;
  42. }
  43. public function testSetOptionsWithSupportedOptions()
  44. {
  45. $this->router->setOptions([
  46. 'cache_dir' => './cache',
  47. 'debug' => true,
  48. 'resource_type' => 'ResourceType',
  49. ]);
  50. $this->assertSame('./cache', $this->router->getOption('cache_dir'));
  51. $this->assertTrue($this->router->getOption('debug'));
  52. $this->assertSame('ResourceType', $this->router->getOption('resource_type'));
  53. }
  54. public function testSetOptionsWithUnsupportedOptions()
  55. {
  56. $this->expectException(\InvalidArgumentException::class);
  57. $this->expectExceptionMessage('The Router does not support the following options: "option_foo", "option_bar"');
  58. $this->router->setOptions([
  59. 'cache_dir' => './cache',
  60. 'option_foo' => true,
  61. 'option_bar' => 'baz',
  62. 'resource_type' => 'ResourceType',
  63. ]);
  64. }
  65. public function testSetOptionWithSupportedOption()
  66. {
  67. $this->router->setOption('cache_dir', './cache');
  68. $this->assertSame('./cache', $this->router->getOption('cache_dir'));
  69. }
  70. public function testSetOptionWithUnsupportedOption()
  71. {
  72. $this->expectException(\InvalidArgumentException::class);
  73. $this->expectExceptionMessage('The Router does not support the "option_foo" option');
  74. $this->router->setOption('option_foo', true);
  75. }
  76. public function testGetOptionWithUnsupportedOption()
  77. {
  78. $this->expectException(\InvalidArgumentException::class);
  79. $this->expectExceptionMessage('The Router does not support the "option_foo" option');
  80. $this->router->getOption('option_foo', true);
  81. }
  82. public function testThatRouteCollectionIsLoaded()
  83. {
  84. $this->router->setOption('resource_type', 'ResourceType');
  85. $routeCollection = new RouteCollection();
  86. $this->loader->expects($this->once())
  87. ->method('load')->with('routing.yml', 'ResourceType')
  88. ->willReturn($routeCollection);
  89. $this->assertSame($routeCollection, $this->router->getRouteCollection());
  90. }
  91. public function testMatcherIsCreatedIfCacheIsNotConfigured()
  92. {
  93. $this->router->setOption('cache_dir', null);
  94. $this->loader->expects($this->once())
  95. ->method('load')->with('routing.yml', null)
  96. ->willReturn(new RouteCollection());
  97. $this->assertInstanceOf(UrlMatcher::class, $this->router->getMatcher());
  98. }
  99. public function testGeneratorIsCreatedIfCacheIsNotConfigured()
  100. {
  101. $this->router->setOption('cache_dir', null);
  102. $this->loader->expects($this->once())
  103. ->method('load')->with('routing.yml', null)
  104. ->willReturn(new RouteCollection());
  105. $this->assertInstanceOf(CompiledUrlGenerator::class, $this->router->getGenerator());
  106. }
  107. public function testGeneratorIsCreatedIfCacheIsNotConfiguredNotCompiled()
  108. {
  109. $this->router->setOption('cache_dir', null);
  110. $this->router->setOption('generator_class', UrlGenerator::class);
  111. $this->loader->expects($this->once())
  112. ->method('load')->with('routing.yml', null)
  113. ->willReturn(new RouteCollection());
  114. $this->assertInstanceOf(UrlGenerator::class, $this->router->getGenerator());
  115. $this->assertNotInstanceOf(CompiledUrlGenerator::class, $this->router->getGenerator());
  116. }
  117. public function testMatchRequestWithUrlMatcherInterface()
  118. {
  119. $matcher = $this->createMock(UrlMatcherInterface::class);
  120. $matcher->expects($this->once())->method('match');
  121. $p = new \ReflectionProperty($this->router, 'matcher');
  122. $p->setAccessible(true);
  123. $p->setValue($this->router, $matcher);
  124. $this->router->matchRequest(Request::create('/'));
  125. }
  126. public function testMatchRequestWithRequestMatcherInterface()
  127. {
  128. $matcher = $this->createMock(RequestMatcherInterface::class);
  129. $matcher->expects($this->once())->method('matchRequest');
  130. $p = new \ReflectionProperty($this->router, 'matcher');
  131. $p->setAccessible(true);
  132. $p->setValue($this->router, $matcher);
  133. $this->router->matchRequest(Request::create('/'));
  134. }
  135. public function testDefaultLocaleIsPassedToGeneratorClass()
  136. {
  137. $this->loader->expects($this->once())
  138. ->method('load')->with('routing.yml', null)
  139. ->willReturn(new RouteCollection());
  140. $router = new Router($this->loader, 'routing.yml', [
  141. 'cache_dir' => null,
  142. ], null, null, 'hr');
  143. $generator = $router->getGenerator();
  144. $this->assertInstanceOf(UrlGeneratorInterface::class, $generator);
  145. $p = new \ReflectionProperty($generator, 'defaultLocale');
  146. $p->setAccessible(true);
  147. $this->assertSame('hr', $p->getValue($generator));
  148. }
  149. public function testDefaultLocaleIsPassedToCompiledGeneratorCacheClass()
  150. {
  151. $this->loader->expects($this->once())
  152. ->method('load')->with('routing.yml', null)
  153. ->willReturn(new RouteCollection());
  154. $router = new Router($this->loader, 'routing.yml', [
  155. 'cache_dir' => $this->cacheDir,
  156. ], null, null, 'hr');
  157. $generator = $router->getGenerator();
  158. $this->assertInstanceOf(UrlGeneratorInterface::class, $generator);
  159. $p = new \ReflectionProperty($generator, 'defaultLocale');
  160. $p->setAccessible(true);
  161. $this->assertSame('hr', $p->getValue($generator));
  162. }
  163. }