Skip to content

Commit

Permalink
tests: Improve coverage of nested multiplier
Browse files Browse the repository at this point in the history
Pressing “Create” inside a nested multiplier with fields with default values
did not work before 39725d3.
This has been reported in the first bullet point in
#56

The second bullet point is already covered by `testSendNested`
change introduced in the fix.
  • Loading branch information
jtojnar committed Feb 18, 2024
1 parent 9b4e600 commit e6f05b4
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/unit/MultiplierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,78 @@ public function testSendNested()
], $send->getValues());
}

/**
* Pressing “Create” inside a nested multiplier with fields with default values.
* This has been reported in the first bullet point in
* https://github.com/contributte/forms-multiplier/issues/56
*/
public function testSendNestedInnerWithDefault()
{
$request = $this->services->form->createRequest(
MultiplierBuilder::create()
->beforeFormModifier(function (Form $form) {
$form->addGroup('testGroup');
})
->multiplierModifier(function (Multiplier $multiplier) {
$multiplier->onCreate[] = function (Container $container) {
$this->parameters['onCreate'][] = $container;
};
})
->containerModifier(function (Container $container) {
$container['m2'] = (new Multiplier(function (Container $container) {
$container->addText('bar2')->setDefaultValue('qux');
}));
$container['m2']->addCreateButton('create');
})
->createForm()
);
$request->setPost([
'm' => [
[
'bar' => 'foo',
'm2' => [
[
'bar2' => 'xx',
],
Multiplier::SUBMIT_CREATE_NAME => '',
],
],
['bar' => 'bar'],
],
]);

$send = $request->send();
$dom = $send->toDomQuery();
$this->assertDomHas($dom, 'input[name="m[0][bar]"]');
$this->assertDomHas($dom, 'input[name="m[0][m2][0][bar2]"]');
$this->assertDomHas($dom, 'input[name="m[0][m2][1][bar2]"]');
$this->assertDomHas($dom, 'input[name="m[0][m2][' . Multiplier::SUBMIT_CREATE_NAME . ']"]');

$form = $send->getForm();
$this->assertSame(
[
'm' => [
[
'bar' => 'foo',
'm2' => [
['bar2' => 'xx'],
['bar2' => 'qux'],
],
],
[
'bar' => 'bar',
'm2' => [
['bar2' => 'qux'],
],
],
],
],
// Pass form, otherwise the values would be limited to m[0][m2] validation scope,
// since that is where the submitter button is pressed.
$form->getValues('array', [$form])
);
}

public function testGroup()
{
$request = $this->services->form->createRequest(
Expand Down

0 comments on commit e6f05b4

Please sign in to comment.