From 16c9bafee4ff9460b9bec2e24f93caae03aae71e Mon Sep 17 00:00:00 2001 From: Antonio Pauletich Date: Wed, 30 Oct 2019 20:24:37 +0100 Subject: [PATCH 1/2] Fix URL generator instantiation --- src/Symfony/Component/Routing/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(); From 9aa66e2f598f932bc178286f7d6de35460dfebd5 Mon Sep 17 00:00:00 2001 From: HypeMC <2445045+HypeMC@users.noreply.github.com> Date: Sun, 3 Nov 2019 00:56:53 +0100 Subject: [PATCH 2/2] Add tests to ensure defaultLocale is properly passed to the URL generator --- .../Component/Routing/Tests/RouterTest.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) 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)); + } }