Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Cleaned up the unit tests. #6989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 76 additions & 68 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,6 @@ public function testHasGet()
$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
Expand All @@ -145,7 +137,17 @@ public function testHasGet()
$p->setAccessible(true);
$p->setValue($application, true);
$command = $application->get('foo:bar');
$this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($command), '->get() returns the help command if --help is provided as the input');
$this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $command, '->get() returns the help command if --help is provided as the input');
}

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

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

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

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

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

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

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

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

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

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

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 provideAmbiguousAbbreviations()
{
return array(
array('f', 'Command "f" is not defined.'),
array('a', 'Command "a" is ambiguous (afoobar, afoobar1 and 1 more).'),
array('foo:b', 'Command "foo:b" is ambiguous (foo:bar, foo:bar1).')
);
}

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

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

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

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

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

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

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

public function testSetApplication()
{
$application = new Application();
Expand Down Expand Up @@ -92,22 +94,25 @@ public function testGetNamespaceGetNameSetName()
$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');
}

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

$command = new \TestCommand();
$command->setName($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 provideInvalidCommandNames()
{
return array(
array(''),
array('foo:')
);
}

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');
}

public function testRun()
public function testRunInteractive()
{
$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 = new CommandTester(new \TestCommand());

$tester->execute(array(), array('interactive' => true));

$this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
}

public function testRunNonInteractive()
{
$tester = new CommandTester(new \TestCommand());

$tester->execute(array(), array('interactive' => false));

$this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage You must override the execute() method in the concrete command class.
*/
public function testExecuteMethodNeedsToBeOverriden()
{
$command = new Command('foo');
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');
}
$command->run(new StringInput(''), new NullOutput());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "--bar" option does not exist.
*/
public function testRunWithInvalidOption()
{
$command = new \TestCommand();
$tester = new CommandTester($command);
$tester->execute(array('--bar' => true));
}

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.
*/
public function testSetCodeWithNonCallable()
Expand Down
28 changes: 23 additions & 5 deletions src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,51 @@

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

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

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

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

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

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

public function testExecuteForCommandWithXmlOption()
{
$command = new HelpCommand();
$commandTester = new CommandTester($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');
}

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

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

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

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