You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/ApplicationTest.php
+76-68Lines changed: 76 additions & 68 deletions
Original file line number
Diff line number
Diff line change
@@ -135,14 +135,6 @@ public function testHasGet()
135
135
$this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
136
136
$this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
137
137
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
-
146
138
$application = newApplication();
147
139
$application->add($foo = new \FooCommand());
148
140
// simulate --help
@@ -151,7 +143,17 @@ public function testHasGet()
151
143
$p->setAccessible(true);
152
144
$p->setValue($application, true);
153
145
$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
+
publicfunctiontestGetInvalidCommand()
154
+
{
155
+
$application = newApplication();
156
+
$application->get('foofoo');
155
157
}
156
158
157
159
publicfunctiontestGetNamespaces()
@@ -170,84 +172,90 @@ public function testFindNamespace()
170
172
$this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
171
173
$application->add(new \Foo2Command());
172
174
$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
+
}
180
176
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
+
publicfunctiontestFindAmbiguousNamespace()
182
+
{
183
+
$application = newApplication();
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
+
publicfunctiontestFindInvalidNamespace()
194
+
{
195
+
$application = newApplication();
196
+
$application->findNamespace('bar');
188
197
}
189
198
190
199
publicfunctiontestFind()
191
200
{
192
201
$application = newApplication();
193
202
$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');
199
203
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');
$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
+
}
218
225
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
+
publicfunctionprovideAmbiguousAbbreviations()
227
+
{
228
+
returnarray(
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).')
$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');
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Command/CommandTest.php
+56-40Lines changed: 56 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -35,17 +35,19 @@ public static function setUpBeforeClass()
35
35
36
36
publicfunctiontestConstructor()
37
37
{
38
-
try {
39
-
$command = newCommand();
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
-
}
45
38
$command = newCommand('foo:bar');
46
39
$this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
47
40
}
48
41
42
+
/**
43
+
* @expectedException \LogicException
44
+
* @expectedExceptionMessage The command name cannot be empty.
45
+
*/
46
+
publicfunctiontestCommandNameCannotBeEmpty()
47
+
{
48
+
newCommand();
49
+
}
50
+
49
51
publicfunctiontestSetApplication()
50
52
{
51
53
$application = newApplication();
@@ -92,22 +94,25 @@ public function testGetNamespaceGetNameSetName()
92
94
$ret = $command->setName('foobar:bar');
93
95
$this->assertEquals($command, $ret, '->setName() implements a fluent interface');
94
96
$this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
97
+
}
98
+
99
+
/**
100
+
* @dataProvider provideInvalidCommandNames
101
+
*/
102
+
publicfunctiontestInvalidCommandNames($name)
103
+
{
104
+
$this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
105
+
106
+
$command = new \TestCommand();
107
+
$command->setName($name);
108
+
}
95
109
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
+
publicfunctionprovideInvalidCommandNames()
111
+
{
112
+
returnarray(
113
+
array(''),
114
+
array('foo:')
115
+
);
111
116
}
112
117
113
118
publicfunctiontestGetSetDescription()
@@ -193,32 +198,43 @@ public function testMergeApplicationDefinition()
193
198
$this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
194
199
}
195
200
196
-
publicfunctiontestRun()
201
+
publicfunctiontestRunInteractive()
197
202
{
198
-
$command = new \TestCommand();
199
-
$tester = newCommandTester($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');
$this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
$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');
$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
29
+
}
30
30
31
+
publicfunctiontestExecuteForCommand()
32
+
{
31
33
$command = newHelpCommand();
32
-
33
34
$commandTester = newCommandTester($command);
34
35
$command->setCommand(newListCommand());
35
36
$commandTester->execute(array());
37
+
36
38
$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');
0 commit comments