Skip to content

Commit

Permalink
Revert type casting because Symfony forms can return integer keys (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
willemverspyck committed Jun 17, 2022
1 parent 81d2c7e commit 2030cfb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/EventListener/ResizeFormListener.php
Expand Up @@ -88,8 +88,10 @@ public function preSetData(FormEvent $event): void
}

// First remove all rows except for the prototype row
// Type cast to string, because Symfony form can returns integer keys
foreach ($form as $name => $child) {
$form->remove($name);
// @phpstan-ignore-next-line
$form->remove((string) $name);
}

// Then add all rows again in the correct order
Expand Down Expand Up @@ -126,12 +128,15 @@ public function preSubmit(FormEvent $event): void
}

// Remove all empty rows except for the prototype row
// Type cast to string, because Symfony form can returns integer keys
foreach ($form as $name => $child) {
$form->remove($name);
// @phpstan-ignore-next-line
$form->remove((string) $name);
}

// Add all additional rows
foreach ($data as $name => $value) {
// Type cast to string, because Symfony form can returns integer keys
if (!$form->has((string) $name)) {
$buildOptions = [
'property_path' => '['.$name.']',
Expand Down Expand Up @@ -178,6 +183,7 @@ public function onSubmit(FormEvent $event): void
}

foreach ($data as $name => $child) {
// Type cast to string, because Symfony form can returns integer keys
if (!$form->has((string) $name)) {
unset($data[$name]);
}
Expand Down
37 changes: 27 additions & 10 deletions tests/EventListener/ResizeFormListenerTest.php
Expand Up @@ -190,23 +190,37 @@ public function testPreSubmitData(): void

$listener = new ResizeFormListener('form', $typeOptions, true, null);

$options = [
$options1 = [
'property_path' => '[baz]',
'default' => 'option',
];

$options2 = [
'property_path' => '[0]',
'default' => 'option',
];

$form = $this->createMock(Form::class);
$form->expects(static::once())
->method('getIterator')
->willReturn(new \ArrayIterator(['foo' => 'bar']));
$form->expects(static::once())
->willReturn(new \ArrayIterator([
'foo' => 'bar',
0 => 'daz',
]));
$form->expects(static::exactly(2))
->method('remove')
->with('foo');
$form->expects(static::once())
->withConsecutive(
['foo'],
[0]
);
$form->expects(static::exactly(2))
->method('add')
->with('baz', 'form', $options);
->withConsecutive(
['baz', 'form', $options1],
[0, 'form', $options2]
);

$data = ['baz' => 'caz'];
$data = ['baz' => 'caz', 0 => 'daz'];

$event = new FormEvent($form, $data);

Expand Down Expand Up @@ -295,23 +309,26 @@ public function testOnSubmit(): void

$form = $this->createMock(Form::class);
$form
->expects(static::exactly(3))
->expects(static::exactly(4))
->method('has')
->withConsecutive(
['foo'],
['bar'],
['baz']
['baz'],
[0]
)
->willReturnOnConsecutiveCalls(
false,
false,
true
true,
false,
);

$data = [
'foo' => 'foo-value',
'bar' => 'bar-value',
'baz' => 'baz-value',
0 => '0-value',
];

$removedData = [
Expand Down

0 comments on commit 2030cfb

Please sign in to comment.