diff --git a/src/Symfony/Component/Routing/Router.php b/src/Symfony/Component/Routing/Router.php index 5f8d9ffb159d..c4fb3e2151bd 100644 --- a/src/Symfony/Component/Routing/Router.php +++ b/src/Symfony/Component/Routing/Router.php @@ -369,7 +369,7 @@ function (ConfigCacheInterface $cache) { ); if ($compiled) { - $this->generator = new $this->options['generator_class'](require $cache->getPath(), $this->context, $this->logger); + $this->generator = new $this->options['generator_class'](require $cache->getPath(), $this->context, $this->logger, $this->defaultLocale); } else { if (!class_exists($this->options['generator_cache_class'], false)) { require_once $cache->getPath(); diff --git a/src/Symfony/Component/Routing/Tests/RouterTest.php b/src/Symfony/Component/Routing/Tests/RouterTest.php index fa0a2f5399ac..0d7c4bee456c 100644 --- a/src/Symfony/Component/Routing/Tests/RouterTest.php +++ b/src/Symfony/Component/Routing/Tests/RouterTest.php @@ -22,10 +22,26 @@ class RouterTest extends TestCase private $loader = null; + private $cacheDir; + protected function setUp(): void { $this->loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock(); $this->router = new Router($this->loader, 'routing.yml'); + + $this->cacheDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('router_', true); + } + + protected function tearDown(): void + { + if (is_dir($this->cacheDir)) { + array_map('unlink', glob($this->cacheDir.\DIRECTORY_SEPARATOR.'*')); + rmdir($this->cacheDir); + } + + $this->loader = null; + $this->router = null; + $this->cacheDir = null; } public function testSetOptionsWithSupportedOptions() @@ -132,4 +148,68 @@ public function testMatchRequestWithRequestMatcherInterface() $this->router->matchRequest(Request::create('/')); } + + public function testDefaultLocaleIsPassedToGeneratorClass() + { + $this->loader->expects($this->once()) + ->method('load')->with('routing.yml', null) + ->willReturn(new RouteCollection()); + + $router = new Router($this->loader, 'routing.yml', [ + 'cache_dir' => null, + ], null, null, 'hr'); + + $generator = $router->getGenerator(); + + $this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator); + + $p = new \ReflectionProperty($generator, 'defaultLocale'); + $p->setAccessible(true); + + $this->assertSame('hr', $p->getValue($generator)); + } + + public function testDefaultLocaleIsPassedToCompiledGeneratorCacheClass() + { + $this->loader->expects($this->once()) + ->method('load')->with('routing.yml', null) + ->willReturn(new RouteCollection()); + + $router = new Router($this->loader, 'routing.yml', [ + 'cache_dir' => $this->cacheDir, + ], null, null, 'hr'); + + $generator = $router->getGenerator(); + + $this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator); + + $p = new \ReflectionProperty($generator, 'defaultLocale'); + $p->setAccessible(true); + + $this->assertSame('hr', $p->getValue($generator)); + } + + /** + * @group legacy + */ + public function testDefaultLocaleIsPassedToNotCompiledGeneratorCacheClass() + { + $this->loader->expects($this->once()) + ->method('load')->with('routing.yml', null) + ->willReturn(new RouteCollection()); + + $router = new Router($this->loader, 'routing.yml', [ + 'cache_dir' => $this->cacheDir, + 'generator_class' => 'Symfony\Component\Routing\Generator\UrlGenerator', + ], null, null, 'hr'); + + $generator = $router->getGenerator(); + + $this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator); + + $p = new \ReflectionProperty($generator, 'defaultLocale'); + $p->setAccessible(true); + + $this->assertSame('hr', $p->getValue($generator)); + } }