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
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
@@ -129,14 +129,6 @@ public function testHasGet()
129
129
$this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
130
130
$this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
131
131
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
-
140
132
$application = newApplication();
141
133
$application->add($foo = new \FooCommand());
142
134
// simulate --help
@@ -145,7 +137,17 @@ public function testHasGet()
145
137
$p->setAccessible(true);
146
138
$p->setValue($application, true);
147
139
$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
+
publicfunctiontestGetInvalidCommand()
148
+
{
149
+
$application = newApplication();
150
+
$application->get('foofoo');
149
151
}
150
152
151
153
publicfunctiontestGetNamespaces()
@@ -164,84 +166,90 @@ public function testFindNamespace()
164
166
$this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
165
167
$application->add(new \Foo2Command());
166
168
$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
+
}
174
170
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
+
publicfunctiontestFindAmbiguousNamespace()
176
+
{
177
+
$application = newApplication();
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
+
publicfunctiontestFindInvalidNamespace()
188
+
{
189
+
$application = newApplication();
190
+
$application->findNamespace('bar');
182
191
}
183
192
184
193
publicfunctiontestFind()
185
194
{
186
195
$application = newApplication();
187
196
$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');
193
197
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');
$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
+
}
212
219
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
+
publicfunctionprovideAmbiguousAbbreviations()
221
+
{
222
+
returnarray(
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).')
$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');
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