Skip to content

Commit bac993f

Browse files
Add failing test for PR #61
1 parent f613de4 commit bac993f

File tree

6 files changed

+101
-7
lines changed

6 files changed

+101
-7
lines changed

features/non-interactive.feature

Lines changed: 15 additions & 0 deletions
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 |

test/Command/EditUserCommand.php

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

test/Command/PrintFormDataCommand.php

Lines changed: 14 additions & 7 deletions
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

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

test/Form/EditUserType.php

Lines changed: 30 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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)