4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests
;
14 use Symfony\Component\Form\Form
;
15 use Symfony\Component\Form\FormEvent
;
16 use Symfony\Component\Form\FormEvents
;
17 use Symfony\Component\PropertyAccess\PropertyPath
;
18 use Symfony\Component\Form\FormConfigBuilder
;
19 use Symfony\Component\Form\FormError
;
20 use Symfony\Component\Form\Exception\TransformationFailedException
;
21 use Symfony\Component\EventDispatcher\EventDispatcher
;
22 use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer
;
23 use Symfony\Component\Form\Tests\Fixtures\FixedFilterListener
;
25 class SimpleFormTest_Countable
implements \Countable
29 public function __construct($count)
31 $this->count
= $count;
34 public function count()
40 class SimpleFormTest_Traversable
implements \IteratorAggregate
44 public function __construct($count)
46 $this->iterator
= new \
ArrayIterator($count > 0 ? array_fill(0, $count, 'Foo') : array());
49 public function getIterator()
51 return $this->iterator
;
55 class SimpleFormTest
extends AbstractFormTest
57 public function testDataIsInitializedToConfiguredValue()
59 $model = new FixedDataTransformer(array(
62 $view = new FixedDataTransformer(array(
66 $config = new FormConfigBuilder('name', null, $this->dispatcher
);
67 $config->addViewTransformer($view);
68 $config->addModelTransformer($model);
69 $config->setData('default');
70 $form = new Form($config);
72 $this->assertSame('default', $form->getData());
73 $this->assertSame('foo', $form->getNormData());
74 $this->assertSame('bar', $form->getViewData());
77 // https://github.com/symfony/symfony/commit/d4f4038f6daf7cf88ca7c7ab089473cce5ebf7d8#commitcomment-1632879
78 public function testDataIsInitializedFromSubmit()
80 $mock = $this->getMockBuilder('\stdClass')
81 ->setMethods(array('preSetData', 'preSubmit'))
83 $mock->expects($this->at(0))
84 ->method('preSetData');
85 $mock->expects($this->at(1))
86 ->method('preSubmit');
88 $config = new FormConfigBuilder('name', null, $this->dispatcher
);
89 $config->addEventListener(FormEvents
::PRE_SET_DATA
, array($mock, 'preSetData'));
90 $config->addEventListener(FormEvents
::PRE_SUBMIT
, array($mock, 'preSubmit'));
91 $form = new Form($config);
93 // no call to setData() or similar where the object would be
94 // initialized otherwise
96 $form->submit('foobar');
99 // https://github.com/symfony/symfony/pull/7789
100 public function testFalseIsConvertedToNull()
102 $mock = $this->getMockBuilder('\stdClass')
103 ->setMethods(array('preBind'))
105 $mock->expects($this->once())
107 ->with($this->callback(function ($event) {
108 return null === $event->getData();
111 $config = new FormConfigBuilder('name', null, $this->dispatcher
);
112 $config->addEventListener(FormEvents
::PRE_BIND
, array($mock, 'preBind'));
113 $form = new Form($config);
117 $this->assertTrue($form->isValid());
118 $this->assertNull($form->getData());
122 * @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
124 public function testSubmitThrowsExceptionIfAlreadySubmitted()
126 $this->form
->submit(array());
127 $this->form
->submit(array());
130 public function testSubmitIsIgnoredIfDisabled()
132 $form = $this->getBuilder()
137 $form->submit('new');
139 $this->assertEquals('initial', $form->getData());
140 $this->assertTrue($form->isSubmitted());
143 public function testNeverRequiredIfParentNotRequired()
145 $parent = $this->getBuilder()->setRequired(false)->getForm();
146 $child = $this->getBuilder()->setRequired(true)->getForm();
148 $child->setParent($parent);
150 $this->assertFalse($child->isRequired());
153 public function testRequired()
155 $parent = $this->getBuilder()->setRequired(true)->getForm();
156 $child = $this->getBuilder()->setRequired(true)->getForm();
158 $child->setParent($parent);
160 $this->assertTrue($child->isRequired());
163 public function testNotRequired()
165 $parent = $this->getBuilder()->setRequired(true)->getForm();
166 $child = $this->getBuilder()->setRequired(false)->getForm();
168 $child->setParent($parent);
170 $this->assertFalse($child->isRequired());
173 public function testAlwaysDisabledIfParentDisabled()
175 $parent = $this->getBuilder()->setDisabled(true)->getForm();
176 $child = $this->getBuilder()->setDisabled(false)->getForm();
178 $child->setParent($parent);
180 $this->assertTrue($child->isDisabled());
183 public function testDisabled()
185 $parent = $this->getBuilder()->setDisabled(false)->getForm();
186 $child = $this->getBuilder()->setDisabled(true)->getForm();
188 $child->setParent($parent);
190 $this->assertTrue($child->isDisabled());
193 public function testNotDisabled()
195 $parent = $this->getBuilder()->setDisabled(false)->getForm();
196 $child = $this->getBuilder()->setDisabled(false)->getForm();
198 $child->setParent($parent);
200 $this->assertFalse($child->isDisabled());
203 public function testGetRootReturnsRootOfParent()
205 $parent = $this->getMockForm();
206 $parent->expects($this->once())
208 ->will($this->returnValue('ROOT'));
210 $this->form
->setParent($parent);
212 $this->assertEquals('ROOT', $this->form
->getRoot());
215 public function testGetRootReturnsSelfIfNoParent()
217 $this->assertSame($this->form
, $this->form
->getRoot());
220 public function testEmptyIfEmptyArray()
222 $this->form
->setData(array());
224 $this->assertTrue($this->form
->isEmpty());
227 public function testEmptyIfEmptyCountable()
229 $this->form
= new Form(new FormConfigBuilder('name', __NAMESPACE__
.'\SimpleFormTest_Countable', $this->dispatcher
));
231 $this->form
->setData(new SimpleFormTest_Countable(0));
233 $this->assertTrue($this->form
->isEmpty());
236 public function testNotEmptyIfFilledCountable()
238 $this->form
= new Form(new FormConfigBuilder('name', __NAMESPACE__
.'\SimpleFormTest_Countable', $this->dispatcher
));
240 $this->form
->setData(new SimpleFormTest_Countable(1));
242 $this->assertFalse($this->form
->isEmpty());
245 public function testEmptyIfEmptyTraversable()
247 $this->form
= new Form(new FormConfigBuilder('name', __NAMESPACE__
.'\SimpleFormTest_Traversable', $this->dispatcher
));
249 $this->form
->setData(new SimpleFormTest_Traversable(0));
251 $this->assertTrue($this->form
->isEmpty());
254 public function testNotEmptyIfFilledTraversable()
256 $this->form
= new Form(new FormConfigBuilder('name', __NAMESPACE__
.'\SimpleFormTest_Traversable', $this->dispatcher
));
258 $this->form
->setData(new SimpleFormTest_Traversable(1));
260 $this->assertFalse($this->form
->isEmpty());
263 public function testEmptyIfNull()
265 $this->form
->setData(null);
267 $this->assertTrue($this->form
->isEmpty());
270 public function testEmptyIfEmptyString()
272 $this->form
->setData('');
274 $this->assertTrue($this->form
->isEmpty());
277 public function testNotEmptyIfText()
279 $this->form
->setData('foobar');
281 $this->assertFalse($this->form
->isEmpty());
284 public function testValidIfSubmitted()
286 $form = $this->getBuilder()->getForm();
287 $form->submit('foobar');
289 $this->assertTrue($form->isValid());
292 public function testValidIfSubmittedAndDisabled()
294 $form = $this->getBuilder()->setDisabled(true)->getForm();
295 $form->submit('foobar');
297 $this->assertTrue($form->isValid());
300 public function testNotValidIfNotSubmitted()
302 $this->assertFalse($this->form
->isValid());
305 public function testNotValidIfErrors()
307 $form = $this->getBuilder()->getForm();
308 $form->submit('foobar');
309 $form->addError(new FormError('Error!'));
311 $this->assertFalse($form->isValid());
314 public function testHasErrors()
316 $this->form
->addError(new FormError('Error!'));
318 $this->assertCount(1, $this->form
->getErrors());
321 public function testHasNoErrors()
323 $this->assertCount(0, $this->form
->getErrors());
327 * @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
329 public function testSetParentThrowsExceptionIfAlreadySubmitted()
331 $this->form
->submit(array());
332 $this->form
->setParent($this->getBuilder('parent')->getForm());
335 public function testSubmitted()
337 $form = $this->getBuilder()->getForm();
338 $form->submit('foobar');
340 $this->assertTrue($form->isSubmitted());
343 public function testNotSubmitted()
345 $this->assertFalse($this->form
->isSubmitted());
349 * @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
351 public function testSetDataThrowsExceptionIfAlreadySubmitted()
353 $this->form
->submit(array());
354 $this->form
->setData(null);
357 public function testSetDataClonesObjectIfNotByReference()
359 $data = new \
stdClass();
360 $form = $this->getBuilder('name', null, '\stdClass')->setByReference(false)->getForm();
361 $form->setData($data);
363 $this->assertNotSame($data, $form->getData());
364 $this->assertEquals($data, $form->getData());
367 public function testSetDataDoesNotCloneObjectIfByReference()
369 $data = new \
stdClass();
370 $form = $this->getBuilder('name', null, '\stdClass')->setByReference(true)->getForm();
371 $form->setData($data);
373 $this->assertSame($data, $form->getData());
376 public function testSetDataExecutesTransformationChain()
378 // use real event dispatcher now
379 $form = $this->getBuilder('name', new EventDispatcher())
380 ->addEventSubscriber(new FixedFilterListener(array(
381 'preSetData' => array(
385 ->addModelTransformer(new FixedDataTransformer(array(
387 'filtered' => 'norm',
389 ->addViewTransformer(new FixedDataTransformer(array(
395 $form->setData('app');
397 $this->assertEquals('filtered', $form->getData());
398 $this->assertEquals('norm', $form->getNormData());
399 $this->assertEquals('client', $form->getViewData());
402 public function testSetDataExecutesViewTransformersInOrder()
404 $form = $this->getBuilder()
405 ->addViewTransformer(new FixedDataTransformer(array(
409 ->addViewTransformer(new FixedDataTransformer(array(
415 $form->setData('first');
417 $this->assertEquals('third', $form->getViewData());
420 public function testSetDataExecutesModelTransformersInReverseOrder()
422 $form = $this->getBuilder()
423 ->addModelTransformer(new FixedDataTransformer(array(
427 ->addModelTransformer(new FixedDataTransformer(array(
433 $form->setData('first');
435 $this->assertEquals('third', $form->getNormData());
439 * When there is no data transformer, the data must have the same format
440 * in all three representations
442 public function testSetDataConvertsScalarToStringIfNoTransformer()
444 $form = $this->getBuilder()->getForm();
448 $this->assertSame('1', $form->getData());
449 $this->assertSame('1', $form->getNormData());
450 $this->assertSame('1', $form->getViewData());
454 * Data in client format should, if possible, always be a string to
455 * facilitate differentiation between '0' and ''
457 public function testSetDataConvertsScalarToStringIfOnlyModelTransformer()
459 $form = $this->getBuilder()
460 ->addModelTransformer(new FixedDataTransformer(array(
468 $this->assertSame(1, $form->getData());
469 $this->assertSame(23, $form->getNormData());
470 $this->assertSame('23', $form->getViewData());
474 * NULL remains NULL in app and norm format to remove the need to treat
475 * empty values and NULL explicitly in the application
477 public function testSetDataConvertsNullToStringIfNoTransformer()
479 $form = $this->getBuilder()->getForm();
481 $form->setData(null);
483 $this->assertNull($form->getData());
484 $this->assertNull($form->getNormData());
485 $this->assertSame('', $form->getViewData());
488 public function testSetDataIsIgnoredIfDataIsLocked()
490 $form = $this->getBuilder()
492 ->setDataLocked(true)
495 $form->setData('foobar');
497 $this->assertSame('default', $form->getData());
500 public function testSubmitConvertsEmptyToNullIfNoTransformer()
502 $form = $this->getBuilder()->getForm();
506 $this->assertNull($form->getData());
507 $this->assertNull($form->getNormData());
508 $this->assertSame('', $form->getViewData());
511 public function testSubmitExecutesTransformationChain()
513 // use real event dispatcher now
514 $form = $this->getBuilder('name', new EventDispatcher())
515 ->addEventSubscriber(new FixedFilterListener(array(
516 'preSubmit' => array(
517 'client' => 'filteredclient',
520 'norm' => 'filterednorm',
523 ->addViewTransformer(new FixedDataTransformer(array(
525 // direction is reversed!
526 'norm' => 'filteredclient',
527 'filterednorm' => 'cleanedclient'
529 ->addModelTransformer(new FixedDataTransformer(array(
531 // direction is reversed!
532 'app' => 'filterednorm',
536 $form->submit('client');
538 $this->assertEquals('app', $form->getData());
539 $this->assertEquals('filterednorm', $form->getNormData());
540 $this->assertEquals('cleanedclient', $form->getViewData());
543 public function testSubmitExecutesViewTransformersInReverseOrder()
545 $form = $this->getBuilder()
546 ->addViewTransformer(new FixedDataTransformer(array(
550 ->addViewTransformer(new FixedDataTransformer(array(
556 $form->submit('first');
558 $this->assertEquals('third', $form->getNormData());
561 public function testSubmitExecutesModelTransformersInOrder()
563 $form = $this->getBuilder()
564 ->addModelTransformer(new FixedDataTransformer(array(
568 ->addModelTransformer(new FixedDataTransformer(array(
574 $form->submit('first');
576 $this->assertEquals('third', $form->getData());
579 public function testSynchronizedByDefault()
581 $this->assertTrue($this->form
->isSynchronized());
584 public function testSynchronizedAfterSubmission()
586 $this->form
->submit('foobar');
588 $this->assertTrue($this->form
->isSynchronized());
591 public function testNotSynchronizedIfViewReverseTransformationFailed()
593 $transformer = $this->getDataTransformer();
594 $transformer->expects($this->once())
595 ->method('reverseTransform')
596 ->will($this->throwException(new TransformationFailedException()));
598 $form = $this->getBuilder()
599 ->addViewTransformer($transformer)
602 $form->submit('foobar');
604 $this->assertFalse($form->isSynchronized());
607 public function testNotSynchronizedIfModelReverseTransformationFailed()
609 $transformer = $this->getDataTransformer();
610 $transformer->expects($this->once())
611 ->method('reverseTransform')
612 ->will($this->throwException(new TransformationFailedException()));
614 $form = $this->getBuilder()
615 ->addModelTransformer($transformer)
618 $form->submit('foobar');
620 $this->assertFalse($form->isSynchronized());
623 public function testEmptyDataCreatedBeforeTransforming()
625 $form = $this->getBuilder()
626 ->setEmptyData('foo')
627 ->addViewTransformer(new FixedDataTransformer(array(
629 // direction is reversed!
636 $this->assertEquals('bar', $form->getData());
639 public function testEmptyDataFromClosure()
642 $form = $this->getBuilder()
643 ->setEmptyData(function ($form) use ($test) {
644 // the form instance is passed to the closure to allow use
645 // of form data when creating the empty value
646 $test->assertInstanceOf('Symfony\Component\Form\FormInterface', $form);
650 ->addViewTransformer(new FixedDataTransformer(array(
652 // direction is reversed!
659 $this->assertEquals('bar', $form->getData());
662 public function testSubmitResetsErrors()
664 $this->form
->addError(new FormError('Error!'));
665 $this->form
->submit('foobar');
667 $this->assertSame(array(), $this->form
->getErrors());
670 public function testCreateView()
672 $type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
673 $view = $this->getMock('Symfony\Component\Form\FormView');
674 $form = $this->getBuilder()->setType($type)->getForm();
676 $type->expects($this->once())
677 ->method('createView')
679 ->will($this->returnValue($view));
681 $this->assertSame($view, $form->createView());
684 public function testCreateViewWithParent()
686 $type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
687 $view = $this->getMock('Symfony\Component\Form\FormView');
688 $parentForm = $this->getMock('Symfony\Component\Form\Test\FormInterface');
689 $parentView = $this->getMock('Symfony\Component\Form\FormView');
690 $form = $this->getBuilder()->setType($type)->getForm();
691 $form->setParent($parentForm);
693 $parentForm->expects($this->once())
694 ->method('createView')
695 ->will($this->returnValue($parentView));
697 $type->expects($this->once())
698 ->method('createView')
699 ->with($form, $parentView)
700 ->will($this->returnValue($view));
702 $this->assertSame($view, $form->createView());
705 public function testCreateViewWithExplicitParent()
707 $type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
708 $view = $this->getMock('Symfony\Component\Form\FormView');
709 $parentView = $this->getMock('Symfony\Component\Form\FormView');
710 $form = $this->getBuilder()->setType($type)->getForm();
712 $type->expects($this->once())
713 ->method('createView')
714 ->with($form, $parentView)
715 ->will($this->returnValue($view));
717 $this->assertSame($view, $form->createView($parentView));
720 public function testGetErrorsAsString()
722 $this->form
->addError(new FormError('Error!'));
724 $this->assertEquals("ERROR: Error!\n", $this->form
->getErrorsAsString());
727 public function testFormCanHaveEmptyName()
729 $form = $this->getBuilder('')->getForm();
731 $this->assertEquals('', $form->getName());
734 public function testSetNullParentWorksWithEmptyName()
736 $form = $this->getBuilder('')->getForm();
737 $form->setParent(null);
739 $this->assertNull($form->getParent());
743 * @expectedException \Symfony\Component\Form\Exception\LogicException
744 * @expectedExceptionMessage A form with an empty name cannot have a parent form.
746 public function testFormCannotHaveEmptyNameNotInRootLevel()
750 ->setDataMapper($this->getDataMapper())
751 ->add($this->getBuilder(''))
755 public function testGetPropertyPathReturnsConfiguredPath()
757 $form = $this->getBuilder()->setPropertyPath('address.street')->getForm();
759 $this->assertEquals(new PropertyPath('address.street'), $form->getPropertyPath());
762 // see https://github.com/symfony/symfony/issues/3903
763 public function testGetPropertyPathDefaultsToNameIfParentHasDataClass()
765 $parent = $this->getBuilder(null, null, 'stdClass')
767 ->setDataMapper($this->getDataMapper())
769 $form = $this->getBuilder('name')->getForm();
772 $this->assertEquals(new PropertyPath('name'), $form->getPropertyPath());
775 // see https://github.com/symfony/symfony/issues/3903
776 public function testGetPropertyPathDefaultsToIndexedNameIfParentDataClassIsNull()
778 $parent = $this->getBuilder()
780 ->setDataMapper($this->getDataMapper())
782 $form = $this->getBuilder('name')->getForm();
785 $this->assertEquals(new PropertyPath('[name]'), $form->getPropertyPath());
788 public function testGetPropertyPathDefaultsToNameIfFirstParentWithoutInheritDataHasDataClass()
790 $grandParent = $this->getBuilder(null, null, 'stdClass')
792 ->setDataMapper($this->getDataMapper())
794 $parent = $this->getBuilder()
796 ->setDataMapper($this->getDataMapper())
797 ->setInheritData(true)
799 $form = $this->getBuilder('name')->getForm();
800 $grandParent->add($parent);
803 $this->assertEquals(new PropertyPath('name'), $form->getPropertyPath());
806 public function testGetPropertyPathDefaultsToIndexedNameIfDataClassOfFirstParentWithoutInheritDataIsNull()
808 $grandParent = $this->getBuilder()
810 ->setDataMapper($this->getDataMapper())
812 $parent = $this->getBuilder()
814 ->setDataMapper($this->getDataMapper())
815 ->setInheritData(true)
817 $form = $this->getBuilder('name')->getForm();
818 $grandParent->add($parent);
821 $this->assertEquals(new PropertyPath('[name]'), $form->getPropertyPath());
825 * @expectedException \Symfony\Component\Form\Exception\LogicException
827 public function testViewDataMustNotBeObjectIfDataClassIsNull()
829 $config = new FormConfigBuilder('name', null, $this->dispatcher
);
830 $config->addViewTransformer(new FixedDataTransformer(array(
832 'foo' => new \
stdClass(),
834 $form = new Form($config);
836 $form->setData('foo');
839 public function testViewDataMayBeArrayAccessIfDataClassIsNull()
841 $arrayAccess = $this->getMock('\ArrayAccess');
842 $config = new FormConfigBuilder('name', null, $this->dispatcher
);
843 $config->addViewTransformer(new FixedDataTransformer(array(
845 'foo' => $arrayAccess,
847 $form = new Form($config);
849 $form->setData('foo');
851 $this->assertSame($arrayAccess, $form->getViewData());
855 * @expectedException \Symfony\Component\Form\Exception\LogicException
857 public function testViewDataMustBeObjectIfDataClassIsSet()
859 $config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher
);
860 $config->addViewTransformer(new FixedDataTransformer(array(
862 'foo' => array('bar' => 'baz'),
864 $form = new Form($config);
866 $form->setData('foo');
870 * @expectedException \Symfony\Component\Form\Exception\RuntimeException
872 public function testSetDataCannotInvokeItself()
874 // Cycle detection to prevent endless loops
875 $config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher
);
876 $config->addEventListener(FormEvents
::PRE_SET_DATA
, function (FormEvent
$event) {
877 $event->getForm()->setData('bar');
879 $form = new Form($config);
881 $form->setData('foo');
884 public function testSubmittingWrongDataIsIgnored()
888 $child = $this->getBuilder('child', $this->dispatcher
);
889 $child->addEventListener(FormEvents
::PRE_SUBMIT
, function (FormEvent
$event) use ($test) {
890 // child form doesn't receive the wrong data that is submitted on parent
891 $test->assertNull($event->getData());
894 $parent = $this->getBuilder('parent', new EventDispatcher())
896 ->setDataMapper($this->getDataMapper())
900 $parent->submit('not-an-array');
903 public function testHandleRequestForwardsToRequestHandler()
905 $handler = $this->getMock('Symfony\Component\Form\RequestHandlerInterface');
907 $form = $this->getBuilder()
908 ->setRequestHandler($handler)
911 $handler->expects($this->once())
912 ->method('handleRequest')
913 ->with($this->identicalTo($form), 'REQUEST');
915 $this->assertSame($form, $form->handleRequest('REQUEST'));
918 public function testFormInheritsParentData()
920 $child = $this->getBuilder('child')
921 ->setInheritData(true);
923 $parent = $this->getBuilder('parent')
925 ->setDataMapper($this->getDataMapper())
927 ->addModelTransformer(new FixedDataTransformer(array(
928 'foo' => 'norm[foo]',
930 ->addViewTransformer(new FixedDataTransformer(array(
931 'norm[foo]' => 'view[foo]',
936 $this->assertSame('foo', $parent->get('child')->getData());
937 $this->assertSame('norm[foo]', $parent->get('child')->getNormData());
938 $this->assertSame('view[foo]', $parent->get('child')->getViewData());
942 * @expectedException \Symfony\Component\Form\Exception\RuntimeException
944 public function testInheritDataDisallowsSetData()
946 $form = $this->getBuilder()
947 ->setInheritData(true)
950 $form->setData('foo');
954 * @expectedException \Symfony\Component\Form\Exception\RuntimeException
956 public function testGetDataRequiresParentToBeSetIfInheritData()
958 $form = $this->getBuilder()
959 ->setInheritData(true)
966 * @expectedException \Symfony\Component\Form\Exception\RuntimeException
968 public function testGetNormDataRequiresParentToBeSetIfInheritData()
970 $form = $this->getBuilder()
971 ->setInheritData(true)
974 $form->getNormData();
978 * @expectedException \Symfony\Component\Form\Exception\RuntimeException
980 public function testGetViewDataRequiresParentToBeSetIfInheritData()
982 $form = $this->getBuilder()
983 ->setInheritData(true)
986 $form->getViewData();
989 public function testPostSubmitDataIsNullIfInheritData()
992 $form = $this->getBuilder()
993 ->addEventListener(FormEvents
::POST_SUBMIT
, function (FormEvent
$event) use ($test) {
994 $test->assertNull($event->getData());
996 ->setInheritData(true)
999 $form->submit('foo');
1002 public function testSubmitIsNeverFiredIfInheritData()
1005 $form = $this->getBuilder()
1006 ->addEventListener(FormEvents
::SUBMIT
, function (FormEvent
$event) use ($test) {
1007 $test->fail('The SUBMIT event should not be fired');
1009 ->setInheritData(true)
1012 $form->submit('foo');
1015 public function testInitializeSetsDefaultData()
1017 $config = $this->getBuilder()->setData('DEFAULT')->getFormConfig();
1018 $form = $this->getMock('Symfony\Component\Form\Form', array('setData'), array($config));
1020 $form->expects($this->once())
1022 ->with($this->identicalTo('DEFAULT'));
1024 /* @var Form $form */
1025 $form->initialize();
1029 * @expectedException \Symfony\Component\Form\Exception\RuntimeException
1031 public function testInitializeFailsIfParent()
1033 $parent = $this->getBuilder()->setRequired(false)->getForm();
1034 $child = $this->getBuilder()->setRequired(true)->getForm();
1036 $child->setParent($parent);
1038 $child->initialize();
1041 protected function createForm()
1043 return $this->getBuilder()->getForm();