[Form] Added a test that ensures that setData() reacts to dynamic modifications of a form's children
This commit is contained in:
parent
07d14e5ff2
commit
3cb8a804f4
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Form\Tests;
|
namespace Symfony\Component\Form\Tests;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
|
||||||
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
|
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
|
||||||
use Symfony\Component\Form\FormError;
|
use Symfony\Component\Form\FormError;
|
||||||
use Symfony\Component\Form\Forms;
|
use Symfony\Component\Form\Forms;
|
||||||
@ -372,6 +373,41 @@ class CompoundFormTest extends AbstractFormTest
|
|||||||
$form->add($child);
|
$form->add($child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetDataSupportsDynamicAdditionAndRemovalOfChildren()
|
||||||
|
{
|
||||||
|
$form = $this->getBuilder()
|
||||||
|
->setCompound(true)
|
||||||
|
// We test using PropertyPathMapper on purpose. The traversal logic
|
||||||
|
// is currently contained in InheritDataAwareIterator, but even
|
||||||
|
// if that changes, this test should still function.
|
||||||
|
->setDataMapper(new PropertyPathMapper())
|
||||||
|
->getForm();
|
||||||
|
|
||||||
|
$child = $this->getMockForm('child');
|
||||||
|
$childToBeRemoved = $this->getMockForm('removed');
|
||||||
|
$childToBeAdded = $this->getMockForm('added');
|
||||||
|
|
||||||
|
$form->add($child);
|
||||||
|
$form->add($childToBeRemoved);
|
||||||
|
|
||||||
|
$child->expects($this->once())
|
||||||
|
->method('setData')
|
||||||
|
->will($this->returnCallback(function () use ($form, $childToBeAdded) {
|
||||||
|
$form->remove('removed');
|
||||||
|
$form->add($childToBeAdded);
|
||||||
|
}));
|
||||||
|
|
||||||
|
$childToBeRemoved->expects($this->never())
|
||||||
|
->method('setData');
|
||||||
|
|
||||||
|
// once when it it is created, once when it is added
|
||||||
|
$childToBeAdded->expects($this->exactly(2))
|
||||||
|
->method('setData');
|
||||||
|
|
||||||
|
// pass NULL to all children
|
||||||
|
$form->setData(array());
|
||||||
|
}
|
||||||
|
|
||||||
public function testSetDataMapsViewDataToChildren()
|
public function testSetDataMapsViewDataToChildren()
|
||||||
{
|
{
|
||||||
$test = $this;
|
$test = $this;
|
||||||
|
Reference in New Issue
Block a user