diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 23edd490b3..2d2da94542 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -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('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->add($foo = new \FooCommand()); // simulate --help @@ -145,7 +137,17 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase $p->setAccessible(true); $p->setValue($application, true); $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() @@ -164,84 +166,90 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation'); $application->add(new \Foo2Command()); $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'); - $this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); - $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'); - } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The namespace "f" is ambiguous (foo, foo1). + */ + public function testFindAmbiguousNamespace() + { + $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() { $application = new Application(); $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 \Foo2Command()); - try { - $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'); - } + $application->find($abbreviation); } - 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->add(new \FooCommand()); + $application->find($name); + } - // Command + 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'); - } - - // 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 provideInvalidCommandNamesSingle() + { + return array( + array('foo:baR'), + array('foO:bar') + ); + } + public function testFindAlternativeExceptionMessageMultiple() + { + $application = new Application(); + $application->add(new \FooCommand()); $application->add(new \Foo1Command()); $application->add(new \Foo2Command()); diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 166ce7879e..acbe2ea10c 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -35,17 +35,19 @@ class CommandTest extends \PHPUnit_Framework_TestCase 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'); $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() { $application = new Application(); @@ -92,22 +94,25 @@ class CommandTest extends \PHPUnit_Framework_TestCase $ret = $command->setName('foobar:bar'); $this->assertEquals($command, $ret, '->setName() implements a fluent interface'); $this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name'); + } - try { - $command->setName(''); - $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'); - $this->assertEquals('Command name "" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty'); - } + /** + * @dataProvider provideInvalidCommandNames + */ + public function testInvalidCommandNames($name) + { + $this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name)); - try { - $command->setName('foo:'); - $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'); - $this->assertEquals('Command name "foo:" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty'); - } + $command = new \TestCommand(); + $command->setName($name); + } + + public function provideInvalidCommandNames() + { + return array( + array(''), + array('foo:') + ); } 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'); } - 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(); $tester = new CommandTester($command); - try { - $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'); - } + $tester->execute(array('--bar' => true)); } public function testRunReturnsAlwaysInteger() @@ -251,7 +267,7 @@ class CommandTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException + * @expectedException \InvalidArgumentException * @expectedExceptionMessage Invalid callable provided to Command::setCode. */ public function testSetCodeWithNonCallable() diff --git a/src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php b/src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php index b62973d9d6..f9b72e6ae3 100644 --- a/src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php @@ -18,33 +18,51 @@ use Symfony\Component\Console\Application; class HelpCommandTest extends \PHPUnit_Framework_TestCase { - public function testExecute() + public function testExecuteForCommandAlias() { $command = new HelpCommand(); - - $application = new Application(); - $command->setApplication($application); + $command->setApplication(new Application()); $commandTester = new CommandTester($command); $commandTester->execute(array('command_name' => 'li')); + $this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias'); + } + public function testExecuteForCommand() + { $command = new HelpCommand(); - $commandTester = new CommandTester($command); $command->setCommand(new ListCommand()); $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()); $commandTester->execute(array('--xml' => true)); - $this->assertRegExp('/getDisplay(), '->execute() returns an XML help text if --xml is passed'); + $this->assertRegExp('/getDisplay(), '->execute() returns an XML help text if --xml is passed'); + } + + public function testExecuteForApplicationCommand() + { $application = new Application(); $commandTester = new CommandTester($application->get('help')); $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)); + $this->assertRegExp('/getDisplay(), '->execute() returns an XML help text if --xml is passed'); } } diff --git a/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php b/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php index fb0eaccb0c..cad313652c 100644 --- a/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php @@ -16,23 +16,35 @@ use Symfony\Component\Console\Application; class ListCommandTest extends \PHPUnit_Framework_TestCase { - public function testExecute() + public function testExecuteListsCommands() { $application = new Application(); - $commandTester = new CommandTester($command = $application->get('list')); $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'); + } + public function testExecuteListsCommandsWithXmlOption() + { + $application = new Application(); + $commandTester = new CommandTester($command = $application->get('list')); $commandTester->execute(array('command' => $command->getName(), '--xml' => true)); - $this->assertRegExp('//', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed'); + $this->assertRegExp('//', $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)); $output = <<assertEquals(str_replace("\n", PHP_EOL, $output), $commandTester->getDisplay(), 'boo'); + + $this->assertEquals(str_replace("\n", PHP_EOL, $output), $commandTester->getDisplay()); } } diff --git a/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php b/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php index fc7ab21834..38cc038ea7 100644 --- a/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php @@ -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'); } - public function testParser() + public function testParseArguments() { $input = new ArgvInput(array('cli.php', 'foo')); $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')))); $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')))); - $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without a value'); + /** + * @dataProvider provideOptions + */ + public function testParseOptions($input, $options, $expectedOptions, $message) + { + $input = new ArgvInput($input); + $input->bind(new InputDefinition($options)); - $input = new ArgvInput(array('cli.php', '--foo=bar')); - $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)'); + $this->assertEquals($expectedOptions, $input->getOptions(), $message); + } - $input = new ArgvInput(array('cli.php', '--foo', 'bar')); - $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)'); + public function provideOptions() + { + 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')); - $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'); - } + /** + * @dataProvider provideInvalidInput + */ + public function testInvalidInput($argv, $definition, $expectedExceptionMessage) + { + $this->setExpectedException('RuntimeException', $expectedExceptionMessage); - $input = new ArgvInput(array('cli.php', '-f')); - $input->bind(new InputDefinition(array(new InputOption('foo', 'f')))); - $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without a value'); + $input = new ArgvInput($argv); + $input->bind($definition); + } - $input = new ArgvInput(array('cli.php', '-fbar')); - $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)'); + public function provideInvalidInput() + { + 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')); - $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)'); + public function testParseArrayArgument() + { + $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', '')); - $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'); - } + $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments'); + } + public function testParseArrayOption() + { $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)))); + $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions()); + } - try { - $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'); - } - + public function testParseNegativeNumberAfterDoubleDash() + { $input = new ArgvInput(array('cli.php', '--', '-1')); $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'); @@ -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)))); $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'); + } + public function testParseEmptyStringArgument() + { $input = new ArgvInput(array('cli.php', '-f', 'bar', '')); $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'); } diff --git a/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php b/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php index 82f477ef8e..e1838a721e 100644 --- a/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php @@ -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'); } - public function testParse() + public function testParseArguments() { $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); + $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments'); + } - try { - $input = new ArrayInput(array('foo' => 'foo'), new InputDefinition(array(new InputArgument('name')))); - $this->fail('->parse() throws an \InvalidArgumentException exception if an invalid argument is passed'); - } catch (\Exception $e) { - $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'); - } + /** + * @dataProvider provideOptions + */ + public function testParseOptions($input, $options, $expectedOptions, $message) + { + $input = new ArrayInput($input, new InputDefinition($options)); - $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo')))); - $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options'); + $this->assertEquals($expectedOptions, $input->getOptions(), $message); + } - $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')))); - $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a default value'); + public function provideOptions() + { + 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 { - $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'); - } + new ArrayInput($parameters, $definition); + } - try { - $input = new ArrayInput(array('--foo' => 'foo'), new InputDefinition()); - $this->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); - $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); - } - - $input = new ArrayInput(array('-f' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f')))); - $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options'); - - try { - $input = new ArrayInput(array('-o' => 'foo'), new InputDefinition()); - $this->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); - $this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); - } + public function provideInvalidInput() + { + return array( + array( + array('foo' => 'foo'), + new InputDefinition(array(new InputArgument('name'))), + 'The "foo" argument does not exist.' + ), + array( + array('--foo' => null), + new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))), + 'The "--foo" option requires a value.' + ), + array( + array('--foo' => 'foo'), + new InputDefinition(), + 'The "--foo" option does not exist.' + ), + array( + array('-o' => 'foo'), + new InputDefinition(), + 'The "-o" option does not exist.' + ) + ); } } diff --git a/src/Symfony/Component/Console/Tests/Input/InputArgumentTest.php b/src/Symfony/Component/Console/Tests/Input/InputArgumentTest.php index 1d3144f628..3bfc796696 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputArgumentTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputArgumentTest.php @@ -19,8 +19,10 @@ class InputArgumentTest extends \PHPUnit_Framework_TestCase { $argument = new InputArgument('foo'); $this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument'); + } - // mode argument + public function testModes() + { $argument = new InputArgument('foo'); $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); $this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode'); + } - try { - $argument = new InputArgument('foo', 'ANOTHER_ONE'); - $this->fail('__construct() throws an \InvalidArgumentException if the mode is not valid'); - } catch (\Exception $e) { - $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()); - } - try { - $argument = new InputArgument('foo', -1); - $this->fail('__construct() throws an \InvalidArgumentException if the mode is not valid'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the mode is not valid'); - $this->assertEquals('Argument mode "-1" is not valid.', $e->getMessage()); - } + /** + * @dataProvider provideInvalidModes + */ + public function testInvalidModes($mode) + { + $this->setExpectedException('InvalidArgumentException', sprintf('Argument mode "%s" is not valid.', $mode)); + + new InputArgument('foo', $mode); + } + + public function provideInvalidModes() + { + return array( + array('ANOTHER_ONE'), + array(-1) + ); } public function testIsArray() @@ -82,23 +87,25 @@ class InputArgumentTest extends \PHPUnit_Framework_TestCase $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY); $argument->setDefault(array(1, 2)); $this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value'); + } - try { - $argument = new InputArgument('foo', InputArgument::REQUIRED); - $argument->setDefault('default'); - $this->fail('->setDefault() throws a \LogicException if you give a default value for a required argument'); - } catch (\Exception $e) { - $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()); - } + /** + * @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'); + } - try { - $argument = new InputArgument('foo', InputArgument::IS_ARRAY); - $argument->setDefault('default'); - $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) { - $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()); - } + /** + * @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'); } } diff --git a/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php b/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php index c322ed42bc..76316bd863 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php @@ -26,7 +26,7 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase self::$fixtures = __DIR__.'/../Fixtures/'; } - public function testConstructor() + public function testConstructorArguments() { $this->initializeArguments(); @@ -35,7 +35,10 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase $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'); + } + public function testConstructorOptions() + { $this->initializeOptions(); $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'); $definition->addArgument($this->bar); $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArgument() adds a InputArgument object'); + } - // arguments must have different names - try { - $definition->addArgument($this->foo1); - $this->fail('->addArgument() throws a \LogicException if another argument is already registered with the same name'); - } catch (\Exception $e) { - $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()); - } + /** + * @expectedException \LogicException + * @expectedExceptionMessage An argument with name "foo" already exists. + */ + public function testArgumentsMustHaveDifferentNames() + { + $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->addArgument($this->foo); - try { - $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'); - $this->assertEquals('Cannot add a required argument after an optional one.', $e->getMessage()); - } + $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() @@ -116,13 +128,19 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase $definition = new InputDefinition(); $definition->addArguments(array($this->foo)); $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) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->getArgument() throws an \InvalidArgumentException if the InputArgument name does not exist'); - $this->assertEquals('The "bar" argument does not exist.', $e->getMessage()); - } + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The "bar" argument does not exist. + */ + public function testGetInvalidArgument() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $definition->addArguments(array($this->foo)); + $definition->getArgument('bar'); } public function testHasArgument() @@ -131,6 +149,7 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase $definition = new InputDefinition(); $definition->addArguments(array($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'); } @@ -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'); $definition->setOptions(array($this->bar)); $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) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOptions() clears all InputOption objects'); - $this->assertEquals('The "-f" option does not exist.', $e->getMessage()); - } + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The "-f" option does not exist. + */ + public function testSetOptionsClearsOptions() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $definition->setOptions(array($this->bar)); + $definition->getOptionForShortcut('f'); } 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'); $definition->addOption($this->bar); $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) { - $this->assertInstanceOf('\LogicException', $e, '->addOption() throws a \LogicException if the another option is already registered with the same name'); - $this->assertEquals('An option named "foo" already exists.', $e->getMessage()); - } - try { - $definition->addOption($this->foo1); - $this->fail('->addOption() throws a \LogicException if the another option is already registered with the same shortcut'); - } catch (\Exception $e) { - $this->assertInstanceOf('\LogicException', $e, '->addOption() throws a \LogicException if the another option is already registered with the same shortcut'); - $this->assertEquals('An option with shortcut "f" already exists.', $e->getMessage()); - } + } + + /** + * @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() @@ -231,13 +268,18 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase $definition = new InputDefinition(array($this->foo)); $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) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->getOption() throws an \InvalidArgumentException if the option name does not exist'); - $this->assertEquals('The "--bar" option does not exist.', $e->getMessage()); - } + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The "--bar" option does not exist. + */ + public function testGetInvalidOption() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $definition->getOption('bar'); } public function testHasOption() @@ -264,13 +306,18 @@ class InputDefinitionTest extends \PHPUnit_Framework_TestCase $definition = new InputDefinition(array($this->foo)); $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) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->getOption() throws an \InvalidArgumentException if the shortcut does not exist'); - $this->assertEquals('The "-l" option does not exist.', $e->getMessage()); - } + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The "-l" option does not exist. + */ + public function testGetOptionForInvalidShortcut() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $definition->getOptionForShortcut('l'); } public function testGetOptionDefaults() diff --git a/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php b/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php index 1e0de35e3f..b50c090484 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php @@ -21,24 +21,29 @@ class InputOptionTest extends \PHPUnit_Framework_TestCase $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'); + } - try { - $option = new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY); - $this->fail('__construct() throws an \InvalidArgumentException if VALUE_IS_ARRAY option is used when an 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'); - $this->assertEquals('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.', $e->getMessage()); - } + /** + * @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); + } - // shortcut argument + 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'); $this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut'); $option = new InputOption('foo'); $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default'); + } - // mode argument + 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'); @@ -63,21 +68,24 @@ class InputOptionTest extends \PHPUnit_Framework_TestCase $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'); + } - try { - $option = new InputOption('foo', 'f', 'ANOTHER_ONE'); - $this->fail('__construct() throws an \InvalidArgumentException if the mode is not valid'); - } catch (\Exception $e) { - $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()); - } - try { - $option = new InputOption('foo', 'f', -1); - $this->fail('__construct() throws an \InvalidArgumentException if the mode is not valid'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the mode is not valid'); - $this->assertEquals('Option mode "-1" is not valid.', $e->getMessage()); - } + /** + * @dataProvider provideInvalidModes + */ + public function testInvalidModes($mode) + { + $this->setExpectedException('InvalidArgumentException', sprintf('Option mode "%s" is not valid.', $mode)); + + new InputOption('foo', 'f', $mode); + } + + public function provideInvalidModes() + { + 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->setDefault(array(1, 2)); $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); - try { - $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()); - } + $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); - try { - $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()); - } + $option->setDefault('default'); } public function testEquals() diff --git a/src/Symfony/Component/Console/Tests/Input/InputTest.php b/src/Symfony/Component/Console/Tests/Input/InputTest.php index 2863ea4f34..0b3e38fb6f 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputTest.php @@ -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')))); $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'); + } - try { - $input->setOption('foo', 'bar'); - $this->fail('->setOption() throws a \InvalidArgumentException if the option does not exist'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist'); - $this->assertEquals('The "foo" option does not exist.', $e->getMessage()); - } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The "foo" option does not exist. + */ + public function testSetInvalidOption() + { + $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'); - $this->fail('->getOption() throws a \InvalidArgumentException if the option does not exist'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist'); - $this->assertEquals('The "foo" option does not exist.', $e->getMessage()); - } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The "foo" option does not exist. + */ + public function testGetInvalidOption() + { + $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() @@ -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')))); $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'); + } - try { - $input->setArgument('foo', 'bar'); - $this->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist'); - $this->assertEquals('The "foo" argument does not exist.', $e->getMessage()); - } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The "foo" argument does not exist. + */ + public function testSetInvalidArgument() + { + $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'); - $this->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist'); - $this->assertEquals('The "foo" argument does not exist.', $e->getMessage()); - } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The "foo" argument does not exist. + */ + public function testGetInvalidArgument() + { + $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() { - $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->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED)))); - try { - $input->validate(); - } catch (\RuntimeException $e) { - $this->fail('->validate() does not throw a \RuntimeException if enough arguments are given'); - } + $this->assertNull($input->validate()); } public function testSetGetInteractive() diff --git a/src/Symfony/Component/Console/Tests/Output/OutputTest.php b/src/Symfony/Component/Console/Tests/Output/OutputTest.php index aa4a2046b8..be58b72d3c 100644 --- a/src/Symfony/Component/Console/Tests/Output/OutputTest.php +++ b/src/Symfony/Component/Console/Tests/Output/OutputTest.php @@ -37,43 +37,69 @@ class OutputTest extends \PHPUnit_Framework_TestCase $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->writeln('foo'); $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET'); + } + public function testWriteAnArrayOfMessages() + { $output = new TestOutput(); $output->writeln(array('foo', 'bar')); $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->writeln('foo', Output::OUTPUT_RAW); - $this->assertEquals("foo\n", $output->output, '->writeln() outputs the raw message if OUTPUT_RAW is specified'); + $output->writeln($message, $type); + $this->assertEquals($expectedOutput, $output->output); + } - $output = new TestOutput(); - $output->writeln('foo', Output::OUTPUT_PLAIN); - $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if OUTPUT_PLAIN is specified'); + public function provideWriteArguments() + { + return array( + array('foo', Output::OUTPUT_RAW, "foo\n"), + array('foo', Output::OUTPUT_PLAIN, "foo\n"), + ); + } + public function testWriteWithDecorationTurnedOff() + { $output = new TestOutput(); $output->setDecorated(false); $output->writeln('foo'); $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->getFormatter()->setStyle('FOO', $fooStyle); $output->setDecorated(true); $output->writeln('foo'); $this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output'); + } - try { - $output->writeln('foo', 24); - $this->fail('->writeln() throws an \InvalidArgumentException when the type does not exist'); - } catch (\Exception $e) { - $this->assertInstanceOf('\InvalidArgumentException', $e, '->writeln() throws an \InvalidArgumentException when the type does not exist'); - $this->assertEquals('Unknown output type given (24)', $e->getMessage()); - } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Unknown output type given (24) + */ + public function testWriteWithInvalidOutputType() + { + $output = new TestOutput(); + $output->writeln('foo', 24); + } + + public function testWriteWithInvalidStyle() + { + $output = new TestOutput(); $output->clear(); $output->write('foo'); diff --git a/src/Symfony/Component/Console/Tests/Output/StreamOutputTest.php b/src/Symfony/Component/Console/Tests/Output/StreamOutputTest.php index b151a486df..2fd4f61214 100644 --- a/src/Symfony/Component/Console/Tests/Output/StreamOutputTest.php +++ b/src/Symfony/Component/Console/Tests/Output/StreamOutputTest.php @@ -30,19 +30,20 @@ class StreamOutputTest extends \PHPUnit_Framework_TestCase 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); $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'); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The StreamOutput class needs a stream as its first argument. + */ + public function testStreamIsRequired() + { + new StreamOutput('foo'); + } + public function testGetStream() { $output = new StreamOutput($this->stream);