Skip to content

Commit

Permalink
ParamConverterManager now checks if the value is the correct instance.
Browse files Browse the repository at this point in the history
This is needed because:
    {% render 'MyVenBundle:Controller:Action', { 'entity' : entity } %}

would pass an already converted value.
  • Loading branch information
henrikbjorn committed Dec 14, 2011
1 parent 041c3f4 commit 3eda845
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Request/ParamConverter/ParamConverterManager.php
Expand Up @@ -40,6 +40,13 @@ public function apply(Request $request, $configurations)
}

foreach ($configurations as $configuration) {
// If the value is already an instance of the class we are trying to convert it into
// we should continue as no convertion is required `is_a` was un-deprecated in 5.3
$value = $request->attributes->get($configuration->getName());
if (is_object($value) && is_a($value, $configuration->getClass())) {
continue;
}

foreach ($this->all() as $converter) {
if ($converter->supports($configuration)) {
if ($converter->apply($request, $configuration)) {
Expand Down
27 changes: 27 additions & 0 deletions Tests/Request/ParamConverter/ParamConverterManagerTest.php
Expand Up @@ -65,6 +65,33 @@ public function testApply()
$this->manager->apply(new Request(), $configurations);
}

public function testApplyNotCalledOnAlreadyConvertedObjects()
{

$converter = $this->createParamConverterMock();
$converter
->expects($this->never())
->method('supports')
;

$converter
->expects($this->never())
->method('apply')
;

$this->manager->add($converter);

$request = new Request();
$request->attributes->set('converted', new \stdClass);

$configuration = new Configuration\ParamConverter(array(
'name' => 'converted',
'class' => 'stdClass',
));

$this->manager->apply($request, array($configuration));
}

protected function createParamConverterMock()
{
return $this->getMock('Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface');
Expand Down

0 comments on commit 3eda845

Please sign in to comment.