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

Skip to content

Commit 9aa66e2

Browse files
HypeMCnicolas-grekas
authored andcommitted
Add tests to ensure defaultLocale is properly passed to the URL generator
1 parent 16c9baf commit 9aa66e2

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

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

Lines changed: 80 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,68 @@ 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+
*/
195+
public function testDefaultLocaleIsPassedToNotCompiledGeneratorCacheClass()
196+
{
197+
$this->loader->expects($this->once())
198+
->method('load')->with('routing.yml', null)
199+
->willReturn(new RouteCollection());
200+
201+
$router = new Router($this->loader, 'routing.yml', [
202+
'cache_dir' => $this->cacheDir,
203+
'generator_class' => 'Symfony\Component\Routing\Generator\UrlGenerator',
204+
], null, null, 'hr');
205+
206+
$generator = $router->getGenerator();
207+
208+
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
209+
210+
$p = new \ReflectionProperty($generator, 'defaultLocale');
211+
$p->setAccessible(true);
212+
213+
$this->assertSame('hr', $p->getValue($generator));
214+
}
135215
}

0 commit comments

Comments
 (0)