Skip to content

Commit b963109

Browse files
committed
minor #9413 Add a 2nd argument to create() (ismail1432)
This PR was submitted for the 4.0 branch but it was squashed and merged into the 2.7 branch instead (closes #9413). Discussion ---------- Add a 2nd argument to create() Maybe I'm wrong but I tried the exemple with my case and the test failed because IMO if we don't pass an object to second argument, $form->getData() will return an array and this->assertEquals() can't compare an object with an array. What do you think :-) Commits ------- ecef1fc Add a 2nd argument to create()
2 parents e37d7e3 + ecef1fc commit b963109

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

form/unit_testing.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ The simplest ``TypeTestCase`` implementation looks like the following::
5252
'test2' => 'test2',
5353
);
5454

55-
$type = new TestedType();
56-
$form = $this->factory->create($type);
55+
$objectToCompare = new TestObject();
56+
// $objectToCompare will retrieve data from the form submission; pass it as the second argument
57+
$form = $this->factory->create(TestedType::class, $objectToCompare);
5758

5859
$object = new TestObject();
5960
// ...populate $object properties with the data stored in $formData
6061

6162
// submit the data to the form directly
6263
$form->submit($formData);
6364

65+
$objectToCompare = $form->getData();
6466
$this->assertTrue($form->isSynchronized());
65-
$this->assertEquals($object, $form->getData());
67+
$this->assertEquals($object, $objectToCompare);
6668

6769
$view = $form->createView();
6870
$children = $view->children;
@@ -79,8 +81,7 @@ First you verify if the ``FormType`` compiles. This includes basic class
7981
inheritance, the ``buildForm()`` function and options resolution. This should
8082
be the first test you write::
8183

82-
$type = new TestedType();
83-
$form = $this->factory->create($type);
84+
$form = $this->factory->create(TestedType::class, $objectToCompare);
8485

8586
This test checks that none of your data transformers used by the form
8687
failed. The :method:`Symfony\\Component\\Form\\FormInterface::isSynchronized`
@@ -98,7 +99,7 @@ method is only set to ``false`` if a data transformer throws an exception::
9899
Next, verify the submission and mapping of the form. The test below
99100
checks if all the fields are correctly specified::
100101

101-
$this->assertEquals($object, $form->getData());
102+
$this->assertEquals($object, $objectToCompare);
102103

103104
Finally, check the creation of the ``FormView``. You should check if all
104105
widgets you want to display are available in the children property::

0 commit comments

Comments
 (0)