|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Form\Tests;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; |
14 | 15 | use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
|
15 | 16 | use Symfony\Component\Form\FormError;
|
16 | 17 | use Symfony\Component\Form\Forms;
|
@@ -372,6 +373,41 @@ public function testAddDoesNotMapViewDataToFormIfInheritData()
|
372 | 373 | $form->add($child);
|
373 | 374 | }
|
374 | 375 |
|
| 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 | + |
375 | 411 | public function testSetDataMapsViewDataToChildren()
|
376 | 412 | {
|
377 | 413 | $test = $this;
|
|
0 commit comments