Skip to content

Commit

Permalink
merged branch jakzal/console-tests-cleanup (PR #6989)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

[Console] Cleaned up the unit tests.

Cleaned up some unit tests in the Console component as suggested in #6935. I didn't fully cleanup the Application tests to not to delay this PR. I might do it later as a separate one.

| Q              | A                                        |
|--------------|--------------------------------|
| Bug fix?          | no |
|New feature? | no |
|BC breaks?    | no |
|Deprecations? |	no |
|Tests pass? | yes |
|Fixed tickets | #6935 |
|License | MIT |
|Doc PR | n/a |

Commits
-------

5ca04b0 [Console] Cleaned up the unit tests.
  • Loading branch information
fabpot committed Apr 9, 2013
2 parents 17e065f + 5ca04b0 commit 6f0a5ad
Show file tree
Hide file tree
Showing 12 changed files with 715 additions and 486 deletions.
144 changes: 76 additions & 68 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -135,14 +135,6 @@ public function testHasGet()
$this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name'); $this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
$this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias'); $this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');


try {
$application->get('foofoo');
$this->fail('->get() throws an \InvalidArgumentException if the command does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the command does not exist');
$this->assertEquals('The command "foofoo" does not exist.', $e->getMessage(), '->get() throws an \InvalidArgumentException if the command does not exist');
}

$application = new Application(); $application = new Application();
$application->add($foo = new \FooCommand()); $application->add($foo = new \FooCommand());
// simulate --help // simulate --help
Expand All @@ -151,7 +143,17 @@ public function testHasGet()
$p->setAccessible(true); $p->setAccessible(true);
$p->setValue($application, true); $p->setValue($application, true);
$command = $application->get('foo:bar'); $command = $application->get('foo:bar');
$this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($command), '->get() returns the help command if --help is provided as the input'); $this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $command, '->get() returns the help command if --help is provided as the input');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The command "foofoo" does not exist.
*/
public function testGetInvalidCommand()
{
$application = new Application();
$application->get('foofoo');
} }


public function testGetNamespaces() public function testGetNamespaces()
Expand All @@ -170,84 +172,90 @@ public function testFindNamespace()
$this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation'); $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
$application->add(new \Foo2Command()); $application->add(new \Foo2Command());
$this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists'); $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
try { }
$application->findNamespace('f');
$this->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
$this->assertEquals('The namespace "f" is ambiguous (foo, foo1).', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
}


try { /**
$application->findNamespace('bar'); * @expectedException \InvalidArgumentException
$this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); * @expectedExceptionMessage The namespace "f" is ambiguous (foo, foo1).
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); public function testFindAmbiguousNamespace()
$this->assertEquals('There are no commands defined in the "bar" namespace.', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); {
} $application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo2Command());
$application->findNamespace('f');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage There are no commands defined in the "bar" namespace.
*/
public function testFindInvalidNamespace()
{
$application = new Application();
$application->findNamespace('bar');
} }


public function testFind() public function testFind()
{ {
$application = new Application(); $application = new Application();
$application->add(new \FooCommand()); $application->add(new \FooCommand());
$this->assertEquals('FooCommand', get_class($application->find('foo:bar')), '->find() returns a command if its name exists');
$this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($application->find('h')), '->find() returns a command if its name exists');
$this->assertEquals('FooCommand', get_class($application->find('f:bar')), '->find() returns a command if the abbreviation for the namespace exists');
$this->assertEquals('FooCommand', get_class($application->find('f:b')), '->find() returns a command if the abbreviation for the namespace and the command name exist');
$this->assertEquals('FooCommand', get_class($application->find('a')), '->find() returns a command if the abbreviation exists for an alias');


$this->assertInstanceOf('FooCommand', $application->find('foo:bar'), '->find() returns a command if its name exists');
$this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $application->find('h'), '->find() returns a command if its name exists');
$this->assertInstanceOf('FooCommand', $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
$this->assertInstanceOf('FooCommand', $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
$this->assertInstanceOf('FooCommand', $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
}

/**
* @dataProvider provideAmbiguousAbbreviations
*/
public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExceptionMessage)
{
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);

$application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo1Command()); $application->add(new \Foo1Command());
$application->add(new \Foo2Command()); $application->add(new \Foo2Command());


try { $application->find($abbreviation);
$application->find('f'); }
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
$this->assertRegExp('/Command "f" is not defined./', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
}

try {
$application->find('a');
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
$this->assertEquals('Command "a" is ambiguous (afoobar, afoobar1 and 1 more).', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
}


try { public function provideAmbiguousAbbreviations()
$application->find('foo:b'); {
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command'); return array(
} catch (\Exception $e) { array('f', 'Command "f" is not defined.'),
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command'); array('a', 'Command "a" is ambiguous (afoobar, afoobar1 and 1 more).'),
$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'); array('foo:b', 'Command "foo:b" is ambiguous (foo:bar, foo:bar1).')
} );
} }


public function testFindAlternativeExceptionMessage() /**
* @dataProvider provideInvalidCommandNamesSingle
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Did you mean this
*/
public function testFindAlternativeExceptionMessageSingle($name)
{ {
$application = new Application(); $application = new Application();
$application->add(new \FooCommand()); $application->add(new \FooCommand());
$application->find($name);
}


// Command + singular public function provideInvalidCommandNamesSingle()
try { {
$application->find('foo:baR'); return array(
$this->fail('->find() throws an \InvalidArgumentException if command does not exist, with one alternative'); array('foo:baR'),
} catch (\Exception $e) { array('foO:bar')
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with one alternative'); );
$this->assertRegExp('/Did you mean this/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with one alternative'); }
}

// Namespace + singular
try {
$application->find('foO:bar');
$this->fail('->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
$this->assertRegExp('/Did you mean this/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
}


public function testFindAlternativeExceptionMessageMultiple()
{
$application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo1Command()); $application->add(new \Foo1Command());
$application->add(new \Foo2Command()); $application->add(new \Foo2Command());


Expand Down
96 changes: 56 additions & 40 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Expand Up @@ -35,17 +35,19 @@ public static function setUpBeforeClass()


public function testConstructor() public function testConstructor()
{ {
try {
$command = new Command();
$this->fail('__construct() throws a \LogicException if the name is null');
} catch (\Exception $e) {
$this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the name is null');
$this->assertEquals('The command name cannot be empty.', $e->getMessage(), '__construct() throws a \LogicException if the name is null');
}
$command = new Command('foo:bar'); $command = new Command('foo:bar');
$this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument'); $this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
} }


/**
* @expectedException \LogicException
* @expectedExceptionMessage The command name cannot be empty.
*/
public function testCommandNameCannotBeEmpty()
{
new Command();
}

public function testSetApplication() public function testSetApplication()
{ {
$application = new Application(); $application = new Application();
Expand Down Expand Up @@ -92,22 +94,25 @@ public function testGetNamespaceGetNameSetName()
$ret = $command->setName('foobar:bar'); $ret = $command->setName('foobar:bar');
$this->assertEquals($command, $ret, '->setName() implements a fluent interface'); $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
$this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name'); $this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
}

/**
* @dataProvider provideInvalidCommandNames
*/
public function testInvalidCommandNames($name)
{
$this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));

$command = new \TestCommand();
$command->setName($name);
}


try { public function provideInvalidCommandNames()
$command->setName(''); {
$this->fail('->setName() throws an \InvalidArgumentException if the name is empty'); return array(
} catch (\Exception $e) { array(''),
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty'); array('foo:')
$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() public function testGetSetDescription()
Expand Down Expand Up @@ -193,32 +198,43 @@ public function testMergeApplicationDefinition()
$this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options'); $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
} }


public function testRun() public function testRunInteractive()
{ {
$command = new \TestCommand(); $tester = new CommandTester(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)); $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'); $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)); $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'); $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 = new Command('foo');
try { $command->run(new StringInput(''), new NullOutput());
$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'); * @expectedException \InvalidArgumentException
$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'); * @expectedExceptionMessage The "--bar" option does not exist.
} */
public function testRunWithInvalidOption()
{
$command = new \TestCommand();
$tester = new CommandTester($command);
$tester->execute(array('--bar' => true));
} }


public function testRunReturnsAlwaysInteger() public function testRunReturnsAlwaysInteger()
Expand Down Expand Up @@ -251,7 +267,7 @@ public function testSetCodeWithNonClosureCallable()
} }


/** /**
* @expectedException InvalidArgumentException * @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid callable provided to Command::setCode. * @expectedExceptionMessage Invalid callable provided to Command::setCode.
*/ */
public function testSetCodeWithNonCallable() public function testSetCodeWithNonCallable()
Expand Down
28 changes: 23 additions & 5 deletions src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php
Expand Up @@ -18,33 +18,51 @@


class HelpCommandTest extends \PHPUnit_Framework_TestCase class HelpCommandTest extends \PHPUnit_Framework_TestCase
{ {
public function testExecute() public function testExecuteForCommandAlias()
{ {
$command = new HelpCommand(); $command = new HelpCommand();

$command->setApplication(new Application());
$application = new Application();
$command->setApplication($application);
$commandTester = new CommandTester($command); $commandTester = new CommandTester($command);
$commandTester->execute(array('command_name' => 'li')); $commandTester->execute(array('command_name' => 'li'));

$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias'); $this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
}


public function testExecuteForCommand()
{
$command = new HelpCommand(); $command = new HelpCommand();

$commandTester = new CommandTester($command); $commandTester = new CommandTester($command);
$command->setCommand(new ListCommand()); $command->setCommand(new ListCommand());
$commandTester->execute(array()); $commandTester->execute(array());

$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command'); $this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
}


public function testExecuteForCommandWithXmlOption()
{
$command = new HelpCommand();
$commandTester = new CommandTester($command);
$command->setCommand(new ListCommand()); $command->setCommand(new ListCommand());
$commandTester->execute(array('--xml' => true)); $commandTester->execute(array('--xml' => true));

$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed'); $this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
}


public function testExecuteForApplicationCommand()
{
$application = new Application(); $application = new Application();
$commandTester = new CommandTester($application->get('help')); $commandTester = new CommandTester($application->get('help'));
$commandTester->execute(array('command_name' => 'list')); $commandTester->execute(array('command_name' => 'list'));

$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command'); $this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
}


public function testExecuteForApplicationCommandWithXmlOption()
{
$application = new Application();
$commandTester = new CommandTester($application->get('help'));
$commandTester->execute(array('command_name' => 'list', '--xml' => true)); $commandTester->execute(array('command_name' => 'list', '--xml' => true));

$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed'); $this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
} }
} }

0 comments on commit 6f0a5ad

Please sign in to comment.