Merge branch 'form-submit-2.2' into form-submit-2.3

Conflicts:
	src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php
This commit is contained in:
Bernhard Schussek 2013-08-22 14:12:19 +02:00
commit 878e27cd99
2 changed files with 41 additions and 8 deletions

View File

@ -43,11 +43,9 @@ class PropertyPathMapper implements DataMapperInterface
*/ */
public function mapDataToForms($data, $forms) public function mapDataToForms($data, $forms)
{ {
if (null === $data || array() === $data) { $empty = null === $data || array() === $data;
return;
}
if (!is_array($data) && !is_object($data)) { if (!$empty && !is_array($data) && !is_object($data)) {
throw new UnexpectedTypeException($data, 'object, array or empty'); throw new UnexpectedTypeException($data, 'object, array or empty');
} }
@ -55,8 +53,10 @@ class PropertyPathMapper implements DataMapperInterface
$propertyPath = $form->getPropertyPath(); $propertyPath = $form->getPropertyPath();
$config = $form->getConfig(); $config = $form->getConfig();
if (null !== $propertyPath && $config->getMapped()) { if (!$empty && null !== $propertyPath && $config->getMapped()) {
$form->setData($this->propertyAccessor->getValue($data, $propertyPath)); $form->setData($this->propertyAccessor->getValue($data, $propertyPath));
} else {
$form->setData($form->getConfig()->getData());
} }
} }
} }

View File

@ -169,8 +169,9 @@ class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase
$this->assertNull($form->getData()); $this->assertNull($form->getData());
} }
public function testMapDataToFormsIgnoresEmptyData() public function testMapDataToFormsSetsDefaultDataIfPassedDataIsNull()
{ {
$default = new \stdClass();
$propertyPath = $this->getPropertyPath('engine'); $propertyPath = $this->getPropertyPath('engine');
$this->propertyAccessor->expects($this->never()) $this->propertyAccessor->expects($this->never())
@ -179,11 +180,43 @@ class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase
$config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
$config->setByReference(true); $config->setByReference(true);
$config->setPropertyPath($propertyPath); $config->setPropertyPath($propertyPath);
$form = $this->getForm($config); $config->setData($default);
$form = $this->getMockBuilder('Symfony\Component\Form\Form')
->setConstructorArgs(array($config))
->setMethods(array('setData'))
->getMock();
$form->expects($this->once())
->method('setData')
->with($default);
$this->mapper->mapDataToForms(null, array($form)); $this->mapper->mapDataToForms(null, array($form));
}
$this->assertNull($form->getData()); public function testMapDataToFormsSetsDefaultDataIfPassedDataIsEmptyArray()
{
$default = new \stdClass();
$propertyPath = $this->getPropertyPath('engine');
$this->propertyAccessor->expects($this->never())
->method('getValue');
$config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
$config->setByReference(true);
$config->setPropertyPath($propertyPath);
$config->setData($default);
$form = $this->getMockBuilder('Symfony\Component\Form\Form')
->setConstructorArgs(array($config))
->setMethods(array('setData'))
->getMock();
$form->expects($this->once())
->method('setData')
->with($default);
$this->mapper->mapDataToForms(array(), array($form));
} }
public function testMapFormsToDataWritesBackIfNotByReference() public function testMapFormsToDataWritesBackIfNotByReference()