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

Skip to content

Commit 6f0a5ad

Browse files
committed
merged branch jakzal/console-tests-cleanup (PR #6989)
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.
2 parents 17e065f + 5ca04b0 commit 6f0a5ad

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
@@ -135,14 +135,6 @@ public function testHasGet()
135135
$this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
136136
$this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
137137

138-
try {
139-
$application->get('foofoo');
140-
$this->fail('->get() throws an \InvalidArgumentException if the command does not exist');
141-
} catch (\Exception $e) {
142-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the command does not exist');
143-
$this->assertEquals('The command "foofoo" does not exist.', $e->getMessage(), '->get() throws an \InvalidArgumentException if the command does not exist');
144-
}
145-
146138
$application = new Application();
147139
$application->add($foo = new \FooCommand());
148140
// simulate --help
@@ -151,7 +143,17 @@ public function testHasGet()
151143
$p->setAccessible(true);
152144
$p->setValue($application, true);
153145
$command = $application->get('foo:bar');
154-
$this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($command), '->get() returns the help command if --help is provided as the input');
146+
$this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $command, '->get() returns the help command if --help is provided as the input');
147+
}
148+
149+
/**
150+
* @expectedException \InvalidArgumentException
151+
* @expectedExceptionMessage The command "foofoo" does not exist.
152+
*/
153+
public function testGetInvalidCommand()
154+
{
155+
$application = new Application();
156+
$application->get('foofoo');
155157
}
156158

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

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

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

204+
$this->assertInstanceOf('FooCommand', $application->find('foo:bar'), '->find() returns a command if its name exists');
205+
$this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $application->find('h'), '->find() returns a command if its name exists');
206+
$this->assertInstanceOf('FooCommand', $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
207+
$this->assertInstanceOf('FooCommand', $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
208+
$this->assertInstanceOf('FooCommand', $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
209+
}
210+
211+
/**
212+
* @dataProvider provideAmbiguousAbbreviations
213+
*/
214+
public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExceptionMessage)
215+
{
216+
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
217+
218+
$application = new Application();
219+
$application->add(new \FooCommand());
200220
$application->add(new \Foo1Command());
201221
$application->add(new \Foo2Command());
202222

203-
try {
204-
$application->find('f');
205-
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
206-
} catch (\Exception $e) {
207-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
208-
$this->assertRegExp('/Command "f" is not defined./', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
209-
}
210-
211-
try {
212-
$application->find('a');
213-
$this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
214-
} catch (\Exception $e) {
215-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
216-
$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');
217-
}
223+
$application->find($abbreviation);
224+
}
218225

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

228-
public function testFindAlternativeExceptionMessage()
235+
/**
236+
* @dataProvider provideInvalidCommandNamesSingle
237+
* @expectedException \InvalidArgumentException
238+
* @expectedExceptionMessage Did you mean this
239+
*/
240+
public function testFindAlternativeExceptionMessageSingle($name)
229241
{
230242
$application = new Application();
231243
$application->add(new \FooCommand());
244+
$application->find($name);
245+
}
232246

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

255+
public function testFindAlternativeExceptionMessageMultiple()
256+
{
257+
$application = new Application();
258+
$application->add(new \FooCommand());
251259
$application->add(new \Foo1Command());
252260
$application->add(new \Foo2Command());
253261

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)