This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Console/Tests
Fabien Potencier efd68e6b71 bug #14648 [Console] Fix first choice was invalid when using value (ogizanagi)
This PR was merged into the 2.7 branch.

Discussion
----------

[Console] Fix first choice was invalid when using value

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This PR solves the following issues encountered using question helper and choices questions:
- First choice was not selectable by value.
- ChoiceList with associative choices with mixed string and int keys has same issue with first choice.
- Fix inconsistency by always returning values as strings.

First point exemple:
![screenshot 2015-05-15 a 17 16 12](https://cloud.githubusercontent.com/assets/2211145/7655757/3344b39a-fb26-11e4-9fe7-0775616619bf.PNG)

Last two points are mainly edge cases. Indeed, if a QuestionChoice has something like :
```php
array(
    '0' => 'No environment',
    '1' => 'My environment 1',
    'env_2' => 'My environment 2',
    3 => 'My environment 3',
);
```
as choices, you will not be able to select the first choice and get an `InvalidArgumentException`:

```
There were 2 errors:

1) Symfony\Component\Console\Tests\Helper\QuestionHelperTest::testChoiceFromChoicelistWithMixedKeys with data set #0 ('0', '0')
InvalidArgumentException: Value "0" is invalid

2) Symfony\Component\Console\Tests\Helper\QuestionHelperTest::testChoiceFromChoicelistWithMixedKeys with data set #1 ('No environment', '0')
InvalidArgumentException: Value "No environment" is invalid
```

Moreover, even if you were able to select by value (`No environment`), you'll get an integer instead of a string:
```
Failed asserting that '0' is identical to 0.
```
For more consistency, the solution is to always return a string.

The issue does not exist in 2.6, as the `QuestionChoice::getDefaultValidator` handled things differently.

Commits
-------

03e4ab6 [Console] Fix first choice was invalid when using value
2015-05-20 10:25:50 +02:00
..
Command feature #13220 [Console] Made output docopt compatible (WouterJ) 2015-04-08 07:37:48 +02:00
Descriptor [Console] Made output docopt compatible 2015-04-08 07:37:46 +02:00
Fixtures feature #13220 [Console] Made output docopt compatible (WouterJ) 2015-04-08 07:37:48 +02:00
Formatter Merge branch '2.3' into 2.6 2015-04-18 17:02:51 +02:00
Helper bug #14648 [Console] Fix first choice was invalid when using value (ogizanagi) 2015-05-20 10:25:50 +02:00
Input feature #13220 [Console] Made output docopt compatible (WouterJ) 2015-04-08 07:37:48 +02:00
Logger Merge branch '2.4' into 2.5 2014-09-22 11:14:18 +02:00
Output Merge branch '2.4' into 2.5 2014-07-08 14:21:33 +02:00
Tester [Console] added some tests for previous merge (refs #8626) 2013-09-30 21:59:36 +02:00
ApplicationTest.php Merge branch '2.3' into 2.6 2015-03-13 18:11:24 +01:00