[Console] Cleaned up the unit tests.

This commit is contained in:
Jakub Zalas 2013-02-05 21:55:33 +00:00
parent eb2bcc5db9
commit 5ca04b02fd
12 changed files with 721 additions and 492 deletions

View File

@ -129,14 +129,6 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name'); $this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
$this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias'); $this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
try {
$application->get('foofoo');
$this->fail('->get() throws an \InvalidArgumentException if the command does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the command does not exist');
$this->assertEquals('The command "foofoo" does not exist.', $e->getMessage(), '->get() throws an \InvalidArgumentException if the command does not exist');
}
$application = new Application(); $application = new Application();
$application->add($foo = new \FooCommand()); $application->add($foo = new \FooCommand());
// simulate --help // simulate --help
@ -145,7 +137,17 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$p->setAccessible(true); $p->setAccessible(true);
$p->setValue($application, true); $p->setValue($application, true);
$command = $application->get('foo:bar'); $command = $application->get('foo:bar');
$this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($command), '->get() returns the help command if --help is provided as the input'); $this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $command, '->get() returns the help command if --help is provided as the input');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The command "foofoo" does not exist.
*/
public function testGetInvalidCommand()
{
$application = new Application();
$application->get('foofoo');
} }
public function testGetNamespaces() public function testGetNamespaces()
@ -164,84 +166,90 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation'); $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
$application->add(new \Foo2Command()); $application->add(new \Foo2Command());
$this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists'); $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
try { }
$application->findNamespace('f');
$this->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
$this->assertEquals('The namespace "f" is ambiguous (foo, foo1).', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
}
try { /**
$application->findNamespace('bar'); * @expectedException \InvalidArgumentException
$this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); * @expectedExceptionMessage The namespace "f" is ambiguous (foo, foo1).
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); public function testFindAmbiguousNamespace()
$this->assertEquals('There are no commands defined in the "bar" namespace.', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); {
} $application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo2Command());
$application->findNamespace('f');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage There are no commands defined in the "bar" namespace.
*/
public function testFindInvalidNamespace()
{
$application = new Application();
$application->findNamespace('bar');
} }
public function testFind() public function testFind()
{ {
$application = new Application(); $application = new Application();
$application->add(new \FooCommand()); $application->add(new \FooCommand());
$this->assertEquals('FooCommand', get_class($application->find('foo:bar')), '->find() returns a command if its name exists');
$this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($application->find('h')), '->find() returns a command if its name exists');
$this->assertEquals('FooCommand', get_class($application->find('f:bar')), '->find() returns a command if the abbreviation for the namespace exists');
$this->assertEquals('FooCommand', get_class($application->find('f:b')), '->find() returns a command if the abbreviation for the namespace and the command name exist');
$this->assertEquals('FooCommand', get_class($application->find('a')), '->find() returns a command if the abbreviation exists for an alias');
$this->assertInstanceOf('FooCommand', $application->find('foo:bar'), '->find() returns a command if its name exists');
$this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $application->find('h'), '->find() returns a command if its name exists');
$this->assertInstanceOf('FooCommand', $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
$this->assertInstanceOf('FooCommand', $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
$this->assertInstanceOf('FooCommand', $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
}
/**
* @dataProvider provideAmbiguousAbbreviations
*/
public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExceptionMessage)
{
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
$application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo1Command()); $application->add(new \Foo1Command());
$application->add(new \Foo2Command()); $application->add(new \Foo2Command());
try { $application->find($abbreviation);
$application->find('f');
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
$this->assertRegExp('/Command "f" is not defined./', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
}
try {
$application->find('a');
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
$this->assertEquals('Command "a" is ambiguous (afoobar, afoobar1 and 1 more).', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
}
try {
$application->find('foo:b');
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
$this->assertEquals('Command "foo:b" is ambiguous (foo:bar, foo:bar1).', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
}
} }
public function testFindAlternativeExceptionMessage() public function provideAmbiguousAbbreviations()
{
return array(
array('f', 'Command "f" is not defined.'),
array('a', 'Command "a" is ambiguous (afoobar, afoobar1 and 1 more).'),
array('foo:b', 'Command "foo:b" is ambiguous (foo:bar, foo:bar1).')
);
}
/**
* @dataProvider provideInvalidCommandNamesSingle
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Did you mean this
*/
public function testFindAlternativeExceptionMessageSingle($name)
{ {
$application = new Application(); $application = new Application();
$application->add(new \FooCommand()); $application->add(new \FooCommand());
$application->find($name);
}
// Command + singular public function provideInvalidCommandNamesSingle()
try { {
$application->find('foo:baR'); return array(
$this->fail('->find() throws an \InvalidArgumentException if command does not exist, with one alternative'); array('foo:baR'),
} catch (\Exception $e) { array('foO:bar')
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with one alternative'); );
$this->assertRegExp('/Did you mean this/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with one alternative'); }
}
// Namespace + singular
try {
$application->find('foO:bar');
$this->fail('->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
$this->assertRegExp('/Did you mean this/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
}
public function testFindAlternativeExceptionMessageMultiple()
{
$application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo1Command()); $application->add(new \Foo1Command());
$application->add(new \Foo2Command()); $application->add(new \Foo2Command());

View File

@ -35,17 +35,19 @@ class CommandTest extends \PHPUnit_Framework_TestCase
public function testConstructor() public function testConstructor()
{ {
try {
$command = new Command();
$this->fail('__construct() throws a \LogicException if the name is null');
} catch (\Exception $e) {
$this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the name is null');
$this->assertEquals('The command name cannot be empty.', $e->getMessage(), '__construct() throws a \LogicException if the name is null');
}
$command = new Command('foo:bar'); $command = new Command('foo:bar');
$this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument'); $this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
} }
/**
* @expectedException \LogicException
* @expectedExceptionMessage The command name cannot be empty.
*/
public function testCommandNameCannotBeEmpty()
{
new Command();
}
public function testSetApplication() public function testSetApplication()
{ {
$application = new Application(); $application = new Application();
@ -92,22 +94,25 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$ret = $command->setName('foobar:bar'); $ret = $command->setName('foobar:bar');
$this->assertEquals($command, $ret, '->setName() implements a fluent interface'); $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
$this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name'); $this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
}
try { /**
$command->setName(''); * @dataProvider provideInvalidCommandNames
$this->fail('->setName() throws an \InvalidArgumentException if the name is empty'); */
} catch (\Exception $e) { public function testInvalidCommandNames($name)
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty'); {
$this->assertEquals('Command name "" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty'); $this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
}
try { $command = new \TestCommand();
$command->setName('foo:'); $command->setName($name);
$this->fail('->setName() throws an \InvalidArgumentException if the name is empty'); }
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty'); public function provideInvalidCommandNames()
$this->assertEquals('Command name "foo:" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty'); {
} return array(
array(''),
array('foo:')
);
} }
public function testGetSetDescription() public function testGetSetDescription()
@ -193,32 +198,43 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options'); $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
} }
public function testRun() public function testRunInteractive()
{
$tester = new CommandTester(new \TestCommand());
$tester->execute(array(), array('interactive' => true));
$this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
}
public function testRunNonInteractive()
{
$tester = new CommandTester(new \TestCommand());
$tester->execute(array(), array('interactive' => false));
$this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage You must override the execute() method in the concrete command class.
*/
public function testExecuteMethodNeedsToBeOverriden()
{
$command = new Command('foo');
$command->run(new StringInput(''), new NullOutput());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "--bar" option does not exist.
*/
public function testRunWithInvalidOption()
{ {
$command = new \TestCommand(); $command = new \TestCommand();
$tester = new CommandTester($command); $tester = new CommandTester($command);
try { $tester->execute(array('--bar' => true));
$tester->execute(array('--bar' => true));
$this->fail('->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
$this->assertEquals('The "--bar" option does not exist.', $e->getMessage(), '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
}
$tester->execute(array(), array('interactive' => true));
$this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
$tester->execute(array(), array('interactive' => false));
$this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
$command = new Command('foo');
try {
$command->run(new StringInput(''), new NullOutput());
$this->fail('->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
} catch (\Exception $e) {
$this->assertInstanceOf('\LogicException', $e, '->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
$this->assertEquals('You must override the execute() method in the concrete command class.', $e->getMessage(), '->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
}
} }
public function testRunReturnsAlwaysInteger() public function testRunReturnsAlwaysInteger()
@ -251,7 +267,7 @@ class CommandTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @expectedException InvalidArgumentException * @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid callable provided to Command::setCode. * @expectedExceptionMessage Invalid callable provided to Command::setCode.
*/ */
public function testSetCodeWithNonCallable() public function testSetCodeWithNonCallable()

View File

@ -18,33 +18,51 @@ use Symfony\Component\Console\Application;
class HelpCommandTest extends \PHPUnit_Framework_TestCase class HelpCommandTest extends \PHPUnit_Framework_TestCase
{ {
public function testExecute() public function testExecuteForCommandAlias()
{ {
$command = new HelpCommand(); $command = new HelpCommand();
$command->setApplication(new Application());
$application = new Application();
$command->setApplication($application);
$commandTester = new CommandTester($command); $commandTester = new CommandTester($command);
$commandTester->execute(array('command_name' => 'li')); $commandTester->execute(array('command_name' => 'li'));
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias'); $this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
}
public function testExecuteForCommand()
{
$command = new HelpCommand(); $command = new HelpCommand();
$commandTester = new CommandTester($command); $commandTester = new CommandTester($command);
$command->setCommand(new ListCommand()); $command->setCommand(new ListCommand());
$commandTester->execute(array()); $commandTester->execute(array());
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
}
public function testExecuteForCommandWithXmlOption()
{
$command = new HelpCommand();
$commandTester = new CommandTester($command);
$command->setCommand(new ListCommand()); $command->setCommand(new ListCommand());
$commandTester->execute(array('--xml' => true)); $commandTester->execute(array('--xml' => true));
$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
}
public function testExecuteForApplicationCommand()
{
$application = new Application(); $application = new Application();
$commandTester = new CommandTester($application->get('help')); $commandTester = new CommandTester($application->get('help'));
$commandTester->execute(array('command_name' => 'list')); $commandTester->execute(array('command_name' => 'list'));
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
}
public function testExecuteForApplicationCommandWithXmlOption()
{
$application = new Application();
$commandTester = new CommandTester($application->get('help'));
$commandTester->execute(array('command_name' => 'list', '--xml' => true)); $commandTester->execute(array('command_name' => 'list', '--xml' => true));
$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed'); $this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
} }
} }

View File

@ -16,23 +16,35 @@ use Symfony\Component\Console\Application;
class ListCommandTest extends \PHPUnit_Framework_TestCase class ListCommandTest extends \PHPUnit_Framework_TestCase
{ {
public function testExecute() public function testExecuteListsCommands()
{ {
$application = new Application(); $application = new Application();
$commandTester = new CommandTester($command = $application->get('list')); $commandTester = new CommandTester($command = $application->get('list'));
$commandTester->execute(array('command' => $command->getName()), array('decorated' => false)); $commandTester->execute(array('command' => $command->getName()), array('decorated' => false));
$this->assertRegExp('/help Displays help for a command/', $commandTester->getDisplay(), '->execute() returns a list of available commands'); $this->assertRegExp('/help Displays help for a command/', $commandTester->getDisplay(), '->execute() returns a list of available commands');
}
public function testExecuteListsCommandsWithXmlOption()
{
$application = new Application();
$commandTester = new CommandTester($command = $application->get('list'));
$commandTester->execute(array('command' => $command->getName(), '--xml' => true)); $commandTester->execute(array('command' => $command->getName(), '--xml' => true));
$this->assertRegExp('/<command id="list" name="list">/', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed');
$this->assertRegExp('/<command id="list" name="list">/', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed');
}
public function testExecuteListsCommandsWithRawOption()
{
$application = new Application();
$commandTester = new CommandTester($command = $application->get('list'));
$commandTester->execute(array('command' => $command->getName(), '--raw' => true)); $commandTester->execute(array('command' => $command->getName(), '--raw' => true));
$output = <<<EOF $output = <<<EOF
help Displays help for a command help Displays help for a command
list Lists commands list Lists commands
EOF; EOF;
$this->assertEquals(str_replace("\n", PHP_EOL, $output), $commandTester->getDisplay(), 'boo');
$this->assertEquals(str_replace("\n", PHP_EOL, $output), $commandTester->getDisplay());
} }
} }

View File

@ -29,7 +29,7 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo'), $p->getValue($input), '__construct() automatically get its input from the argv server variable'); $this->assertEquals(array('foo'), $p->getValue($input), '__construct() automatically get its input from the argv server variable');
} }
public function testParser() public function testParseArguments()
{ {
$input = new ArgvInput(array('cli.php', 'foo')); $input = new ArgvInput(array('cli.php', 'foo'));
$input->bind(new InputDefinition(array(new InputArgument('name')))); $input->bind(new InputDefinition(array(new InputArgument('name'))));
@ -37,142 +37,185 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
$input->bind(new InputDefinition(array(new InputArgument('name')))); $input->bind(new InputDefinition(array(new InputArgument('name'))));
$this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless'); $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
}
$input = new ArgvInput(array('cli.php', '--foo')); /**
$input->bind(new InputDefinition(array(new InputOption('foo')))); * @dataProvider provideOptions
$this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without a value'); */
public function testParseOptions($input, $options, $expectedOptions, $message)
{
$input = new ArgvInput($input);
$input->bind(new InputDefinition($options));
$input = new ArgvInput(array('cli.php', '--foo=bar')); $this->assertEquals($expectedOptions, $input->getOptions(), $message);
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)))); }
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a = separator)');
$input = new ArgvInput(array('cli.php', '--foo', 'bar')); public function provideOptions()
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)))); {
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a space separator)'); return array(
array(
array('cli.php', '--foo'),
array(new InputOption('foo')),
array('foo' => true),
'->parse() parses long options without a value'
),
array(
array('cli.php', '--foo=bar'),
array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
array('foo' => 'bar'),
'->parse() parses long options with a required value (with a = separator)'
),
array(
array('cli.php', '--foo', 'bar'),
array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
array('foo' => 'bar'),
'->parse() parses long options with a required value (with a space separator)'
),
array(
array('cli.php', '-f'),
array(new InputOption('foo', 'f')),
array('foo' => true),
'->parse() parses short options without a value'
),
array(
array('cli.php', '-fbar'),
array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
array('foo' => 'bar'),
'->parse() parses short options with a required value (with no separator)'
),
array(
array('cli.php', '-f', 'bar'),
array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
array('foo' => 'bar'),
'->parse() parses short options with a required value (with a space separator)'
),
array(
array('cli.php', '-f', ''),
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
array('foo' => ''),
'->parse() parses short options with an optional empty value'
),
array(
array('cli.php', '-f', '', 'foo'),
array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
array('foo' => ''),
'->parse() parses short options with an optional empty value followed by an argument'
),
array(
array('cli.php', '-f', '', '-b'),
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
array('foo' => '', 'bar' => true),
'->parse() parses short options with an optional empty value followed by an option'
),
array(
array('cli.php', '-f', '-b', 'foo'),
array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
array('foo' => null, 'bar' => true),
'->parse() parses short options with an optional value which is not present'
),
array(
array('cli.php', '-fb'),
array(new InputOption('foo', 'f'), new InputOption('bar', 'b')),
array('foo' => true, 'bar' => true),
'->parse() parses short options when they are aggregated as a single one'
),
array(
array('cli.php', '-fb', 'bar'),
array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED)),
array('foo' => true, 'bar' => 'bar'),
'->parse() parses short options when they are aggregated as a single one and the last one has a required value'
),
array(
array('cli.php', '-fb', 'bar'),
array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
array('foo' => true, 'bar' => 'bar'),
'->parse() parses short options when they are aggregated as a single one and the last one has an optional value'
),
array(
array('cli.php', '-fbbar'),
array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
array('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'
),
array(
array('cli.php', '-fbbar'),
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
array('foo' => 'bbar', 'bar' => null),
'->parse() parses short options when they are aggregated as a single one and one of them takes a value'
)
);
}
try { /**
$input = new ArgvInput(array('cli.php', '--foo')); * @dataProvider provideInvalidInput
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)))); */
$this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required'); public function testInvalidInput($argv, $definition, $expectedExceptionMessage)
} catch (\Exception $e) { {
$this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required'); $this->setExpectedException('RuntimeException', $expectedExceptionMessage);
$this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
}
$input = new ArgvInput(array('cli.php', '-f')); $input = new ArgvInput($argv);
$input->bind(new InputDefinition(array(new InputOption('foo', 'f')))); $input->bind($definition);
$this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without a value'); }
$input = new ArgvInput(array('cli.php', '-fbar')); public function provideInvalidInput()
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)))); {
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with no separator)'); return array(
array(
array('cli.php', '--foo'),
new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
'The "--foo" option requires a value.'
),
array(
array('cli.php', '-f'),
new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
'The "--foo" option requires a value.'
),
array(
array('cli.php', '-ffoo'),
new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))),
'The "-o" option does not exist.'
),
array(
array('cli.php', 'foo', 'bar'),
new InputDefinition(),
'Too many arguments.'
),
array(
array('cli.php', '--foo'),
new InputDefinition(),
'The "--foo" option does not exist.'
),
array(
array('cli.php', '-f'),
new InputDefinition(),
'The "-f" option does not exist.'
),
array(
array('cli.php', '-1'),
new InputDefinition(array(new InputArgument('number'))),
'The "-1" option does not exist.'
)
);
}
$input = new ArgvInput(array('cli.php', '-f', 'bar')); public function testParseArrayArgument()
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)))); {
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with a space separator)'); $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
$input = new ArgvInput(array('cli.php', '-f', '')); $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)))); }
$this->assertEquals(array('foo' => ''), $input->getOptions(), '->parse() parses short options with an optional empty value');
$input = new ArgvInput(array('cli.php', '-f', '', 'foo'));
$input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
$this->assertEquals(array('foo' => ''), $input->getOptions(), '->parse() parses short options with an optional empty value followed by an argument');
$input = new ArgvInput(array('cli.php', '-f', '', '-b'));
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
$this->assertEquals(array('foo' => '', 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional empty value followed by an option');
$input = new ArgvInput(array('cli.php', '-f', '-b', 'foo'));
$input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
$this->assertEquals(array('foo' => null, 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional value which is not present');
try {
$input = new ArgvInput(array('cli.php', '-f'));
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
$this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
$this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
}
try {
$input = new ArgvInput(array('cli.php', '-ffoo'));
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
$this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
$this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
}
try {
$input = new ArgvInput(array('cli.php', 'foo', 'bar'));
$input->bind(new InputDefinition());
$this->fail('->parse() throws a \RuntimeException if too many arguments are passed');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if too many arguments are passed');
$this->assertEquals('Too many arguments.', $e->getMessage(), '->parse() throws a \RuntimeException if too many arguments are passed');
}
try {
$input = new ArgvInput(array('cli.php', '--foo'));
$input->bind(new InputDefinition());
$this->fail('->parse() throws a \RuntimeException if an unknown long option is passed');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown long option is passed');
$this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown long option is passed');
}
try {
$input = new ArgvInput(array('cli.php', '-f'));
$input->bind(new InputDefinition());
$this->fail('->parse() throws a \RuntimeException if an unknown short option is passed');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown short option is passed');
$this->assertEquals('The "-f" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown short option is passed');
}
$input = new ArgvInput(array('cli.php', '-fb'));
$input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b'))));
$this->assertEquals(array('foo' => true, 'bar' => true), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one');
$input = new ArgvInput(array('cli.php', '-fb', 'bar'));
$input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED))));
$this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has a required value');
$input = new ArgvInput(array('cli.php', '-fb', 'bar'));
$input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
$this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value');
$input = new ArgvInput(array('cli.php', '-fbbar'));
$input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
$this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator');
$input = new ArgvInput(array('cli.php', '-fbbar'));
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
$this->assertEquals(array('foo' => 'bbar', 'bar' => null), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and one of them takes a value');
try {
$input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
$this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
} catch (\RuntimeException $e) {
$this->assertNotEquals('Too many arguments.', $e->getMessage(), '->parse() parses array arguments');
}
public function testParseArrayOption()
{
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz')); $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY)))); $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
$this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions()); $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions());
}
try { public function testParseNegativeNumberAfterDoubleDash()
$input = new ArgvInput(array('cli.php', '-1')); {
$input->bind(new InputDefinition(array(new InputArgument('number'))));
$this->fail('->parse() throws a \RuntimeException if an unknown option is passed');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e, '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
$this->assertEquals('The "-1" option does not exist.', $e->getMessage(), '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
}
$input = new ArgvInput(array('cli.php', '--', '-1')); $input = new ArgvInput(array('cli.php', '--', '-1'));
$input->bind(new InputDefinition(array(new InputArgument('number')))); $input->bind(new InputDefinition(array(new InputArgument('number'))));
$this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence'); $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
@ -181,9 +224,13 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
$input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)))); $input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence'); $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
$this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence'); $this->assertEquals(array('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(array('cli.php', '-f', 'bar', '')); $input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
$input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)))); $input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
$this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments'); $this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
} }

View File

@ -38,53 +38,86 @@ class ArrayInputTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters'); $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
} }
public function testParse() public function testParseArguments()
{ {
$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
$this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments'); $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
}
try { /**
$input = new ArrayInput(array('foo' => 'foo'), new InputDefinition(array(new InputArgument('name')))); * @dataProvider provideOptions
$this->fail('->parse() throws an \InvalidArgumentException exception if an invalid argument is passed'); */
} catch (\Exception $e) { public function testParseOptions($input, $options, $expectedOptions, $message)
$this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException exception if an invalid argument is passed'); {
$this->assertEquals('The "foo" argument does not exist.', $e->getMessage(), '->parse() throws an \InvalidArgumentException exception if an invalid argument is passed'); $input = new ArrayInput($input, new InputDefinition($options));
}
$input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo')))); $this->assertEquals($expectedOptions, $input->getOptions(), $message);
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options'); }
$input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')))); public function provideOptions()
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a default value'); {
return array(
array(
array('--foo' => 'bar'),
array(new InputOption('foo')),
array('foo' => 'bar'),
'->parse() parses long options'
),
array(
array('--foo' => 'bar'),
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
array('foo' => 'bar'),
'->parse() parses long options with a default value'
),
array(
array('--foo' => null),
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
array('foo' => 'default'),
'->parse() parses long options with a default value'
),
array(
array('-f' => 'bar'),
array(new InputOption('foo', 'f')),
array('foo' => 'bar'),
'->parse() parses short options'
)
);
}
$input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')))); /**
$this->assertEquals(array('foo' => 'default'), $input->getOptions(), '->parse() parses long options with a default value'); * @dataProvider provideInvalidInput
*/
public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
{
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
try { new ArrayInput($parameters, $definition);
$input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)))); }
$this->fail('->parse() throws an \InvalidArgumentException exception if a required option is passed without a value');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException exception if a required option is passed without a value');
$this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws an \InvalidArgumentException exception if a required option is passed without a value');
}
try { public function provideInvalidInput()
$input = new ArrayInput(array('--foo' => 'foo'), new InputDefinition()); {
$this->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); return array(
} catch (\Exception $e) { array(
$this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); array('foo' => 'foo'),
$this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); new InputDefinition(array(new InputArgument('name'))),
} 'The "foo" argument does not exist.'
),
$input = new ArrayInput(array('-f' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f')))); array(
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options'); array('--foo' => null),
new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
try { 'The "--foo" option requires a value.'
$input = new ArrayInput(array('-o' => 'foo'), new InputDefinition()); ),
$this->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); array(
} catch (\Exception $e) { array('--foo' => 'foo'),
$this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); new InputDefinition(),
$this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); 'The "--foo" option does not exist.'
} ),
array(
array('-o' => 'foo'),
new InputDefinition(),
'The "-o" option does not exist.'
)
);
} }
} }

View File

@ -19,8 +19,10 @@ class InputArgumentTest extends \PHPUnit_Framework_TestCase
{ {
$argument = new InputArgument('foo'); $argument = new InputArgument('foo');
$this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument'); $this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
}
// mode argument public function testModes()
{
$argument = new InputArgument('foo'); $argument = new InputArgument('foo');
$this->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default'); $this->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default');
@ -32,21 +34,24 @@ class InputArgumentTest extends \PHPUnit_Framework_TestCase
$argument = new InputArgument('foo', InputArgument::REQUIRED); $argument = new InputArgument('foo', InputArgument::REQUIRED);
$this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode'); $this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
}
try { /**
$argument = new InputArgument('foo', 'ANOTHER_ONE'); * @dataProvider provideInvalidModes
$this->fail('__construct() throws an \InvalidArgumentException if the mode is not valid'); */
} catch (\Exception $e) { public function testInvalidModes($mode)
$this->assertInstanceOf('\InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the mode is not valid'); {
$this->assertEquals('Argument mode "ANOTHER_ONE" is not valid.', $e->getMessage()); $this->setExpectedException('InvalidArgumentException', sprintf('Argument mode "%s" is not valid.', $mode));
}
try { new InputArgument('foo', $mode);
$argument = new InputArgument('foo', -1); }
$this->fail('__construct() throws an \InvalidArgumentException if the mode is not valid');
} catch (\Exception $e) { public function provideInvalidModes()
$this->assertInstanceOf('\InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the mode is not valid'); {
$this->assertEquals('Argument mode "-1" is not valid.', $e->getMessage()); return array(
} array('ANOTHER_ONE'),
array(-1)
);
} }
public function testIsArray() public function testIsArray()
@ -82,23 +87,25 @@ class InputArgumentTest extends \PHPUnit_Framework_TestCase
$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY); $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
$argument->setDefault(array(1, 2)); $argument->setDefault(array(1, 2));
$this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value'); $this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
}
try { /**
$argument = new InputArgument('foo', InputArgument::REQUIRED); * @expectedException \LogicException
$argument->setDefault('default'); * @expectedExceptionMessage Cannot set a default value except for InputArgument::OPTIONAL mode.
$this->fail('->setDefault() throws a \LogicException if you give a default value for a required argument'); */
} catch (\Exception $e) { public function testSetDefaultWithRequiredArgument()
$this->assertInstanceOf('\LogicException', $e, '->setDefault() throws a \LogicException exception if an invalid option is passed'); {
$this->assertEquals('Cannot set a default value except for InputArgument::OPTIONAL mode.', $e->getMessage()); $argument = new InputArgument('foo', InputArgument::REQUIRED);
} $argument->setDefault('default');
}
try { /**
$argument = new InputArgument('foo', InputArgument::IS_ARRAY); * @expectedException \LogicException
$argument->setDefault('default'); * @expectedExceptionMessage A default value for an array argument must be an array.
$this->fail('->setDefault() throws a \LogicException if you give a default value which is not an array for a IS_ARRAY option'); */
} catch (\Exception $e) { public function testSetDefaultWithArrayArgument()
$this->assertInstanceOf('\LogicException', $e, '->setDefault() throws a \LogicException if you give a default value which is not an array for a IS_ARRAY option'); {
$this->assertEquals('A default value for an array argument must be an array.', $e->getMessage()); $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
} $argument->setDefault('default');
} }
} }

View File

@ -26,7 +26,7 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
self::$fixtures = __DIR__.'/../Fixtures/'; self::$fixtures = __DIR__.'/../Fixtures/';
} }
public function testConstructor() public function testConstructorArguments()
{ {
$this->initializeArguments(); $this->initializeArguments();
@ -35,7 +35,10 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
$definition = new InputDefinition(array($this->foo, $this->bar)); $definition = new InputDefinition(array($this->foo, $this->bar));
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '__construct() takes an array of InputArgument objects as its first argument'); $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '__construct() takes an array of InputArgument objects as its first argument');
}
public function testConstructorOptions()
{
$this->initializeOptions(); $this->initializeOptions();
$definition = new InputDefinition(); $definition = new InputDefinition();
@ -77,36 +80,45 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArgument() adds a InputArgument object'); $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArgument() adds a InputArgument object');
$definition->addArgument($this->bar); $definition->addArgument($this->bar);
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArgument() adds a InputArgument object'); $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArgument() adds a InputArgument object');
}
// arguments must have different names /**
try { * @expectedException \LogicException
$definition->addArgument($this->foo1); * @expectedExceptionMessage An argument with name "foo" already exists.
$this->fail('->addArgument() throws a \LogicException if another argument is already registered with the same name'); */
} catch (\Exception $e) { public function testArgumentsMustHaveDifferentNames()
$this->assertInstanceOf('\LogicException', $e, '->addArgument() throws a \LogicException if another argument is already registered with the same name'); {
$this->assertEquals('An argument with name "foo" already exists.', $e->getMessage()); $this->initializeArguments();
}
// cannot add a parameter after an array parameter
$definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
try {
$definition->addArgument(new InputArgument('anotherbar'));
$this->fail('->addArgument() throws a \LogicException if there is an array parameter already registered');
} catch (\Exception $e) {
$this->assertInstanceOf('\LogicException', $e, '->addArgument() throws a \LogicException if there is an array parameter already registered');
$this->assertEquals('Cannot add an argument after an array argument.', $e->getMessage());
}
// cannot add a required argument after an optional one
$definition = new InputDefinition(); $definition = new InputDefinition();
$definition->addArgument($this->foo); $definition->addArgument($this->foo);
try { $definition->addArgument($this->foo1);
$definition->addArgument($this->foo2); }
$this->fail('->addArgument() throws a \LogicException if you try to add a required argument after an optional one');
} catch (\Exception $e) { /**
$this->assertInstanceOf('\LogicException', $e, '->addArgument() throws a \LogicException if you try to add a required argument after an optional one'); * @expectedException \LogicException
$this->assertEquals('Cannot add a required argument after an optional one.', $e->getMessage()); * @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() public function testGetArgument()
@ -116,13 +128,19 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
$definition = new InputDefinition(); $definition = new InputDefinition();
$definition->addArguments(array($this->foo)); $definition->addArguments(array($this->foo));
$this->assertEquals($this->foo, $definition->getArgument('foo'), '->getArgument() returns a InputArgument by its name'); $this->assertEquals($this->foo, $definition->getArgument('foo'), '->getArgument() returns a InputArgument by its name');
try { }
$definition->getArgument('bar');
$this->fail('->getArgument() throws an \InvalidArgumentException if the InputArgument name does not exist'); /**
} catch (\Exception $e) { * @expectedException \InvalidArgumentException
$this->assertInstanceOf('\InvalidArgumentException', $e, '->getArgument() throws an \InvalidArgumentException if the InputArgument name does not exist'); * @expectedExceptionMessage The "bar" argument does not exist.
$this->assertEquals('The "bar" argument does not exist.', $e->getMessage()); */
} public function testGetInvalidArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments(array($this->foo));
$definition->getArgument('bar');
} }
public function testHasArgument() public function testHasArgument()
@ -131,6 +149,7 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
$definition = new InputDefinition(); $definition = new InputDefinition();
$definition->addArguments(array($this->foo)); $definition->addArguments(array($this->foo));
$this->assertTrue($definition->hasArgument('foo'), '->hasArgument() returns true if a InputArgument exists for the given name'); $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'); $this->assertFalse($definition->hasArgument('bar'), '->hasArgument() returns false if a InputArgument exists for the given name');
} }
@ -181,13 +200,19 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->setOptions() sets the array of InputOption objects'); $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->setOptions() sets the array of InputOption objects');
$definition->setOptions(array($this->bar)); $definition->setOptions(array($this->bar));
$this->assertEquals(array('bar' => $this->bar), $definition->getOptions(), '->setOptions() clears all InputOption objects'); $this->assertEquals(array('bar' => $this->bar), $definition->getOptions(), '->setOptions() clears all InputOption objects');
try { }
$definition->getOptionForShortcut('f');
$this->fail('->setOptions() clears all InputOption objects'); /**
} catch (\Exception $e) { * @expectedException \InvalidArgumentException
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOptions() clears all InputOption objects'); * @expectedExceptionMessage The "-f" option does not exist.
$this->assertEquals('The "-f" option does not exist.', $e->getMessage()); */
} public function testSetOptionsClearsOptions()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$definition->setOptions(array($this->bar));
$definition->getOptionForShortcut('f');
} }
public function testAddOptions() public function testAddOptions()
@ -209,20 +234,32 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOption() adds a InputOption object'); $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOption() adds a InputOption object');
$definition->addOption($this->bar); $definition->addOption($this->bar);
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOption() adds a InputOption object'); $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOption() adds a InputOption object');
try { }
$definition->addOption($this->foo2);
$this->fail('->addOption() throws a \LogicException if the another option is already registered with the same name'); /**
} catch (\Exception $e) { * @expectedException \LogicException
$this->assertInstanceOf('\LogicException', $e, '->addOption() throws a \LogicException if the another option is already registered with the same name'); * @expectedExceptionMessage An option named "foo" already exists.
$this->assertEquals('An option named "foo" already exists.', $e->getMessage()); */
} public function testAddDuplicateOption()
try { {
$definition->addOption($this->foo1); $this->initializeOptions();
$this->fail('->addOption() throws a \LogicException if the another option is already registered with the same shortcut');
} catch (\Exception $e) { $definition = new InputDefinition();
$this->assertInstanceOf('\LogicException', $e, '->addOption() throws a \LogicException if the another option is already registered with the same shortcut'); $definition->addOption($this->foo);
$this->assertEquals('An option with shortcut "f" already exists.', $e->getMessage()); $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() public function testGetOption()
@ -231,13 +268,18 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
$definition = new InputDefinition(array($this->foo)); $definition = new InputDefinition(array($this->foo));
$this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name'); $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
try { }
$definition->getOption('bar');
$this->fail('->getOption() throws an \InvalidArgumentException if the option name does not exist'); /**
} catch (\Exception $e) { * @expectedException \InvalidArgumentException
$this->assertInstanceOf('\InvalidArgumentException', $e, '->getOption() throws an \InvalidArgumentException if the option name does not exist'); * @expectedExceptionMessage The "--bar" option does not exist.
$this->assertEquals('The "--bar" option does not exist.', $e->getMessage()); */
} public function testGetInvalidOption()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$definition->getOption('bar');
} }
public function testHasOption() public function testHasOption()
@ -264,13 +306,18 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase
$definition = new InputDefinition(array($this->foo)); $definition = new InputDefinition(array($this->foo));
$this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut'); $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
try { }
$definition->getOptionForShortcut('l');
$this->fail('->getOption() throws an \InvalidArgumentException if the shortcut does not exist'); /**
} catch (\Exception $e) { * @expectedException \InvalidArgumentException
$this->assertInstanceOf('\InvalidArgumentException', $e, '->getOption() throws an \InvalidArgumentException if the shortcut does not exist'); * @expectedExceptionMessage The "-l" option does not exist.
$this->assertEquals('The "-l" option does not exist.', $e->getMessage()); */
} public function testGetOptionForInvalidShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$definition->getOptionForShortcut('l');
} }
public function testGetOptionDefaults() public function testGetOptionDefaults()

View File

@ -21,24 +21,29 @@ class InputOptionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foo', $option->getName(), '__construct() takes a name as its first argument'); $this->assertEquals('foo', $option->getName(), '__construct() takes a name as its first argument');
$option = new InputOption('--foo'); $option = new InputOption('--foo');
$this->assertEquals('foo', $option->getName(), '__construct() removes the leading -- of the option name'); $this->assertEquals('foo', $option->getName(), '__construct() removes the leading -- of the option name');
}
try { /**
$option = new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY); * @expectedException \InvalidArgumentException
$this->fail('__construct() throws an \InvalidArgumentException if VALUE_IS_ARRAY option is used when an option does not accept a value'); * @expectedExceptionMessage Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if VALUE_IS_ARRAY option is used when an option does not accept a value'); public function testArrayModeWithoutValue()
$this->assertEquals('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.', $e->getMessage()); {
} new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY);
}
// shortcut argument public function testShortcut()
{
$option = new InputOption('foo', 'f'); $option = new InputOption('foo', 'f');
$this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument'); $this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument');
$option = new InputOption('foo', '-f'); $option = new InputOption('foo', '-f');
$this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut'); $this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut');
$option = new InputOption('foo'); $option = new InputOption('foo');
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default'); $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
}
// mode argument public function testModes()
{
$option = new InputOption('foo', 'f'); $option = new InputOption('foo', 'f');
$this->assertFalse($option->acceptValue(), '__construct() gives a "InputOption::VALUE_NONE" mode by default'); $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->isValueRequired(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
@ -63,21 +68,24 @@ class InputOptionTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode'); $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->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'); $this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
}
try { /**
$option = new InputOption('foo', 'f', 'ANOTHER_ONE'); * @dataProvider provideInvalidModes
$this->fail('__construct() throws an \InvalidArgumentException if the mode is not valid'); */
} catch (\Exception $e) { public function testInvalidModes($mode)
$this->assertInstanceOf('\InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the mode is not valid'); {
$this->assertEquals('Option mode "ANOTHER_ONE" is not valid.', $e->getMessage()); $this->setExpectedException('InvalidArgumentException', sprintf('Option mode "%s" is not valid.', $mode));
}
try { new InputOption('foo', 'f', $mode);
$option = new InputOption('foo', 'f', -1); }
$this->fail('__construct() throws an \InvalidArgumentException if the mode is not valid');
} catch (\Exception $e) { public function provideInvalidModes()
$this->assertInstanceOf('\InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the mode is not valid'); {
$this->assertEquals('Option mode "-1" is not valid.', $e->getMessage()); return array(
} array('ANOTHER_ONE'),
array(-1)
);
} }
/** /**
@ -147,24 +155,26 @@ class InputOptionTest extends \PHPUnit_Framework_TestCase
$option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY); $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY);
$option->setDefault(array(1, 2)); $option->setDefault(array(1, 2));
$this->assertEquals(array(1, 2), $option->getDefault(), '->setDefault() changes the default value'); $this->assertEquals(array(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 = new InputOption('foo', 'f', InputOption::VALUE_NONE);
try { $option->setDefault('default');
$option->setDefault('default'); }
$this->fail('->setDefault() throws a \LogicException if you give a default value for a VALUE_NONE option');
} catch (\Exception $e) {
$this->assertInstanceOf('\LogicException', $e, '->setDefault() throws a \LogicException if you give a default value for a VALUE_NONE option');
$this->assertEquals('Cannot set a default value when using InputOption::VALUE_NONE mode.', $e->getMessage());
}
/**
* @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 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
try { $option->setDefault('default');
$option->setDefault('default');
$this->fail('->setDefault() throws a \LogicException if you give a default value which is not an array for a VALUE_IS_ARRAY option');
} catch (\Exception $e) {
$this->assertInstanceOf('\LogicException', $e, '->setDefault() throws a \LogicException if you give a default value which is not an array for a VALUE_IS_ARRAY option');
$this->assertEquals('A default value for an array option must be an array.', $e->getMessage());
}
} }
public function testEquals() public function testEquals()

View File

@ -36,22 +36,26 @@ class InputTest extends \PHPUnit_Framework_TestCase
$input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')))); $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(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('default', $input->getOption('bar'), '->getOption() returns the default value for optional options');
$this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getOptions(), '->getOptions() returns all option values, even optional ones'); $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getOptions(), '->getOptions() returns all option values, even optional ones');
}
try { /**
$input->setOption('foo', 'bar'); * @expectedException \InvalidArgumentException
$this->fail('->setOption() throws a \InvalidArgumentException if the option does not exist'); * @expectedExceptionMessage The "foo" option does not exist.
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist'); public function testSetInvalidOption()
$this->assertEquals('The "foo" option does not exist.', $e->getMessage()); {
} $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
$input->setOption('foo', 'bar');
}
try { /**
$input->getOption('foo'); * @expectedException \InvalidArgumentException
$this->fail('->getOption() throws a \InvalidArgumentException if the option does not exist'); * @expectedExceptionMessage The "foo" option does not exist.
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist'); public function testGetInvalidOption()
$this->assertEquals('The "foo" option does not exist.', $e->getMessage()); {
} $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
$input->getOption('foo');
} }
public function testArguments() public function testArguments()
@ -66,45 +70,45 @@ class InputTest extends \PHPUnit_Framework_TestCase
$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')))); $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(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('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
$this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones'); $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
}
try { /**
$input->setArgument('foo', 'bar'); * @expectedException \InvalidArgumentException
$this->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist'); * @expectedExceptionMessage The "foo" argument does not exist.
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist'); public function testSetInvalidArgument()
$this->assertEquals('The "foo" argument does not exist.', $e->getMessage()); {
} $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
$input->setArgument('foo', 'bar');
}
try { /**
$input->getArgument('foo'); * @expectedException \InvalidArgumentException
$this->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist'); * @expectedExceptionMessage The "foo" argument does not exist.
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist'); public function testGetInvalidArgument()
$this->assertEquals('The "foo" argument does not exist.', $e->getMessage()); {
} $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
$input->getArgument('foo');
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Not enough arguments.
*/
public function testValidateWithMissingArguments()
{
$input = new ArrayInput(array());
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
$input->validate();
} }
public function testValidate() public function testValidate()
{ {
$input = new ArrayInput(array());
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
try {
$input->validate();
$this->fail('->validate() throws a \RuntimeException if not enough arguments are given');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e, '->validate() throws a \RuntimeException if not enough arguments are given');
$this->assertEquals('Not enough arguments.', $e->getMessage());
}
$input = new ArrayInput(array('name' => 'foo')); $input = new ArrayInput(array('name' => 'foo'));
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED)))); $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
try { $this->assertNull($input->validate());
$input->validate();
} catch (\RuntimeException $e) {
$this->fail('->validate() does not throw a \RuntimeException if enough arguments are given');
}
} }
public function testSetGetInteractive() public function testSetGetInteractive()

View File

@ -37,43 +37,69 @@ class OutputTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity'); $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity');
} }
public function testWrite() public function testWriteWithVerbosityQuiet()
{ {
$fooStyle = new OutputFormatterStyle('yellow', 'red', array('blink'));
$output = new TestOutput(Output::VERBOSITY_QUIET); $output = new TestOutput(Output::VERBOSITY_QUIET);
$output->writeln('foo'); $output->writeln('foo');
$this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET'); $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
}
public function testWriteAnArrayOfMessages()
{
$output = new TestOutput(); $output = new TestOutput();
$output->writeln(array('foo', 'bar')); $output->writeln(array('foo', 'bar'));
$this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output'); $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
}
/**
* @dataProvider provideWriteArguments
*/
public function testWriteRawMessage($message, $type, $expectedOutput)
{
$output = new TestOutput(); $output = new TestOutput();
$output->writeln('<info>foo</info>', Output::OUTPUT_RAW); $output->writeln($message, $type);
$this->assertEquals("<info>foo</info>\n", $output->output, '->writeln() outputs the raw message if OUTPUT_RAW is specified'); $this->assertEquals($expectedOutput, $output->output);
}
$output = new TestOutput(); public function provideWriteArguments()
$output->writeln('<info>foo</info>', Output::OUTPUT_PLAIN); {
$this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if OUTPUT_PLAIN is specified'); return array(
array('<info>foo</info>', Output::OUTPUT_RAW, "<info>foo</info>\n"),
array('<info>foo</info>', Output::OUTPUT_PLAIN, "foo\n"),
);
}
public function testWriteWithDecorationTurnedOff()
{
$output = new TestOutput(); $output = new TestOutput();
$output->setDecorated(false); $output->setDecorated(false);
$output->writeln('<info>foo</info>'); $output->writeln('<info>foo</info>');
$this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false'); $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
}
public function testWriteDecoratedMessage()
{
$fooStyle = new OutputFormatterStyle('yellow', 'red', array('blink'));
$output = new TestOutput(); $output = new TestOutput();
$output->getFormatter()->setStyle('FOO', $fooStyle); $output->getFormatter()->setStyle('FOO', $fooStyle);
$output->setDecorated(true); $output->setDecorated(true);
$output->writeln('<foo>foo</foo>'); $output->writeln('<foo>foo</foo>');
$this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output'); $this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output');
}
try { /**
$output->writeln('<foo>foo</foo>', 24); * @expectedException \InvalidArgumentException
$this->fail('->writeln() throws an \InvalidArgumentException when the type does not exist'); * @expectedExceptionMessage Unknown output type given (24)
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '->writeln() throws an \InvalidArgumentException when the type does not exist'); public function testWriteWithInvalidOutputType()
$this->assertEquals('Unknown output type given (24)', $e->getMessage()); {
} $output = new TestOutput();
$output->writeln('<foo>foo</foo>', 24);
}
public function testWriteWithInvalidStyle()
{
$output = new TestOutput();
$output->clear(); $output->clear();
$output->write('<bar>foo</bar>'); $output->write('<bar>foo</bar>');

View File

@ -30,19 +30,20 @@ class StreamOutputTest extends \PHPUnit_Framework_TestCase
public function testConstructor() public function testConstructor()
{ {
try {
$output = new StreamOutput('foo');
$this->fail('__construct() throws an \InvalidArgumentException if the first argument is not a stream');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the first argument is not a stream');
$this->assertEquals('The StreamOutput class needs a stream as its first argument.', $e->getMessage());
}
$output = new StreamOutput($this->stream, Output::VERBOSITY_QUIET, true); $output = new StreamOutput($this->stream, Output::VERBOSITY_QUIET, true);
$this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument'); $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
$this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument'); $this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument');
} }
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The StreamOutput class needs a stream as its first argument.
*/
public function testStreamIsRequired()
{
new StreamOutput('foo');
}
public function testGetStream() public function testGetStream()
{ {
$output = new StreamOutput($this->stream); $output = new StreamOutput($this->stream);