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

Skip to content

Commit 928d2e3

Browse files
committed
Merge pull request symfony#31 from krizon/fix-issue-19
Chainrouter: don't pass non default string route names to default router
2 parents 026fbba + cb115fe commit 928d2e3

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

ChainRouter.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,15 @@ public function generate($name, $parameters = array(), $absolute = false)
199199
/** @var $router ChainedRouterInterface */
200200
foreach ($this->all() as $router) {
201201

202-
if ($name && !is_string($name) && !$router instanceof ChainedRouterInterface ) {
203-
continue;
202+
// if $name and $router does not implement ChainedRouterInterface and $name is not a string, continue
203+
// if $name and $router does not implement ChainedRouterInterface and $name is string but does not match a default Symfony2 route name, continue
204+
if ($name && !$router instanceof ChainedRouterInterface) {
205+
if (!is_string($name) || !preg_match('/^[a-z0-9A-Z_.]+$/', $name)) {
206+
continue;
207+
}
204208
}
205209

210+
// If $router implements ChainedRouterInterface but doesn't support this route name, continue
206211
if ($router instanceof ChainedRouterInterface && !$router->supports($name)) {
207212
continue;
208213
}

Tests/Routing/ChainRouterTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,68 @@ public function testGenerateNotFound()
428428
$this->assertEquals($url, $result);
429429
}
430430

431+
public function testGenerateObjectName()
432+
{
433+
$name = new \stdClass();
434+
$parameters = array('test' => 'value');
435+
436+
$defaultRouter = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
437+
$chainedRouter = $this->getMock('Symfony\\Cmf\\Component\\Routing\\ChainedRouterInterface');
438+
439+
$defaultRouter
440+
->expects($this->never())
441+
->method('generate')
442+
;
443+
$chainedRouter
444+
->expects($this->once())
445+
->method('supports')
446+
->will($this->returnValue(true))
447+
;
448+
$chainedRouter
449+
->expects($this->once())
450+
->method('generate')
451+
->with($name, $parameters, false)
452+
->will($this->returnValue($name))
453+
;
454+
455+
$this->router->add($defaultRouter, 200);
456+
$this->router->add($chainedRouter, 100);
457+
458+
$result = $this->router->generate($name, $parameters);
459+
$this->assertEquals($name, $result);
460+
}
461+
462+
public function testGenerateNonDefaultStringName()
463+
{
464+
$name = '/test/this';
465+
$parameters = array('test' => 'value');
466+
467+
$defaultRouter = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
468+
$chainedRouter = $this->getMock('Symfony\\Cmf\\Component\\Routing\\ChainedRouterInterface');
469+
470+
$defaultRouter
471+
->expects($this->never())
472+
->method('generate')
473+
;
474+
$chainedRouter
475+
->expects($this->once())
476+
->method('supports')
477+
->will($this->returnValue(true))
478+
;
479+
$chainedRouter
480+
->expects($this->once())
481+
->method('generate')
482+
->with($name, $parameters, false)
483+
->will($this->returnValue($name))
484+
;
485+
486+
$this->router->add($defaultRouter, 200);
487+
$this->router->add($chainedRouter, 100);
488+
489+
$result = $this->router->generate($name, $parameters);
490+
$this->assertEquals($name, $result);
491+
}
492+
431493
public function testWarmup()
432494
{
433495
$dir = 'test_dir';

0 commit comments

Comments
 (0)