[FrameworkBundle] allowed attributes of the render() method to be arrays

This commit is contained in:
Fabien Potencier 2012-01-02 16:31:32 +01:00
parent cadf3d4243
commit 789d5ad20f
2 changed files with 63 additions and 5 deletions

View File

@ -107,11 +107,11 @@ class HttpKernel extends BaseHttpKernel
// enforce all attribute values to be scalars to be sure that the same
// render() call will work with or without a reverse proxy
foreach ($options['attributes'] as $key => $value) {
if (!is_scalar($value)) {
throw new \InvalidArgumentException(sprintf('Unable to render the "%s" controller as the "%s" attribute is not a scalar.', $controller, $key));
array_walk_recursive($options['attributes'], function ($value) use ($controller) {
if (is_object($value)) {
throw new \InvalidArgumentException(sprintf('Unable to render the "%s" controller as one of the attribute is an object.', $controller));
}
}
});
if (!is_array($options['alt'])) {
$options['alt'] = array($options['alt']);

View File

@ -220,6 +220,64 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Foo', ob_get_clean());
}
public function testRenderAttributes()
{
$dispatcher = new EventDispatcher();
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
$phpunit = $this;
$esi = $this->getMock('Symfony\\Component\\HttpKernel\\HttpCache\\Esi');
$esi
->expects($this->once())
->method('hasSurrogateEsiCapability')
->will($this->returnValue(true))
;
$esi
->expects($this->once())
->method('renderIncludeTag')
->will($this->returnCallback(function ($uri) use ($phpunit) {
$phpunit->assertEquals('foo[bar]=foo', urldecode($uri));
return 'foo';
}))
;
$router = $this->getMock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface');
$router
->expects($this->once())
->method('generate')
->will($this->returnCallback(function ($name, $options) {
return $options['path'];
}))
;
$request = new Request();
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(2))->method('get')->with($this->equalTo('esi'))->will($this->returnValue($esi));
$container->expects($this->at(3))->method('get')->with($this->equalTo('request'))->will($this->returnValue($request));
$container->expects($this->at(4))->method('get')->with($this->equalTo('router'))->will($this->returnValue($router));
$container->expects($this->at(5))->method('get')->with($this->equalTo('request'))->will($this->returnValue($request));
$container->expects($this->at(6))->method('get')->with($this->equalTo('esi'))->will($this->returnValue($esi));
$container
->expects($this->once())
->method('has')
->with($this->equalTo('esi'))
->will($this->returnValue(true))
;
$kernel = new HttpKernel($dispatcher, $container, $resolver);
$content = $kernel->render('foo_controller', array(
'standalone' => true,
'attributes' => array('foo' => array('bar' => 'foo')),
));
// if this assertion fails, it means that the assertion in 'returnCallback' failed
$this->assertEquals('foo', $content);
}
/**
* @expectedException \InvalidArgumentException
*/
@ -230,6 +288,6 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$kernel = new HttpKernel($dispatcher, $container, $resolver);
$kernel->render('/', array('attributes' => array('foo' => new \stdClass())));
$kernel->render('/', array('attributes' => array('foo' => array('bar' => new \stdClass()))));
}
}