Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moved component and bridge unit tests to the src/ directory

This is the first step to make each Symfony Component and Bridge self-contained.
  • Loading branch information...
commit 81b09796160a382f6c01a54f6c7d7e091ae1b7bf 1 parent 911c0dc
@fabpot fabpot authored
Showing with 3,195 additions and 2 deletions.
  1. +2 −2 README.md
  2. +448 −0 Tests/ApplicationTest.php
  3. +254 −0 Tests/Command/CommandTest.php
  4. +42 −0 Tests/Command/HelpCommandTest.php
  5. +38 −0 Tests/Command/ListCommandTest.php
  6. +26 −0 Tests/Fixtures/Foo1Command.php
  7. +21 −0 Tests/Fixtures/Foo2Command.php
  8. +25 −0 Tests/Fixtures/Foo3Command.php
  9. +33 −0 Tests/Fixtures/FooCommand.php
  10. +28 −0 Tests/Fixtures/TestCommand.php
  11. +20 −0 Tests/Fixtures/application_astext1.txt
  12. +16 −0 Tests/Fixtures/application_astext2.txt
  13. +83 −0 Tests/Fixtures/application_asxml1.txt
  14. +15 −0 Tests/Fixtures/application_asxml2.txt
  15. +13 −0 Tests/Fixtures/application_gethelp.txt
  16. +8 −0 Tests/Fixtures/application_renderexception1.txt
  17. +11 −0 Tests/Fixtures/application_renderexception2.txt
  18. +19 −0 Tests/Fixtures/application_renderexception3.txt
  19. +17 −0 Tests/Fixtures/application_run1.txt
  20. +26 −0 Tests/Fixtures/application_run2.txt
  21. +27 −0 Tests/Fixtures/application_run3.txt
  22. +1 −0  Tests/Fixtures/application_run4.txt
  23. +18 −0 Tests/Fixtures/command_astext.txt
  24. +38 −0 Tests/Fixtures/command_asxml.txt
  25. +11 −0 Tests/Fixtures/definition_astext.txt
  26. +39 −0 Tests/Fixtures/definition_asxml.txt
  27. +77 −0 Tests/Formatter/OutputFormatterStyleTest.php
  28. +182 −0 Tests/Formatter/OutputFormatterTest.php
  29. +93 −0 Tests/Helper/DialogHelperTest.php
  30. +71 −0 Tests/Helper/FormatterHelperTest.php
  31. +213 −0 Tests/Input/ArgvInputTest.php
  32. +90 −0 Tests/Input/ArrayInputTest.php
  33. +104 −0 Tests/Input/InputArgumentTest.php
  34. +364 −0 Tests/Input/InputDefinitionTest.php
  35. +166 −0 Tests/Input/InputOptionTest.php
  36. +117 −0 Tests/Input/InputTest.php
  37. +58 −0 Tests/Input/StringInputTest.php
  38. +24 −0 Tests/Output/ConsoleOutputTest.php
  39. +24 −0 Tests/Output/NullOutputTest.php
  40. +101 −0 Tests/Output/OutputTest.php
  41. +59 −0 Tests/Output/StreamOutputTest.php
  42. +64 −0 Tests/Tester/ApplicationTesterTest.php
  43. +62 −0 Tests/Tester/CommandTesterTest.php
  44. +18 −0 Tests/bootstrap.php
  45. +29 −0 phpunit.xml.dist
View
4 README.md
@@ -43,6 +43,6 @@ validation, automatic help messages, ...
Resources
---------
-Unit tests:
+You can run the unit tests with the following command:
-https://github.com/symfony/symfony/tree/master/tests/Symfony/Tests/Component/Console
+ phpunit -c src/Symfony/Component/Console/
View
448 Tests/ApplicationTest.php
@@ -0,0 +1,448 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests;
+
+use Symfony\Component\Console\Application;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\NullOutput;
+use Symfony\Component\Console\Output\Output;
+use Symfony\Component\Console\Tester\ApplicationTester;
+
+class ApplicationTest extends \PHPUnit_Framework_TestCase
+{
+ static protected $fixturesPath;
+
+ static public function setUpBeforeClass()
+ {
+ self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
+ require_once self::$fixturesPath.'/FooCommand.php';
+ require_once self::$fixturesPath.'/Foo1Command.php';
+ require_once self::$fixturesPath.'/Foo2Command.php';
+ require_once self::$fixturesPath.'/Foo3Command.php';
+ }
+
+ protected function normalizeLineBreaks($text)
+ {
+ return str_replace(PHP_EOL, "\n", $text);
+ }
+
+ /**
+ * Replaces the dynamic placeholders of the command help text with a static version.
+ * The placeholder %command.full_name% includes the script path that is not predictable
+ * and can not be tested against.
+ */
+ protected function ensureStaticCommandHelp(Application $application)
+ {
+ foreach ($application->all() as $command) {
+ $command->setHelp(str_replace('%command.full_name%', 'app/console %command.name%', $command->getHelp()));
+ }
+ }
+
+ public function testConstructor()
+ {
+ $application = new Application('foo', 'bar');
+ $this->assertEquals('foo', $application->getName(), '__construct() takes the application name as its first argument');
+ $this->assertEquals('bar', $application->getVersion(), '__construct() takes the application version as its first argument');
+ $this->assertEquals(array('help', 'list'), array_keys($application->all()), '__construct() registered the help and list commands by default');
+ }
+
+ public function testSetGetName()
+ {
+ $application = new Application();
+ $application->setName('foo');
+ $this->assertEquals('foo', $application->getName(), '->setName() sets the name of the application');
+ }
+
+ public function testSetGetVersion()
+ {
+ $application = new Application();
+ $application->setVersion('bar');
+ $this->assertEquals('bar', $application->getVersion(), '->setVersion() sets the version of the application');
+ }
+
+ public function testGetLongVersion()
+ {
+ $application = new Application('foo', 'bar');
+ $this->assertEquals('<info>foo</info> version <comment>bar</comment>', $application->getLongVersion(), '->getLongVersion() returns the long version of the application');
+ }
+
+ public function testHelp()
+ {
+ $application = new Application();
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_gethelp.txt', $this->normalizeLineBreaks($application->getHelp()), '->setHelp() returns a help message');
+ }
+
+ public function testAll()
+ {
+ $application = new Application();
+ $commands = $application->all();
+ $this->assertEquals('Symfony\\Component\\Console\\Command\\HelpCommand', get_class($commands['help']), '->all() returns the registered commands');
+
+ $application->add(new \FooCommand());
+ $commands = $application->all('foo');
+ $this->assertEquals(1, count($commands), '->all() takes a namespace as its first argument');
+ }
+
+ public function testRegister()
+ {
+ $application = new Application();
+ $command = $application->register('foo');
+ $this->assertEquals('foo', $command->getName(), '->register() registers a new command');
+ }
+
+ public function testAdd()
+ {
+ $application = new Application();
+ $application->add($foo = new \FooCommand());
+ $commands = $application->all();
+ $this->assertEquals($foo, $commands['foo:bar'], '->add() registers a command');
+
+ $application = new Application();
+ $application->addCommands(array($foo = new \FooCommand(), $foo1 = new \Foo1Command()));
+ $commands = $application->all();
+ $this->assertEquals(array($foo, $foo1), array($commands['foo:bar'], $commands['foo:bar1']), '->addCommands() registers an array of commands');
+ }
+
+ public function testHasGet()
+ {
+ $application = new Application();
+ $this->assertTrue($application->has('list'), '->has() returns true if a named command is registered');
+ $this->assertFalse($application->has('afoobar'), '->has() returns false if a named command is not registered');
+
+ $application->add($foo = new \FooCommand());
+ $this->assertTrue($application->has('afoobar'), '->has() returns true if an alias is registered');
+ $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
+ $r = new \ReflectionObject($application);
+ $p = $r->getProperty('wantHelps');
+ $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');
+ }
+
+ public function testGetNamespaces()
+ {
+ $application = new Application();
+ $application->add(new \FooCommand());
+ $application->add(new \Foo1Command());
+ $this->assertEquals(array('foo'), $application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces');
+ }
+
+ public function testFindNamespace()
+ {
+ $application = new Application();
+ $application->add(new \FooCommand());
+ $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
+ $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');
+ }
+ }
+
+ 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');
+
+ $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');
+ }
+ }
+
+ public function testFindAlternativeCommands()
+ {
+ $application = new Application();
+
+ $application->add(new \FooCommand());
+ $application->add(new \Foo1Command());
+ $application->add(new \Foo2Command());
+
+ try {
+ $application->find($commandName = 'Unknow command');
+ $this->fail('->find() throws an \InvalidArgumentException if command does not exist');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist');
+ $this->assertEquals(sprintf('Command "%s" is not defined.', $commandName), $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, without alternatives');
+ }
+
+ try {
+ $application->find($commandName = 'foo');
+ $this->fail('->find() throws an \InvalidArgumentException if command does not exist');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist');
+ $this->assertRegExp(sprintf('/Command "%s" is not defined./', $commandName), $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
+ $this->assertRegExp('/foo:bar/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternative : "foo:bar"');
+ $this->assertRegExp('/foo1:bar/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternative : "foo1:bar"');
+ $this->assertRegExp('/foo:bar1/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternative : "foo:bar1"');
+ }
+
+ // Test if "foo1" command throw an "\InvalidArgumentException" and does not contain
+ // "foo:bar" as alternative because "foo1" is too far from "foo:bar"
+ try {
+ $application->find($commandName = 'foo1');
+ $this->fail('->find() throws an \InvalidArgumentException if command does not exist');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist');
+ $this->assertRegExp(sprintf('/Command "%s" is not defined./', $commandName), $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
+ $this->assertFalse(strpos($e->getMessage(), 'foo:bar'), '->find() throws an \InvalidArgumentException if command does not exist, without "foo:bar" alternative');
+ }
+ }
+
+ public function testFindAlternativeNamespace()
+ {
+ $application = new Application();
+
+ $application->add(new \FooCommand());
+ $application->add(new \Foo1Command());
+ $application->add(new \Foo2Command());
+ $application->add(new \foo3Command());
+
+ try {
+ $application->find('Unknow-namespace:Unknow-command');
+ $this->fail('->find() throws an \InvalidArgumentException if namespace does not exist');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if namespace does not exist');
+ $this->assertEquals('There are no commands defined in the "Unknow-namespace" namespace.', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, without alternatives');
+ }
+
+ try {
+ $application->find('foo2:command');
+ $this->fail('->find() throws an \InvalidArgumentException if namespace does not exist');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if namespace does not exist');
+ $this->assertRegExp('/There are no commands defined in the "foo2" namespace./', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative');
+ $this->assertRegExp('/foo/', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo"');
+ $this->assertRegExp('/foo1/', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo1"');
+ $this->assertRegExp('/foo3/', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo3"');
+ }
+ }
+
+ public function testSetCatchExceptions()
+ {
+ $application = new Application();
+ $application->setAutoExit(false);
+ $tester = new ApplicationTester($application);
+
+ $application->setCatchExceptions(true);
+ $tester->run(array('command' => 'foo'), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->setCatchExceptions() sets the catch exception flag');
+
+ $application->setCatchExceptions(false);
+ try {
+ $tester->run(array('command' => 'foo'), array('decorated' => false));
+ $this->fail('->setCatchExceptions() sets the catch exception flag');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('\Exception', $e, '->setCatchExceptions() sets the catch exception flag');
+ $this->assertEquals('Command "foo" is not defined.', $e->getMessage(), '->setCatchExceptions() sets the catch exception flag');
+ }
+ }
+
+ public function testAsText()
+ {
+ $application = new Application();
+ $application->add(new \FooCommand);
+ $this->ensureStaticCommandHelp($application);
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext1.txt', $this->normalizeLineBreaks($application->asText()), '->asText() returns a text representation of the application');
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext2.txt', $this->normalizeLineBreaks($application->asText('foo')), '->asText() returns a text representation of the application');
+ }
+
+ public function testAsXml()
+ {
+ $application = new Application();
+ $application->add(new \FooCommand);
+ $this->ensureStaticCommandHelp($application);
+ $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml1.txt', $application->asXml(), '->asXml() returns an XML representation of the application');
+ $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml2.txt', $application->asXml('foo'), '->asXml() returns an XML representation of the application');
+ }
+
+ public function testRenderException()
+ {
+ $application = new Application();
+ $application->setAutoExit(false);
+ $tester = new ApplicationTester($application);
+
+ $tester->run(array('command' => 'foo'), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exception');
+
+ $tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
+ $this->assertContains('Exception trace', $tester->getDisplay(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
+
+ $tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
+
+ $application->add(new \Foo3Command);
+ $tester = new ApplicationTester($application);
+ $tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions');
+
+ }
+
+ public function testRun()
+ {
+ $application = new Application();
+ $application->setAutoExit(false);
+ $application->setCatchExceptions(false);
+ $application->add($command = new \Foo1Command());
+ $_SERVER['argv'] = array('cli.php', 'foo:bar1');
+
+ ob_start();
+ $application->run();
+ ob_end_clean();
+
+ $this->assertSame('Symfony\Component\Console\Input\ArgvInput', get_class($command->input), '->run() creates an ArgvInput by default if none is given');
+ $this->assertSame('Symfony\Component\Console\Output\ConsoleOutput', get_class($command->output), '->run() creates a ConsoleOutput by default if none is given');
+
+ $application = new Application();
+ $application->setAutoExit(false);
+ $application->setCatchExceptions(false);
+
+ $this->ensureStaticCommandHelp($application);
+ $tester = new ApplicationTester($application);
+
+ $tester->run(array(), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the list command if no argument is passed');
+
+ $tester->run(array('--help' => true), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the help command if --help is passed');
+
+ $tester->run(array('-h' => true), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the help command if -h is passed');
+
+ $tester->run(array('command' => 'list', '--help' => true), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the help if --help is passed');
+
+ $tester->run(array('command' => 'list', '-h' => true), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the help if -h is passed');
+
+ $tester->run(array('--ansi' => true));
+ $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --ansi is passed');
+
+ $tester->run(array('--no-ansi' => true));
+ $this->assertFalse($tester->getOutput()->isDecorated(), '->run() forces color output to be disabled if --no-ansi is passed');
+
+ $tester->run(array('--version' => true), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the program version if --version is passed');
+
+ $tester->run(array('-V' => true), array('decorated' => false));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the program version if -v is passed');
+
+ $tester->run(array('command' => 'list', '--quiet' => true));
+ $this->assertSame('', $tester->getDisplay(), '->run() removes all output if --quiet is passed');
+
+ $tester->run(array('command' => 'list', '-q' => true));
+ $this->assertSame('', $tester->getDisplay(), '->run() removes all output if -q is passed');
+
+ $tester->run(array('command' => 'list', '--verbose' => true));
+ $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose is passed');
+
+ $tester->run(array('command' => 'list', '-v' => true));
+ $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
+
+ $application = new Application();
+ $application->setAutoExit(false);
+ $application->setCatchExceptions(false);
+ $application->add(new \FooCommand());
+ $tester = new ApplicationTester($application);
+
+ $tester->run(array('command' => 'foo:bar', '--no-interaction' => true), array('decorated' => false));
+ $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if --no-interaction is passed');
+
+ $tester->run(array('command' => 'foo:bar', '-n' => true), array('decorated' => false));
+ $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
+ }
+
+ /**
+ * @expectedException \LogicException
+ * @dataProvider getAddingAlreadySetDefinitionElementData
+ */
+ public function testAddingAlreadySetDefinitionElementData($def)
+ {
+ $application = new Application();
+ $application->setAutoExit(false);
+ $application->setCatchExceptions(false);
+ $application
+ ->register('foo')
+ ->setDefinition(array($def))
+ ->setCode(function (InputInterface $input, OutputInterface $output) {})
+ ;
+
+ $input = new ArrayInput(array('command' => 'foo'));
+ $output = new NullOutput();
+ $application->run($input, $output);
+ }
+
+ public function getAddingAlreadySetDefinitionElementData()
+ {
+ return array(
+ array(new InputArgument('command', InputArgument::REQUIRED)),
+ array(new InputOption('quiet', '', InputOption::VALUE_NONE)),
+ array(new InputOption('query', 'q', InputOption::VALUE_NONE)),
+ );
+ }
+}
View
254 Tests/Command/CommandTest.php
@@ -0,0 +1,254 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests\Command;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\FormatterHelper;
+use Symfony\Component\Console\Application;
+use Symfony\Component\Console\Input\InputDefinition;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\StringInput;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Output\NullOutput;
+use Symfony\Component\Console\Tester\CommandTester;
+
+class CommandTest extends \PHPUnit_Framework_TestCase
+{
+ static protected $fixturesPath;
+
+ static public function setUpBeforeClass()
+ {
+ self::$fixturesPath = __DIR__.'/../Fixtures/';
+ require_once self::$fixturesPath.'/TestCommand.php';
+ }
+
+ 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');
+ }
+
+ public function testSetApplication()
+ {
+ $application = new Application();
+ $command = new \TestCommand();
+ $command->setApplication($application);
+ $this->assertEquals($application, $command->getApplication(), '->setApplication() sets the current application');
+ }
+
+ public function testSetGetDefinition()
+ {
+ $command = new \TestCommand();
+ $ret = $command->setDefinition($definition = new InputDefinition());
+ $this->assertEquals($command, $ret, '->setDefinition() implements a fluent interface');
+ $this->assertEquals($definition, $command->getDefinition(), '->setDefinition() sets the current InputDefinition instance');
+ $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar')));
+ $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
+ $this->assertTrue($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
+ $command->setDefinition(new InputDefinition());
+ }
+
+ public function testAddArgument()
+ {
+ $command = new \TestCommand();
+ $ret = $command->addArgument('foo');
+ $this->assertEquals($command, $ret, '->addArgument() implements a fluent interface');
+ $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
+ }
+
+ public function testAddOption()
+ {
+ $command = new \TestCommand();
+ $ret = $command->addOption('foo');
+ $this->assertEquals($command, $ret, '->addOption() implements a fluent interface');
+ $this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
+ }
+
+ public function testGetNamespaceGetNameSetName()
+ {
+ $command = new \TestCommand();
+ $this->assertEquals('namespace:name', $command->getName(), '->getName() returns the command name');
+ $command->setName('foo');
+ $this->assertEquals('foo', $command->getName(), '->setName() sets the command name');
+
+ $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');
+ }
+
+ 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');
+ }
+ }
+
+ public function testGetSetDescription()
+ {
+ $command = new \TestCommand();
+ $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
+ $ret = $command->setDescription('description1');
+ $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
+ $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
+ }
+
+ public function testGetSetHelp()
+ {
+ $command = new \TestCommand();
+ $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
+ $ret = $command->setHelp('help1');
+ $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
+ $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
+ }
+
+ public function testGetProcessedHelp()
+ {
+ $command = new \TestCommand();
+ $command->setHelp('The %command.name% command does... Example: php %command.full_name%.');
+ $this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly');
+ $this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name%');
+ }
+
+ public function testGetSetAliases()
+ {
+ $command = new \TestCommand();
+ $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
+ $ret = $command->setAliases(array('name1'));
+ $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
+ $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
+ }
+
+ public function testGetSynopsis()
+ {
+ $command = new \TestCommand();
+ $command->addOption('foo');
+ $command->addArgument('foo');
+ $this->assertEquals('namespace:name [--foo] [foo]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
+ }
+
+ public function testGetHelper()
+ {
+ $application = new Application();
+ $command = new \TestCommand();
+ $command->setApplication($application);
+ $formatterHelper = new FormatterHelper();
+ $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
+ }
+
+ public function testGet()
+ {
+ $application = new Application();
+ $command = new \TestCommand();
+ $command->setApplication($application);
+ $formatterHelper = new FormatterHelper();
+ $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->__get() returns the correct helper');
+ }
+
+ public function testMergeApplicationDefinition()
+ {
+ $application1 = new Application();
+ $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
+ $application1->getDefinition()->addOptions(array(new InputOption('bar')));
+ $command = new \TestCommand();
+ $command->setApplication($application1);
+ $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
+
+ $r = new \ReflectionObject($command);
+ $m = $r->getMethod('mergeApplicationDefinition');
+ $m->setAccessible(true);
+ $m->invoke($command);
+ $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
+ $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
+ $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
+ $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
+
+ $m->invoke($command);
+ $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
+ }
+
+ public function testRun()
+ {
+ $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');
+ }
+ }
+
+ public function testSetCode()
+ {
+ $command = new \TestCommand();
+ $ret = $command->setCode(function (InputInterface $input, OutputInterface $output)
+ {
+ $output->writeln('from the code...');
+ });
+ $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
+ $tester = new CommandTester($command);
+ $tester->execute(array());
+ $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
+ }
+
+ public function testAsText()
+ {
+ $command = new \TestCommand();
+ $command->setApplication(new Application());
+ $tester = new CommandTester($command);
+ $tester->execute(array('command' => $command->getName()));
+ $this->assertStringEqualsFile(self::$fixturesPath.'/command_astext.txt', $command->asText(), '->asText() returns a text representation of the command');
+ }
+
+ public function testAsXml()
+ {
+ $command = new \TestCommand();
+ $command->setApplication(new Application());
+ $tester = new CommandTester($command);
+ $tester->execute(array('command' => $command->getName()));
+ $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/command_asxml.txt', $command->asXml(), '->asXml() returns an XML representation of the command');
+ }
+}
View
42 Tests/Command/HelpCommandTest.php
@@ -0,0 +1,42 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests\Command;
+
+use Symfony\Component\Console\Tester\CommandTester;
+use Symfony\Component\Console\Command\HelpCommand;
+use Symfony\Component\Console\Command\ListCommand;
+use Symfony\Component\Console\Application;
+
+class HelpCommandTest extends \PHPUnit_Framework_TestCase
+{
+ public function testExecute()
+ {
+ $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');
+
+ $command->setCommand(new ListCommand());
+ $commandTester->execute(array('--xml' => true));
+ $this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
+
+ $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');
+
+ $commandTester->execute(array('command_name' => 'list', '--xml' => true));
+ $this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
+ }
+}
View
38 Tests/Command/ListCommandTest.php
@@ -0,0 +1,38 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests\Command;
+
+use Symfony\Component\Console\Tester\CommandTester;
+use Symfony\Component\Console\Application;
+
+class ListCommandTest extends \PHPUnit_Framework_TestCase
+{
+ public function testExecute()
+ {
+ $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');
+
+ $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');
+
+ $commandTester->execute(array('command' => $command->getName(), '--raw' => true));
+ $output = <<<EOF
+help Displays help for a command
+list Lists commands
+
+EOF;
+ $this->assertEquals(str_replace("\n", PHP_EOL, $output), $commandTester->getDisplay(), 'boo');
+ }
+}
View
26 Tests/Fixtures/Foo1Command.php
@@ -0,0 +1,26 @@
+<?php
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Foo1Command extends Command
+{
+ public $input;
+ public $output;
+
+ protected function configure()
+ {
+ $this
+ ->setName('foo:bar1')
+ ->setDescription('The foo:bar1 command')
+ ->setAliases(array('afoobar1'))
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $this->input = $input;
+ $this->output = $output;
+ }
+}
View
21 Tests/Fixtures/Foo2Command.php
@@ -0,0 +1,21 @@
+<?php
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Foo2Command extends Command
+{
+ protected function configure()
+ {
+ $this
+ ->setName('foo1:bar')
+ ->setDescription('The foo1:bar command')
+ ->setAliases(array('afoobar2'))
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ }
+}
View
25 Tests/Fixtures/Foo3Command.php
@@ -0,0 +1,25 @@
+<?php
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Foo3Command extends Command
+{
+ protected function configure()
+ {
+ $this
+ ->setName('foo3:bar')
+ ->setDescription('The foo3:bar command')
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ try {
+ throw new \Exception("First exception");
+ } catch (\Exception $e) {
+ throw new \Exception("Second exception", 0, $e);
+ }
+ }
+}
View
33 Tests/Fixtures/FooCommand.php
@@ -0,0 +1,33 @@
+<?php
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class FooCommand extends Command
+{
+ public $input;
+ public $output;
+
+ protected function configure()
+ {
+ $this
+ ->setName('foo:bar')
+ ->setDescription('The foo:bar command')
+ ->setAliases(array('afoobar'))
+ ;
+ }
+
+ protected function interact(InputInterface $input, OutputInterface $output)
+ {
+ $output->writeln('interact called');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $this->input = $input;
+ $this->output = $output;
+
+ $output->writeln('called');
+ }
+}
View
28 Tests/Fixtures/TestCommand.php
@@ -0,0 +1,28 @@
+<?php
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class TestCommand extends Command
+{
+ protected function configure()
+ {
+ $this
+ ->setName('namespace:name')
+ ->setAliases(array('name'))
+ ->setDescription('description')
+ ->setHelp('help')
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $output->writeln('execute called');
+ }
+
+ protected function interact(InputInterface $input, OutputInterface $output)
+ {
+ $output->writeln('interact called');
+ }
+}
View
20 Tests/Fixtures/application_astext1.txt
@@ -0,0 +1,20 @@
+<info>Console Tool</info>
+
+<comment>Usage:</comment>
+ [options] command [arguments]
+
+<comment>Options:</comment>
+ <info>--help</info> <info>-h</info> Display this help message.
+ <info>--quiet</info> <info>-q</info> Do not output any message.
+ <info>--verbose</info> <info>-v</info> Increase verbosity of messages.
+ <info>--version</info> <info>-V</info> Display this application version.
+ <info>--ansi</info> Force ANSI output.
+ <info>--no-ansi</info> Disable ANSI output.
+ <info>--no-interaction</info> <info>-n</info> Do not ask any interactive question.
+
+<comment>Available commands:</comment>
+ <info>afoobar </info> The foo:bar command
+ <info>help </info> Displays help for a command
+ <info>list </info> Lists commands
+<comment>foo</comment>
+ <info>foo:bar </info> The foo:bar command
View
16 Tests/Fixtures/application_astext2.txt
@@ -0,0 +1,16 @@
+<info>Console Tool</info>
+
+<comment>Usage:</comment>
+ [options] command [arguments]
+
+<comment>Options:</comment>
+ <info>--help</info> <info>-h</info> Display this help message.
+ <info>--quiet</info> <info>-q</info> Do not output any message.
+ <info>--verbose</info> <info>-v</info> Increase verbosity of messages.
+ <info>--version</info> <info>-V</info> Display this application version.
+ <info>--ansi</info> Force ANSI output.
+ <info>--no-ansi</info> Disable ANSI output.
+ <info>--no-interaction</info> <info>-n</info> Do not ask any interactive question.
+
+<comment>Available commands for the "foo" namespace:</comment>
+ <info>foo:bar </info> The foo:bar command
View
83 Tests/Fixtures/application_asxml1.txt
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<symfony>
+ <commands>
+ <command id="help" name="help">
+ <usage>help [--xml] [command_name]</usage>
+ <description>Displays help for a command</description>
+ <help>The &lt;info&gt;help&lt;/info&gt; command displays help for a given command:
+
+ &lt;info&gt;php app/console help list&lt;/info&gt;
+
+ You can also output the help as XML by using the &lt;comment&gt;--xml&lt;/comment&gt; option:
+
+ &lt;info&gt;php app/console help --xml list&lt;/info&gt;</help>
+ <aliases />
+ <arguments>
+ <argument name="command_name" is_required="0" is_array="0">
+ <description>The command name</description>
+ <defaults>
+ <default>help</default>
+ </defaults>
+ </argument>
+ </arguments>
+ <options>
+ <option name="--xml" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>To output help as XML</description>
+ </option>
+ </options>
+</command>
+ <command id="list" name="list">
+ <usage>list [--xml] [--raw] [namespace]</usage>
+ <description>Lists commands</description>
+ <help>The &lt;info&gt;list&lt;/info&gt; command lists all commands:
+
+ &lt;info&gt;php app/console list&lt;/info&gt;
+
+ You can also display the commands for a specific namespace:
+
+ &lt;info&gt;php app/console list test&lt;/info&gt;
+
+ You can also output the information as XML by using the &lt;comment&gt;--xml&lt;/comment&gt; option:
+
+ &lt;info&gt;php app/console list --xml&lt;/info&gt;
+
+ It's also possible to get raw list of commands (useful for embedding command runner):
+
+ &lt;info&gt;php app/console list --raw&lt;/info&gt;</help>
+ <aliases/>
+ <arguments>
+ <argument name="namespace" is_required="0" is_array="0">
+ <description>The namespace name</description>
+ <defaults/>
+ </argument>
+ </arguments>
+ <options>
+ <option name="--xml" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>To output help as XML</description>
+ </option>
+ <option name="--raw" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>To output raw command list</description>
+ </option>
+ </options>
+</command>
+ <command id="foo:bar" name="foo:bar">
+ <usage>foo:bar</usage>
+ <description>The foo:bar command</description>
+ <help/>
+ <aliases>
+ <alias>afoobar</alias>
+ </aliases>
+ <arguments/>
+ <options/>
+</command>
+ </commands>
+ <namespaces>
+ <namespace id="_global">
+ <command>help</command>
+ <command>list</command>
+ </namespace>
+ <namespace id="foo">
+ <command>foo:bar</command>
+ </namespace>
+ </namespaces>
+</symfony>
View
15 Tests/Fixtures/application_asxml2.txt
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<symfony>
+ <commands namespace="foo">
+ <command id="foo:bar" name="foo:bar">
+ <usage>foo:bar</usage>
+ <description>The foo:bar command</description>
+ <help/>
+ <aliases>
+ <alias>afoobar</alias>
+ </aliases>
+ <arguments/>
+ <options/>
+</command>
+ </commands>
+</symfony>
View
13 Tests/Fixtures/application_gethelp.txt
@@ -0,0 +1,13 @@
+<info>Console Tool</info>
+
+<comment>Usage:</comment>
+ [options] command [arguments]
+
+<comment>Options:</comment>
+ <info>--help</info> <info>-h</info> Display this help message.
+ <info>--quiet</info> <info>-q</info> Do not output any message.
+ <info>--verbose</info> <info>-v</info> Increase verbosity of messages.
+ <info>--version</info> <info>-V</info> Display this application version.
+ <info>--ansi</info> Force ANSI output.
+ <info>--no-ansi</info> Disable ANSI output.
+ <info>--no-interaction</info> <info>-n</info> Do not ask any interactive question.
View
8 Tests/Fixtures/application_renderexception1.txt
@@ -0,0 +1,8 @@
+
+
+
+ [InvalidArgumentException]
+ Command "foo" is not defined.
+
+
+
View
11 Tests/Fixtures/application_renderexception2.txt
@@ -0,0 +1,11 @@
+
+
+
+ [InvalidArgumentException]
+ The "--foo" option does not exist.
+
+
+
+list [--xml] [--raw] [namespace]
+
+
View
19 Tests/Fixtures/application_renderexception3.txt
@@ -0,0 +1,19 @@
+
+
+
+ [Exception]
+ Second exception
+
+
+
+
+
+
+ [Exception]
+ First exception
+
+
+
+foo3:bar
+
+
View
17 Tests/Fixtures/application_run1.txt
@@ -0,0 +1,17 @@
+Console Tool
+
+Usage:
+ [options] command [arguments]
+
+Options:
+ --help -h Display this help message.
+ --quiet -q Do not output any message.
+ --verbose -v Increase verbosity of messages.
+ --version -V Display this application version.
+ --ansi Force ANSI output.
+ --no-ansi Disable ANSI output.
+ --no-interaction -n Do not ask any interactive question.
+
+Available commands:
+ help Displays help for a command
+ list Lists commands
View
26 Tests/Fixtures/application_run2.txt
@@ -0,0 +1,26 @@
+Usage:
+ help [--xml] [command_name]
+
+Arguments:
+ command The command to execute
+ command_name The command name (default: 'help')
+
+Options:
+ --xml To output help as XML
+ --help (-h) Display this help message.
+ --quiet (-q) Do not output any message.
+ --verbose (-v) Increase verbosity of messages.
+ --version (-V) Display this application version.
+ --ansi Force ANSI output.
+ --no-ansi Disable ANSI output.
+ --no-interaction (-n) Do not ask any interactive question.
+
+Help:
+ The help command displays help for a given command:
+
+ php app/console help list
+
+ You can also output the help as XML by using the --xml option:
+
+ php app/console help --xml list
+
View
27 Tests/Fixtures/application_run3.txt
@@ -0,0 +1,27 @@
+Usage:
+ list [--xml] [--raw] [namespace]
+
+Arguments:
+ namespace The namespace name
+
+Options:
+ --xml To output help as XML
+ --raw To output raw command list
+
+Help:
+ The list command lists all commands:
+
+ php app/console list
+
+ You can also display the commands for a specific namespace:
+
+ php app/console list test
+
+ You can also output the information as XML by using the --xml option:
+
+ php app/console list --xml
+
+ It's also possible to get raw list of commands (useful for embedding command runner):
+
+ php app/console list --raw
+
View
1  Tests/Fixtures/application_run4.txt
@@ -0,0 +1 @@
+Console Tool
View
18 Tests/Fixtures/command_astext.txt
@@ -0,0 +1,18 @@
+<comment>Usage:</comment>
+ namespace:name
+
+<comment>Aliases:</comment> <info>name</info>
+<comment>Arguments:</comment>
+ <info>command </info> The command to execute
+
+<comment>Options:</comment>
+ <info>--help</info> (-h) Display this help message.
+ <info>--quiet</info> (-q) Do not output any message.
+ <info>--verbose</info> (-v) Increase verbosity of messages.
+ <info>--version</info> (-V) Display this application version.
+ <info>--ansi</info> Force ANSI output.
+ <info>--no-ansi</info> Disable ANSI output.
+ <info>--no-interaction</info> (-n) Do not ask any interactive question.
+
+<comment>Help:</comment>
+ help
View
38 Tests/Fixtures/command_asxml.txt
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<command id="namespace:name" name="namespace:name">
+ <usage>namespace:name</usage>
+ <description>description</description>
+ <help>help</help>
+ <aliases>
+ <alias>name</alias>
+ </aliases>
+ <arguments>
+ <argument name="command" is_required="1" is_array="0">
+ <description>The command to execute</description>
+ <defaults/>
+ </argument>
+ </arguments>
+ <options>
+ <option name="--help" shortcut="-h" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>Display this help message.</description>
+ </option>
+ <option name="--quiet" shortcut="-q" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>Do not output any message.</description>
+ </option>
+ <option name="--verbose" shortcut="-v" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>Increase verbosity of messages.</description>
+ </option>
+ <option name="--version" shortcut="-V" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>Display this application version.</description>
+ </option>
+ <option name="--ansi" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>Force ANSI output.</description>
+ </option>
+ <option name="--no-ansi" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>Disable ANSI output.</description>
+ </option>
+ <option name="--no-interaction" shortcut="-n" accept_value="0" is_value_required="0" is_multiple="0">
+ <description>Do not ask any interactive question.</description>
+ </option>
+ </options>
+</command>
View
11 Tests/Fixtures/definition_astext.txt
@@ -0,0 +1,11 @@
+<comment>Arguments:</comment>
+ <info>foo </info> The foo argument
+ <info>baz </info> The baz argument<comment> (default: true)</comment>
+ <info>bar </info> The bar argument<comment> (default: array('bar'))</comment>
+
+<comment>Options:</comment>
+ <info>--foo</info> (-f) The foo option
+ <info>--baz</info> The baz option<comment> (default: false)</comment>
+ <info>--bar</info> (-b) The bar option<comment> (default: 'bar')</comment>
+ <info>--qux</info> The qux option<comment> (default: array('foo', 'bar'))</comment><comment> (multiple values allowed)</comment>
+ <info>--qux2</info> The qux2 option<comment> (default: array ( 'foo' => 'bar',))</comment><comment> (multiple values allowed)</comment>
View
39 Tests/Fixtures/definition_asxml.txt
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<definition>
+ <arguments>
+ <argument name="foo" is_required="0" is_array="0">
+ <description>The foo argument</description>
+ <defaults/>
+ </argument>
+ <argument name="baz" is_required="0" is_array="0">
+ <description>The baz argument</description>
+ <defaults>
+ <default>true</default>
+ </defaults>
+ </argument>
+ <argument name="bar" is_required="0" is_array="1">
+ <description>The bar argument</description>
+ <defaults>
+ <default>bar</default>
+ </defaults>
+ </argument>
+ </arguments>
+ <options>
+ <option name="--foo" shortcut="-f" accept_value="1" is_value_required="1" is_multiple="0">
+ <description>The foo option</description>
+ <defaults/>
+ </option>
+ <option name="--baz" shortcut="" accept_value="1" is_value_required="0" is_multiple="0">
+ <description>The baz option</description>
+ <defaults>
+ <default>false</default>
+ </defaults>
+ </option>
+ <option name="--bar" shortcut="-b" accept_value="1" is_value_required="0" is_multiple="0">
+ <description>The bar option</description>
+ <defaults>
+ <default>bar</default>
+ </defaults>
+ </option>
+ </options>
+</definition>
View
77 Tests/Formatter/OutputFormatterStyleTest.php
@@ -0,0 +1,77 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests\Formatter;
+
+use Symfony\Component\Console\Formatter\OutputFormatterStyle;
+
+class OutputFormatterStyleTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstructor()
+ {
+ $style = new OutputFormatterStyle('green', 'black', array('bold', 'underscore'));
+ $this->assertEquals("\033[32;40;1;4mfoo\033[0m", $style->apply('foo'));
+
+ $style = new OutputFormatterStyle('red', null, array('blink'));
+ $this->assertEquals("\033[31;5mfoo\033[0m", $style->apply('foo'));
+
+ $style = new OutputFormatterStyle(null, 'white');
+ $this->assertEquals("\033[47mfoo\033[0m", $style->apply('foo'));
+ }
+
+ public function testForeground()
+ {
+ $style = new OutputFormatterStyle();
+
+ $style->setForeground('black');
+ $this->assertEquals("\033[30mfoo\033[0m", $style->apply('foo'));
+
+ $style->setForeground('blue');
+ $this->assertEquals("\033[34mfoo\033[0m", $style->apply('foo'));
+
+ $this->setExpectedException('InvalidArgumentException');
+ $style->setForeground('undefined-color');
+ }
+
+ public function testBackground()
+ {
+ $style = new OutputFormatterStyle();
+
+ $style->setBackground('black');
+ $this->assertEquals("\033[40mfoo\033[0m", $style->apply('foo'));
+
+ $style->setBackground('yellow');
+ $this->assertEquals("\033[43mfoo\033[0m", $style->apply('foo'));
+
+ $this->setExpectedException('InvalidArgumentException');
+ $style->setBackground('undefined-color');
+ }
+
+ public function testOptions()
+ {
+ $style = new OutputFormatterStyle();
+
+ $style->setOptions(array('reverse', 'conceal'));
+ $this->assertEquals("\033[7;8mfoo\033[0m", $style->apply('foo'));
+
+ $style->setOption('bold');
+ $this->assertEquals("\033[7;8;1mfoo\033[0m", $style->apply('foo'));
+
+ $style->unsetOption('reverse');
+ $this->assertEquals("\033[8;1mfoo\033[0m", $style->apply('foo'));
+
+ $style->setOption('bold');
+ $this->assertEquals("\033[8;1mfoo\033[0m", $style->apply('foo'));
+
+ $style->setOptions(array('bold'));
+ $this->assertEquals("\033[1mfoo\033[0m", $style->apply('foo'));
+ }
+}
View
182 Tests/Formatter/OutputFormatterTest.php
@@ -0,0 +1,182 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests\Formatter;
+
+use Symfony\Component\Console\Formatter\OutputFormatter;
+
+class FormatterStyleTest extends \PHPUnit_Framework_TestCase
+{
+ public function testBundledStyles()
+ {
+ $formatter = new OutputFormatter(true);
+
+ $this->assertTrue($formatter->hasStyle('error'));
+ $this->assertTrue($formatter->hasStyle('info'));
+ $this->assertTrue($formatter->hasStyle('comment'));
+ $this->assertTrue($formatter->hasStyle('question'));
+
+ $this->assertEquals(
+ "\033[37;41msome error\033[0m", $formatter->format('<error>some error</error>')
+ );
+ $this->assertEquals(
+ "\033[32msome info\033[0m", $formatter->format('<info>some info</info>')
+ );
+ $this->assertEquals(
+ "\033[33msome comment\033[0m", $formatter->format('<comment>some comment</comment>')
+ );
+ $this->assertEquals(
+ "\033[30;46msome question\033[0m", $formatter->format('<question>some question</question>')
+ );
+ }
+
+ public function testNestedStyles()
+ {
+ $formatter = new OutputFormatter(true);
+
+ $this->assertEquals(
+ "\033[37;41msome \033[32msome info\033[0m error\033[0m", $formatter->format('<error>some <info>some info</info> error</error>')
+ );
+ }
+
+ public function testNewStyle()
+ {
+ $formatter = new OutputFormatter(true);
+
+ $style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')->getMock();
+ $formatter->setStyle('test', $style);
+
+ $this->assertEquals($style, $formatter->getStyle('test'));
+ $this->assertNotEquals($style, $formatter->getStyle('info'));
+
+ $style
+ ->expects($this->once())
+ ->method('apply')
+ ->will($this->returnValue('[STYLE_BEG]some custom msg[STYLE_END]'));
+
+ $this->assertEquals("[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<test>some custom msg</test>'));
+ }
+
+ public function testRedefineStyle()
+ {
+ $formatter = new OutputFormatter(true);
+
+ $style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')
+ ->getMock();
+ $formatter->setStyle('info', $style);
+
+ $style
+ ->expects($this->once())
+ ->method('apply')
+ ->will($this->returnValue('[STYLE_BEG]some custom msg[STYLE_END]'));
+
+ $this->assertEquals(
+ "[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<info>some custom msg</info>')
+ );
+ }
+
+ public function testInlineStyle()
+ {
+ $formatter = new OutputFormatter(true);
+
+ $this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</>'));
+ $this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
+ }
+
+ public function testNotDecoratedFormatter()
+ {
+ $formatter = new OutputFormatter(false);
+
+ $this->assertTrue($formatter->hasStyle('error'));
+ $this->assertTrue($formatter->hasStyle('info'));
+ $this->assertTrue($formatter->hasStyle('comment'));
+ $this->assertTrue($formatter->hasStyle('question'));
+
+ $this->assertEquals(
+ "some error", $formatter->format('<error>some error</error>')
+ );
+ $this->assertEquals(
+ "some info", $formatter->format('<info>some info</info>')
+ );
+ $this->assertEquals(
+ "some comment", $formatter->format('<comment>some comment</comment>')
+ );
+ $this->assertEquals(
+ "some question", $formatter->format('<question>some question</question>')
+ );
+
+ $formatter->setDecorated(true);
+
+ $this->assertEquals(
+ "\033[37;41msome error\033[0m", $formatter->format('<error>some error</error>')
+ );
+ $this->assertEquals(
+ "\033[32msome info\033[0m", $formatter->format('<info>some info</info>')
+ );
+ $this->assertEquals(
+ "\033[33msome comment\033[0m", $formatter->format('<comment>some comment</comment>')
+ );
+ $this->assertEquals(
+ "\033[30;46msome question\033[0m", $formatter->format('<question>some question</question>')
+ );
+ }
+
+ public function testContentWithLineBreaks()
+ {
+ $formatter = new OutputFormatter(true);
+
+ $this->assertEquals(<<<EOF
+\033[32m
+some text\033[0m
+EOF
+ , $formatter->format(<<<EOF
+<info>
+some text</info>
+EOF
+ ));
+
+ $this->assertEquals(<<<EOF
+\033[32msome text
+\033[0m
+EOF
+ , $formatter->format(<<<EOF
+<info>some text
+</info>
+EOF
+ ));
+
+ $this->assertEquals(<<<EOF
+\033[32m
+some text
+\033[0m
+EOF
+ , $formatter->format(<<<EOF
+<info>
+some text
+</info>
+EOF
+ ));
+
+ $this->assertEquals(<<<EOF
+\033[32m
+some text
+more text
+\033[0m
+EOF
+ , $formatter->format(<<<EOF
+<info>
+some text
+more text
+</info>
+EOF
+ ));
+ }
+}
View
93 Tests/Helper/DialogHelperTest.php
@@ -0,0 +1,93 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests\Helper;
+
+use Symfony\Component\Console\Helper\DialogHelper;
+use Symfony\Component\Console\Helper\HelperSet;
+use Symfony\Component\Console\Helper\FormatterHelper;
+use Symfony\Component\Console\Output\StreamOutput;
+
+class DialogHelperTest extends \PHPUnit_Framework_TestCase
+{
+ public function testAsk()
+ {
+ $dialog = new DialogHelper();
+
+ $dialog->setInputStream($this->getInputStream("\n8AM\n"));
+
+ $this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM'));
+ $this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM'));
+
+ rewind($output->getStream());
+ $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
+ }
+
+ public function testAskConfirmation()
+ {
+ $dialog = new DialogHelper();
+
+ $dialog->setInputStream($this->getInputStream("\n\n"));
+ $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?'));
+ $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
+
+ $dialog->setInputStream($this->getInputStream("y\nyes\n"));
+ $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
+ $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
+
+ $dialog->setInputStream($this->getInputStream("n\nno\n"));
+ $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
+ $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
+ }
+
+ public function testAskAndValidate()
+ {
+ $dialog = new DialogHelper();
+ $helperSet = new HelperSet(array(new FormatterHelper()));
+ $dialog->setHelperSet($helperSet);
+
+ $question ='What color was the white horse of Henry IV?';
+ $error = 'This is not a color!';
+ $validator = function ($color) use ($error) {
+ if (!in_array($color, array('white', 'black'))) {
+ throw new \InvalidArgumentException($error);
+ }
+
+ return $color;
+ };
+
+ $dialog->setInputStream($this->getInputStream("\nblack\n"));
+ $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
+ $this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
+
+ $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
+ try {
+ $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
+ $this->fail();
+ } catch (\InvalidArgumentException $e) {
+ $this->assertEquals($error, $e->getMessage());
+ }
+ }
+
+ protected function getInputStream($input)
+ {
+ $stream = fopen('php://memory', 'r+', false);
+ fputs($stream, $input);
+ rewind($stream);
+
+ return $stream;
+ }
+
+ protected function getOutputStream()
+ {
+ return new StreamOutput(fopen('php://memory', 'r+', false));
+ }
+}
View
71 Tests/Helper/FormatterHelperTest.php
@@ -0,0 +1,71 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests\Helper;
+
+use Symfony\Component\Console\Helper\FormatterHelper;
+
+class FormatterHelperTest extends \PHPUnit_Framework_TestCase
+{
+ public function testFormatSection()
+ {
+ $formatter = new FormatterHelper();
+
+ $this->assertEquals(
+ '<info>[cli]</info> Some text to display',
+ $formatter->formatSection('cli', 'Some text to display'),
+ '::formatSection() formats a message in a section'
+ );
+ }
+
+ public function testFormatBlock()
+ {
+ $formatter = new FormatterHelper();
+
+ $this->assertEquals(
+ '<error> Some text to display </error>',
+ $formatter->formatBlock('Some text to display', 'error'),
+ '::formatBlock() formats a message in a block'
+ );
+
+ $this->assertEquals(
+ '<error> Some text to display </error>' . "\n" .
+ '<error> foo bar </error>',
+ $formatter->formatBlock(array('Some text to display', 'foo bar'), 'error'),
+ '::formatBlock() formats a message in a block'
+ );
+
+ $this->assertEquals(
+ '<error> </error>' . "\n" .
+ '<error> Some text to display </error>' . "\n" .
+ '<error> </error>',
+ $formatter->formatBlock('Some text to display', 'error', true),
+ '::formatBlock() formats a message in a block'
+ );
+ }
+
+ public function testFormatBlockWithDiacriticLetters()
+ {
+ if (!extension_loaded('mbstring')) {
+ $this->markTestSkipped('This test requires mbstring to work.');
+ }
+
+ $formatter = new FormatterHelper();
+
+ $this->assertEquals(
+ '<error> </error>' . "\n" .
+ '<error> Du texte à afficher </error>' . "\n" .
+ '<error> </error>',
+ $formatter->formatBlock('Du texte à afficher', 'error', true),
+ '::formatBlock() formats a message in a block'
+ );
+ }
+}
View
213 Tests/Input/ArgvInputTest.php
@@ -0,0 +1,213 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Console\Tests\Input;
+
+use Symfony\Component\Console\Input\ArgvInput;
+use Symfony\Component\Console\Input\InputDefinition;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+
+class ArgvInputTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstructor()
+ {
+ $_SERVER['argv'] = array('cli.php', 'foo');
+ $input = new ArgvInput();
+ $r = new \ReflectionObject($input);
+ $p = $r->getProperty('tokens');
+ $p->setAccessible(true);
+
+ $this->assertEquals(array('foo'), $p->getValue($input), '__construct() automatically get its input from the argv server variable');
+ }
+
+ public function testParser()
+ {
+ $input = new ArgvInput(array('cli.php', 'foo'));
+ $input->bind(new InputDefinition(array(new InputArgument('name'))));
+ $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
+
+ $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');
+
+ $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)');
+
+ $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)');
+
+ 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');
+ }
+
+ $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(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)');
+
+ $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)');
+
+ $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');
+ }
+
+ $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');
+ }
+
+ $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');
+
+ $input = new ArgvInput(array('cli.php', '-f', 'bar', '--', '-1'));
+ $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 testGetFirstArgument()
+ {
+ $input = new ArgvInput(array('c