Skip to content

Commit 6529c0e

Browse files
Merge pull request #62 from matthiasnoback/add_test
Add test
2 parents f613de4 + 17cc7ce commit 6529c0e

File tree

8 files changed

+111
-9
lines changed

8 files changed

+111
-9
lines changed

features/non-interactive.feature

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ Feature: It is possible to interactively fill in a form from the CLI
8484
)
8585
"""
8686

87+
Scenario: A form field is optional and no option has been provided for it
88+
89+
The default form data for this command (EditUserCommand) has name Mario, lastName Rossi. When running the command,
90+
only --lastName is provided. Instead of setting name to null, the existing name should not be modified.
91+
92+
When I run a command non-interactively with parameters
93+
| Parameter | Value |
94+
| command | form:edit_user |
95+
| --lastName | Verdi |
96+
Then the command has finished successfully
97+
And the output should contain
98+
"""
99+
name: Mario, lastName: Verdi
100+
"""
101+
87102
Scenario: Nested form type in non-interactive mode
88103
When I run a command non-interactively with parameters
89104
| Parameter | Value |

src/Console/Helper/FormHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function interactUsingNamedForm(
6363

6464
$submittedData = $this->formInteractor->interactWith($form, $this->getHelperSet(), $input, $output);
6565

66-
$form->submit($submittedData);
66+
$form->submit($submittedData, false);
6767

6868
// save the current data
6969
$data = $form->getData();

src/Form/EventListener/UseInputOptionsAsEventDataEventSubscriber.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ private function convertInputToSubmittedData(InputInterface $input, FormInterfac
3939
$submittedData = $this->convertInputToSubmittedData($input, $field, $name ?? $form->getName());
4040
} else {
4141
$subName = $name === null ? $childName : $name . '[' . $childName . ']';
42-
$submittedData[$childName] = $this->convertInputToSubmittedData($input, $field, $subName);
42+
$subValue = $this->convertInputToSubmittedData($input, $field, $subName);
43+
if ($subValue !== null) {
44+
$submittedData[$childName] = $subValue;
45+
}
4346
}
4447
}
48+
if (empty($submittedData)) {
49+
$submittedData = null;
50+
}
4551
} else {
4652
$name = $name ?? $form->getName();
4753
if ($input->hasOption($name)) {

test/Command/EditUserCommand.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Matthias\SymfonyConsoleForm\Tests\Command;
6+
7+
use Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommandWithDefault;
8+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\User;
9+
10+
final class EditUserCommand extends PrintFormDataCommand implements FormBasedCommandWithDefault
11+
{
12+
public function getFormDefault()
13+
{
14+
$user = new User();
15+
$user->name = 'Mario';
16+
$user->lastName = 'Rossi';
17+
18+
return $user;
19+
}
20+
}

test/Command/PrintFormDataCommand.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
1212
{
1313
$formData = $this->formData();
1414

15-
$printData = array_map(function ($data) {
16-
if ($data instanceof \DateTime) {
17-
return $data->format(\DateTime::ISO8601);
18-
}
15+
if (is_object($formData) && method_exists($formData, '__toString')) {
16+
$printData = $formData->__toString();
17+
} else {
18+
$printData = print_r(
19+
array_map(function ($data) {
20+
if ($data instanceof \DateTime) {
21+
return $data->format(\DateTime::ISO8601);
22+
}
1923

20-
return $data;
21-
}, (array)$formData);
24+
return $data;
25+
}, (array)$formData),
26+
true
27+
);
28+
}
2229

23-
$output->write(print_r($printData, true));
30+
$output->write($printData);
2431

2532
return 0;
2633
}

test/Form/Data/User.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Matthias\SymfonyConsoleForm\Tests\Form\Data;
6+
7+
final class User
8+
{
9+
public string $name;
10+
public string $lastName;
11+
12+
public function __toString(): string
13+
{
14+
return 'name: ' . $this->name . ', lastName: ' . $this->lastName;
15+
}
16+
}

test/Form/EditUserType.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
6+
7+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\User;
8+
use Symfony\Component\Form\AbstractType;
9+
use Symfony\Component\Form\Extension\Core\Type\TextType;
10+
use Symfony\Component\Form\FormBuilderInterface;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
12+
13+
class EditUserType extends AbstractType
14+
{
15+
public function buildForm(FormBuilderInterface $builder, array $options)
16+
{
17+
$builder
18+
->add('name', TextType::class)
19+
->add('lastName', TextType::class);
20+
}
21+
22+
public function configureOptions(OptionsResolver $resolver)
23+
{
24+
$resolver->setDefaults(
25+
[
26+
'data_class' => User::class,
27+
]
28+
);
29+
}
30+
}

test/config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ services:
170170
tags:
171171
- { name: console.command }
172172

173+
edit_user_command:
174+
class: Matthias\SymfonyConsoleForm\Tests\Command\EditUserCommand
175+
arguments:
176+
- Matthias\SymfonyConsoleForm\Tests\Form\EditUserType
177+
- edit_user
178+
tags:
179+
- { name: console.command }
180+
173181
framework:
174182
form:
175183
csrf_protection: true

0 commit comments

Comments
 (0)