Fix the retrieval of the value with property path when using a loader
This commit is contained in:
parent
fac7f2146e
commit
5df64dca52
@ -141,7 +141,13 @@ class PropertyAccessDecorator implements ChoiceListFactoryInterface
|
||||
if ($value instanceof PropertyPath) {
|
||||
$accessor = $this->propertyAccessor;
|
||||
$value = function ($choice) use ($accessor, $value) {
|
||||
return $accessor->getValue($choice, $value);
|
||||
// The callable may be invoked with a non-object/array value
|
||||
// when such values are passed to
|
||||
// ChoiceListInterface::getValuesForChoices(). Handle this case
|
||||
// so that the call to getValue() doesn't break.
|
||||
if (is_object($choice) || is_array($choice)) {
|
||||
return $accessor->getValue($choice, $value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -93,6 +93,36 @@ class PropertyAccessDecoratorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertSame('value', $this->factory->createListFromLoader($loader, 'property'));
|
||||
}
|
||||
|
||||
// https://github.com/symfony/symfony/issues/5494
|
||||
public function testCreateFromChoicesAssumeNullIfValuePropertyPathUnreadable()
|
||||
{
|
||||
$choices = array(null);
|
||||
|
||||
$this->decoratedFactory->expects($this->once())
|
||||
->method('createListFromChoices')
|
||||
->with($choices, $this->isInstanceOf('\Closure'))
|
||||
->will($this->returnCallback(function ($choices, $callback) {
|
||||
return array_map($callback, $choices);
|
||||
}));
|
||||
|
||||
$this->assertSame(array(null), $this->factory->createListFromChoices($choices, 'property'));
|
||||
}
|
||||
|
||||
// https://github.com/symfony/symfony/issues/5494
|
||||
public function testCreateFromChoiceLoaderAssumeNullIfValuePropertyPathUnreadable()
|
||||
{
|
||||
$loader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface');
|
||||
|
||||
$this->decoratedFactory->expects($this->once())
|
||||
->method('createListFromLoader')
|
||||
->with($loader, $this->isInstanceOf('\Closure'))
|
||||
->will($this->returnCallback(function ($loader, $callback) {
|
||||
return $callback(null);
|
||||
}));
|
||||
|
||||
$this->assertNull($this->factory->createListFromLoader($loader, 'property'));
|
||||
}
|
||||
|
||||
public function testCreateFromLoaderPropertyPathInstance()
|
||||
{
|
||||
$loader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface');
|
||||
|
Reference in New Issue
Block a user