Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't cast form->data to array if it's not iterable #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"symfony/validator": "~5.0",
"symfony/yaml": "~5.0",
"symfony/security-csrf": "~5.0",
"phpunit/phpunit": "^7"
"phpunit/phpunit": "^7",
"doctrine/collections": "^1.6"
}
}
29 changes: 29 additions & 0 deletions features/interactive.feature
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,35 @@ Feature: It is possible to interactively fill in a form from the CLI
[name] => Jelmer
"""

Scenario: Form Type with ArrayCollection
When I run the command "form:array_collection_form_addresses" and I provide as input
| Input |
| y |
| foo |
| y |
| bar |
| n |
And the output should contain
"""
Matthias\SymfonyConsoleForm\Tests\Model\Addresses Object
(
[addresses] => Doctrine\Common\Collections\ArrayCollection Object
(
[elements:Doctrine\Common\Collections\ArrayCollection:private] => Array
(
[0] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
(
[street] => foo
)
[1] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
(
[street] => bar
)
)
)
)
"""

Scenario: Remove an address from pre filled collection of blocked addresses
When I run the command "form:blocked_addresses" and I provide as input
| Input |
Expand Down
19 changes: 14 additions & 5 deletions src/Bridge/Interaction/CollectionInteractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,42 @@ public function interactWith(
throw new CanNotInteractWithForm('Expected a "collection" form');
}

if (!$form->getConfig()->getOption('allow_add') && empty($form->getData())) {
$config = $form->getConfig();
$data = $form->getData() ?: $config->getEmptyData();

if (!$config->getOption('allow_add') && empty($data)) {
throw new FormNotReadyForInteraction(
'The "collection" form should have the option "allow_add" or have existing entries'
);
}

if (!is_iterable($data)) {
throw new FormNotReadyForInteraction(
'The "collection" form must be iterable'
);
}

$this->printHeader($form, $output);

$submittedData = [];
$prototype = $form->getConfig()->getAttribute('prototype');
$prototype = $config->getAttribute('prototype');
$originalData = $prototype->getData();

$askIfEntryNeedsToBeSubmitted = function ($entryNumber) use ($helperSet, $input, $output) {
return $this->askIfExistingEntryShouldBeAdded($helperSet, $input, $output, $entryNumber);
};

foreach ((array) $form->getData() as $key => $entryData) {
foreach ($data as $key => $entryData) {
$this->printEditEntryHeader($key, $output);
$prototype->setData($entryData);

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

if ($form->getConfig()->getOption('allow_add')) {
if ($config->getOption('allow_add')) {
// reset the prototype
$prototype->setData($originalData);
$key = count($submittedData) - 1;
Expand Down
16 changes: 10 additions & 6 deletions test/Command/PrintFormDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$formData = $this->formData();

$printData = array_map(function ($data) {
if ($data instanceof \DateTime) {
return $data->format(\DateTime::ISO8601);
}
if (is_iterable($formData)) {
$printData = array_map(function ($data) {
if ($data instanceof \DateTime) {
return $data->format(\DateTime::ISO8601);
}

return $data;
}, (array)$formData);
return $data;
}, (array)$formData);
} else {
$printData = $formData;
}

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

Expand Down
4 changes: 4 additions & 0 deletions test/Form/AddressType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;

Expand All @@ -27,6 +28,9 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults(
[
'data_class' => Address::class,
'empty_data' => function () {
return new Address('');
}
]
);
}
Expand Down
31 changes: 31 additions & 0 deletions test/Form/AddressesType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace Matthias\SymfonyConsoleForm\Tests\Form;

use Doctrine\Common\Collections\ArrayCollection;
use Matthias\SymfonyConsoleForm\Tests\Model\Addresses;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AddressesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'addresses',
CollectionType::class,
[
'entry_type' => AddressType::class,
'allow_add' => true,
'empty_data' => new ArrayCollection()
]
);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('data_class', Addresses::class);
}
}
15 changes: 15 additions & 0 deletions test/Model/Addresses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace Matthias\SymfonyConsoleForm\Tests\Model;

use Doctrine\Common\Collections\ArrayCollection;

class Addresses
{
public $addresses;

public function __construct()
{
$this->addresses = new ArrayCollection();
}
}
8 changes: 8 additions & 0 deletions test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ services:
tags:
- { name: console.command }

array_collection_form_addresses:
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
arguments:
- Matthias\SymfonyConsoleForm\Tests\Form\AddressesType
- array_collection_form_addresses
tags:
- { name: console.command }

blocked_addresses_command:
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
arguments:
Expand Down