Add Composer autoloading functions and PHPStan for testing

This commit is contained in:
Alex Cabal 2019-02-26 13:03:45 -06:00
parent e198c4db65
commit f5d7d4e02a
1518 changed files with 169063 additions and 30 deletions

View file

@ -0,0 +1,458 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests\Input;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
class ArgvInputTest extends TestCase
{
public function testConstructor()
{
$_SERVER['argv'] = ['cli.php', 'foo'];
$input = new ArgvInput();
$r = new \ReflectionObject($input);
$p = $r->getProperty('tokens');
$p->setAccessible(true);
$this->assertEquals(['foo'], $p->getValue($input), '__construct() automatically get its input from the argv server variable');
}
public function testParseArguments()
{
$input = new ArgvInput(['cli.php', 'foo']);
$input->bind(new InputDefinition([new InputArgument('name')]));
$this->assertEquals(['name' => 'foo'], $input->getArguments(), '->parse() parses required arguments');
$input->bind(new InputDefinition([new InputArgument('name')]));
$this->assertEquals(['name' => 'foo'], $input->getArguments(), '->parse() is stateless');
}
/**
* @dataProvider provideOptions
*/
public function testParseOptions($input, $options, $expectedOptions, $message)
{
$input = new ArgvInput($input);
$input->bind(new InputDefinition($options));
$this->assertSame($expectedOptions, $input->getOptions(), $message);
}
public function provideOptions()
{
return [
[
['cli.php', '--foo'],
[new InputOption('foo')],
['foo' => true],
'->parse() parses long options without a value',
],
[
['cli.php', '--foo=bar'],
[new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)],
['foo' => 'bar'],
'->parse() parses long options with a required value (with a = separator)',
],
[
['cli.php', '--foo', 'bar'],
[new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)],
['foo' => 'bar'],
'->parse() parses long options with a required value (with a space separator)',
],
[
['cli.php', '--foo='],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)],
['foo' => ''],
'->parse() parses long options with optional value which is empty (with a = separator) as empty string',
],
[
['cli.php', '--foo=', 'bar'],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)],
['foo' => ''],
'->parse() parses long options with optional value without value specified or an empty string (with a = separator) followed by an argument as empty string',
],
[
['cli.php', 'bar', '--foo'],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)],
['foo' => null],
'->parse() parses long options with optional value which is empty (with a = separator) preceded by an argument',
],
[
['cli.php', '--foo', '', 'bar'],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)],
['foo' => ''],
'->parse() parses long options with optional value which is empty as empty string even followed by an argument',
],
[
['cli.php', '--foo'],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)],
['foo' => null],
'->parse() parses long options with optional value specified with no separator and no value as null',
],
[
['cli.php', '-f'],
[new InputOption('foo', 'f')],
['foo' => true],
'->parse() parses short options without a value',
],
[
['cli.php', '-fbar'],
[new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)],
['foo' => 'bar'],
'->parse() parses short options with a required value (with no separator)',
],
[
['cli.php', '-f', 'bar'],
[new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)],
['foo' => 'bar'],
'->parse() parses short options with a required value (with a space separator)',
],
[
['cli.php', '-f', ''],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)],
['foo' => ''],
'->parse() parses short options with an optional empty value',
],
[
['cli.php', '-f', '', 'foo'],
[new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)],
['foo' => ''],
'->parse() parses short options with an optional empty value followed by an argument',
],
[
['cli.php', '-f', '', '-b'],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')],
['foo' => '', 'bar' => true],
'->parse() parses short options with an optional empty value followed by an option',
],
[
['cli.php', '-f', '-b', 'foo'],
[new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')],
['foo' => null, 'bar' => true],
'->parse() parses short options with an optional value which is not present',
],
[
['cli.php', '-fb'],
[new InputOption('foo', 'f'), new InputOption('bar', 'b')],
['foo' => true, 'bar' => true],
'->parse() parses short options when they are aggregated as a single one',
],
[
['cli.php', '-fb', 'bar'],
[new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED)],
['foo' => true, 'bar' => 'bar'],
'->parse() parses short options when they are aggregated as a single one and the last one has a required value',
],
[
['cli.php', '-fb', 'bar'],
[new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)],
['foo' => true, 'bar' => 'bar'],
'->parse() parses short options when they are aggregated as a single one and the last one has an optional value',
],
[
['cli.php', '-fbbar'],
[new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)],
['foo' => true, 'bar' => 'bar'],
'->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator',
],
[
['cli.php', '-fbbar'],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)],
['foo' => 'bbar', 'bar' => null],
'->parse() parses short options when they are aggregated as a single one and one of them takes a value',
],
];
}
/**
* @dataProvider provideInvalidInput
*/
public function testInvalidInput($argv, $definition, $expectedExceptionMessage)
{
if (method_exists($this, 'expectException')) {
$this->expectException('RuntimeException');
$this->expectExceptionMessage($expectedExceptionMessage);
} else {
$this->setExpectedException('RuntimeException', $expectedExceptionMessage);
}
$input = new ArgvInput($argv);
$input->bind($definition);
}
public function provideInvalidInput()
{
return [
[
['cli.php', '--foo'],
new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]),
'The "--foo" option requires a value.',
],
[
['cli.php', '-f'],
new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]),
'The "--foo" option requires a value.',
],
[
['cli.php', '-ffoo'],
new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE)]),
'The "-o" option does not exist.',
],
[
['cli.php', '--foo=bar'],
new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE)]),
'The "--foo" option does not accept a value.',
],
[
['cli.php', 'foo', 'bar'],
new InputDefinition(),
'No arguments expected, got "foo".',
],
[
['cli.php', 'foo', 'bar'],
new InputDefinition([new InputArgument('number')]),
'Too many arguments, expected arguments "number".',
],
[
['cli.php', 'foo', 'bar', 'zzz'],
new InputDefinition([new InputArgument('number'), new InputArgument('county')]),
'Too many arguments, expected arguments "number" "county".',
],
[
['cli.php', '--foo'],
new InputDefinition(),
'The "--foo" option does not exist.',
],
[
['cli.php', '-f'],
new InputDefinition(),
'The "-f" option does not exist.',
],
[
['cli.php', '-1'],
new InputDefinition([new InputArgument('number')]),
'The "-1" option does not exist.',
],
[
['cli.php', '-fЩ'],
new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE)]),
'The "-Щ" option does not exist.',
],
];
}
public function testParseArrayArgument()
{
$input = new ArgvInput(['cli.php', 'foo', 'bar', 'baz', 'bat']);
$input->bind(new InputDefinition([new InputArgument('name', InputArgument::IS_ARRAY)]));
$this->assertEquals(['name' => ['foo', 'bar', 'baz', 'bat']], $input->getArguments(), '->parse() parses array arguments');
}
public function testParseArrayOption()
{
$input = new ArgvInput(['cli.php', '--name=foo', '--name=bar', '--name=baz']);
$input->bind(new InputDefinition([new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY)]));
$this->assertEquals(['name' => ['foo', 'bar', 'baz']], $input->getOptions(), '->parse() parses array options ("--option=value" syntax)');
$input = new ArgvInput(['cli.php', '--name', 'foo', '--name', 'bar', '--name', 'baz']);
$input->bind(new InputDefinition([new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY)]));
$this->assertEquals(['name' => ['foo', 'bar', 'baz']], $input->getOptions(), '->parse() parses array options ("--option value" syntax)');
$input = new ArgvInput(['cli.php', '--name=foo', '--name=bar', '--name=']);
$input->bind(new InputDefinition([new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY)]));
$this->assertSame(['name' => ['foo', 'bar', '']], $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
$input = new ArgvInput(['cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption']);
$input->bind(new InputDefinition([
new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
new InputOption('anotherOption', null, InputOption::VALUE_NONE),
]));
$this->assertSame(['name' => ['foo', 'bar', null], 'anotherOption' => true], $input->getOptions(), '->parse() parses empty array options ("--option value" syntax)');
}
public function testParseNegativeNumberAfterDoubleDash()
{
$input = new ArgvInput(['cli.php', '--', '-1']);
$input->bind(new InputDefinition([new InputArgument('number')]));
$this->assertEquals(['number' => '-1'], $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
$input = new ArgvInput(['cli.php', '-f', 'bar', '--', '-1']);
$input->bind(new InputDefinition([new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)]));
$this->assertEquals(['foo' => 'bar'], $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
$this->assertEquals(['number' => '-1'], $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
}
public function testParseEmptyStringArgument()
{
$input = new ArgvInput(['cli.php', '-f', 'bar', '']);
$input->bind(new InputDefinition([new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)]));
$this->assertEquals(['empty' => ''], $input->getArguments(), '->parse() parses empty string arguments');
}
public function testGetFirstArgument()
{
$input = new ArgvInput(['cli.php', '-fbbar']);
$this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null when there is no arguments');
$input = new ArgvInput(['cli.php', '-fbbar', 'foo']);
$this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
}
public function testHasParameterOption()
{
$input = new ArgvInput(['cli.php', '-f', 'foo']);
$this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
$input = new ArgvInput(['cli.php', '-etest']);
$this->assertTrue($input->hasParameterOption('-e'), '->hasParameterOption() returns true if the given short option is in the raw input');
$this->assertFalse($input->hasParameterOption('-s'), '->hasParameterOption() returns true if the given short option is in the raw input');
$input = new ArgvInput(['cli.php', '--foo', 'foo']);
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
$input = new ArgvInput(['cli.php', 'foo']);
$this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
$input = new ArgvInput(['cli.php', '--foo=bar']);
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given option with provided value is in the raw input');
}
public function testHasParameterOptionOnlyOptions()
{
$input = new ArgvInput(['cli.php', '-f', 'foo']);
$this->assertTrue($input->hasParameterOption('-f', true), '->hasParameterOption() returns true if the given short option is in the raw input');
$input = new ArgvInput(['cli.php', '--foo', '--', 'foo']);
$this->assertTrue($input->hasParameterOption('--foo', true), '->hasParameterOption() returns true if the given long option is in the raw input');
$input = new ArgvInput(['cli.php', '--foo=bar', 'foo']);
$this->assertTrue($input->hasParameterOption('--foo', true), '->hasParameterOption() returns true if the given long option with provided value is in the raw input');
$input = new ArgvInput(['cli.php', '--', '--foo']);
$this->assertFalse($input->hasParameterOption('--foo', true), '->hasParameterOption() returns false if the given option is in the raw input but after an end of options signal');
}
public function testHasParameterOptionEdgeCasesAndLimitations()
{
$input = new ArgvInput(['cli.php', '-fh']);
// hasParameterOption does not know if the previous short option, -f,
// takes a value or not. If -f takes a value, then -fh does NOT include
// -h; Otherwise it does. Since we do not know which short options take
// values, hasParameterOption does not support this use-case.
$this->assertFalse($input->hasParameterOption('-h'), '->hasParameterOption() returns true if the given short option is in the raw input');
// hasParameterOption does detect that `-fh` contains `-f`, since
// `-f` is the first short option in the set.
$this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
// The test below happens to pass, although it might make more sense
// to disallow it, and require the use of
// $input->hasParameterOption('-f') && $input->hasParameterOption('-h')
// instead.
$this->assertTrue($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
// In theory, if -fh is supported, then -hf should also work.
// However, this is not supported.
$this->assertFalse($input->hasParameterOption('-hf'), '->hasParameterOption() returns true if the given short option is in the raw input');
$input = new ArgvInput(['cli.php', '-f', '-h']);
// If hasParameterOption('-fh') is supported for 'cli.php -fh', then
// one might also expect that it should also be supported for
// 'cli.php -f -h'. However, this is not supported.
$this->assertFalse($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
}
public function testNoWarningOnInvalidParameterOption()
{
$input = new ArgvInput(['cli.php', '-edev']);
$this->assertTrue($input->hasParameterOption(['-e', '']));
// No warning thrown
$this->assertFalse($input->hasParameterOption(['-m', '']));
$this->assertEquals('dev', $input->getParameterOption(['-e', '']));
// No warning thrown
$this->assertFalse($input->getParameterOption(['-m', '']));
}
public function testToString()
{
$input = new ArgvInput(['cli.php', '-f', 'foo']);
$this->assertEquals('-f foo', (string) $input);
$input = new ArgvInput(['cli.php', '-f', '--bar=foo', 'a b c d', "A\nB'C"]);
$this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
}
/**
* @dataProvider provideGetParameterOptionValues
*/
public function testGetParameterOptionEqualSign($argv, $key, $default, $onlyParams, $expected)
{
$input = new ArgvInput($argv);
$this->assertEquals($expected, $input->getParameterOption($key, $default, $onlyParams), '->getParameterOption() returns the expected value');
}
public function provideGetParameterOptionValues()
{
return [
[['app/console', 'foo:bar'], '-e', 'default', false, 'default'],
[['app/console', 'foo:bar', '-e', 'dev'], '-e', 'default', false, 'dev'],
[['app/console', 'foo:bar', '--env=dev'], '--env', 'default', false, 'dev'],
[['app/console', 'foo:bar', '-e', 'dev'], ['-e', '--env'], 'default', false, 'dev'],
[['app/console', 'foo:bar', '--env=dev'], ['-e', '--env'], 'default', false, 'dev'],
[['app/console', 'foo:bar', '--env=dev', '--en=1'], ['--en'], 'default', false, '1'],
[['app/console', 'foo:bar', '--env=dev', '', '--en=1'], ['--en'], 'default', false, '1'],
[['app/console', 'foo:bar', '--env', 'val'], '--env', 'default', false, 'val'],
[['app/console', 'foo:bar', '--env', 'val', '--dummy'], '--env', 'default', false, 'val'],
[['app/console', 'foo:bar', '--', '--env=dev'], '--env', 'default', false, 'dev'],
[['app/console', 'foo:bar', '--', '--env=dev'], '--env', 'default', true, 'default'],
];
}
public function testParseSingleDashAsArgument()
{
$input = new ArgvInput(['cli.php', '-']);
$input->bind(new InputDefinition([new InputArgument('file')]));
$this->assertEquals(['file' => '-'], $input->getArguments(), '->parse() parses single dash as an argument');
}
public function testParseOptionWithValueOptionalGivenEmptyAndRequiredArgument()
{
$input = new ArgvInput(['cli.php', '--foo=', 'bar']);
$input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)]));
$this->assertEquals(['foo' => null], $input->getOptions(), '->parse() parses optional options with empty value as null');
$this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses required arguments');
$input = new ArgvInput(['cli.php', '--foo=0', 'bar']);
$input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)]));
$this->assertEquals(['foo' => '0'], $input->getOptions(), '->parse() parses optional options with empty value as null');
$this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses required arguments');
}
public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
{
$input = new ArgvInput(['cli.php', '--foo=', 'bar']);
$input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL)]));
$this->assertEquals(['foo' => null], $input->getOptions(), '->parse() parses optional options with empty value as null');
$this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses optional arguments');
$input = new ArgvInput(['cli.php', '--foo=0', 'bar']);
$input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL)]));
$this->assertEquals(['foo' => '0'], $input->getOptions(), '->parse() parses optional options with empty value as null');
$this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses optional arguments');
}
}

View file

@ -0,0 +1,177 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests\Input;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
class ArrayInputTest extends TestCase
{
public function testGetFirstArgument()
{
$input = new ArrayInput([]);
$this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null if no argument were passed');
$input = new ArrayInput(['name' => 'Fabien']);
$this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
$input = new ArrayInput(['--foo' => 'bar', 'name' => 'Fabien']);
$this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
}
public function testHasParameterOption()
{
$input = new ArrayInput(['name' => 'Fabien', '--foo' => 'bar']);
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
$this->assertFalse($input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters');
$input = new ArrayInput(['--foo']);
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
$input = new ArrayInput(['--foo', '--', '--bar']);
$this->assertTrue($input->hasParameterOption('--bar'), '->hasParameterOption() returns true if an option is present in the passed parameters');
$this->assertFalse($input->hasParameterOption('--bar', true), '->hasParameterOption() returns false if an option is present in the passed parameters after an end of options signal');
}
public function testGetParameterOption()
{
$input = new ArrayInput(['name' => 'Fabien', '--foo' => 'bar']);
$this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
$this->assertEquals('default', $input->getParameterOption('--bar', 'default'), '->getParameterOption() returns the default value if an option is not present in the passed parameters');
$input = new ArrayInput(['Fabien', '--foo' => 'bar']);
$this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
$input = new ArrayInput(['--foo', '--', '--bar' => 'woop']);
$this->assertEquals('woop', $input->getParameterOption('--bar'), '->getParameterOption() returns the correct value if an option is present in the passed parameters');
$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');
}
public function testParseArguments()
{
$input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name')]));
$this->assertEquals(['name' => 'foo'], $input->getArguments(), '->parse() parses required arguments');
}
/**
* @dataProvider provideOptions
*/
public function testParseOptions($input, $options, $expectedOptions, $message)
{
$input = new ArrayInput($input, new InputDefinition($options));
$this->assertEquals($expectedOptions, $input->getOptions(), $message);
}
public function provideOptions()
{
return [
[
['--foo' => 'bar'],
[new InputOption('foo')],
['foo' => 'bar'],
'->parse() parses long options',
],
[
['--foo' => 'bar'],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')],
['foo' => 'bar'],
'->parse() parses long options with a default value',
],
[
[],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')],
['foo' => 'default'],
'->parse() uses the default value for long options with value optional which are not passed',
],
[
['--foo' => null],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')],
['foo' => null],
'->parse() parses long options with a default value',
],
[
['-f' => 'bar'],
[new InputOption('foo', 'f')],
['foo' => 'bar'],
'->parse() parses short options',
],
[
['--' => null, '-f' => 'bar'],
[new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')],
['foo' => 'default'],
'->parse() does not parse opts after an end of options signal',
],
[
['--' => null],
[],
[],
'->parse() does not choke on end of options signal',
],
];
}
/**
* @dataProvider provideInvalidInput
*/
public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
{
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($expectedExceptionMessage);
} else {
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
}
new ArrayInput($parameters, $definition);
}
public function provideInvalidInput()
{
return [
[
['foo' => 'foo'],
new InputDefinition([new InputArgument('name')]),
'The "foo" argument does not exist.',
],
[
['--foo' => null],
new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]),
'The "--foo" option requires a value.',
],
[
['--foo' => 'foo'],
new InputDefinition(),
'The "--foo" option does not exist.',
],
[
['-o' => 'foo'],
new InputDefinition(),
'The "-o" option does not exist.',
],
];
}
public function testToString()
{
$input = new ArrayInput(['-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo', 'test2' => "A\nB'C"]);
$this->assertEquals('-f -b=bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input);
$input = new ArrayInput(['-b' => ['bval_1', 'bval_2'], '--f' => ['fval_1', 'fval_2']]);
$this->assertSame('-b=bval_1 -b=bval_2 --f=fval_1 --f=fval_2', (string) $input);
$input = new ArrayInput(['array_arg' => ['val_1', 'val_2']]);
$this->assertSame('val_1 val_2', (string) $input);
}
}

View file

@ -0,0 +1,103 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests\Input;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputArgument;
class InputArgumentTest extends TestCase
{
public function testConstructor()
{
$argument = new InputArgument('foo');
$this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
}
public function testModes()
{
$argument = new InputArgument('foo');
$this->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default');
$argument = new InputArgument('foo', null);
$this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
$argument = new InputArgument('foo', InputArgument::OPTIONAL);
$this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
$argument = new InputArgument('foo', InputArgument::REQUIRED);
$this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Argument mode "-1" is not valid.
*/
public function testInvalidModes()
{
new InputArgument('foo', '-1');
}
public function testIsArray()
{
$argument = new InputArgument('foo', InputArgument::IS_ARRAY);
$this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
$this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
$argument = new InputArgument('foo', InputArgument::OPTIONAL);
$this->assertFalse($argument->isArray(), '->isArray() returns false if the argument can not be an array');
}
public function testGetDescription()
{
$argument = new InputArgument('foo', null, 'Some description');
$this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
}
public function testGetDefault()
{
$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
$this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
}
public function testSetDefault()
{
$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
$argument->setDefault(null);
$this->assertNull($argument->getDefault(), '->setDefault() can reset the default value by passing null');
$argument->setDefault('another');
$this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
$argument->setDefault([1, 2]);
$this->assertEquals([1, 2], $argument->getDefault(), '->setDefault() changes the default value');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot set a default value except for InputArgument::OPTIONAL mode.
*/
public function testSetDefaultWithRequiredArgument()
{
$argument = new InputArgument('foo', InputArgument::REQUIRED);
$argument->setDefault('default');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage A default value for an array argument must be an array.
*/
public function testSetDefaultWithArrayArgument()
{
$argument = new InputArgument('foo', InputArgument::IS_ARRAY);
$argument->setDefault('default');
}
}

View file

@ -0,0 +1,407 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests\Input;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
class InputDefinitionTest extends TestCase
{
protected static $fixtures;
protected $foo;
protected $bar;
protected $foo1;
protected $foo2;
public static function setUpBeforeClass()
{
self::$fixtures = __DIR__.'/../Fixtures/';
}
public function testConstructorArguments()
{
$this->initializeArguments();
$definition = new InputDefinition();
$this->assertEquals([], $definition->getArguments(), '__construct() creates a new InputDefinition object');
$definition = new InputDefinition([$this->foo, $this->bar]);
$this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getArguments(), '__construct() takes an array of InputArgument objects as its first argument');
}
public function testConstructorOptions()
{
$this->initializeOptions();
$definition = new InputDefinition();
$this->assertEquals([], $definition->getOptions(), '__construct() creates a new InputDefinition object');
$definition = new InputDefinition([$this->foo, $this->bar]);
$this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getOptions(), '__construct() takes an array of InputOption objects as its first argument');
}
public function testSetArguments()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->setArguments([$this->foo]);
$this->assertEquals(['foo' => $this->foo], $definition->getArguments(), '->setArguments() sets the array of InputArgument objects');
$definition->setArguments([$this->bar]);
$this->assertEquals(['bar' => $this->bar], $definition->getArguments(), '->setArguments() clears all InputArgument objects');
}
public function testAddArguments()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments([$this->foo]);
$this->assertEquals(['foo' => $this->foo], $definition->getArguments(), '->addArguments() adds an array of InputArgument objects');
$definition->addArguments([$this->bar]);
$this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getArguments(), '->addArguments() does not clear existing InputArgument objects');
}
public function testAddArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo);
$this->assertEquals(['foo' => $this->foo], $definition->getArguments(), '->addArgument() adds a InputArgument object');
$definition->addArgument($this->bar);
$this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getArguments(), '->addArgument() adds a InputArgument object');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage An argument with name "foo" already exists.
*/
public function testArgumentsMustHaveDifferentNames()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo);
$definition->addArgument($this->foo1);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot add an argument after an array argument.
*/
public function testArrayArgumentHasToBeLast()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
$definition->addArgument(new InputArgument('anotherbar'));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot add a required argument after an optional one.
*/
public function testRequiredArgumentCannotFollowAnOptionalOne()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo);
$definition->addArgument($this->foo2);
}
public function testGetArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments([$this->foo]);
$this->assertEquals($this->foo, $definition->getArgument('foo'), '->getArgument() returns a InputArgument by its name');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "bar" argument does not exist.
*/
public function testGetInvalidArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments([$this->foo]);
$definition->getArgument('bar');
}
public function testHasArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments([$this->foo]);
$this->assertTrue($definition->hasArgument('foo'), '->hasArgument() returns true if a InputArgument exists for the given name');
$this->assertFalse($definition->hasArgument('bar'), '->hasArgument() returns false if a InputArgument exists for the given name');
}
public function testGetArgumentRequiredCount()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo2);
$this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
$definition->addArgument($this->foo);
$this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
}
public function testGetArgumentCount()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo2);
$this->assertEquals(1, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
$definition->addArgument($this->foo);
$this->assertEquals(2, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
}
public function testGetArgumentDefaults()
{
$definition = new InputDefinition([
new InputArgument('foo1', InputArgument::OPTIONAL),
new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'),
new InputArgument('foo3', InputArgument::OPTIONAL | InputArgument::IS_ARRAY),
// new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', [1, 2]),
]);
$this->assertEquals(['foo1' => null, 'foo2' => 'default', 'foo3' => []], $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
$definition = new InputDefinition([
new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', [1, 2]),
]);
$this->assertEquals(['foo4' => [1, 2]], $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
}
public function testSetOptions()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$this->assertEquals(['foo' => $this->foo], $definition->getOptions(), '->setOptions() sets the array of InputOption objects');
$definition->setOptions([$this->bar]);
$this->assertEquals(['bar' => $this->bar], $definition->getOptions(), '->setOptions() clears all InputOption objects');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "-f" option does not exist.
*/
public function testSetOptionsClearsOptions()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$definition->setOptions([$this->bar]);
$definition->getOptionForShortcut('f');
}
public function testAddOptions()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$this->assertEquals(['foo' => $this->foo], $definition->getOptions(), '->addOptions() adds an array of InputOption objects');
$definition->addOptions([$this->bar]);
$this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getOptions(), '->addOptions() does not clear existing InputOption objects');
}
public function testAddOption()
{
$this->initializeOptions();
$definition = new InputDefinition();
$definition->addOption($this->foo);
$this->assertEquals(['foo' => $this->foo], $definition->getOptions(), '->addOption() adds a InputOption object');
$definition->addOption($this->bar);
$this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getOptions(), '->addOption() adds a InputOption object');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage An option named "foo" already exists.
*/
public function testAddDuplicateOption()
{
$this->initializeOptions();
$definition = new InputDefinition();
$definition->addOption($this->foo);
$definition->addOption($this->foo2);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage An option with shortcut "f" already exists.
*/
public function testAddDuplicateShortcutOption()
{
$this->initializeOptions();
$definition = new InputDefinition();
$definition->addOption($this->foo);
$definition->addOption($this->foo1);
}
public function testGetOption()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "--bar" option does not exist.
*/
public function testGetInvalidOption()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$definition->getOption('bar');
}
public function testHasOption()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$this->assertTrue($definition->hasOption('foo'), '->hasOption() returns true if a InputOption exists for the given name');
$this->assertFalse($definition->hasOption('bar'), '->hasOption() returns false if a InputOption exists for the given name');
}
public function testHasShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$this->assertTrue($definition->hasShortcut('f'), '->hasShortcut() returns true if a InputOption exists for the given shortcut');
$this->assertFalse($definition->hasShortcut('b'), '->hasShortcut() returns false if a InputOption exists for the given shortcut');
}
public function testGetOptionForShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
}
public function testGetOptionForMultiShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->multi]);
$this->assertEquals($this->multi, $definition->getOptionForShortcut('m'), '->getOptionForShortcut() returns a InputOption by its shortcut');
$this->assertEquals($this->multi, $definition->getOptionForShortcut('mmm'), '->getOptionForShortcut() returns a InputOption by its shortcut');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "-l" option does not exist.
*/
public function testGetOptionForInvalidShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition([$this->foo]);
$definition->getOptionForShortcut('l');
}
public function testGetOptionDefaults()
{
$definition = new InputDefinition([
new InputOption('foo1', null, InputOption::VALUE_NONE),
new InputOption('foo2', null, InputOption::VALUE_REQUIRED),
new InputOption('foo3', null, InputOption::VALUE_REQUIRED, '', 'default'),
new InputOption('foo4', null, InputOption::VALUE_OPTIONAL),
new InputOption('foo5', null, InputOption::VALUE_OPTIONAL, '', 'default'),
new InputOption('foo6', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
new InputOption('foo7', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', [1, 2]),
]);
$defaults = [
'foo1' => false,
'foo2' => null,
'foo3' => 'default',
'foo4' => null,
'foo5' => 'default',
'foo6' => [],
'foo7' => [1, 2],
];
$this->assertSame($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
}
/**
* @dataProvider getGetSynopsisData
*/
public function testGetSynopsis(InputDefinition $definition, $expectedSynopsis, $message = null)
{
$this->assertEquals($expectedSynopsis, $definition->getSynopsis(), $message ? '->getSynopsis() '.$message : '');
}
public function getGetSynopsisData()
{
return [
[new InputDefinition([new InputOption('foo')]), '[--foo]', 'puts optional options in square brackets'],
[new InputDefinition([new InputOption('foo', 'f')]), '[-f|--foo]', 'separates shortcut with a pipe'],
[new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]), '[-f|--foo FOO]', 'uses shortcut as value placeholder'],
[new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)]), '[-f|--foo [FOO]]', 'puts optional values in square brackets'],
[new InputDefinition([new InputArgument('foo', InputArgument::REQUIRED)]), '<foo>', 'puts arguments in angle brackets'],
[new InputDefinition([new InputArgument('foo')]), '[<foo>]', 'puts optional arguments in square brackets'],
[new InputDefinition([new InputArgument('foo'), new InputArgument('bar')]), '[<foo> [<bar>]]', 'chains optional arguments inside brackets'],
[new InputDefinition([new InputArgument('foo', InputArgument::IS_ARRAY)]), '[<foo>...]', 'uses an ellipsis for array arguments'],
[new InputDefinition([new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY)]), '<foo>...', 'uses an ellipsis for required array arguments'],
[new InputDefinition([new InputOption('foo'), new InputArgument('foo', InputArgument::REQUIRED)]), '[--foo] [--] <foo>', 'puts [--] between options and arguments'],
];
}
public function testGetShortSynopsis()
{
$definition = new InputDefinition([new InputOption('foo'), new InputOption('bar'), new InputArgument('cat')]);
$this->assertEquals('[options] [--] [<cat>]', $definition->getSynopsis(true), '->getSynopsis(true) groups options in [options]');
}
protected function initializeArguments()
{
$this->foo = new InputArgument('foo');
$this->bar = new InputArgument('bar');
$this->foo1 = new InputArgument('foo');
$this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
}
protected function initializeOptions()
{
$this->foo = new InputOption('foo', 'f');
$this->bar = new InputOption('bar', 'b');
$this->foo1 = new InputOption('fooBis', 'f');
$this->foo2 = new InputOption('foo', 'p');
$this->multi = new InputOption('multi', 'm|mm|mmm');
}
}

View file

@ -0,0 +1,196 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests\Input;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputOption;
class InputOptionTest extends TestCase
{
public function testConstructor()
{
$option = new InputOption('foo');
$this->assertEquals('foo', $option->getName(), '__construct() takes a name as its first argument');
$option = new InputOption('--foo');
$this->assertEquals('foo', $option->getName(), '__construct() removes the leading -- of the option name');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.
*/
public function testArrayModeWithoutValue()
{
new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY);
}
public function testShortcut()
{
$option = new InputOption('foo', 'f');
$this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument');
$option = new InputOption('foo', '-f|-ff|fff');
$this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
$option = new InputOption('foo', ['f', 'ff', '-fff']);
$this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
$option = new InputOption('foo');
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
}
public function testModes()
{
$option = new InputOption('foo', 'f');
$this->assertFalse($option->acceptValue(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
$this->assertFalse($option->isValueRequired(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
$this->assertFalse($option->isValueOptional(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
$option = new InputOption('foo', 'f', null);
$this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
$this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$option = new InputOption('foo', 'f', InputOption::VALUE_REQUIRED);
$this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
$this->assertTrue($option->isValueRequired(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
$this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
$option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL);
$this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
$this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
$this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Option mode "-1" is not valid.
*/
public function testInvalidModes()
{
new InputOption('foo', 'f', '-1');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testEmptyNameIsInvalid()
{
new InputOption('');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testDoubleDashNameIsInvalid()
{
new InputOption('--');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testSingleDashOptionIsInvalid()
{
new InputOption('foo', '-');
}
public function testIsArray()
{
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
$this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array');
$option = new InputOption('foo', null, InputOption::VALUE_NONE);
$this->assertFalse($option->isArray(), '->isArray() returns false if the option can not be an array');
}
public function testGetDescription()
{
$option = new InputOption('foo', 'f', null, 'Some description');
$this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
}
public function testGetDefault()
{
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', 'default');
$this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
$option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
$this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
$option = new InputOption('foo', null, InputOption::VALUE_REQUIRED);
$this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
$this->assertEquals([], $option->getDefault(), '->getDefault() returns an empty array if option is an array');
$option = new InputOption('foo', null, InputOption::VALUE_NONE);
$this->assertFalse($option->getDefault(), '->getDefault() returns false if the option does not take a value');
}
public function testSetDefault()
{
$option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
$option->setDefault(null);
$this->assertNull($option->getDefault(), '->setDefault() can reset the default value by passing null');
$option->setDefault('another');
$this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
$option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY);
$option->setDefault([1, 2]);
$this->assertEquals([1, 2], $option->getDefault(), '->setDefault() changes the default value');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot set a default value when using InputOption::VALUE_NONE mode.
*/
public function testDefaultValueWithValueNoneMode()
{
$option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
$option->setDefault('default');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage A default value for an array option must be an array.
*/
public function testDefaultValueWithIsArrayMode()
{
$option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
$option->setDefault('default');
}
public function testEquals()
{
$option = new InputOption('foo', 'f', null, 'Some description');
$option2 = new InputOption('foo', 'f', null, 'Alternative description');
$this->assertTrue($option->equals($option2));
$option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
$option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description', true);
$this->assertFalse($option->equals($option2));
$option = new InputOption('foo', 'f', null, 'Some description');
$option2 = new InputOption('bar', 'f', null, 'Some description');
$this->assertFalse($option->equals($option2));
$option = new InputOption('foo', 'f', null, 'Some description');
$option2 = new InputOption('foo', '', null, 'Some description');
$this->assertFalse($option->equals($option2));
$option = new InputOption('foo', 'f', null, 'Some description');
$option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
$this->assertFalse($option->equals($option2));
}
}

View file

@ -0,0 +1,149 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests\Input;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
class InputTest extends TestCase
{
public function testConstructor()
{
$input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name')]));
$this->assertEquals('foo', $input->getArgument('name'), '->__construct() takes a InputDefinition as an argument');
}
public function testOptions()
{
$input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name')]));
$this->assertEquals('foo', $input->getOption('name'), '->getOption() returns the value for the given option');
$input->setOption('name', 'bar');
$this->assertEquals('bar', $input->getOption('name'), '->setOption() sets the value for a given option');
$this->assertEquals(['name' => 'bar'], $input->getOptions(), '->getOptions() returns all option values');
$input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
$this->assertEquals('default', $input->getOption('bar'), '->getOption() returns the default value for optional options');
$this->assertEquals(['name' => 'foo', 'bar' => 'default'], $input->getOptions(), '->getOptions() returns all option values, even optional ones');
$input = new ArrayInput(['--name' => 'foo', '--bar' => ''], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
$this->assertEquals('', $input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
$this->assertEquals(['name' => 'foo', 'bar' => ''], $input->getOptions(), '->getOptions() returns all option values.');
$input = new ArrayInput(['--name' => 'foo', '--bar' => null], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
$this->assertNull($input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
$this->assertEquals(['name' => 'foo', 'bar' => null], $input->getOptions(), '->getOptions() returns all option values');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "foo" option does not exist.
*/
public function testSetInvalidOption()
{
$input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
$input->setOption('foo', 'bar');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "foo" option does not exist.
*/
public function testGetInvalidOption()
{
$input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
$input->getOption('foo');
}
public function testArguments()
{
$input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name')]));
$this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
$input->setArgument('name', 'bar');
$this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
$this->assertEquals(['name' => 'bar'], $input->getArguments(), '->getArguments() returns all argument values');
$input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
$this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
$this->assertEquals(['name' => 'foo', 'bar' => 'default'], $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "foo" argument does not exist.
*/
public function testSetInvalidArgument()
{
$input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
$input->setArgument('foo', 'bar');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "foo" argument does not exist.
*/
public function testGetInvalidArgument()
{
$input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
$input->getArgument('foo');
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Not enough arguments (missing: "name").
*/
public function testValidateWithMissingArguments()
{
$input = new ArrayInput([]);
$input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]));
$input->validate();
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Not enough arguments (missing: "name").
*/
public function testValidateWithMissingRequiredArguments()
{
$input = new ArrayInput(['bar' => 'baz']);
$input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL)]));
$input->validate();
}
public function testValidate()
{
$input = new ArrayInput(['name' => 'foo']);
$input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]));
$this->assertNull($input->validate());
}
public function testSetGetInteractive()
{
$input = new ArrayInput([]);
$this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
$input->setInteractive(false);
$this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
}
public function testSetGetStream()
{
$input = new ArrayInput([]);
$stream = fopen('php://memory', 'r+', false);
$input->setStream($stream);
$this->assertSame($stream, $input->getStream());
}
}

View file

@ -0,0 +1,87 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests\Input;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\StringInput;
class StringInputTest extends TestCase
{
/**
* @dataProvider getTokenizeData
*/
public function testTokenize($input, $tokens, $message)
{
$input = new StringInput($input);
$r = new \ReflectionClass('Symfony\Component\Console\Input\ArgvInput');
$p = $r->getProperty('tokens');
$p->setAccessible(true);
$this->assertEquals($tokens, $p->getValue($input), $message);
}
public function testInputOptionWithGivenString()
{
$definition = new InputDefinition(
[new InputOption('foo', null, InputOption::VALUE_REQUIRED)]
);
// call to bind
$input = new StringInput('--foo=bar');
$input->bind($definition);
$this->assertEquals('bar', $input->getOption('foo'));
}
public function getTokenizeData()
{
return [
['', [], '->tokenize() parses an empty string'],
['foo', ['foo'], '->tokenize() parses arguments'],
[' foo bar ', ['foo', 'bar'], '->tokenize() ignores whitespaces between arguments'],
['"quoted"', ['quoted'], '->tokenize() parses quoted arguments'],
["'quoted'", ['quoted'], '->tokenize() parses quoted arguments'],
["'a\rb\nc\td'", ["a\rb\nc\td"], '->tokenize() parses whitespace chars in strings'],
["'a'\r'b'\n'c'\t'd'", ['a', 'b', 'c', 'd'], '->tokenize() parses whitespace chars between args as spaces'],
['\"quoted\"', ['"quoted"'], '->tokenize() parses escaped-quoted arguments'],
["\'quoted\'", ['\'quoted\''], '->tokenize() parses escaped-quoted arguments'],
['-a', ['-a'], '->tokenize() parses short options'],
['-azc', ['-azc'], '->tokenize() parses aggregated short options'],
['-awithavalue', ['-awithavalue'], '->tokenize() parses short options with a value'],
['-a"foo bar"', ['-afoo bar'], '->tokenize() parses short options with a value'],
['-a"foo bar""foo bar"', ['-afoo barfoo bar'], '->tokenize() parses short options with a value'],
['-a\'foo bar\'', ['-afoo bar'], '->tokenize() parses short options with a value'],
['-a\'foo bar\'\'foo bar\'', ['-afoo barfoo bar'], '->tokenize() parses short options with a value'],
['-a\'foo bar\'"foo bar"', ['-afoo barfoo bar'], '->tokenize() parses short options with a value'],
['--long-option', ['--long-option'], '->tokenize() parses long options'],
['--long-option=foo', ['--long-option=foo'], '->tokenize() parses long options with a value'],
['--long-option="foo bar"', ['--long-option=foo bar'], '->tokenize() parses long options with a value'],
['--long-option="foo bar""another"', ['--long-option=foo baranother'], '->tokenize() parses long options with a value'],
['--long-option=\'foo bar\'', ['--long-option=foo bar'], '->tokenize() parses long options with a value'],
["--long-option='foo bar''another'", ['--long-option=foo baranother'], '->tokenize() parses long options with a value'],
["--long-option='foo bar'\"another\"", ['--long-option=foo baranother'], '->tokenize() parses long options with a value'],
['foo -a -ffoo --long bar', ['foo', '-a', '-ffoo', '--long', 'bar'], '->tokenize() parses when several arguments and options'],
];
}
public function testToString()
{
$input = new StringInput('-f foo');
$this->assertEquals('-f foo', (string) $input);
$input = new StringInput('-f --bar=foo "a b c d"');
$this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d'), (string) $input);
$input = new StringInput('-f --bar=foo \'a b c d\' '."'A\nB\\'C'");
$this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
}
}