Skip to content

Commit ac69816

Browse files
committed
[FrameworkBundle] added tests for DIC parameters replacements in route defaults
1 parent 1bf647f commit ac69816

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

Routing/Router.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,13 @@ private function applyParameters(RouteCollection $collection)
7171
$this->applyParameters($route);
7272
} else {
7373
foreach ($route->getDefaults() as $name => $value) {
74-
if (preg_match('#^%(.+)%$#', $value, $matches) && $this->container->hasParameter($matches[1])) {
75-
$route->setDefault($name, $this->container->getParameter($matches[1]));
74+
if (!$value || '%' !== $value[0] || '%' !== substr($value, -1)) {
75+
continue;
76+
}
77+
78+
$key = substr($value, 1, -1);
79+
if ($this->container->hasParameter($key)) {
80+
$route->setDefault($name, $this->container->getParameter($key));
7681
}
7782
}
7883
}

Tests/Routing/RouterTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony framework.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Routing;
13+
14+
use Symfony\Bundle\FrameworkBundle\Routing\Router;
15+
use Symfony\Component\Routing\Route;
16+
use Symfony\Component\Routing\RouteCollection;
17+
18+
class RoutingTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testPlaceholders()
21+
{
22+
$routes = new RouteCollection();
23+
$routes->add('foo', new Route('/foo', array(
24+
'foo' => '%foo%',
25+
'bar' => '%bar%',
26+
'foobar' => 'foobar',
27+
'foo1' => '%foo',
28+
'foo2' => 'foo%',
29+
'foo3' => 'f%o%o',
30+
)));
31+
32+
$sc = $this->getServiceContainer($routes);
33+
$sc
34+
->expects($this->at(1))
35+
->method('hasParameter')
36+
->will($this->returnValue(false))
37+
;
38+
$sc
39+
->expects($this->at(2))
40+
->method('hasParameter')
41+
->will($this->returnValue(true))
42+
;
43+
$sc
44+
->expects($this->at(3))
45+
->method('getParameter')
46+
->will($this->returnValue('bar'))
47+
;
48+
49+
$router = new Router($sc, 'foo');
50+
$route = $router->getRouteCollection()->get('foo');
51+
52+
$this->assertEquals('%foo%', $route->getDefault('foo'));
53+
$this->assertEquals('bar', $route->getDefault('bar'));
54+
$this->assertEquals('foobar', $route->getDefault('foobar'));
55+
$this->assertEquals('%foo', $route->getDefault('foo1'));
56+
$this->assertEquals('foo%', $route->getDefault('foo2'));
57+
$this->assertEquals('f%o%o', $route->getDefault('foo3'));
58+
}
59+
60+
private function getServiceContainer(RouteCollection $routes)
61+
{
62+
$sc = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
63+
$sc
64+
->expects($this->once())
65+
->method('get')
66+
->will($this->returnValue($this->getLoader($routes)))
67+
;
68+
69+
return $sc;
70+
}
71+
72+
private function getLoader(RouteCollection $routes)
73+
{
74+
$loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
75+
$loader
76+
->expects($this->any())
77+
->method('load')
78+
->will($this->returnValue($routes))
79+
;
80+
81+
return $loader;
82+
}
83+
}

0 commit comments

Comments
 (0)