Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2290cce

Browse files
HypeMCX-Coder264
authored andcommitted
Add tests to ensure defaultLocale is properly passed to the URL generator
1 parent 16c9baf commit 2290cce

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/Symfony/Component/Routing/Tests/RouterTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,26 @@ class RouterTest extends TestCase
2222

2323
private $loader = null;
2424

25+
private $cacheDir;
26+
2527
protected function setUp(): void
2628
{
2729
$this->loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
2830
$this->router = new Router($this->loader, 'routing.yml');
31+
32+
$this->cacheDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('router_', true);
33+
}
34+
35+
protected function tearDown(): void
36+
{
37+
if (is_dir($this->cacheDir)) {
38+
array_map('unlink', glob($this->cacheDir.\DIRECTORY_SEPARATOR.'*'));
39+
rmdir($this->cacheDir);
40+
}
41+
42+
$this->loader = null;
43+
$this->router = null;
44+
$this->cacheDir = null;
2945
}
3046

3147
public function testSetOptionsWithSupportedOptions()
@@ -132,4 +148,69 @@ public function testMatchRequestWithRequestMatcherInterface()
132148

133149
$this->router->matchRequest(Request::create('/'));
134150
}
151+
152+
public function testDefaultLocaleIsPassedToGeneratorClass()
153+
{
154+
$this->loader->expects($this->once())
155+
->method('load')->with('routing.yml', null)
156+
->willReturn(new RouteCollection());
157+
158+
$router = new Router($this->loader, 'routing.yml', [
159+
'cache_dir' => null,
160+
], null, null, 'hr');
161+
162+
$generator = $router->getGenerator();
163+
164+
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
165+
166+
$p = new \ReflectionProperty($generator, 'defaultLocale');
167+
$p->setAccessible(true);
168+
169+
$this->assertSame('hr', $p->getValue($generator));
170+
}
171+
172+
public function testDefaultLocaleIsPassedToCompiledGeneratorCacheClass()
173+
{
174+
$this->loader->expects($this->once())
175+
->method('load')->with('routing.yml', null)
176+
->willReturn(new RouteCollection());
177+
178+
$router = new Router($this->loader, 'routing.yml', [
179+
'cache_dir' => $this->cacheDir,
180+
], null, null, 'hr');
181+
182+
$generator = $router->getGenerator();
183+
184+
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
185+
186+
$p = new \ReflectionProperty($generator, 'defaultLocale');
187+
$p->setAccessible(true);
188+
189+
$this->assertSame('hr', $p->getValue($generator));
190+
}
191+
192+
/**
193+
* @group legacy
194+
* @expectedDeprecation The "Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper" class is deprecated since Symfony 4.3, use "CompiledUrlGeneratorDumper" instead.
195+
*/
196+
public function testDefaultLocaleIsPassedToNotCompiledGeneratorCacheClass()
197+
{
198+
$this->loader->expects($this->once())
199+
->method('load')->with('routing.yml', null)
200+
->willReturn(new RouteCollection());
201+
202+
$router = new Router($this->loader, 'routing.yml', [
203+
'cache_dir' => $this->cacheDir,
204+
'generator_class' => 'Symfony\Component\Routing\Generator\UrlGenerator',
205+
], null, null, 'hr');
206+
207+
$generator = $router->getGenerator();
208+
209+
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
210+
211+
$p = new \ReflectionProperty($generator, 'defaultLocale');
212+
$p->setAccessible(true);
213+
214+
$this->assertSame('hr', $p->getValue($generator));
215+
}
135216
}

0 commit comments

Comments
 (0)