Skip to content

Commit de571ec

Browse files
committed
added test case
1 parent 3c8fc11 commit de571ec

File tree

9 files changed

+109
-14
lines changed

9 files changed

+109
-14
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"symfony/validator": "~5.0",
3838
"symfony/yaml": "~5.0",
3939
"symfony/security-csrf": "~5.0",
40-
"phpunit/phpunit": "^7"
40+
"phpunit/phpunit": "^7",
41+
"doctrine/collections": "^1.6"
4142
}
4243
}

features/interactive.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,35 @@ Feature: It is possible to interactively fill in a form from the CLI
202202
[name] => Jelmer
203203
"""
204204

205+
Scenario: Form Type with ArrayCollection
206+
When I run the command "form:array_collection_form_addresses" and I provide as input
207+
| Input |
208+
| y |
209+
| foo |
210+
| y |
211+
| bar |
212+
| n |
213+
And the output should contain
214+
"""
215+
Matthias\SymfonyConsoleForm\Tests\Model\Addresses Object
216+
(
217+
[addresses] => Doctrine\Common\Collections\ArrayCollection Object
218+
(
219+
[elements:Doctrine\Common\Collections\ArrayCollection:private] => Array
220+
(
221+
[0] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
222+
(
223+
[street] => foo
224+
)
225+
[1] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
226+
(
227+
[street] => bar
228+
)
229+
)
230+
)
231+
)
232+
"""
233+
205234
Scenario: Remove an address from pre filled collection of blocked addresses
206235
When I run the command "form:blocked_addresses" and I provide as input
207236
| Input |

src/Bridge/Interaction/CollectionInteractor.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public function interactWith(
5050
throw new CanNotInteractWithForm('Expected a "collection" form');
5151
}
5252

53-
$data = $form->getData();
54-
if (!$form->getConfig()->getOption('allow_add') && empty($data)) {
53+
$config = $form->getConfig();
54+
$data = $form->getData() ?: $config->getEmptyData();
55+
56+
if (!$config->getOption('allow_add') && empty($data)) {
5557
throw new FormNotReadyForInteraction(
5658
'The "collection" form should have the option "allow_add" or have existing entries'
5759
);
@@ -66,7 +68,7 @@ public function interactWith(
6668
$this->printHeader($form, $output);
6769

6870
$submittedData = [];
69-
$prototype = $form->getConfig()->getAttribute('prototype');
71+
$prototype = $config->getAttribute('prototype');
7072
$originalData = $prototype->getData();
7173

7274
$askIfEntryNeedsToBeSubmitted = function ($entryNumber) use ($helperSet, $input, $output) {
@@ -78,12 +80,12 @@ public function interactWith(
7880
$prototype->setData($entryData);
7981

8082
$submittedEntry = $this->formInteractor->interactWith($prototype, $helperSet, $input, $output);
81-
if (!$form->getConfig()->getOption('allow_delete') || $askIfEntryNeedsToBeSubmitted($key)) {
83+
if (!$config->getOption('allow_delete') || $askIfEntryNeedsToBeSubmitted($key)) {
8284
$submittedData[] = $submittedEntry;
8385
}
8486
}
8587

86-
if ($form->getConfig()->getOption('allow_add')) {
88+
if ($config->getOption('allow_add')) {
8789
// reset the prototype
8890
$prototype->setData($originalData);
8991
$key = count($submittedData) - 1;

test/Command/PrintFormDataCommand.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ 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_iterable($formData)) {
16+
$printData = array_map(function ($data) {
17+
if ($data instanceof \DateTime) {
18+
return $data->format(\DateTime::ISO8601);
19+
}
1920

20-
return $data;
21-
}, (array)$formData);
21+
return $data;
22+
}, (array)$formData);
23+
} else {
24+
$printData = $formData;
25+
}
2226

2327
$output->write(print_r($printData, true));
2428

test/Form/AddressType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Symfony\Component\Form\AbstractType;
66
use Symfony\Component\Form\Extension\Core\Type\TextType;
77
use Symfony\Component\Form\FormBuilderInterface;
8+
use Symfony\Component\Form\FormInterface;
89
use Symfony\Component\OptionsResolver\OptionsResolver;
10+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
911

1012
class AddressType extends AbstractType
1113
{
@@ -25,7 +27,10 @@ public function configureOptions(OptionsResolver $resolver)
2527
{
2628
$resolver->setDefaults(
2729
[
28-
'data_class' => 'Matthias\SymfonyConsoleForm\Tests\Form\Data\Address',
30+
'data_class' => Address::class,
31+
'empty_data' => function (FormInterface $form, $viewData) {
32+
return new Address('');
33+
}
2934
]
3035
);
3136
}

test/Form/AddressesType.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Matthias\SymfonyConsoleForm\Tests\Model\Addresses;
7+
use Symfony\Component\Form\AbstractType;
8+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
9+
use Symfony\Component\Form\FormBuilderInterface;
10+
use Symfony\Component\OptionsResolver\OptionsResolver;
11+
12+
class AddressesType extends AbstractType
13+
{
14+
public function buildForm(FormBuilderInterface $builder, array $options)
15+
{
16+
$builder->add(
17+
'addresses',
18+
CollectionType::class,
19+
[
20+
'entry_type' => AddressType::class,
21+
'allow_add' => true,
22+
'empty_data' => new ArrayCollection()
23+
]
24+
);
25+
}
26+
27+
public function configureOptions(OptionsResolver $resolver)
28+
{
29+
$resolver->setDefault('data_class', Addresses::class);
30+
}
31+
}

test/Model/Addresses.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Model;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
7+
class Addresses
8+
{
9+
public $addresses;
10+
11+
public function __construct()
12+
{
13+
$this->addresses = new ArrayCollection();
14+
}
15+
}

test/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ services:
2626
tags:
2727
- { name: console.command }
2828

29+
array_collection_form_addresses:
30+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
31+
arguments:
32+
- Matthias\SymfonyConsoleForm\Tests\Form\AddressesType
33+
- array_collection_form_addresses
34+
tags:
35+
- { name: console.command }
36+
2937
blocked_addresses_command:
3038
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
3139
arguments:

test/console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Matthias\SymfonyConsoleForm\Tests\AppKernel;
66
use Symfony\Bundle\FrameworkBundle\Console\Application;
7-
use Symfony\Component\Debug\Debug;
7+
use Symfony\Component\ErrorHandler\Debug;
88

99
Debug::enable();
1010

0 commit comments

Comments
 (0)