Skip to content

Commit e649823

Browse files
add failing test for non-compound forms
1 parent 3b30c12 commit e649823

File tree

5 files changed

+94
-8
lines changed

5 files changed

+94
-8
lines changed

features/interactive.feature

+21
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,24 @@ Feature: It is possible to interactively fill in a form from the CLI
364364
[street] => foo
365365
)
366366
"""
367+
368+
Scenario: Non-compound form type in interactive mode
369+
When I run the command "form:non_compound_color" and I provide as input "blue" with parameters
370+
| Parameter | Value |
371+
| --color | yellow |
372+
Then the command has finished successfully
373+
And the output should contain
374+
"""
375+
Select color [yellow]:
376+
[red ] Red
377+
[blue ] Blue
378+
[yellow] Yellow
379+
>
380+
"""
381+
And the output should contain
382+
"""
383+
Array
384+
(
385+
[0] => blue
386+
)
387+
"""

features/non-interactive.feature

+14
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,17 @@ Feature: It is possible to interactively fill in a form from the CLI
6969
)
7070
)
7171
"""
72+
73+
Scenario: Non-compound form type in non-interactive mode
74+
When I run a command non-interactively with parameters
75+
| Parameter | Value |
76+
| command | form:non_compound_color |
77+
| --color | blue |
78+
Then the command has finished successfully
79+
And the output should contain
80+
"""
81+
Array
82+
(
83+
[0] => blue
84+
)
85+
"""

src/Console/Input/FormBasedInputDefinitionFactory.php

+17-8
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,28 @@ public function createForFormType(string $formType, array &$resources = []): Inp
3535

3636
$inputDefinition = new InputDefinition();
3737

38+
if (!$form->getConfig()->getCompound()) {
39+
$this->addFormToInputDefinition($form->getName(), $form, $inputDefinition);
40+
}
41+
3842
foreach ($form->all() as $name => $field) {
39-
if (!$this->isFormFieldSupported($field)) {
40-
continue;
41-
}
43+
$this->addFormToInputDefinition($name, $field, $inputDefinition);
44+
}
4245

43-
$type = InputOption::VALUE_REQUIRED;
44-
$default = $this->resolveDefaultValue($field);
45-
$description = FormUtil::label($field);
46+
return $inputDefinition;
47+
}
4648

47-
$inputDefinition->addOption(new InputOption($name, null, $type, $description, $default));
49+
private function addFormToInputDefinition(string $name, FormInterface $form, InputDefinition $inputDefinition): void
50+
{
51+
if (!$this->isFormFieldSupported($form)) {
52+
return;
4853
}
4954

50-
return $inputDefinition;
55+
$type = InputOption::VALUE_REQUIRED;
56+
$default = $this->resolveDefaultValue($form);
57+
$description = FormUtil::label($form);
58+
59+
$inputDefinition->addOption(new InputOption($name, null, $type, $description, $default));
5160
}
5261

5362
private function isFormFieldSupported(FormInterface $field): bool

test/Form/NonCompoundColorType.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
class NonCompoundColorType extends AbstractType
11+
{
12+
public function configureOptions(OptionsResolver $resolver)
13+
{
14+
$resolver->setDefaults([
15+
'label' => 'Select color',
16+
'choices' => [
17+
'Red' => 'red',
18+
'Blue' => 'blue',
19+
'Yellow' => 'yellow',
20+
],
21+
'data' => 'red',
22+
]);
23+
}
24+
25+
public function getParent()
26+
{
27+
return ChoiceType::class;
28+
}
29+
30+
public function getBlockPrefix()
31+
{
32+
return 'color';
33+
}
34+
}

test/config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ services:
5050
tags:
5151
- { name: console.command }
5252

53+
non_compound_color_command:
54+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
55+
arguments:
56+
- Matthias\SymfonyConsoleForm\Tests\Form\NonCompoundColorType
57+
- non_compound_color
58+
tags:
59+
- { name: console.command }
60+
5361
multi_select_command:
5462
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
5563
arguments:

0 commit comments

Comments
 (0)