| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Tests\Input; |
| 13 |
|
| 14 |
use PHPUnit\Framework\TestCase; |
| 15 |
use Symfony\Component\Console\Input\ArrayInput; |
| 16 |
use Symfony\Component\Console\Input\InputArgument; |
| 17 |
use Symfony\Component\Console\Input\InputDefinition; |
| 18 |
use Symfony\Component\Console\Input\InputOption; |
| 19 |
|
| 20 |
class ArrayInputTest extends TestCase |
| 21 |
{ |
| 22 |
public function testGetFirstArgument() |
| 23 |
{ |
| 24 |
$input = new ArrayInput([]); |
| 25 |
$this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null if no argument were passed'); |
| 26 |
$input = new ArrayInput(['name' => 'Fabien']); |
| 27 |
$this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument'); |
| 28 |
$input = new ArrayInput(['--foo' => 'bar', 'name' => 'Fabien']); |
| 29 |
$this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument'); |
| 30 |
} |
| 31 |
|
| 32 |
public function testHasParameterOption() |
| 33 |
{ |
| 34 |
$input = new ArrayInput(['name' => 'Fabien', '--foo' => 'bar']); |
| 35 |
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters'); |
| 36 |
$this->assertFalse($input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters'); |
| 37 |
|
| 38 |
$input = new ArrayInput(['--foo']); |
| 39 |
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters'); |
| 40 |
|
| 41 |
$input = new ArrayInput(['--foo', '--', '--bar']); |
| 42 |
$this->assertTrue($input->hasParameterOption('--bar'), '->hasParameterOption() returns true if an option is present in the passed parameters'); |
| 43 |
$this->assertFalse($input->hasParameterOption('--bar', true), '->hasParameterOption() returns false if an option is present in the passed parameters after an end of options signal'); |
| 44 |
} |
| 45 |
|
| 46 |
public function testGetParameterOption() |
| 47 |
{ |
| 48 |
$input = new ArrayInput(['name' => 'Fabien', '--foo' => 'bar']); |
| 49 |
$this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name'); |
| 50 |
$this->assertEquals('default', $input->getParameterOption('--bar', 'default'), '->getParameterOption() returns the default value if an option is not present in the passed parameters'); |
| 51 |
|
| 52 |
$input = new ArrayInput(['Fabien', '--foo' => 'bar']); |
| 53 |
$this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name'); |
| 54 |
|
| 55 |
$input = new ArrayInput(['--foo', '--', '--bar' => 'woop']); |
| 56 |
$this->assertEquals('woop', $input->getParameterOption('--bar'), '->getParameterOption() returns the correct value if an option is present in the passed parameters'); |
| 57 |
$this->assertEquals('default', $input->getParameterOption('--bar', 'default', true), '->getParameterOption() returns the default value if an option is present in the passed parameters after an end of options signal'); |
| 58 |
} |
| 59 |
|
| 60 |
public function testParseArguments() |
| 61 |
{ |
| 62 |
$input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name')])); |
| 63 |
|
| 64 |
$this->assertEquals(['name' => 'foo'], $input->getArguments(), '->parse() parses required arguments'); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @dataProvider provideOptions |
| 69 |
*/ |
| 70 |
public function testParseOptions($input, $options, $expectedOptions, $message) |
| 71 |
{ |
| 72 |
$input = new ArrayInput($input, new InputDefinition($options)); |
| 73 |
|
| 74 |
$this->assertEquals($expectedOptions, $input->getOptions(), $message); |
| 75 |
} |
| 76 |
|
| 77 |
public function provideOptions() |
| 78 |
{ |
| 79 |
return [ |
| 80 |
[ |
| 81 |
['--foo' => 'bar'], |
| 82 |
[new InputOption('foo')], |
| 83 |
['foo' => 'bar'], |
| 84 |
'->parse() parses long options', |
| 85 |
], |
| 86 |
[ |
| 87 |
['--foo' => 'bar'], |
| 88 |
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')], |
| 89 |
['foo' => 'bar'], |
| 90 |
'->parse() parses long options with a default value', |
| 91 |
], |
| 92 |
[ |
| 93 |
[], |
| 94 |
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')], |
| 95 |
['foo' => 'default'], |
| 96 |
'->parse() uses the default value for long options with value optional which are not passed', |
| 97 |
], |
| 98 |
[ |
| 99 |
['--foo' => null], |
| 100 |
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')], |
| 101 |
['foo' => null], |
| 102 |
'->parse() parses long options with a default value', |
| 103 |
], |
| 104 |
[ |
| 105 |
['-f' => 'bar'], |
| 106 |
[new InputOption('foo', 'f')], |
| 107 |
['foo' => 'bar'], |
| 108 |
'->parse() parses short options', |
| 109 |
], |
| 110 |
[ |
| 111 |
['--' => null, '-f' => 'bar'], |
| 112 |
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')], |
| 113 |
['foo' => 'default'], |
| 114 |
'->parse() does not parse opts after an end of options signal', |
| 115 |
], |
| 116 |
[ |
| 117 |
['--' => null], |
| 118 |
[], |
| 119 |
[], |
| 120 |
'->parse() does not choke on end of options signal', |
| 121 |
], |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @dataProvider provideInvalidInput |
| 127 |
*/ |
| 128 |
public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage) |
| 129 |
{ |
| 130 |
$this->expectException('InvalidArgumentException'); |
| 131 |
$this->expectExceptionMessage($expectedExceptionMessage); |
| 132 |
|
| 133 |
new ArrayInput($parameters, $definition); |
| 134 |
} |
| 135 |
|
| 136 |
public function provideInvalidInput() |
| 137 |
{ |
| 138 |
return [ |
| 139 |
[ |
| 140 |
['foo' => 'foo'], |
| 141 |
new InputDefinition([new InputArgument('name')]), |
| 142 |
'The "foo" argument does not exist.', |
| 143 |
], |
| 144 |
[ |
| 145 |
['--foo' => null], |
| 146 |
new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]), |
| 147 |
'The "--foo" option requires a value.', |
| 148 |
], |
| 149 |
[ |
| 150 |
['--foo' => 'foo'], |
| 151 |
new InputDefinition(), |
| 152 |
'The "--foo" option does not exist.', |
| 153 |
], |
| 154 |
[ |
| 155 |
['-o' => 'foo'], |
| 156 |
new InputDefinition(), |
| 157 |
'The "-o" option does not exist.', |
| 158 |
], |
| 159 |
]; |
| 160 |
} |
| 161 |
|
| 162 |
public function testToString() |
| 163 |
{ |
| 164 |
$input = new ArrayInput(['-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo', 'test2' => "A\nB'C"]); |
| 165 |
$this->assertEquals('-f -b=bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input); |
| 166 |
|
| 167 |
$input = new ArrayInput(['-b' => ['bval_1', 'bval_2'], '--f' => ['fval_1', 'fval_2']]); |
| 168 |
$this->assertSame('-b=bval_1 -b=bval_2 --f=fval_1 --f=fval_2', (string) $input); |
| 169 |
|
| 170 |
$input = new ArrayInput(['array_arg' => ['val_1', 'val_2']]); |
| 171 |
$this->assertSame('val_1 val_2', (string) $input); |
| 172 |
} |
| 173 |
} |
| 174 |
|