Skip to content

Commit

Permalink
[Form] Added a test that ensures that setData() reacts to dynamic mod…
Browse files Browse the repository at this point in the history
…ifications of a form's children
  • Loading branch information
webmozart committed Aug 22, 2013
1 parent 07d14e5 commit 3cb8a80
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests;

use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\Forms;
Expand Down Expand Up @@ -372,6 +373,41 @@ public function testAddDoesNotMapViewDataToFormIfInheritData()
$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()
{
$test = $this;
Expand Down

0 comments on commit 3cb8a80

Please sign in to comment.