Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Jan 15, 2014
1 parent 5dde52e commit b65818a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
5 changes: 4 additions & 1 deletion Admin/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
* - link_parameters (o) : add link parameter to the related Admin class when
* the Admin.generateUrl is called
* - code : the method name to retrieve the related value
* - associated_tostring : the method to retrieve the "string" representation
* - associated_tostring : (deprecated, use associated_property option)
* the method to retrieve the "string" representation
* of the collection element.
* - associated_property : property path to retrieve the "string" representation
* of the collection element.
*
* Form Field options :
Expand Down
57 changes: 47 additions & 10 deletions Tests/Twig/Extension/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,17 @@ public function testRenderRelationElementNoObject()

public function testRenderRelationElementToString()
{
$this->fieldDescription->expects($this->once())
$this->fieldDescription->expects($this->exactly(2))
->method('getOption')
->with($this->identicalTo('associated_tostring'))
->will($this->returnValue('__toString'));
->will($this->returnCallback(function($value, $default = null) {
if ($value == 'associated_property') {
return $default;
}

if ($value == 'associated_tostring') {
return '__toString';
}
}));

$element = $this->getMock('stdClass', array('__toString'));
$element->expects($this->any())
Expand All @@ -545,10 +552,18 @@ public function testRenderRelationElementToString()

public function testRenderRelationElementCustomToString()
{
$this->fieldDescription->expects($this->any())
$this->fieldDescription->expects($this->exactly(2))
->method('getOption')
->with($this->identicalTo('associated_tostring'))
->will($this->returnValue('customToString'));
->will($this->returnCallback(function($value, $default = null) {
if ($value == 'associated_property') {
return $default;
}

if ($value == 'associated_tostring') {
return 'customToString';
}
}));


$element = $this->getMock('stdClass', array('customToString'));
$element->expects($this->any())
Expand All @@ -560,24 +575,46 @@ public function testRenderRelationElementCustomToString()

public function testRenderRelationElementMethodNotExist()
{
$this->fieldDescription->expects($this->any())
$this->fieldDescription->expects($this->exactly(2))
->method('getOption')
->with($this->identicalTo('associated_tostring'))
->will($this->returnValue('nonExistedMethod'));

->will($this->returnCallback(function($value, $default = null) {
if ($value == 'associated_tostring') {
return 'nonExistedMethod';
}
}));

$element = new \stdClass();

try {
$this->twigExtension->renderRelationElement($element, $this->fieldDescription);
} catch (\RuntimeException $e) {
$this->assertContains('You must define an `associated_tostring` option or create a `stdClass::__toString` method to the field option "fd_name" from service "xyz".', $e->getMessage());
$this->assertContains('You must define an `associated_property` option or create a `stdClass::__toString', $e->getMessage());

return;
}

$this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
}

public function testRenderRelationElementWithPropertyPath()
{
$this->fieldDescription->expects($this->exactly(1))
->method('getOption')

->will($this->returnCallback(function($value, $default = null) {
if ($value == 'associated_property') {
return 'foo';
}
}));

$element = new \stdClass();
$element->foo = "bar";

$this->assertEquals('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));

}

public function testGetUrlsafeIdentifier()
{
$enitity = new \stdClass();
Expand Down
21 changes: 17 additions & 4 deletions Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Exception\NoValueException;
use Sonata\AdminBundle\Admin\Pool;
use Symfony\Component\PropertyAccess\PropertyAccess;

class SonataAdminExtension extends \Twig_Extension
{
Expand Down Expand Up @@ -208,13 +209,25 @@ public function renderRelationElement($element, FieldDescriptionInterface $field
return $element;
}

$method = $fieldDescription->getOption('associated_tostring', '__toString');
$propertyPath = $fieldDescription->getOption('associated_property');

if (!method_exists($element, $method)) {
throw new \RunTimeException(sprintf('You must define an `associated_tostring` option or create a `%s::__toString` method to the field option "%s" from service "%s".', get_class($element), $fieldDescription->getName(), $fieldDescription->getAdmin()->getCode()));
if (null === $propertyPath) {
// For BC kept associated_tostring option behavior
$method = $fieldDescription->getOption('associated_tostring', '__toString');

if (!method_exists($element, $method)) {
throw new \RuntimeException(sprintf(
'You must define an `associated_property` option or create a `%s::__toString` method to the field option %s from service %s is ',
get_class($element),
$fieldDescription->getName(),
$fieldDescription->getAdmin()->getCode()
));
}

return call_user_func(array($element, $method));
}

return call_user_func(array($element, $method));
return PropertyAccess::createPropertyAccessor()->getValue($element, $propertyPath);
}

/**
Expand Down

0 comments on commit b65818a

Please sign in to comment.