Skip to content

Commit

Permalink
[FrameworkBundle] added tests for DIC parameters replacements in rout…
Browse files Browse the repository at this point in the history
…e defaults
  • Loading branch information
fabpot committed Sep 14, 2011
1 parent 400159d commit fabec37
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Expand Up @@ -71,8 +71,13 @@ private function applyParameters(RouteCollection $collection)
$this->applyParameters($route);
} else {
foreach ($route->getDefaults() as $name => $value) {
if (preg_match('#^%(.+)%$#', $value, $matches) && $this->container->hasParameter($matches[1])) {
$route->setDefault($name, $this->container->getParameter($matches[1]));
if (!$value || '%' !== $value[0] || '%' !== substr($value, -1)) {
continue;
}

$key = substr($value, 1, -1);
if ($this->container->hasParameter($key)) {
$route->setDefault($name, $this->container->getParameter($key));
}
}
}
Expand Down
83 changes: 83 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Routing;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class RoutingTest extends \PHPUnit_Framework_TestCase
{
public function testPlaceholders()
{
$routes = new RouteCollection();
$routes->add('foo', new Route('/foo', array(
'foo' => '%foo%',
'bar' => '%bar%',
'foobar' => 'foobar',
'foo1' => '%foo',
'foo2' => 'foo%',
'foo3' => 'f%o%o',
)));

$sc = $this->getServiceContainer($routes);
$sc
->expects($this->at(1))
->method('hasParameter')
->will($this->returnValue(false))
;
$sc
->expects($this->at(2))
->method('hasParameter')
->will($this->returnValue(true))
;
$sc
->expects($this->at(3))
->method('getParameter')
->will($this->returnValue('bar'))
;

$router = new Router($sc, 'foo');
$route = $router->getRouteCollection()->get('foo');

$this->assertEquals('%foo%', $route->getDefault('foo'));
$this->assertEquals('bar', $route->getDefault('bar'));
$this->assertEquals('foobar', $route->getDefault('foobar'));
$this->assertEquals('%foo', $route->getDefault('foo1'));
$this->assertEquals('foo%', $route->getDefault('foo2'));
$this->assertEquals('f%o%o', $route->getDefault('foo3'));
}

private function getServiceContainer(RouteCollection $routes)
{
$sc = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$sc
->expects($this->once())
->method('get')
->will($this->returnValue($this->getLoader($routes)))
;

return $sc;
}

private function getLoader(RouteCollection $routes)
{
$loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
$loader
->expects($this->any())
->method('load')
->will($this->returnValue($routes))
;

return $loader;
}
}

0 comments on commit fabec37

Please sign in to comment.