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

Skip to content

Commit 5ca04b0

Browse files
committed
[Console] Cleaned up the unit tests.
1 parent eb2bcc5 commit 5ca04b0

12 files changed

+715
-486
lines changed

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,6 @@ public function testHasGet()
129129
$this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
130130
$this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
131131

132-
try {
133-
$application->get('foofoo');
134-
$this->fail('->get() throws an \InvalidArgumentException if the command does not exist');
135-
} catch (\Exception $e) {
136-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the command does not exist');
137-
$this->assertEquals('The command "foofoo" does not exist.', $e->getMessage(), '->get() throws an \InvalidArgumentException if the command does not exist');
138-
}
139-
140132
$application = new Application();
141133
$application->add($foo = new \FooCommand());
142134
// simulate --help
@@ -145,7 +137,17 @@ public function testHasGet()
145137
$p->setAccessible(true);
146138
$p->setValue($application, true);
147139
$command = $application->get('foo:bar');
148-
$this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($command), '->get() returns the help command if --help is provided as the input');
140+
$this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $command, '->get() returns the help command if --help is provided as the input');
141+
}
142+
143+
/**
144+
* @expectedException \InvalidArgumentException
145+
* @expectedExceptionMessage The command "foofoo" does not exist.
146+
*/
147+
public function testGetInvalidCommand()
148+
{
149+
$application = new Application();
150+
$application->get('foofoo');
149151
}
150152

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

175-
try {
176-
$application->findNamespace('bar');
177-
$this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
178-
} catch (\Exception $e) {
179-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
180-
$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');
181-
}
171+
/**
172+
* @expectedException \InvalidArgumentException
173+
* @expectedExceptionMessage The namespace "f" is ambiguous (foo, foo1).
174+
*/
175+
public function testFindAmbiguousNamespace()
176+
{
177+
$application = new Application();
178+
$application->add(new \FooCommand());
179+
$application->add(new \Foo2Command());
180+
$application->findNamespace('f');
181+
}
182+
183+
/**
184+
* @expectedException \InvalidArgumentException
185+
* @expectedExceptionMessage There are no commands defined in the "bar" namespace.
186+
*/
187+
public function testFindInvalidNamespace()
188+
{
189+
$application = new Application();
190+
$application->findNamespace('bar');
182191
}
183192

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

198+
$this->assertInstanceOf('FooCommand', $application->find('foo:bar'), '->find() returns a command if its name exists');
199+
$this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $application->find('h'), '->find() returns a command if its name exists');
200+
$this->assertInstanceOf('FooCommand', $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
201+
$this->assertInstanceOf('FooCommand', $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
202+
$this->assertInstanceOf('FooCommand', $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
203+
}
204+
205+
/**
206+
* @dataProvider provideAmbiguousAbbreviations
207+
*/
208+
public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExceptionMessage)
209+
{
210+
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
211+
212+
$application = new Application();
213+
$application->add(new \FooCommand());
194214
$application->add(new \Foo1Command());
195215
$application->add(new \Foo2Command());
196216

197-
try {
198-
$application->find('f');
199-
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
200-
} catch (\Exception $e) {
201-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
202-
$this->assertRegExp('/Command "f" is not defined./', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
203-
}
204-
205-
try {
206-
$application->find('a');
207-
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
208-
} catch (\Exception $e) {
209-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
210-
$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');
211-
}
217+
$application->find($abbreviation);
218+
}
212219

213-
try {
214-
$application->find('foo:b');
215-
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
216-
} catch (\Exception $e) {
217-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
218-
$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');
219-
}
220+
public function provideAmbiguousAbbreviations()
221+
{
222+
return array(
223+
array('f', 'Command "f" is not defined.'),
224+
array('a', 'Command "a" is ambiguous (afoobar, afoobar1 and 1 more).'),
225+
array('foo:b', 'Command "foo:b" is ambiguous (foo:bar, foo:bar1).')
226+
);
220227
}
221228

222-
public function testFindAlternativeExceptionMessage()
229+
/**
230+
* @dataProvider provideInvalidCommandNamesSingle
231+
* @expectedException \InvalidArgumentException
232+
* @expectedExceptionMessage Did you mean this
233+
*/
234+
public function testFindAlternativeExceptionMessageSingle($name)
223235
{
224236
$application = new Application();
225237
$application->add(new \FooCommand());
238+
$application->find($name);
239+
}
226240

227-
// Command + singular
228-
try {
229-
$application->find('foo:baR');
230-
$this->fail('->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
231-
} catch (\Exception $e) {
232-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
233-
$this->assertRegExp('/Did you mean this/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
234-
}
235-
236-
// Namespace + singular
237-
try {
238-
$application->find('foO:bar');
239-
$this->fail('->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
240-
} catch (\Exception $e) {
241-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
242-
$this->assertRegExp('/Did you mean this/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
243-
}
241+
public function provideInvalidCommandNamesSingle()
242+
{
243+
return array(
244+
array('foo:baR'),
245+
array('foO:bar')
246+
);
247+
}
244248

249+
public function testFindAlternativeExceptionMessageMultiple()
250+
{
251+
$application = new Application();
252+
$application->add(new \FooCommand());
245253
$application->add(new \Foo1Command());
246254
$application->add(new \Foo2Command());
247255

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,19 @@ public static function setUpBeforeClass()
3535

3636
public function testConstructor()
3737
{
38-
try {
39-
$command = new Command();
40-
$this->fail('__construct() throws a \LogicException if the name is null');
41-
} catch (\Exception $e) {
42-
$this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the name is null');
43-
$this->assertEquals('The command name cannot be empty.', $e->getMessage(), '__construct() throws a \LogicException if the name is null');
44-
}
4538
$command = new Command('foo:bar');
4639
$this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
4740
}
4841

42+
/**
43+
* @expectedException \LogicException
44+
* @expectedExceptionMessage The command name cannot be empty.
45+
*/
46+
public function testCommandNameCannotBeEmpty()
47+
{
48+
new Command();
49+
}
50+
4951
public function testSetApplication()
5052
{
5153
$application = new Application();
@@ -92,22 +94,25 @@ public function testGetNamespaceGetNameSetName()
9294
$ret = $command->setName('foobar:bar');
9395
$this->assertEquals($command, $ret, '->setName() implements a fluent interface');
9496
$this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
97+
}
98+
99+
/**
100+
* @dataProvider provideInvalidCommandNames
101+
*/
102+
public function testInvalidCommandNames($name)
103+
{
104+
$this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
105+
106+
$command = new \TestCommand();
107+
$command->setName($name);
108+
}
95109

96-
try {
97-
$command->setName('');
98-
$this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
99-
} catch (\Exception $e) {
100-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty');
101-
$this->assertEquals('Command name "" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty');
102-
}
103-
104-
try {
105-
$command->setName('foo:');
106-
$this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
107-
} catch (\Exception $e) {
108-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty');
109-
$this->assertEquals('Command name "foo:" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty');
110-
}
110+
public function provideInvalidCommandNames()
111+
{
112+
return array(
113+
array(''),
114+
array('foo:')
115+
);
111116
}
112117

113118
public function testGetSetDescription()
@@ -193,32 +198,43 @@ public function testMergeApplicationDefinition()
193198
$this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
194199
}
195200

196-
public function testRun()
201+
public function testRunInteractive()
197202
{
198-
$command = new \TestCommand();
199-
$tester = new CommandTester($command);
200-
try {
201-
$tester->execute(array('--bar' => true));
202-
$this->fail('->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
203-
} catch (\Exception $e) {
204-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
205-
$this->assertEquals('The "--bar" option does not exist.', $e->getMessage(), '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
206-
}
203+
$tester = new CommandTester(new \TestCommand());
207204

208205
$tester->execute(array(), array('interactive' => true));
206+
209207
$this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
208+
}
209+
210+
public function testRunNonInteractive()
211+
{
212+
$tester = new CommandTester(new \TestCommand());
210213

211214
$tester->execute(array(), array('interactive' => false));
215+
212216
$this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
217+
}
213218

219+
/**
220+
* @expectedException \LogicException
221+
* @expectedExceptionMessage You must override the execute() method in the concrete command class.
222+
*/
223+
public function testExecuteMethodNeedsToBeOverriden()
224+
{
214225
$command = new Command('foo');
215-
try {
216-
$command->run(new StringInput(''), new NullOutput());
217-
$this->fail('->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
218-
} catch (\Exception $e) {
219-
$this->assertInstanceOf('\LogicException', $e, '->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
220-
$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');
221-
}
226+
$command->run(new StringInput(''), new NullOutput());
227+
}
228+
229+
/**
230+
* @expectedException \InvalidArgumentException
231+
* @expectedExceptionMessage The "--bar" option does not exist.
232+
*/
233+
public function testRunWithInvalidOption()
234+
{
235+
$command = new \TestCommand();
236+
$tester = new CommandTester($command);
237+
$tester->execute(array('--bar' => true));
222238
}
223239

224240
public function testRunReturnsAlwaysInteger()
@@ -251,7 +267,7 @@ public function testSetCodeWithNonClosureCallable()
251267
}
252268

253269
/**
254-
* @expectedException InvalidArgumentException
270+
* @expectedException \InvalidArgumentException
255271
* @expectedExceptionMessage Invalid callable provided to Command::setCode.
256272
*/
257273
public function testSetCodeWithNonCallable()

src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,51 @@
1818

1919
class HelpCommandTest extends \PHPUnit_Framework_TestCase
2020
{
21-
public function testExecute()
21+
public function testExecuteForCommandAlias()
2222
{
2323
$command = new HelpCommand();
24-
25-
$application = new Application();
26-
$command->setApplication($application);
24+
$command->setApplication(new Application());
2725
$commandTester = new CommandTester($command);
2826
$commandTester->execute(array('command_name' => 'li'));
27+
2928
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
29+
}
3030

31+
public function testExecuteForCommand()
32+
{
3133
$command = new HelpCommand();
32-
3334
$commandTester = new CommandTester($command);
3435
$command->setCommand(new ListCommand());
3536
$commandTester->execute(array());
37+
3638
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
39+
}
3740

41+
public function testExecuteForCommandWithXmlOption()
42+
{
43+
$command = new HelpCommand();
44+
$commandTester = new CommandTester($command);
3845
$command->setCommand(new ListCommand());
3946
$commandTester->execute(array('--xml' => true));
47+
4048
$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
49+
}
4150

51+
public function testExecuteForApplicationCommand()
52+
{
4253
$application = new Application();
4354
$commandTester = new CommandTester($application->get('help'));
4455
$commandTester->execute(array('command_name' => 'list'));
56+
4557
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
58+
}
4659

60+
public function testExecuteForApplicationCommandWithXmlOption()
61+
{
62+
$application = new Application();
63+
$commandTester = new CommandTester($application->get('help'));
4764
$commandTester->execute(array('command_name' => 'list', '--xml' => true));
65+
4866
$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
4967
}
5068
}

0 commit comments

Comments
 (0)