Skip to content

Commit

Permalink
Handle __call method in FieldDescription::getFieldValue
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelVella committed Mar 17, 2015
1 parent 61c2b52 commit 418d8b3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Admin/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ public function getFieldValue($object, $fieldName)
}
}

if (method_exists($object, '__call')) {
return call_user_func_array(array($object, '__call'), array($fieldName, $parameters));
}

if (isset($object->{$fieldName})) {
return $object->{$fieldName};
}
Expand Down
39 changes: 39 additions & 0 deletions Tests/Admin/BaseFieldDescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Sonata\AdminBundle\Admin\BaseFieldDescription;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;

class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -190,4 +192,41 @@ public function testCamelize()
$this->assertEquals('FooBar', BaseFieldDescription::camelize('foo bar'));
$this->assertEquals('FOoBar', BaseFieldDescription::camelize('fOo bar'));
}

public function testGetFieldValue()
{
$foo = new Foo();
$foo->setBar('Bar');

$description = new FieldDescription();
$this->assertEquals('Bar', $description->getFieldValue($foo, 'bar'));

$this->setExpectedException('Sonata\AdminBundle\Exception\NoValueException');
$description->getFieldValue($foo, 'inexistantMethod');
}

public function testGetFieldValueWithCodeOption()
{
$foo = new Foo();
$foo->setBaz('Baz');

$description = new FieldDescription();

$description->setOption('code', 'getBaz');
$this->assertEquals('Baz', $description->getFieldValue($foo, 'inexistantMethod'));

$description->setOption('code', 'inexistantMethod');
$this->setExpectedException('Sonata\AdminBundle\Exception\NoValueException');
$description->getFieldValue($foo, 'inexistantMethod');
}

public function testGetFieldValueMagicCall()
{
$parameters = array('foo', 'bar');
$foo = new FooCall();

$description = new FieldDescription();
$description->setOption('parameters', $parameters);
$this->assertEquals(array('inexistantMethod', $parameters), $description->getFieldValue($foo, 'inexistantMethod'));
}
}
11 changes: 11 additions & 0 deletions Tests/Fixtures/Entity/FooCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Sonata\AdminBundle\Tests\Fixtures\Entity;

class FooCall
{
public function __call($method, $arguments)
{
return array($method, $arguments);
}
}

0 comments on commit 418d8b3

Please sign in to comment.