Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 623 lines (518 sloc) 33.009 kb
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Console\Tests;
13
14 use Symfony\Component\Console\Application;
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
15 use Symfony\Component\Console\Helper\HelperSet;
16 use Symfony\Component\Console\Helper\FormatterHelper;
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
17 use Symfony\Component\Console\Input\ArrayInput;
18 use Symfony\Component\Console\Input\InputArgument;
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
19 use Symfony\Component\Console\Input\InputDefinition;
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
20 use Symfony\Component\Console\Input\InputOption;
21 use Symfony\Component\Console\Output\NullOutput;
22 use Symfony\Component\Console\Output\Output;
23 use Symfony\Component\Console\Tester\ApplicationTester;
24
25 class ApplicationTest extends \PHPUnit_Framework_TestCase
26 {
2da4fd0 Fabien Potencier fixed CS
fabpot authored
27 protected static $fixturesPath;
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
28
2da4fd0 Fabien Potencier fixed CS
fabpot authored
29 public static function setUpBeforeClass()
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
30 {
31 self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
32 require_once self::$fixturesPath.'/FooCommand.php';
33 require_once self::$fixturesPath.'/Foo1Command.php';
34 require_once self::$fixturesPath.'/Foo2Command.php';
35 require_once self::$fixturesPath.'/Foo3Command.php';
36 }
37
38 protected function normalizeLineBreaks($text)
39 {
40 return str_replace(PHP_EOL, "\n", $text);
41 }
42
43 /**
44 * Replaces the dynamic placeholders of the command help text with a static version.
45 * The placeholder %command.full_name% includes the script path that is not predictable
46 * and can not be tested against.
47 */
48 protected function ensureStaticCommandHelp(Application $application)
49 {
50 foreach ($application->all() as $command) {
51 $command->setHelp(str_replace('%command.full_name%', 'app/console %command.name%', $command->getHelp()));
52 }
53 }
54
55 public function testConstructor()
56 {
57 $application = new Application('foo', 'bar');
58 $this->assertEquals('foo', $application->getName(), '__construct() takes the application name as its first argument');
59 $this->assertEquals('bar', $application->getVersion(), '__construct() takes the application version as its first argument');
60 $this->assertEquals(array('help', 'list'), array_keys($application->all()), '__construct() registered the help and list commands by default');
61 }
62
63 public function testSetGetName()
64 {
65 $application = new Application();
66 $application->setName('foo');
67 $this->assertEquals('foo', $application->getName(), '->setName() sets the name of the application');
68 }
69
70 public function testSetGetVersion()
71 {
72 $application = new Application();
73 $application->setVersion('bar');
74 $this->assertEquals('bar', $application->getVersion(), '->setVersion() sets the version of the application');
75 }
76
77 public function testGetLongVersion()
78 {
79 $application = new Application('foo', 'bar');
80 $this->assertEquals('<info>foo</info> version <comment>bar</comment>', $application->getLongVersion(), '->getLongVersion() returns the long version of the application');
81 }
82
83 public function testHelp()
84 {
85 $application = new Application();
86 $this->assertStringEqualsFile(self::$fixturesPath.'/application_gethelp.txt', $this->normalizeLineBreaks($application->getHelp()), '->setHelp() returns a help message');
87 }
88
89 public function testAll()
90 {
91 $application = new Application();
92 $commands = $application->all();
93 $this->assertEquals('Symfony\\Component\\Console\\Command\\HelpCommand', get_class($commands['help']), '->all() returns the registered commands');
94
95 $application->add(new \FooCommand());
96 $commands = $application->all('foo');
97 $this->assertEquals(1, count($commands), '->all() takes a namespace as its first argument');
98 }
99
100 public function testRegister()
101 {
102 $application = new Application();
103 $command = $application->register('foo');
104 $this->assertEquals('foo', $command->getName(), '->register() registers a new command');
105 }
106
107 public function testAdd()
108 {
109 $application = new Application();
110 $application->add($foo = new \FooCommand());
111 $commands = $application->all();
112 $this->assertEquals($foo, $commands['foo:bar'], '->add() registers a command');
113
114 $application = new Application();
115 $application->addCommands(array($foo = new \FooCommand(), $foo1 = new \Foo1Command()));
116 $commands = $application->all();
117 $this->assertEquals(array($foo, $foo1), array($commands['foo:bar'], $commands['foo:bar1']), '->addCommands() registers an array of commands');
118 }
119
120 public function testHasGet()
121 {
122 $application = new Application();
123 $this->assertTrue($application->has('list'), '->has() returns true if a named command is registered');
124 $this->assertFalse($application->has('afoobar'), '->has() returns false if a named command is not registered');
125
126 $application->add($foo = new \FooCommand());
127 $this->assertTrue($application->has('afoobar'), '->has() returns true if an alias is registered');
128 $this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
129 $this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
130
131 try {
132 $application->get('foofoo');
133 $this->fail('->get() throws an \InvalidArgumentException if the command does not exist');
134 } catch (\Exception $e) {
135 $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the command does not exist');
136 $this->assertEquals('The command "foofoo" does not exist.', $e->getMessage(), '->get() throws an \InvalidArgumentException if the command does not exist');
137 }
138
139 $application = new Application();
140 $application->add($foo = new \FooCommand());
141 // simulate --help
142 $r = new \ReflectionObject($application);
143 $p = $r->getProperty('wantHelps');
144 $p->setAccessible(true);
145 $p->setValue($application, true);
146 $command = $application->get('foo:bar');
147 $this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($command), '->get() returns the help command if --help is provided as the input');
148 }
149
150 public function testGetNamespaces()
151 {
152 $application = new Application();
153 $application->add(new \FooCommand());
154 $application->add(new \Foo1Command());
155 $this->assertEquals(array('foo'), $application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces');
156 }
157
158 public function testFindNamespace()
159 {
160 $application = new Application();
161 $application->add(new \FooCommand());
162 $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
163 $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
164 $application->add(new \Foo2Command());
165 $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
166 try {
167 $application->findNamespace('f');
168 $this->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
169 } catch (\Exception $e) {
170 $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
171 $this->assertEquals('The namespace "f" is ambiguous (foo, foo1).', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
172 }
173
174 try {
175 $application->findNamespace('bar');
176 $this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
177 } catch (\Exception $e) {
178 $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
179 $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');
180 }
181 }
182
183 public function testFind()
184 {
185 $application = new Application();
186 $application->add(new \FooCommand());
187 $this->assertEquals('FooCommand', get_class($application->find('foo:bar')), '->find() returns a command if its name exists');
188 $this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($application->find('h')), '->find() returns a command if its name exists');
189 $this->assertEquals('FooCommand', get_class($application->find('f:bar')), '->find() returns a command if the abbreviation for the namespace exists');
190 $this->assertEquals('FooCommand', get_class($application->find('f:b')), '->find() returns a command if the abbreviation for the namespace and the command name exist');
191 $this->assertEquals('FooCommand', get_class($application->find('a')), '->find() returns a command if the abbreviation exists for an alias');
192
193 $application->add(new \Foo1Command());
194 $application->add(new \Foo2Command());
195
196 try {
197 $application->find('f');
198 $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
199 } catch (\Exception $e) {
200 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
201 $this->assertRegExp('/Command "f" is not defined./', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
202 }
203
204 try {
205 $application->find('a');
206 $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
207 } catch (\Exception $e) {
208 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
209 $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');
210 }
211
212 try {
213 $application->find('foo:b');
214 $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
215 } catch (\Exception $e) {
216 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
217 $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');
218 }
219 }
220
6fea732 Grégoire Pineau [Console] Added tests for message exception when command is not availabl...
lyrixx authored
221 public function testFindAlternativeExceptionMessage()
222 {
223 $application = new Application();
224 $application->add(new \FooCommand());
225
226 // Command + singular
227 try {
228 $application->find('foo:baR');
229 $this->fail('->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
230 } catch (\Exception $e) {
231 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
232 $this->assertRegExp('/Did you mean this/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
233 }
234
235 // Namespace + singular
236 try {
237 $application->find('foO:bar');
238 $this->fail('->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
239 } catch (\Exception $e) {
240 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
241 $this->assertRegExp('/Did you mean this/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with one alternative');
242 }
243
244
245 $application->add(new \Foo1Command());
246 $application->add(new \Foo2Command());
247
248 // Command + plural
249 try {
250 $application->find('foo:baR');
251 $this->fail('->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
252 } catch (\Exception $e) {
253 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
254 $this->assertRegExp('/Did you mean one of these/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
255 }
256
257 // Namespace + plural
258 try {
259 $application->find('foo2:bar');
260 $this->fail('->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
261 } catch (\Exception $e) {
262 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
263 $this->assertRegExp('/Did you mean one of these/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
264 }
265 }
266
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
267 public function testFindAlternativeCommands()
268 {
269 $application = new Application();
270
271 $application->add(new \FooCommand());
272 $application->add(new \Foo1Command());
273 $application->add(new \Foo2Command());
274
275 try {
7c328a9 Pascal Borreli Fixed typos
pborreli authored
276 $application->find($commandName = 'Unknown command');
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
277 $this->fail('->find() throws an \InvalidArgumentException if command does not exist');
278 } catch (\Exception $e) {
279 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist');
280 $this->assertEquals(sprintf('Command "%s" is not defined.', $commandName), $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, without alternatives');
281 }
282
283 try {
284 $application->find($commandName = 'foo');
285 $this->fail('->find() throws an \InvalidArgumentException if command does not exist');
286 } catch (\Exception $e) {
287 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist');
288 $this->assertRegExp(sprintf('/Command "%s" is not defined./', $commandName), $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
289 $this->assertRegExp('/foo:bar/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternative : "foo:bar"');
290 $this->assertRegExp('/foo1:bar/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternative : "foo1:bar"');
291 $this->assertRegExp('/foo:bar1/', $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternative : "foo:bar1"');
292 }
293
294 // Test if "foo1" command throw an "\InvalidArgumentException" and does not contain
295 // "foo:bar" as alternative because "foo1" is too far from "foo:bar"
296 try {
297 $application->find($commandName = 'foo1');
298 $this->fail('->find() throws an \InvalidArgumentException if command does not exist');
299 } catch (\Exception $e) {
300 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if command does not exist');
301 $this->assertRegExp(sprintf('/Command "%s" is not defined./', $commandName), $e->getMessage(), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives');
302 $this->assertFalse(strpos($e->getMessage(), 'foo:bar'), '->find() throws an \InvalidArgumentException if command does not exist, without "foo:bar" alternative');
303 }
304 }
305
306 public function testFindAlternativeNamespace()
307 {
308 $application = new Application();
309
310 $application->add(new \FooCommand());
311 $application->add(new \Foo1Command());
312 $application->add(new \Foo2Command());
313 $application->add(new \foo3Command());
314
315 try {
7c328a9 Pascal Borreli Fixed typos
pborreli authored
316 $application->find('Unknown-namespace:Unknown-command');
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
317 $this->fail('->find() throws an \InvalidArgumentException if namespace does not exist');
318 } catch (\Exception $e) {
319 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if namespace does not exist');
7c328a9 Pascal Borreli Fixed typos
pborreli authored
320 $this->assertEquals('There are no commands defined in the "Unknown-namespace" namespace.', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, without alternatives');
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
321 }
322
323 try {
324 $application->find('foo2:command');
325 $this->fail('->find() throws an \InvalidArgumentException if namespace does not exist');
326 } catch (\Exception $e) {
327 $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if namespace does not exist');
328 $this->assertRegExp('/There are no commands defined in the "foo2" namespace./', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative');
329 $this->assertRegExp('/foo/', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo"');
330 $this->assertRegExp('/foo1/', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo1"');
331 $this->assertRegExp('/foo3/', $e->getMessage(), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo3"');
332 }
333 }
334
335 public function testSetCatchExceptions()
336 {
2be318e Fabien Potencier merged 2.0
fabpot authored
337 $application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
338 $application->setAutoExit(false);
2be318e Fabien Potencier merged 2.0
fabpot authored
339 $application->expects($this->any())
340 ->method('getTerminalWidth')
341 ->will($this->returnValue(120));
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
342 $tester = new ApplicationTester($application);
343
344 $application->setCatchExceptions(true);
345 $tester->run(array('command' => 'foo'), array('decorated' => false));
346 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->setCatchExceptions() sets the catch exception flag');
347
348 $application->setCatchExceptions(false);
349 try {
350 $tester->run(array('command' => 'foo'), array('decorated' => false));
351 $this->fail('->setCatchExceptions() sets the catch exception flag');
352 } catch (\Exception $e) {
353 $this->assertInstanceOf('\Exception', $e, '->setCatchExceptions() sets the catch exception flag');
354 $this->assertEquals('Command "foo" is not defined.', $e->getMessage(), '->setCatchExceptions() sets the catch exception flag');
355 }
356 }
357
358 public function testAsText()
359 {
360 $application = new Application();
361 $application->add(new \FooCommand);
362 $this->ensureStaticCommandHelp($application);
363 $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext1.txt', $this->normalizeLineBreaks($application->asText()), '->asText() returns a text representation of the application');
364 $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext2.txt', $this->normalizeLineBreaks($application->asText('foo')), '->asText() returns a text representation of the application');
365 }
366
367 public function testAsXml()
368 {
369 $application = new Application();
370 $application->add(new \FooCommand);
371 $this->ensureStaticCommandHelp($application);
372 $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml1.txt', $application->asXml(), '->asXml() returns an XML representation of the application');
373 $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml2.txt', $application->asXml('foo'), '->asXml() returns an XML representation of the application');
374 }
375
376 public function testRenderException()
377 {
2be318e Fabien Potencier merged 2.0
fabpot authored
378 $application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
379 $application->setAutoExit(false);
2be318e Fabien Potencier merged 2.0
fabpot authored
380 $application->expects($this->any())
381 ->method('getTerminalWidth')
382 ->will($this->returnValue(120));
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
383 $tester = new ApplicationTester($application);
384
385 $tester->run(array('command' => 'foo'), array('decorated' => false));
386 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exception');
387
388 $tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
389 $this->assertContains('Exception trace', $tester->getDisplay(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
390
391 $tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false));
392 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
393
394 $application->add(new \Foo3Command);
395 $tester = new ApplicationTester($application);
396 $tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
397 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions');
398
5fdccc0 Fabien Potencier merged 2.0
fabpot authored
399 $application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
400 $application->setAutoExit(false);
401 $application->expects($this->any())
402 ->method('getTerminalWidth')
403 ->will($this->returnValue(32));
404 $tester = new ApplicationTester($application);
405
406 $tester->run(array('command' => 'foo'), array('decorated' => false));
407 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() wraps messages when they are bigger than the terminal');
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
408 }
409
410 public function testRun()
411 {
412 $application = new Application();
413 $application->setAutoExit(false);
414 $application->setCatchExceptions(false);
415 $application->add($command = new \Foo1Command());
416 $_SERVER['argv'] = array('cli.php', 'foo:bar1');
417
418 ob_start();
419 $application->run();
420 ob_end_clean();
421
422 $this->assertSame('Symfony\Component\Console\Input\ArgvInput', get_class($command->input), '->run() creates an ArgvInput by default if none is given');
423 $this->assertSame('Symfony\Component\Console\Output\ConsoleOutput', get_class($command->output), '->run() creates a ConsoleOutput by default if none is given');
424
425 $application = new Application();
426 $application->setAutoExit(false);
427 $application->setCatchExceptions(false);
428
429 $this->ensureStaticCommandHelp($application);
430 $tester = new ApplicationTester($application);
431
432 $tester->run(array(), array('decorated' => false));
433 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the list command if no argument is passed');
434
435 $tester->run(array('--help' => true), array('decorated' => false));
436 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the help command if --help is passed');
437
438 $tester->run(array('-h' => true), array('decorated' => false));
439 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the help command if -h is passed');
440
441 $tester->run(array('command' => 'list', '--help' => true), array('decorated' => false));
442 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the help if --help is passed');
443
444 $tester->run(array('command' => 'list', '-h' => true), array('decorated' => false));
445 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the help if -h is passed');
446
447 $tester->run(array('--ansi' => true));
448 $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --ansi is passed');
449
450 $tester->run(array('--no-ansi' => true));
451 $this->assertFalse($tester->getOutput()->isDecorated(), '->run() forces color output to be disabled if --no-ansi is passed');
452
453 $tester->run(array('--version' => true), array('decorated' => false));
454 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the program version if --version is passed');
455
456 $tester->run(array('-V' => true), array('decorated' => false));
457 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the program version if -v is passed');
458
459 $tester->run(array('command' => 'list', '--quiet' => true));
460 $this->assertSame('', $tester->getDisplay(), '->run() removes all output if --quiet is passed');
461
462 $tester->run(array('command' => 'list', '-q' => true));
463 $this->assertSame('', $tester->getDisplay(), '->run() removes all output if -q is passed');
464
465 $tester->run(array('command' => 'list', '--verbose' => true));
466 $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose is passed');
467
468 $tester->run(array('command' => 'list', '-v' => true));
469 $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
470
471 $application = new Application();
472 $application->setAutoExit(false);
473 $application->setCatchExceptions(false);
474 $application->add(new \FooCommand());
475 $tester = new ApplicationTester($application);
476
477 $tester->run(array('command' => 'foo:bar', '--no-interaction' => true), array('decorated' => false));
478 $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if --no-interaction is passed');
479
480 $tester->run(array('command' => 'foo:bar', '-n' => true), array('decorated' => false));
481 $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
482 }
483
484 /**
485 * @expectedException \LogicException
486 * @dataProvider getAddingAlreadySetDefinitionElementData
487 */
488 public function testAddingAlreadySetDefinitionElementData($def)
489 {
490 $application = new Application();
491 $application->setAutoExit(false);
492 $application->setCatchExceptions(false);
493 $application
494 ->register('foo')
495 ->setDefinition(array($def))
496 ->setCode(function (InputInterface $input, OutputInterface $output) {})
497 ;
498
499 $input = new ArrayInput(array('command' => 'foo'));
500 $output = new NullOutput();
501 $application->run($input, $output);
502 }
503
504 public function getAddingAlreadySetDefinitionElementData()
505 {
506 return array(
507 array(new InputArgument('command', InputArgument::REQUIRED)),
508 array(new InputOption('quiet', '', InputOption::VALUE_NONE)),
509 array(new InputOption('query', 'q', InputOption::VALUE_NONE)),
510 );
511 }
ed8cb0f Fabien Potencier fixed CS
fabpot authored
512
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
513 public function testGetDefaultHelperSetReturnsDefaultValues()
514 {
515 $application = new Application();
516 $application->setAutoExit(false);
517 $application->setCatchExceptions(false);
ed8cb0f Fabien Potencier fixed CS
fabpot authored
518
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
519 $helperSet = $application->getHelperSet();
ed8cb0f Fabien Potencier fixed CS
fabpot authored
520
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
521 $this->assertTrue($helperSet->has('formatter'));
522 $this->assertTrue($helperSet->has('dialog'));
523 $this->assertTrue($helperSet->has('progress'));
524 }
ed8cb0f Fabien Potencier fixed CS
fabpot authored
525
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
526 public function testAddingSingleHelperSetOverwritesDefaultValues()
527 {
528 $application = new Application();
529 $application->setAutoExit(false);
530 $application->setCatchExceptions(false);
ed8cb0f Fabien Potencier fixed CS
fabpot authored
531
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
532 $application->setHelperSet(new HelperSet(array(new FormatterHelper())));
ed8cb0f Fabien Potencier fixed CS
fabpot authored
533
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
534 $helperSet = $application->getHelperSet();
ed8cb0f Fabien Potencier fixed CS
fabpot authored
535
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
536 $this->assertTrue($helperSet->has('formatter'));
ed8cb0f Fabien Potencier fixed CS
fabpot authored
537
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
538 // no other default helper set should be returned
539 $this->assertFalse($helperSet->has('dialog'));
540 $this->assertFalse($helperSet->has('progress'));
541 }
ed8cb0f Fabien Potencier fixed CS
fabpot authored
542
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
543 public function testOverwritingDefaultHelperSetOverwritesDefaultValues()
544 {
545 $application = new CustomApplication();
546 $application->setAutoExit(false);
547 $application->setCatchExceptions(false);
ed8cb0f Fabien Potencier fixed CS
fabpot authored
548
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
549 $application->setHelperSet(new HelperSet(array(new FormatterHelper())));
ed8cb0f Fabien Potencier fixed CS
fabpot authored
550
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
551 $helperSet = $application->getHelperSet();
ed8cb0f Fabien Potencier fixed CS
fabpot authored
552
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
553 $this->assertTrue($helperSet->has('formatter'));
ed8cb0f Fabien Potencier fixed CS
fabpot authored
554
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
555 // no other default helper set should be returned
556 $this->assertFalse($helperSet->has('dialog'));
557 $this->assertFalse($helperSet->has('progress'));
558 }
ed8cb0f Fabien Potencier fixed CS
fabpot authored
559
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
560 public function testGetDefaultInputDefinitionReturnsDefaultValues()
561 {
562 $application = new Application();
563 $application->setAutoExit(false);
564 $application->setCatchExceptions(false);
ed8cb0f Fabien Potencier fixed CS
fabpot authored
565
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
566 $inputDefinition = $application->getDefinition();
567
568 $this->assertTrue($inputDefinition->hasArgument('command'));
ed8cb0f Fabien Potencier fixed CS
fabpot authored
569
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
570 $this->assertTrue($inputDefinition->hasOption('help'));
571 $this->assertTrue($inputDefinition->hasOption('quiet'));
572 $this->assertTrue($inputDefinition->hasOption('verbose'));
573 $this->assertTrue($inputDefinition->hasOption('version'));
574 $this->assertTrue($inputDefinition->hasOption('ansi'));
575 $this->assertTrue($inputDefinition->hasOption('no-ansi'));
576 $this->assertTrue($inputDefinition->hasOption('no-interaction'));
577 }
ed8cb0f Fabien Potencier fixed CS
fabpot authored
578
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
579 public function testOverwritingDefaultInputDefinitionOverwritesDefaultValues()
580 {
581 $application = new CustomApplication();
582 $application->setAutoExit(false);
583 $application->setCatchExceptions(false);
ed8cb0f Fabien Potencier fixed CS
fabpot authored
584
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
585 $inputDefinition = $application->getDefinition();
586
587 // check wether the default arguments and options are not returned any more
588 $this->assertFalse($inputDefinition->hasArgument('command'));
ed8cb0f Fabien Potencier fixed CS
fabpot authored
589
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
590 $this->assertFalse($inputDefinition->hasOption('help'));
591 $this->assertFalse($inputDefinition->hasOption('quiet'));
592 $this->assertFalse($inputDefinition->hasOption('verbose'));
593 $this->assertFalse($inputDefinition->hasOption('version'));
594 $this->assertFalse($inputDefinition->hasOption('ansi'));
595 $this->assertFalse($inputDefinition->hasOption('no-ansi'));
596 $this->assertFalse($inputDefinition->hasOption('no-interaction'));
ed8cb0f Fabien Potencier fixed CS
fabpot authored
597
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
598 $this->assertTrue($inputDefinition->hasOption('custom'));
599 }
81b0979 Fabien Potencier moved component and bridge unit tests to the src/ directory
fabpot authored
600 }
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
601
602 class CustomApplication extends Application
603 {
604 /**
605 * Overwrites the default input definition.
606 *
607 * @return InputDefinition An InputDefinition instance
608 */
609 protected function getDefaultInputDefinition()
610 {
611 return new InputDefinition(array(new InputOption('--custom', '-c', InputOption::VALUE_NONE, 'Set the custom input definition.')));
612 }
ed8cb0f Fabien Potencier fixed CS
fabpot authored
613
b64f659 Dirk Pahl [2.2][Console] Test default input defintion and default helper set
dirkaholic authored
614 /**
615 * Gets the default helper set with the helpers that should always be available.
616 *
617 * @return HelperSet A HelperSet instance
618 */
619 protected function getDefaultHelperSet()
620 {
621 return new HelperSet(array(new FormatterHelper()));
622 }
623 }
Something went wrong with that request. Please try again.