bug #13630 [Console] fixed ArrayInput, if array contains 0 key. (arima-ryunosuke)

This PR was submitted for the 2.6 branch but it was merged into the 2.3 branch instead (closes #13630).

Discussion
----------

[Console] fixed ArrayInput, if array contains 0 key.

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

```php
$input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
var_dump($input->getParameterOption('--foo')); // this is 'Fabien'.
```

Because `in_array` third argument's default is `false`.
`in_array(0, $values)` returns `true` in many cases.

Commits
-------

a642e4b [Console] fixed ArrayInput, if array contains 0 key.
This commit is contained in:
Fabien Potencier 2015-02-11 11:33:23 +01:00
commit c6029aad14
2 changed files with 13 additions and 2 deletions

View File

@ -100,8 +100,10 @@ class ArrayInput extends Input
$values = (array) $values;
foreach ($this->parameters as $k => $v) {
if (is_int($k) && in_array($v, $values)) {
return true;
if (is_int($k)) {
if (in_array($v, $values)) {
return true;
}
} elseif (in_array($k, $values)) {
return $v;
}

View File

@ -38,6 +38,15 @@ class ArrayInputTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
}
public function testGetParameterOption()
{
$input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
$this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
$input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
$this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
}
public function testParseArguments()
{
$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));