Skip to content

Commit eb92ebd

Browse files
committed
Use $initialData instead of 'data' in FormType to make tests easier to follow
1 parent 33dabc0 commit eb92ebd

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/LiveComponent/tests/Fixtures/Component/FormComponentWithManyDifferentFieldsType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ class FormComponentWithManyDifferentFieldsType extends AbstractController
2828
use ComponentWithFormTrait;
2929
use DefaultActionTrait;
3030

31+
public array $initialData = [];
32+
3133
public function __construct(private FormFactoryInterface $formFactory)
3234
{
3335
}
3436

3537
protected function instantiateForm(): FormInterface
3638
{
37-
return $this->formFactory->createNamed('form', FormWithManyDifferentFieldsType::class);
39+
return $this->formFactory->createNamed(
40+
'form',
41+
FormWithManyDifferentFieldsType::class,
42+
$this->initialData
43+
);
3844
}
3945
}

src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,16 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5555
],
5656
'expanded' => true,
5757
'multiple' => true,
58-
'data' => [2],
5958
])
6059
->add('select_multiple', ChoiceType::class, [
6160
'choices' => [
6261
'foo' => 1,
6362
'bar' => 2,
6463
],
6564
'multiple' => true,
66-
'data' => [2],
6765
])
6866
->add('checkbox', CheckboxType::class)
69-
->add('checkbox_checked', CheckboxType::class, [
70-
'data' => true,
71-
])
67+
->add('checkbox_checked', CheckboxType::class)
7268
->add('file', FileType::class)
7369
->add('hidden', HiddenType::class)
7470
;

src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,16 @@ public function testHandleCheckboxChanges(): void
157157
$factory = self::getContainer()->get('ux.twig_component.component_factory');
158158

159159
/** @var FormComponentWithManyDifferentFieldsType $component */
160-
$component = $factory->create('form_with_many_different_fields_type');
160+
$component = $factory->create(
161+
'form_with_many_different_fields_type',
162+
[
163+
'initialData' => [
164+
'choice_multiple' => [2],
165+
'select_multiple' => [2],
166+
'checkbox_checked' => true
167+
]
168+
]
169+
);
161170

162171
$dehydrated = $hydrator->dehydrate($component);
163172
$bareForm = [
@@ -195,7 +204,7 @@ public function testHandleCheckboxChanges(): void
195204
self::assertEquals($fullBareData, $data['form']);
196205

197206
// check both multiple fields
198-
$bareForm['choice_multiple'] = $fullBareData['choice_multiple'] = ['2', '1'];
207+
$bareForm['choice_multiple'] = $fullBareData['choice_multiple'] = ['1', '2'];
199208

200209
$dehydrated['form'] = $bareForm;
201210
})

src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function testFormValues(): void
2525
{
2626
$formFactory = self::getContainer()->get('form.factory');
2727
$component = new FormComponentWithManyDifferentFieldsType($formFactory);
28+
$component->initialData = [
29+
'choice_multiple' => [2],
30+
'select_multiple' => [2],
31+
'checkbox_checked' => true
32+
];
2833
$component->postMount([]);
2934

3035
$this->assertSame(

0 commit comments

Comments
 (0)