Skip to content

Commit 3cb8a80

Browse files
committed
[Form] Added a test that ensures that setData() reacts to dynamic modifications of a form's children
1 parent 07d14e5 commit 3cb8a80

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/Symfony/Component/Form/Tests/CompoundFormTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Tests;
1313

14+
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
1415
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
1516
use Symfony\Component\Form\FormError;
1617
use Symfony\Component\Form\Forms;
@@ -372,6 +373,41 @@ public function testAddDoesNotMapViewDataToFormIfInheritData()
372373
$form->add($child);
373374
}
374375

376+
public function testSetDataSupportsDynamicAdditionAndRemovalOfChildren()
377+
{
378+
$form = $this->getBuilder()
379+
->setCompound(true)
380+
// We test using PropertyPathMapper on purpose. The traversal logic
381+
// is currently contained in InheritDataAwareIterator, but even
382+
// if that changes, this test should still function.
383+
->setDataMapper(new PropertyPathMapper())
384+
->getForm();
385+
386+
$child = $this->getMockForm('child');
387+
$childToBeRemoved = $this->getMockForm('removed');
388+
$childToBeAdded = $this->getMockForm('added');
389+
390+
$form->add($child);
391+
$form->add($childToBeRemoved);
392+
393+
$child->expects($this->once())
394+
->method('setData')
395+
->will($this->returnCallback(function () use ($form, $childToBeAdded) {
396+
$form->remove('removed');
397+
$form->add($childToBeAdded);
398+
}));
399+
400+
$childToBeRemoved->expects($this->never())
401+
->method('setData');
402+
403+
// once when it it is created, once when it is added
404+
$childToBeAdded->expects($this->exactly(2))
405+
->method('setData');
406+
407+
// pass NULL to all children
408+
$form->setData(array());
409+
}
410+
375411
public function testSetDataMapsViewDataToChildren()
376412
{
377413
$test = $this;

0 commit comments

Comments
 (0)