Skip to content

Commit a86fa6d

Browse files
add failing tests for choice_value form option
1 parent a7db37a commit a86fa6d

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ public function iRunTheCommandAndIProvideAsInput($name, TableNode $input)
5151
$this->runCommandWithInteractiveInput($name, $inputs);
5252
}
5353

54+
/**
55+
* @Given /^I run the command "([^"]*)" and I provide as input "([^"]*)" with parameters$/
56+
* @Given /^parameters"$/
57+
*/
58+
public function iRunTheCommandAndIProvideAsInputAndParameters($name, $input, TableNode $parameters)
59+
{
60+
$parameters = $parameters->getHash();
61+
$parameters = array_combine(
62+
array_column($parameters, 'Parameter'),
63+
array_column($parameters, 'Value')
64+
);
65+
66+
$this->runCommandWithInteractiveInputAndParameters($name, [$input], $parameters);
67+
}
68+
5469
/**
5570
* @Given /^I run the command "([^"]*)" and I provide as input "([^"]*)"$/
5671
*/
@@ -70,6 +85,12 @@ public function theOutputShouldBe(PyStringNode $expectedOutput)
7085
);
7186
}
7287

88+
private function runCommandWithInteractiveInputAndParameters($name, array $inputs, array $parameters)
89+
{
90+
$this->tester->setInputs($inputs);
91+
$this->tester->run(\array_merge(['command' => $name], $parameters), array('interactive' => true, 'decorated' => false));
92+
}
93+
7394
private function runCommandWithInteractiveInput($name, array $inputs)
7495
{
7596
$this->tester->setInputs($inputs);

features/interactive.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,30 @@ Feature: It is possible to interactively fill in a form from the CLI
325325
)
326326
"""
327327

328+
Scenario: Choice with object options in interactive mode
329+
Given I run the command "form:unstringable_choices_with_values" and I provide as input "55-rue-du-faubourg-saint-honoré" with parameters
330+
| Parameter | Value |
331+
| --address | 1600-pennsylvania-ave-nw |
332+
Then the command has finished successfully
333+
And the output should contain
334+
"""
335+
Select address [1600-pennsylvania-ave-nw]:
336+
[10-downing-street ] 10 Downing Street
337+
[1600-pennsylvania-ave-nw ] 1600 Pennsylvania Ave NW
338+
[55-rue-du-faubourg-saint-honoré] 55 Rue du Faubourg Saint-Honoré
339+
>
340+
"""
341+
And the output should contain
342+
"""
343+
Array
344+
(
345+
[address] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
346+
(
347+
[street] => 55 Rue du Faubourg Saint-Honoré
348+
)
349+
)
350+
"""
351+
328352
Scenario: Command with default form data
329353
When I run the command "form:default_value_command" and I provide as input
330354
| Input |

features/non-interactive.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,20 @@ Feature: It is possible to interactively fill in a form from the CLI
5252
"""
5353
There were form errors.
5454
"""
55+
56+
Scenario: Choice with object options in non-interactive mode
57+
When I run a command non-interactively with parameters
58+
| Parameter | Value |
59+
| command | form:unstringable_choices_with_values |
60+
| --address | 1600-pennsylvania-ave-nw |
61+
Then the command has finished successfully
62+
And the output should contain
63+
"""
64+
Array
65+
(
66+
[address] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
67+
(
68+
[street] => 1600 Pennsylvania Ave NW
69+
)
70+
)
71+
"""
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
6+
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
8+
use Symfony\Component\Form\FormBuilderInterface;
9+
10+
/**
11+
* Demonstrates handling of choice data which does not support conversion to string (Address has no __toString())
12+
*/
13+
class UnstringableChoicesWithValuesType extends AbstractType
14+
{
15+
public function buildForm(FormBuilderInterface $builder, array $options)
16+
{
17+
$builder
18+
->add('address', ChoiceType::class, [
19+
'label' => 'Select address',
20+
'choices' => [
21+
new Address('10 Downing Street'),
22+
new Address('1600 Pennsylvania Ave NW'),
23+
new Address('55 Rue du Faubourg Saint-Honoré'),
24+
],
25+
'choice_value' => function (Address $address) {
26+
return \strtolower(\str_replace(' ', '-', $address->street));
27+
},
28+
'choice_label' => function (Address $address) {
29+
return $address->street;
30+
},
31+
'data' => new Address('10 Downing Street')
32+
]);
33+
}
34+
}

test/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ services:
130130
tags:
131131
- { name: console.command }
132132

133+
unstringable_choices_with_values_command:
134+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
135+
arguments:
136+
- Matthias\SymfonyConsoleForm\Tests\Form\UnstringableChoicesWithValuesType
137+
- unstringable_choices_with_values
138+
tags:
139+
- { name: console.command }
140+
133141
framework:
134142
form:
135143
csrf_protection: true

0 commit comments

Comments
 (0)