4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Application;
16 use Symfony\Component\Console\Helper\HelperSet;
17 use Symfony\Component\Console\Helper\FormatterHelper;
18 use Symfony\Component\Console\Input\ArgvInput;
19 use Symfony\Component\Console\Input\ArrayInput;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Input\InputArgument;
22 use Symfony\Component\Console\Input\InputDefinition;
23 use Symfony\Component\Console\Input\InputOption;
24 use Symfony\Component\Console\Output\NullOutput;
25 use Symfony\Component\Console\Output\Output;
26 use Symfony\Component\Console\Output\OutputInterface;
27 use Symfony\Component\Console\Output\StreamOutput;
28 use Symfony\Component\Console\Tester\ApplicationTester;
29 use Symfony\Component\Console\Event\ConsoleCommandEvent;
30 use Symfony\Component\Console\Event\ConsoleExceptionEvent;
31 use Symfony\Component\Console\Event\ConsoleTerminateEvent;
32 use Symfony\Component\EventDispatcher\EventDispatcher;
34 class ApplicationTest extends TestCase
36 protected static $fixturesPath;
38 public static function setUpBeforeClass()
40 self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
41 require_once self::$fixturesPath.'/FooCommand.php';
42 require_once self::$fixturesPath.'/FooOptCommand.php';
43 require_once self::$fixturesPath.'/Foo1Command.php';
44 require_once self::$fixturesPath.'/Foo2Command.php';
45 require_once self::$fixturesPath.'/Foo3Command.php';
46 require_once self::$fixturesPath.'/Foo4Command.php';
47 require_once self::$fixturesPath.'/Foo5Command.php';
48 require_once self::$fixturesPath.'/FoobarCommand.php';
49 require_once self::$fixturesPath.'/BarBucCommand.php';
50 require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
51 require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
54 protected function normalizeLineBreaks($text)
56 return str_replace(PHP_EOL, "\n", $text);
60 * Replaces the dynamic placeholders of the command help text with a static version.
61 * The placeholder %command.full_name% includes the script path that is not predictable
62 * and can not be tested against.
64 protected function ensureStaticCommandHelp(Application $application)
66 foreach ($application->all() as $command) {
67 $command->setHelp(str_replace('%command.full_name%', 'app/console %command.name%', $command->getHelp()));
71 public function testConstructor()
73 $application = new Application('foo', 'bar');
74 $this->assertEquals('foo', $application->getName(), '__construct() takes the application name as its first argument');
75 $this->assertEquals('bar', $application->getVersion(), '__construct() takes the application version as its second argument');
76 $this->assertEquals(array('help', 'list'), array_keys($application->all()), '__construct() registered the help and list commands by default');
79 public function testSetGetName()
81 $application = new Application();
82 $application->setName('foo');
83 $this->assertEquals('foo', $application->getName(), '->setName() sets the name of the application');
86 public function testSetGetVersion()
88 $application = new Application();
89 $application->setVersion('bar');
90 $this->assertEquals('bar', $application->getVersion(), '->setVersion() sets the version of the application');
93 public function testGetLongVersion()
95 $application = new Application('foo', 'bar');
96 $this->assertEquals('foo <info>bar</info>', $application->getLongVersion(), '->getLongVersion() returns the long version of the application');
99 public function testHelp()
101 $application = new Application();
102 $this->assertStringEqualsFile(self::$fixturesPath.'/application_gethelp.txt', $this->normalizeLineBreaks($application->getHelp()), '->getHelp() returns a help message');
105 public function testAll()
107 $application = new Application();
108 $commands = $application->all();
109 $this->assertInstanceOf('Symfony\\Component\\Console\\Command\\HelpCommand', $commands['help'], '->all() returns the registered commands');
111 $application->add(new \FooCommand());
112 $commands = $application->all('foo');
113 $this->assertCount(1, $commands, '->all() takes a namespace as its first argument');
116 public function testRegister()
118 $application = new Application();
119 $command = $application->register('foo');
120 $this->assertEquals('foo', $command->getName(), '->register() registers a new command');
123 public function testAdd()
125 $application = new Application();
126 $application->add($foo = new \FooCommand());
127 $commands = $application->all();
128 $this->assertEquals($foo, $commands['foo:bar'], '->add() registers a command');
130 $application = new Application();
131 $application->addCommands(array($foo = new \FooCommand(), $foo1 = new \Foo1Command()));
132 $commands = $application->all();
133 $this->assertEquals(array($foo, $foo1), array($commands['foo:bar'], $commands['foo:bar1']), '->addCommands() registers an array of commands');
137 * @expectedException \LogicException
138 * @expectedExceptionMessage Command class "Foo5Command" is not correctly initialized. You probably forgot to call the parent constructor.
140 public function testAddCommandWithEmptyConstructor()
142 $application = new Application();
143 $application->add(new \Foo5Command());
146 public function testHasGet()
148 $application = new Application();
149 $this->assertTrue($application->has('list'), '->has() returns true if a named command is registered');
150 $this->assertFalse($application->has('afoobar'), '->has() returns false if a named command is not registered');
152 $application->add($foo = new \FooCommand());
153 $this->assertTrue($application->has('afoobar'), '->has() returns true if an alias is registered');
154 $this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
155 $this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
157 $application = new Application();
158 $application->add($foo = new \FooCommand());
160 $r = new \ReflectionObject($application);
161 $p = $r->getProperty('wantHelps');
162 $p->setAccessible(true);
163 $p->setValue($application, true);
164 $command = $application->get('foo:bar');
165 $this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $command, '->get() returns the help command if --help is provided as the input');
168 public function testSilentHelp()
170 $application = new Application();
171 $application->setAutoExit(false);
172 $application->setCatchExceptions(false);
174 $tester = new ApplicationTester($application);
175 $tester->run(array('-h' => true, '-q' => true), array('decorated' => false));
177 $this->assertEmpty($tester->getDisplay(true));
181 * @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
182 * @expectedExceptionMessage The command "foofoo" does not exist.
184 public function testGetInvalidCommand()
186 $application = new Application();
187 $application->get('foofoo');
190 public function testGetNamespaces()
192 $application = new Application();
193 $application->add(new \FooCommand());
194 $application->add(new \Foo1Command());
195 $this->assertEquals(array('foo'), $application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces');
198 public function testFindNamespace()
200 $application = new Application();
201 $application->add(new \FooCommand());
202 $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
203 $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
204 $application->add(new \Foo2Command());
205 $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
208 public function testFindNamespaceWithSubnamespaces()
210 $application = new Application();
211 $application->add(new \FooSubnamespaced1Command());
212 $application->add(new \FooSubnamespaced2Command());
213 $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns commands even if the commands are only contained in subnamespaces');
217 * @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
218 * @expectedExceptionMessage The namespace "f" is ambiguous (foo, foo1).
220 public function testFindAmbiguousNamespace()
222 $application = new Application();
223 $application->add(new \BarBucCommand());
224 $application->add(new \FooCommand());
225 $application->add(new \Foo2Command());
226 $application->findNamespace('f');
230 * @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
231 * @expectedExceptionMessage There are no commands defined in the "bar" namespace.
233 public function testFindInvalidNamespace()
235 $application = new Application();
236 $application->findNamespace('bar');
240 * @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
241 * @expectedExceptionMessage Command "foo1" is not defined
243 public function testFindUniqueNameButNamespaceName()
245 $application = new Application();
246 $application->add(new \FooCommand());
247 $application->add(new \Foo1Command());
248 $application->add(new \Foo2Command());
250 $application->find($commandName = 'foo1');
253 public function testFind()
255 $application = new Application();
256 $application->add(new \FooCommand());
258 $this->assertInstanceOf('FooCommand', $application->find('foo:bar'), '->find() returns a command if its name exists');
259 $this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $application->find('h'), '->find() returns a command if its name exists');
260 $this->assertInstanceOf('FooCommand', $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
261 $this->assertInstanceOf('FooCommand', $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
262 $this->assertInstanceOf('FooCommand', $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
266 * @dataProvider provideAmbiguousAbbreviations
268 public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExceptionMessage)
270 if (method_exists($this, 'expectException')) {
271 $this->expectException('Symfony\Component\Console\Exception\CommandNotFoundException');
272 $this->expectExceptionMessage($expectedExceptionMessage);
274 $this->setExpectedException('Symfony\Component\Console\Exception\CommandNotFoundException', $expectedExceptionMessage);
277 $application = new Application();
278 $application->add(new \FooCommand());
279 $application->add(new \Foo1Command());
280 $application->add(new \Foo2Command());
282 $application->find($abbreviation);
285 public function provideAmbiguousAbbreviations()
288 array('f', 'Command "f" is not defined.'),
289 array('a', 'Command "a" is ambiguous (afoobar, afoobar1 and 1 more).'),
290 array('foo:b', 'Command "foo:b" is ambiguous (foo:bar, foo:bar1 and 1 more).'),
294 public function testFindCommandEqualNamespace()
296 $application = new Application();
297 $application->add(new \Foo3Command());
298 $application->add(new \Foo4Command());
300 $this->assertInstanceOf('Foo3Command', $application->find('foo3:bar'), '->find() returns the good command even if a namespace has same name');
301 $this->assertInstanceOf('Foo4Command', $application->find('foo3:bar:toh'), '->find() returns a command even if its namespace equals another command name');
304 public function testFindCommandWithAmbiguousNamespacesButUniqueName()
306 $application = new Application();
307 $application->add(new \FooCommand());
308 $application->add(new \FoobarCommand());
310 $this->assertInstanceOf('FoobarCommand', $application->find('f:f'));
313 public function testFindCommandWithMissingNamespace()
315 $application = new Application();
316 $application->add(new \Foo4Command());
318 $this->assertInstanceOf('Foo4Command', $application->find('f::t'));
322 * @dataProvider provideInvalidCommandNamesSingle
323 * @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
324 * @expectedExceptionMessage Did you mean this
326 public function testFindAlternativeExceptionMessageSingle($name)
328 $application = new Application();
329 $application->add(new \Foo3Command());
330 $application->find($name);
333 public function provideInvalidCommandNamesSingle()
341 public function testFindAlternativeExceptionMessageMultiple()
343 $application = new Application();
344 $application->add(new \FooCommand());
345 $application->add(new \Foo1Command());
346 $application->add(new \Foo2Command());
350 $application->find('foo:baR');
351 $this->fail('->find() throws a CommandNotFoundException if command does not exist, with alternatives');
352 } catch (\Exception $e) {
353 $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
354 $this->assertRegExp('/Did you mean one of these/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
355 $this->assertRegExp('/foo1:bar/', $e->getMessage());
356 $this->assertRegExp('/foo:bar/', $e->getMessage());
359 // Namespace + plural
361 $application->find('foo2:bar');
362 $this->fail('->find() throws a CommandNotFoundException if command does not exist, with alternatives');
363 } catch (\Exception $e) {
364 $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
365 $this->assertRegExp('/Did you mean one of these/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
366 $this->assertRegExp('/foo1/', $e->getMessage());
369 $application->add(new \Foo3Command());
370 $application->add(new \Foo4Command());
372 // Subnamespace + plural
374 $a = $application->find('foo3:');
375 $this->fail('->find() should throw an Symfony\Component\Console\Exception\CommandNotFoundException if a command is ambiguous because of a subnamespace, with alternatives');
376 } catch (\Exception $e) {
377 $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e);
378 $this->assertRegExp('/foo3:bar/', $e->getMessage());
379 $this->assertRegExp('/foo3:bar:toh/', $e->getMessage());
383 public function testFindAlternativeCommands()
385 $application = new Application();
387 $application->add(new \FooCommand());
388 $application->add(new \Foo1Command());
389 $application->add(new \Foo2Command());
392 $application->find($commandName = 'Unknown command');
393 $this->fail('->find() throws a CommandNotFoundException if command does not exist');
394 } catch (\Exception $e) {
395 $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist');
396 $this->assertSame(array(), $e->getAlternatives());
397 $this->assertEquals(sprintf('Command "%s" is not defined.', $commandName), $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, without alternatives');
400 // Test if "bar1" command throw a "CommandNotFoundException" and does not contain
401 // "foo:bar" as alternative because "bar1" is too far from "foo:bar"
403 $application->find($commandName = 'bar1');
404 $this->fail('->find() throws a CommandNotFoundException if command does not exist');
405 } catch (\Exception $e) {
406 $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist');
407 $this->assertSame(array('afoobar1', 'foo:bar1'), $e->getAlternatives());
408 $this->assertRegExp(sprintf('/Command "%s" is not defined./', $commandName), $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
409 $this->assertRegExp('/afoobar1/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternative : "afoobar1"');
410 $this->assertRegExp('/foo:bar1/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternative : "foo:bar1"');
411 $this->assertNotRegExp('/foo:bar(?>!1)/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, without "foo:bar" alternative');
415 public function testFindAlternativeCommandsWithAnAlias()
417 $fooCommand = new \FooCommand();
418 $fooCommand->setAliases(array('foo2'));
420 $application = new Application();
421 $application->add($fooCommand);
423 $result = $application->find('foo');
425 $this->assertSame($fooCommand, $result);
428 public function testFindAlternativeNamespace()
430 $application = new Application();
432 $application->add(new \FooCommand());
433 $application->add(new \Foo1Command());
434 $application->add(new \Foo2Command());
435 $application->add(new \Foo3Command());
438 $application->find('Unknown-namespace:Unknown-command');
439 $this->fail('->find() throws a CommandNotFoundException if namespace does not exist');
440 } catch (\Exception $e) {
441 $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if namespace does not exist');
442 $this->assertSame(array(), $e->getAlternatives());
443 $this->assertEquals('There are no commands defined in the "Unknown-namespace" namespace.', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, without alternatives');
447 $application->find('foo2:command');
448 $this->fail('->find() throws a CommandNotFoundException if namespace does not exist');
449 } catch (\Exception $e) {
450 $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if namespace does not exist');
451 $this->assertCount(3, $e->getAlternatives());
452 $this->assertContains('foo', $e->getAlternatives());
453 $this->assertContains('foo1', $e->getAlternatives());
454 $this->assertContains('foo3', $e->getAlternatives());
455 $this->assertRegExp('/There are no commands defined in the "foo2" namespace./', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative');
456 $this->assertRegExp('/foo/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo"');
457 $this->assertRegExp('/foo1/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo1"');
458 $this->assertRegExp('/foo3/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo3"');
462 public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces()
464 $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getNamespaces'))->getMock();
465 $application->expects($this->once())
466 ->method('getNamespaces')
467 ->will($this->returnValue(array('foo:sublong', 'bar:sub')));
469 $this->assertEquals('foo:sublong', $application->findNamespace('f:sub'));
473 * @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
474 * @expectedExceptionMessage Command "foo::bar" is not defined.
476 public function testFindWithDoubleColonInNameThrowsException()
478 $application = new Application();
479 $application->add(new \FooCommand());
480 $application->add(new \Foo4Command());
481 $application->find('foo::bar');
484 public function testSetCatchExceptions()
486 $application = new Application();
487 $application->setAutoExit(false);
488 putenv('COLUMNS=120');
489 $tester = new ApplicationTester($application);
491 $application->setCatchExceptions(true);
492 $this->assertTrue($application->areExceptionsCaught());
494 $tester->run(array('command' => 'foo'), array('decorated' => false));
495 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getDisplay(true), '->setCatchExceptions() sets the catch exception flag');
497 $tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
498 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getErrorOutput(true), '->setCatchExceptions() sets the catch exception flag');
499 $this->assertSame('', $tester->getDisplay(true));
501 $application->setCatchExceptions(false);
503 $tester->run(array('command' => 'foo'), array('decorated' => false));
504 $this->fail('->setCatchExceptions() sets the catch exception flag');
505 } catch (\Exception $e) {
506 $this->assertInstanceOf('\Exception', $e, '->setCatchExceptions() sets the catch exception flag');
507 $this->assertEquals('Command "foo" is not defined.', $e->getMessage(), '->setCatchExceptions() sets the catch exception flag');
511 public function testAutoExitSetting()
513 $application = new Application();
514 $this->assertTrue($application->isAutoExitEnabled());
516 $application->setAutoExit(false);
517 $this->assertFalse($application->isAutoExitEnabled());
520 public function testRenderException()
522 $application = new Application();
523 $application->setAutoExit(false);
524 putenv('COLUMNS=120');
525 $tester = new ApplicationTester($application);
527 $tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
528 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exception');
530 $tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE, 'capture_stderr_separately' => true));
531 $this->assertContains('Exception trace', $tester->getErrorOutput(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
533 $tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false, 'capture_stderr_separately' => true));
534 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $tester->getErrorOutput(true), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
536 $application->add(new \Foo3Command());
537 $tester = new ApplicationTester($application);
538 $tester->run(array('command' => 'foo3:bar'), array('decorated' => false, 'capture_stderr_separately' => true));
539 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
541 $tester->run(array('command' => 'foo3:bar'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
542 $this->assertRegExp('/\[Exception\]\s*First exception/', $tester->getDisplay(), '->renderException() renders a pretty exception without code exception when code exception is default and verbosity is verbose');
543 $this->assertRegExp('/\[Exception\]\s*Second exception/', $tester->getDisplay(), '->renderException() renders a pretty exception without code exception when code exception is 0 and verbosity is verbose');
544 $this->assertRegExp('/\[Exception \(404\)\]\s*Third exception/', $tester->getDisplay(), '->renderException() renders a pretty exception with code exception when code exception is 404 and verbosity is verbose');
546 $tester->run(array('command' => 'foo3:bar'), array('decorated' => true));
547 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');
549 $tester->run(array('command' => 'foo3:bar'), array('decorated' => true, 'capture_stderr_separately' => true));
550 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
552 $application = new Application();
553 $application->setAutoExit(false);
554 putenv('COLUMNS=32');
555 $tester = new ApplicationTester($application);
557 $tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
558 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $tester->getErrorOutput(true), '->renderException() wraps messages when they are bigger than the terminal');
559 putenv('COLUMNS=120');
562 public function testRenderExceptionWithDoubleWidthCharacters()
564 $application = new Application();
565 $application->setAutoExit(false);
566 putenv('COLUMNS=120');
567 $application->register('foo')->setCode(function () {
568 throw new \Exception('エラーメッセージ');
570 $tester = new ApplicationTester($application);
572 $tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
573 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
575 $tester->run(array('command' => 'foo'), array('decorated' => true, 'capture_stderr_separately' => true));
576 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1decorated.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
578 $application = new Application();
579 $application->setAutoExit(false);
580 putenv('COLUMNS=32');
581 $application->register('foo')->setCode(function () {
582 throw new \Exception('コマンドの実行中にエラーが発生しました。');
584 $tester = new ApplicationTester($application);
585 $tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
586 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt', $tester->getErrorOutput(true), '->renderException() wraps messages when they are bigger than the terminal');
587 putenv('COLUMNS=120');
590 public function testRenderExceptionEscapesLines()
592 $application = new Application();
593 $application->setAutoExit(false);
594 putenv('COLUMNS=22');
595 $application->register('foo')->setCode(function () {
596 throw new \Exception('dont break here <info>!</info>');
598 $tester = new ApplicationTester($application);
600 $tester->run(array('command' => 'foo'), array('decorated' => false));
601 $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_escapeslines.txt', $tester->getDisplay(true), '->renderException() escapes lines containing formatting');
602 putenv('COLUMNS=120');
605 public function testRun()
607 $application = new Application();
608 $application->setAutoExit(false);
609 $application->setCatchExceptions(false);
610 $application->add($command = new \Foo1Command());
611 $_SERVER['argv'] = array('cli.php', 'foo:bar1');
617 $this->assertInstanceOf('Symfony\Component\Console\Input\ArgvInput', $command->input, '->run() creates an ArgvInput by default if none is given');
618 $this->assertInstanceOf('Symfony\Component\Console\Output\ConsoleOutput', $command->output, '->run() creates a ConsoleOutput by default if none is given');
620 $application = new Application();
621 $application->setAutoExit(false);
622 $application->setCatchExceptions(false);
624 $this->ensureStaticCommandHelp($application);
625 $tester = new ApplicationTester($application);
627 $tester->run(array(), array('decorated' => false));
628 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $tester->getDisplay(true), '->run() runs the list command if no argument is passed');
630 $tester->run(array('--help' => true), array('decorated' => false));
631 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $tester->getDisplay(true), '->run() runs the help command if --help is passed');
633 $tester->run(array('-h' => true), array('decorated' => false));
634 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $tester->getDisplay(true), '->run() runs the help command if -h is passed');
636 $tester->run(array('command' => 'list', '--help' => true), array('decorated' => false));
637 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $tester->getDisplay(true), '->run() displays the help if --help is passed');
639 $tester->run(array('command' => 'list', '-h' => true), array('decorated' => false));
640 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $tester->getDisplay(true), '->run() displays the help if -h is passed');
642 $tester->run(array('--ansi' => true));
643 $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --ansi is passed');
645 $tester->run(array('--no-ansi' => true));
646 $this->assertFalse($tester->getOutput()->isDecorated(), '->run() forces color output to be disabled if --no-ansi is passed');
648 $tester->run(array('--version' => true), array('decorated' => false));
649 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $tester->getDisplay(true), '->run() displays the program version if --version is passed');
651 $tester->run(array('-V' => true), array('decorated' => false));
652 $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $tester->getDisplay(true), '->run() displays the program version if -v is passed');
654 $tester->run(array('command' => 'list', '--quiet' => true));
655 $this->assertSame('', $tester->getDisplay(), '->run() removes all output if --quiet is passed');
656 $this->assertFalse($tester->getInput()->isInteractive(), '->run() sets off the interactive mode if --quiet is passed');
658 $tester->run(array('command' => 'list', '-q' => true));
659 $this->assertSame('', $tester->getDisplay(), '->run() removes all output if -q is passed');
660 $this->assertFalse($tester->getInput()->isInteractive(), '->run() sets off the interactive mode if -q is passed');
662 $tester->run(array('command' => 'list', '--verbose' => true));
663 $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose is passed');
665 $tester->run(array('command' => 'list', '--verbose' => 1));
666 $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose=1 is passed');
668 $tester->run(array('command' => 'list', '--verbose' => 2));
669 $this->assertSame(Output::VERBOSITY_VERY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to very verbose if --verbose=2 is passed');
671 $tester->run(array('command' => 'list', '--verbose' => 3));
672 $this->assertSame(Output::VERBOSITY_DEBUG, $tester->getOutput()->getVerbosity(), '->run() sets the output to debug if --verbose=3 is passed');
674 $tester->run(array('command' => 'list', '--verbose' => 4));
675 $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if unknown --verbose level is passed');
677 $tester->run(array('command' => 'list', '-v' => true));
678 $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
680 $tester->run(array('command' => 'list', '-vv' => true));
681 $this->assertSame(Output::VERBOSITY_VERY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
683 $tester->run(array('command' => 'list', '-vvv' => true));
684 $this->assertSame(Output::VERBOSITY_DEBUG, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
686 $application = new Application();
687 $application->setAutoExit(false);
688 $application->setCatchExceptions(false);
689 $application->add(new \FooCommand());
690 $tester = new ApplicationTester($application);
692 $tester->run(array('command' => 'foo:bar', '--no-interaction' => true), array('decorated' => false));
693 $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if --no-interaction is passed');
695 $tester->run(array('command' => 'foo:bar', '-n' => true), array('decorated' => false));
696 $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
702 * If the "verbose" option is just before an argument in ArgvInput,
703 * an argument value should not be treated as verbosity value.
704 * This test will fail with "Not enough arguments." if broken
706 public function testVerboseValueNotBreakArguments()
708 $application = new Application();
709 $application->setAutoExit(false);
710 $application->setCatchExceptions(false);
711 $application->add(new \FooCommand());
713 $output = new StreamOutput(fopen('php://memory', 'w', false));
715 $input = new ArgvInput(array('cli.php', '-v', 'foo:bar'));
716 $application->run($input, $output);
718 $this->addToAssertionCount(1);
720 $input = new ArgvInput(array('cli.php', '--verbose', 'foo:bar'));
721 $application->run($input, $output);
723 $this->addToAssertionCount(1);
726 public function testRunReturnsIntegerExitCode()
728 $exception = new \Exception('', 4);
730 $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('doRun'))->getMock();
731 $application->setAutoExit(false);
732 $application->expects($this->once())
734 ->will($this->throwException($exception));
736 $exitCode = $application->run(new ArrayInput(array()), new NullOutput());
738 $this->assertSame(4, $exitCode, '->run() returns integer exit code extracted from raised exception');
741 public function testRunReturnsExitCodeOneForExceptionCodeZero()
743 $exception = new \Exception('', 0);
745 $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('doRun'))->getMock();
746 $application->setAutoExit(false);
747 $application->expects($this->once())
749 ->will($this->throwException($exception));
751 $exitCode = $application->run(new ArrayInput(array()), new NullOutput());
753 $this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is 0');
757 * @expectedException \LogicException
758 * @expectedExceptionMessage An option with shortcut "e" already exists.
760 public function testAddingOptionWithDuplicateShortcut()
762 $dispatcher = new EventDispatcher();
763 $application = new Application();
764 $application->setAutoExit(false);
765 $application->setCatchExceptions(false);
766 $application->setDispatcher($dispatcher);
768 $application->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'Environment'));
772 ->setAliases(array('f'))
773 ->setDefinition(array(new InputOption('survey', 'e', InputOption::VALUE_REQUIRED, 'My option with a shortcut.')))
774 ->setCode(function (InputInterface $input, OutputInterface $output) {})
777 $input = new ArrayInput(array('command' => 'foo'));
778 $output = new NullOutput();
780 $application->run($input, $output);
784 * @expectedException \LogicException
785 * @dataProvider getAddingAlreadySetDefinitionElementData
787 public function testAddingAlreadySetDefinitionElementData($def)
789 $application = new Application();
790 $application->setAutoExit(false);
791 $application->setCatchExceptions(false);
794 ->setDefinition(array($def))
795 ->setCode(function (InputInterface $input, OutputInterface $output) {})
798 $input = new ArrayInput(array('command' => 'foo'));
799 $output = new NullOutput();
800 $application->run($input, $output);
803 public function getAddingAlreadySetDefinitionElementData()
806 array(new InputArgument('command', InputArgument::REQUIRED)),
807 array(new InputOption('quiet', '', InputOption::VALUE_NONE)),
808 array(new InputOption('query', 'q', InputOption::VALUE_NONE)),
812 public function testGetDefaultHelperSetReturnsDefaultValues()
814 $application = new Application();
815 $application->setAutoExit(false);
816 $application->setCatchExceptions(false);
818 $helperSet = $application->getHelperSet();
820 $this->assertTrue($helperSet->has('formatter'));
823 public function testAddingSingleHelperSetOverwritesDefaultValues()
825 $application = new Application();
826 $application->setAutoExit(false);
827 $application->setCatchExceptions(false);
829 $application->setHelperSet(new HelperSet(array(new FormatterHelper())));
831 $helperSet = $application->getHelperSet();
833 $this->assertTrue($helperSet->has('formatter'));
835 // no other default helper set should be returned
836 $this->assertFalse($helperSet->has('dialog'));
837 $this->assertFalse($helperSet->has('progress'));
840 public function testOverwritingDefaultHelperSetOverwritesDefaultValues()
842 $application = new CustomApplication();
843 $application->setAutoExit(false);
844 $application->setCatchExceptions(false);
846 $application->setHelperSet(new HelperSet(array(new FormatterHelper())));
848 $helperSet = $application->getHelperSet();
850 $this->assertTrue($helperSet->has('formatter'));
852 // no other default helper set should be returned
853 $this->assertFalse($helperSet->has('dialog'));
854 $this->assertFalse($helperSet->has('progress'));
857 public function testGetDefaultInputDefinitionReturnsDefaultValues()
859 $application = new Application();
860 $application->setAutoExit(false);
861 $application->setCatchExceptions(false);
863 $inputDefinition = $application->getDefinition();
865 $this->assertTrue($inputDefinition->hasArgument('command'));
867 $this->assertTrue($inputDefinition->hasOption('help'));
868 $this->assertTrue($inputDefinition->hasOption('quiet'));
869 $this->assertTrue($inputDefinition->hasOption('verbose'));
870 $this->assertTrue($inputDefinition->hasOption('version'));
871 $this->assertTrue($inputDefinition->hasOption('ansi'));
872 $this->assertTrue($inputDefinition->hasOption('no-ansi'));
873 $this->assertTrue($inputDefinition->hasOption('no-interaction'));
876 public function testOverwritingDefaultInputDefinitionOverwritesDefaultValues()
878 $application = new CustomApplication();
879 $application->setAutoExit(false);
880 $application->setCatchExceptions(false);
882 $inputDefinition = $application->getDefinition();
884 // check whether the default arguments and options are not returned any more
885 $this->assertFalse($inputDefinition->hasArgument('command'));
887 $this->assertFalse($inputDefinition->hasOption('help'));
888 $this->assertFalse($inputDefinition->hasOption('quiet'));
889 $this->assertFalse($inputDefinition->hasOption('verbose'));
890 $this->assertFalse($inputDefinition->hasOption('version'));
891 $this->assertFalse($inputDefinition->hasOption('ansi'));
892 $this->assertFalse($inputDefinition->hasOption('no-ansi'));
893 $this->assertFalse($inputDefinition->hasOption('no-interaction'));
895 $this->assertTrue($inputDefinition->hasOption('custom'));
898 public function testSettingCustomInputDefinitionOverwritesDefaultValues()
900 $application = new Application();
901 $application->setAutoExit(false);
902 $application->setCatchExceptions(false);
904 $application->setDefinition(new InputDefinition(array(new InputOption('--custom', '-c', InputOption::VALUE_NONE, 'Set the custom input definition.'))));
906 $inputDefinition = $application->getDefinition();
908 // check whether the default arguments and options are not returned any more
909 $this->assertFalse($inputDefinition->hasArgument('command'));
911 $this->assertFalse($inputDefinition->hasOption('help'));
912 $this->assertFalse($inputDefinition->hasOption('quiet'));
913 $this->assertFalse($inputDefinition->hasOption('verbose'));
914 $this->assertFalse($inputDefinition->hasOption('version'));
915 $this->assertFalse($inputDefinition->hasOption('ansi'));
916 $this->assertFalse($inputDefinition->hasOption('no-ansi'));
917 $this->assertFalse($inputDefinition->hasOption('no-interaction'));
919 $this->assertTrue($inputDefinition->hasOption('custom'));
922 public function testRunWithDispatcher()
924 $application = new Application();
925 $application->setAutoExit(false);
926 $application->setDispatcher($this->getDispatcher());
928 $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
929 $output->write('foo.');
932 $tester = new ApplicationTester($application);
933 $tester->run(array('command' => 'foo'));
934 $this->assertEquals('before.foo.after.'.PHP_EOL, $tester->getDisplay());
938 * @expectedException \LogicException
939 * @expectedExceptionMessage caught
941 public function testRunWithExceptionAndDispatcher()
943 $application = new Application();
944 $application->setDispatcher($this->getDispatcher());
945 $application->setAutoExit(false);
946 $application->setCatchExceptions(false);
948 $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
949 throw new \RuntimeException('foo');
952 $tester = new ApplicationTester($application);
953 $tester->run(array('command' => 'foo'));
956 public function testRunDispatchesAllEventsWithException()
958 $application = new Application();
959 $application->setDispatcher($this->getDispatcher());
960 $application->setAutoExit(false);
962 $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
963 $output->write('foo.');
965 throw new \RuntimeException('foo');
968 $tester = new ApplicationTester($application);
969 $tester->run(array('command' => 'foo'));
970 $this->assertContains('before.foo.caught.after.', $tester->getDisplay());
973 public function testRunDispatchesAllEventsWithExceptionInListener()
975 $dispatcher = $this->getDispatcher();
976 $dispatcher->addListener('console.command', function () {
977 throw new \RuntimeException('foo');
980 $application = new Application();
981 $application->setDispatcher($dispatcher);
982 $application->setAutoExit(false);
984 $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
985 $output->write('foo.');
988 $tester = new ApplicationTester($application);
989 $tester->run(array('command' => 'foo'));
990 $this->assertContains('before.caught.after.', $tester->getDisplay());
993 public function testRunWithError()
995 $application = new Application();
996 $application->setAutoExit(false);
997 $application->setCatchExceptions(false);
999 $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
1000 $output->write('dym.');
1002 throw new \Error('dymerr');
1005 $tester = new ApplicationTester($application);
1008 $tester->run(array('command' => 'dym'));
1009 $this->fail('Error expected.');
1010 } catch (\Error $e) {
1011 $this->assertSame('dymerr', $e->getMessage());
1016 * @expectedException \LogicException
1017 * @expectedExceptionMessage caught
1019 public function testRunWithErrorAndDispatcher()
1021 $application = new Application();
1022 $application->setDispatcher($this->getDispatcher());
1023 $application->setAutoExit(false);
1024 $application->setCatchExceptions(false);
1026 $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
1027 $output->write('dym.');
1029 throw new \Error('dymerr');
1032 $tester = new ApplicationTester($application);
1033 $tester->run(array('command' => 'dym'));
1034 $this->assertContains('before.dym.caught.after.', $tester->getDisplay(), 'The PHP Error did not dispached events');
1037 public function testRunDispatchesAllEventsWithError()
1039 $application = new Application();
1040 $application->setDispatcher($this->getDispatcher());
1041 $application->setAutoExit(false);
1043 $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
1044 $output->write('dym.');
1046 throw new \Error('dymerr');
1049 $tester = new ApplicationTester($application);
1050 $tester->run(array('command' => 'dym'));
1051 $this->assertContains('before.dym.caught.after.', $tester->getDisplay(), 'The PHP Error did not dispached events');
1054 public function testRunWithErrorFailingStatusCode()
1056 $application = new Application();
1057 $application->setDispatcher($this->getDispatcher());
1058 $application->setAutoExit(false);
1060 $application->register('dus')->setCode(function (InputInterface $input, OutputInterface $output) {
1061 $output->write('dus.');
1063 throw new \Error('duserr');
1066 $tester = new ApplicationTester($application);
1067 $tester->run(array('command' => 'dus'));
1068 $this->assertSame(1, $tester->getStatusCode(), 'Status code should be 1');
1071 public function testRunWithDispatcherSkippingCommand()
1073 $application = new Application();
1074 $application->setDispatcher($this->getDispatcher(true));
1075 $application->setAutoExit(false);
1077 $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
1078 $output->write('foo.');
1081 $tester = new ApplicationTester($application);
1082 $exitCode = $tester->run(array('command' => 'foo'));
1083 $this->assertContains('before.after.', $tester->getDisplay());
1084 $this->assertEquals(ConsoleCommandEvent::RETURN_CODE_DISABLED, $exitCode);
1087 public function testRunWithDispatcherAccessingInputOptions()
1089 $noInteractionValue = null;
1092 $dispatcher = $this->getDispatcher();
1093 $dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use (&$noInteractionValue, &$quietValue) {
1094 $input = $event->getInput();
1096 $noInteractionValue = $input->getOption('no-interaction');
1097 $quietValue = $input->getOption('quiet');
1100 $application = new Application();
1101 $application->setDispatcher($dispatcher);
1102 $application->setAutoExit(false);
1104 $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
1105 $output->write('foo.');
1108 $tester = new ApplicationTester($application);
1109 $tester->run(array('command' => 'foo', '--no-interaction' => true));
1111 $this->assertTrue($noInteractionValue);
1112 $this->assertFalse($quietValue);
1115 public function testRunWithDispatcherAddingInputOptions()
1119 $dispatcher = $this->getDispatcher();
1120 $dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use (&$extraValue) {
1121 $definition = $event->getCommand()->getDefinition();
1122 $input = $event->getInput();
1124 $definition->addOption(new InputOption('extra', null, InputOption::VALUE_REQUIRED));
1125 $input->bind($definition);
1127 $extraValue = $input->getOption('extra');
1130 $application = new Application();
1131 $application->setDispatcher($dispatcher);
1132 $application->setAutoExit(false);
1134 $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
1135 $output->write('foo.');
1138 $tester = new ApplicationTester($application);
1139 $tester->run(array('command' => 'foo', '--extra' => 'some test value'));
1141 $this->assertEquals('some test value', $extraValue);
1147 public function testTerminalDimensions()
1149 $application = new Application();
1150 $originalDimensions = $application->getTerminalDimensions();
1151 $this->assertCount(2, $originalDimensions);
1154 if ($originalDimensions[0] == $width) {
1158 $application->setTerminalDimensions($width, 80);
1159 $this->assertSame(array($width, 80), $application->getTerminalDimensions());
1162 public function testSetRunCustomDefaultCommand()
1164 $command = new \FooCommand();
1166 $application = new Application();
1167 $application->setAutoExit(false);
1168 $application->add($command);
1169 $application->setDefaultCommand($command->getName());
1171 $tester = new ApplicationTester($application);
1172 $tester->run(array(), array('interactive' => false));
1173 $this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1175 $application = new CustomDefaultCommandApplication();
1176 $application->setAutoExit(false);
1178 $tester = new ApplicationTester($application);
1179 $tester->run(array(), array('interactive' => false));
1181 $this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1184 public function testSetRunCustomDefaultCommandWithOption()
1186 $command = new \FooOptCommand();
1188 $application = new Application();
1189 $application->setAutoExit(false);
1190 $application->add($command);
1191 $application->setDefaultCommand($command->getName());
1193 $tester = new ApplicationTester($application);
1194 $tester->run(array('--fooopt' => 'opt'), array('interactive' => false));
1196 $this->assertEquals('called'.PHP_EOL.'opt'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1199 public function testSetRunCustomSingleCommand()
1201 $command = new \FooCommand();
1203 $application = new Application();
1204 $application->setAutoExit(false);
1205 $application->add($command);
1206 $application->setDefaultCommand($command->getName(), true);
1208 $tester = new ApplicationTester($application);
1210 $tester->run(array());
1211 $this->assertContains('called', $tester->getDisplay());
1213 $tester->run(array('--help' => true));
1214 $this->assertContains('The foo:bar command', $tester->getDisplay());
1218 * @requires function posix_isatty
1220 public function testCanCheckIfTerminalIsInteractive()
1222 $application = new CustomDefaultCommandApplication();
1223 $application->setAutoExit(false);
1225 $tester = new ApplicationTester($application);
1226 $tester->run(array('command' => 'help'));
1228 $this->assertFalse($tester->getInput()->hasParameterOption(array('--no-interaction', '-n')));
1230 $inputStream = $tester->getInput()->getStream();
1231 $this->assertEquals($tester->getInput()->isInteractive(), @posix_isatty($inputStream));
1234 protected function getDispatcher($skipCommand = false)
1236 $dispatcher = new EventDispatcher();
1237 $dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use ($skipCommand) {
1238 $event->getOutput()->write('before.');
1241 $event->disableCommand();
1244 $dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) use ($skipCommand) {
1245 $event->getOutput()->writeln('after.');
1247 if (!$skipCommand) {
1248 $event->setExitCode(ConsoleCommandEvent::RETURN_CODE_DISABLED);
1251 $dispatcher->addListener('console.exception', function (ConsoleExceptionEvent $event) {
1252 $event->getOutput()->write('caught.');
1254 $event->setException(new \LogicException('caught.', $event->getExitCode(), $event->getException()));
1263 public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEnabled()
1265 $application = new Application();
1266 $application->setAutoExit(false);
1267 $application->setDispatcher(new EventDispatcher());
1269 $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
1270 new \UnknownClass();
1273 $tester = new ApplicationTester($application);
1276 $tester->run(array('command' => 'dym'));
1277 $this->fail('->run() should rethrow PHP errors if not handled via ConsoleErrorEvent.');
1278 } catch (\Error $e) {
1279 $this->assertSame($e->getMessage(), 'Class \'UnknownClass\' not found');
1284 class CustomApplication extends Application
1287 * Overwrites the default input definition.
1289 * @return InputDefinition An InputDefinition instance
1291 protected function getDefaultInputDefinition()
1293 return new InputDefinition(array(new InputOption('--custom', '-c', InputOption::VALUE_NONE, 'Set the custom input definition.')));
1297 * Gets the default helper set with the helpers that should always be available.
1299 * @return HelperSet A HelperSet instance
1301 protected function getDefaultHelperSet()
1303 return new HelperSet(array(new FormatterHelper()));
1307 class CustomDefaultCommandApplication extends Application
1310 * Overwrites the constructor in order to set a different default command.
1312 public function __construct()
1314 parent::__construct();
1316 $command = new \FooCommand();
1317 $this->add($command);
1318 $this->setDefaultCommand($command->getName());