123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386 |
- <?php
- namespace Symfony\Component\Console\Tests;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Console\Application;
- use Symfony\Component\Console\Event\ConsoleCommandEvent;
- use Symfony\Component\Console\Event\ConsoleExceptionEvent;
- use Symfony\Component\Console\Event\ConsoleTerminateEvent;
- use Symfony\Component\Console\Helper\FormatterHelper;
- use Symfony\Component\Console\Helper\HelperSet;
- use Symfony\Component\Console\Input\ArgvInput;
- use Symfony\Component\Console\Input\ArrayInput;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputDefinition;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\NullOutput;
- use Symfony\Component\Console\Output\Output;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Output\StreamOutput;
- use Symfony\Component\Console\Tester\ApplicationTester;
- use Symfony\Component\EventDispatcher\EventDispatcher;
- class ApplicationTest extends TestCase
- {
- protected static $fixturesPath;
- public static function setUpBeforeClass()
- {
- self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
- require_once self::$fixturesPath.'/FooCommand.php';
- require_once self::$fixturesPath.'/FooOptCommand.php';
- require_once self::$fixturesPath.'/Foo1Command.php';
- require_once self::$fixturesPath.'/Foo2Command.php';
- require_once self::$fixturesPath.'/Foo3Command.php';
- require_once self::$fixturesPath.'/Foo4Command.php';
- require_once self::$fixturesPath.'/Foo5Command.php';
- require_once self::$fixturesPath.'/FoobarCommand.php';
- require_once self::$fixturesPath.'/BarBucCommand.php';
- require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
- require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
- require_once self::$fixturesPath.'/TestTiti.php';
- require_once self::$fixturesPath.'/TestToto.php';
- }
- protected function normalizeLineBreaks($text)
- {
- return str_replace(PHP_EOL, "\n", $text);
- }
-
- protected function ensureStaticCommandHelp(Application $application)
- {
- foreach ($application->all() as $command) {
- $command->setHelp(str_replace('%command.full_name%', 'app/console %command.name%', $command->getHelp()));
- }
- }
- public function testConstructor()
- {
- $application = new Application('foo', 'bar');
- $this->assertEquals('foo', $application->getName(), '__construct() takes the application name as its first argument');
- $this->assertEquals('bar', $application->getVersion(), '__construct() takes the application version as its second argument');
- $this->assertEquals(array('help', 'list'), array_keys($application->all()), '__construct() registered the help and list commands by default');
- }
- public function testSetGetName()
- {
- $application = new Application();
- $application->setName('foo');
- $this->assertEquals('foo', $application->getName(), '->setName() sets the name of the application');
- }
- public function testSetGetVersion()
- {
- $application = new Application();
- $application->setVersion('bar');
- $this->assertEquals('bar', $application->getVersion(), '->setVersion() sets the version of the application');
- }
- public function testGetLongVersion()
- {
- $application = new Application('foo', 'bar');
- $this->assertEquals('<info>foo</info> version <comment>bar</comment>', $application->getLongVersion(), '->getLongVersion() returns the long version of the application');
- }
- public function testHelp()
- {
- $application = new Application();
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_gethelp.txt', $this->normalizeLineBreaks($application->getHelp()), '->getHelp() returns a help message');
- }
- public function testAll()
- {
- $application = new Application();
- $commands = $application->all();
- $this->assertInstanceOf('Symfony\\Component\\Console\\Command\\HelpCommand', $commands['help'], '->all() returns the registered commands');
- $application->add(new \FooCommand());
- $commands = $application->all('foo');
- $this->assertCount(1, $commands, '->all() takes a namespace as its first argument');
- }
- public function testRegister()
- {
- $application = new Application();
- $command = $application->register('foo');
- $this->assertEquals('foo', $command->getName(), '->register() registers a new command');
- }
- public function testAdd()
- {
- $application = new Application();
- $application->add($foo = new \FooCommand());
- $commands = $application->all();
- $this->assertEquals($foo, $commands['foo:bar'], '->add() registers a command');
- $application = new Application();
- $application->addCommands(array($foo = new \FooCommand(), $foo1 = new \Foo1Command()));
- $commands = $application->all();
- $this->assertEquals(array($foo, $foo1), array($commands['foo:bar'], $commands['foo:bar1']), '->addCommands() registers an array of commands');
- }
-
- public function testAddCommandWithEmptyConstructor()
- {
- $application = new Application();
- $application->add(new \Foo5Command());
- }
- public function testHasGet()
- {
- $application = new Application();
- $this->assertTrue($application->has('list'), '->has() returns true if a named command is registered');
- $this->assertFalse($application->has('afoobar'), '->has() returns false if a named command is not registered');
- $application->add($foo = new \FooCommand());
- $this->assertTrue($application->has('afoobar'), '->has() returns true if an alias is registered');
- $this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
- $this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
- $application = new Application();
- $application->add($foo = new \FooCommand());
-
- $r = new \ReflectionObject($application);
- $p = $r->getProperty('wantHelps');
- $p->setAccessible(true);
- $p->setValue($application, true);
- $command = $application->get('foo:bar');
- $this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $command, '->get() returns the help command if --help is provided as the input');
- }
- public function testSilentHelp()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $tester = new ApplicationTester($application);
- $tester->run(array('-h' => true, '-q' => true), array('decorated' => false));
- $this->assertEmpty($tester->getDisplay(true));
- }
-
- public function testGetInvalidCommand()
- {
- $application = new Application();
- $application->get('foofoo');
- }
- public function testGetNamespaces()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $application->add(new \Foo1Command());
- $this->assertEquals(array('foo'), $application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces');
- }
- public function testFindNamespace()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
- $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
- $application->add(new \Foo2Command());
- $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
- }
- public function testFindNamespaceWithSubnamespaces()
- {
- $application = new Application();
- $application->add(new \FooSubnamespaced1Command());
- $application->add(new \FooSubnamespaced2Command());
- $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns commands even if the commands are only contained in subnamespaces');
- }
-
- public function testFindAmbiguousNamespace()
- {
- $application = new Application();
- $application->add(new \BarBucCommand());
- $application->add(new \FooCommand());
- $application->add(new \Foo2Command());
- $application->findNamespace('f');
- }
- public function testFindNonAmbiguous()
- {
- $application = new Application();
- $application->add(new \TestTiti());
- $application->add(new \TestToto());
- $this->assertEquals('test-toto', $application->find('test')->getName());
- }
-
- public function testFindInvalidNamespace()
- {
- $application = new Application();
- $application->findNamespace('bar');
- }
-
- public function testFindUniqueNameButNamespaceName()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $application->add(new \Foo1Command());
- $application->add(new \Foo2Command());
- $application->find($commandName = 'foo1');
- }
- public function testFind()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $this->assertInstanceOf('FooCommand', $application->find('foo:bar'), '->find() returns a command if its name exists');
- $this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $application->find('h'), '->find() returns a command if its name exists');
- $this->assertInstanceOf('FooCommand', $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
- $this->assertInstanceOf('FooCommand', $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
- $this->assertInstanceOf('FooCommand', $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
- }
-
- public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExceptionMessage)
- {
- if (method_exists($this, 'expectException')) {
- $this->expectException('Symfony\Component\Console\Exception\CommandNotFoundException');
- $this->expectExceptionMessage($expectedExceptionMessage);
- } else {
- $this->setExpectedException('Symfony\Component\Console\Exception\CommandNotFoundException', $expectedExceptionMessage);
- }
- $application = new Application();
- $application->add(new \FooCommand());
- $application->add(new \Foo1Command());
- $application->add(new \Foo2Command());
- $application->find($abbreviation);
- }
- public function provideAmbiguousAbbreviations()
- {
- return array(
- array('f', 'Command "f" is not defined.'),
- array('a', 'Command "a" is ambiguous (afoobar, afoobar1 and 1 more).'),
- array('foo:b', 'Command "foo:b" is ambiguous (foo:bar, foo:bar1 and 1 more).'),
- );
- }
- public function testFindCommandEqualNamespace()
- {
- $application = new Application();
- $application->add(new \Foo3Command());
- $application->add(new \Foo4Command());
- $this->assertInstanceOf('Foo3Command', $application->find('foo3:bar'), '->find() returns the good command even if a namespace has same name');
- $this->assertInstanceOf('Foo4Command', $application->find('foo3:bar:toh'), '->find() returns a command even if its namespace equals another command name');
- }
- public function testFindCommandWithAmbiguousNamespacesButUniqueName()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $application->add(new \FoobarCommand());
- $this->assertInstanceOf('FoobarCommand', $application->find('f:f'));
- }
- public function testFindCommandWithMissingNamespace()
- {
- $application = new Application();
- $application->add(new \Foo4Command());
- $this->assertInstanceOf('Foo4Command', $application->find('f::t'));
- }
-
- public function testFindAlternativeExceptionMessageSingle($name)
- {
- $application = new Application();
- $application->add(new \Foo3Command());
- $application->find($name);
- }
- public function provideInvalidCommandNamesSingle()
- {
- return array(
- array('foo3:baR'),
- array('foO3:bar'),
- );
- }
- public function testFindAlternativeExceptionMessageMultiple()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $application->add(new \Foo1Command());
- $application->add(new \Foo2Command());
-
- try {
- $application->find('foo:baR');
- $this->fail('->find() throws a CommandNotFoundException if command does not exist, with alternatives');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
- $this->assertRegExp('/Did you mean one of these/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
- $this->assertRegExp('/foo1:bar/', $e->getMessage());
- $this->assertRegExp('/foo:bar/', $e->getMessage());
- }
-
- try {
- $application->find('foo2:bar');
- $this->fail('->find() throws a CommandNotFoundException if command does not exist, with alternatives');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
- $this->assertRegExp('/Did you mean one of these/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
- $this->assertRegExp('/foo1/', $e->getMessage());
- }
- $application->add(new \Foo3Command());
- $application->add(new \Foo4Command());
-
- try {
- $a = $application->find('foo3:');
- $this->fail('->find() should throw an Symfony\Component\Console\Exception\CommandNotFoundException if a command is ambiguous because of a subnamespace, with alternatives');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e);
- $this->assertRegExp('/foo3:bar/', $e->getMessage());
- $this->assertRegExp('/foo3:bar:toh/', $e->getMessage());
- }
- }
- public function testFindAlternativeCommands()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $application->add(new \Foo1Command());
- $application->add(new \Foo2Command());
- try {
- $application->find($commandName = 'Unknown command');
- $this->fail('->find() throws a CommandNotFoundException if command does not exist');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist');
- $this->assertSame(array(), $e->getAlternatives());
- $this->assertEquals(sprintf('Command "%s" is not defined.', $commandName), $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, without alternatives');
- }
-
-
- try {
- $application->find($commandName = 'bar1');
- $this->fail('->find() throws a CommandNotFoundException if command does not exist');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist');
- $this->assertSame(array('afoobar1', 'foo:bar1'), $e->getAlternatives());
- $this->assertRegExp(sprintf('/Command "%s" is not defined./', $commandName), $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternatives');
- $this->assertRegExp('/afoobar1/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternative : "afoobar1"');
- $this->assertRegExp('/foo:bar1/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternative : "foo:bar1"');
- $this->assertNotRegExp('/foo:bar(?>!1)/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, without "foo:bar" alternative');
- }
- }
- public function testFindAlternativeCommandsWithAnAlias()
- {
- $fooCommand = new \FooCommand();
- $fooCommand->setAliases(array('foo2'));
- $application = new Application();
- $application->add($fooCommand);
- $result = $application->find('foo');
- $this->assertSame($fooCommand, $result);
- }
- public function testFindAlternativeNamespace()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $application->add(new \Foo1Command());
- $application->add(new \Foo2Command());
- $application->add(new \Foo3Command());
- try {
- $application->find('Unknown-namespace:Unknown-command');
- $this->fail('->find() throws a CommandNotFoundException if namespace does not exist');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if namespace does not exist');
- $this->assertSame(array(), $e->getAlternatives());
- $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');
- }
- try {
- $application->find('foo2:command');
- $this->fail('->find() throws a CommandNotFoundException if namespace does not exist');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if namespace does not exist');
- $this->assertCount(3, $e->getAlternatives());
- $this->assertContains('foo', $e->getAlternatives());
- $this->assertContains('foo1', $e->getAlternatives());
- $this->assertContains('foo3', $e->getAlternatives());
- $this->assertRegExp('/There are no commands defined in the "foo2" namespace./', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative');
- $this->assertRegExp('/foo/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo"');
- $this->assertRegExp('/foo1/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo1"');
- $this->assertRegExp('/foo3/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo3"');
- }
- }
- public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces()
- {
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getNamespaces'))->getMock();
- $application->expects($this->once())
- ->method('getNamespaces')
- ->will($this->returnValue(array('foo:sublong', 'bar:sub')));
- $this->assertEquals('foo:sublong', $application->findNamespace('f:sub'));
- }
-
- public function testFindWithDoubleColonInNameThrowsException()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $application->add(new \Foo4Command());
- $application->find('foo::bar');
- }
- public function testSetCatchExceptions()
- {
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->any())
- ->method('getTerminalWidth')
- ->will($this->returnValue(120));
- $tester = new ApplicationTester($application);
- $application->setCatchExceptions(true);
- $tester->run(array('command' => 'foo'), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getDisplay(true), '->setCatchExceptions() sets the catch exception flag');
- $application->setCatchExceptions(false);
- try {
- $tester->run(array('command' => 'foo'), array('decorated' => false));
- $this->fail('->setCatchExceptions() sets the catch exception flag');
- } catch (\Exception $e) {
- $this->assertInstanceOf('\Exception', $e, '->setCatchExceptions() sets the catch exception flag');
- $this->assertEquals('Command "foo" is not defined.', $e->getMessage(), '->setCatchExceptions() sets the catch exception flag');
- }
- }
-
- public function testLegacyAsText()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $this->ensureStaticCommandHelp($application);
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext1.txt', $this->normalizeLineBreaks($application->asText()), '->asText() returns a text representation of the application');
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext2.txt', $this->normalizeLineBreaks($application->asText('foo')), '->asText() returns a text representation of the application');
- }
-
- public function testLegacyAsXml()
- {
- $application = new Application();
- $application->add(new \FooCommand());
- $this->ensureStaticCommandHelp($application);
- $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml1.txt', $application->asXml(), '->asXml() returns an XML representation of the application');
- $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml2.txt', $application->asXml('foo'), '->asXml() returns an XML representation of the application');
- }
- public function testRenderException()
- {
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->any())
- ->method('getTerminalWidth')
- ->will($this->returnValue(120));
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getDisplay(true), '->renderException() renders a pretty exception');
- $tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
- $this->assertContains('Exception trace', $tester->getDisplay(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
- $tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $tester->getDisplay(true), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
- $application->add(new \Foo3Command());
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');
- $tester->run(array('command' => 'foo3:bar'), array('decorated' => true));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->any())
- ->method('getTerminalWidth')
- ->will($this->returnValue(32));
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $tester->getDisplay(true), '->renderException() wraps messages when they are bigger than the terminal');
- }
- public function testRenderExceptionWithDoubleWidthCharacters()
- {
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->any())
- ->method('getTerminalWidth')
- ->will($this->returnValue(120));
- $application->register('foo')->setCode(function () {
- throw new \Exception('エラーメッセージ');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');
- $tester->run(array('command' => 'foo'), array('decorated' => true));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1decorated.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->any())
- ->method('getTerminalWidth')
- ->will($this->returnValue(32));
- $application->register('foo')->setCode(function () {
- throw new \Exception('コマンドの実行中にエラーが発生しました。');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt', $tester->getDisplay(true), '->renderException() wraps messages when they are bigger than the terminal');
- }
- public function testRenderExceptionEscapesLines()
- {
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->any())
- ->method('getTerminalWidth')
- ->will($this->returnValue(22));
- $application->register('foo')->setCode(function () {
- throw new \Exception('dont break here <info>!</info>');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_escapeslines.txt', $tester->getDisplay(true), '->renderException() escapes lines containing formatting');
- }
- public function testRenderExceptionLineBreaks()
- {
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->any())
- ->method('getTerminalWidth')
- ->will($this->returnValue(120));
- $application->register('foo')->setCode(function () {
- throw new \InvalidArgumentException("\n\nline 1 with extra spaces \nline 2\n\nline 4\n");
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_linebreaks.txt', $tester->getDisplay(true), '->renderException() keep multiple line breaks');
- }
- public function testRun()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->add($command = new \Foo1Command());
- $_SERVER['argv'] = array('cli.php', 'foo:bar1');
- ob_start();
- $application->run();
- ob_end_clean();
- $this->assertInstanceOf('Symfony\Component\Console\Input\ArgvInput', $command->input, '->run() creates an ArgvInput by default if none is given');
- $this->assertInstanceOf('Symfony\Component\Console\Output\ConsoleOutput', $command->output, '->run() creates a ConsoleOutput by default if none is given');
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $this->ensureStaticCommandHelp($application);
- $tester = new ApplicationTester($application);
- $tester->run(array(), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $tester->getDisplay(true), '->run() runs the list command if no argument is passed');
- $tester->run(array('--help' => true), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $tester->getDisplay(true), '->run() runs the help command if --help is passed');
- $tester->run(array('-h' => true), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $tester->getDisplay(true), '->run() runs the help command if -h is passed');
- $tester->run(array('command' => 'list', '--help' => true), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $tester->getDisplay(true), '->run() displays the help if --help is passed');
- $tester->run(array('command' => 'list', '-h' => true), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $tester->getDisplay(true), '->run() displays the help if -h is passed');
- $tester->run(array('--ansi' => true));
- $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --ansi is passed');
- $tester->run(array('--no-ansi' => true));
- $this->assertFalse($tester->getOutput()->isDecorated(), '->run() forces color output to be disabled if --no-ansi is passed');
- $tester->run(array('--version' => true), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $tester->getDisplay(true), '->run() displays the program version if --version is passed');
- $tester->run(array('-V' => true), array('decorated' => false));
- $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $tester->getDisplay(true), '->run() displays the program version if -v is passed');
- $tester->run(array('command' => 'list', '--quiet' => true));
- $this->assertSame('', $tester->getDisplay(), '->run() removes all output if --quiet is passed');
- $this->assertFalse($tester->getInput()->isInteractive(), '->run() sets off the interactive mode if --quiet is passed');
- $tester->run(array('command' => 'list', '-q' => true));
- $this->assertSame('', $tester->getDisplay(), '->run() removes all output if -q is passed');
- $this->assertFalse($tester->getInput()->isInteractive(), '->run() sets off the interactive mode if -q is passed');
- $tester->run(array('command' => 'list', '--verbose' => true));
- $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose is passed');
- $tester->run(array('command' => 'list', '--verbose' => 1));
- $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose=1 is passed');
- $tester->run(array('command' => 'list', '--verbose' => 2));
- $this->assertSame(Output::VERBOSITY_VERY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to very verbose if --verbose=2 is passed');
- $tester->run(array('command' => 'list', '--verbose' => 3));
- $this->assertSame(Output::VERBOSITY_DEBUG, $tester->getOutput()->getVerbosity(), '->run() sets the output to debug if --verbose=3 is passed');
- $tester->run(array('command' => 'list', '--verbose' => 4));
- $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if unknown --verbose level is passed');
- $tester->run(array('command' => 'list', '-v' => true));
- $this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
- $tester->run(array('command' => 'list', '-vv' => true));
- $this->assertSame(Output::VERBOSITY_VERY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
- $tester->run(array('command' => 'list', '-vvv' => true));
- $this->assertSame(Output::VERBOSITY_DEBUG, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->add(new \FooCommand());
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo:bar', '--no-interaction' => true), array('decorated' => false));
- $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if --no-interaction is passed');
- $tester->run(array('command' => 'foo:bar', '-n' => true), array('decorated' => false));
- $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
- }
-
- public function testVerboseValueNotBreakArguments()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->add(new \FooCommand());
- $output = new StreamOutput(fopen('php://memory', 'w', false));
- $input = new ArgvInput(array('cli.php', '-v', 'foo:bar'));
- $application->run($input, $output);
- $this->addToAssertionCount(1);
- $input = new ArgvInput(array('cli.php', '--verbose', 'foo:bar'));
- $application->run($input, $output);
- $this->addToAssertionCount(1);
- }
- public function testRunReturnsIntegerExitCode()
- {
- $exception = new \Exception('', 4);
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('doRun'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->once())
- ->method('doRun')
- ->will($this->throwException($exception));
- $exitCode = $application->run(new ArrayInput(array()), new NullOutput());
- $this->assertSame(4, $exitCode, '->run() returns integer exit code extracted from raised exception');
- }
- public function testRunDispatchesIntegerExitCode()
- {
- $passedRightValue = false;
-
- $dispatcher = new EventDispatcher();
- $dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) use (&$passedRightValue) {
- $passedRightValue = (4 === $event->getExitCode());
- });
- $application = new Application();
- $application->setDispatcher($dispatcher);
- $application->setAutoExit(false);
- $application->register('test')->setCode(function (InputInterface $input, OutputInterface $output) {
- throw new \Exception('', 4);
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'test'));
- $this->assertTrue($passedRightValue, '-> exit code 4 was passed in the console.terminate event');
- }
- public function testRunReturnsExitCodeOneForExceptionCodeZero()
- {
- $exception = new \Exception('', 0);
- $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('doRun'))->getMock();
- $application->setAutoExit(false);
- $application->expects($this->once())
- ->method('doRun')
- ->will($this->throwException($exception));
- $exitCode = $application->run(new ArrayInput(array()), new NullOutput());
- $this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is 0');
- }
- public function testRunDispatchesExitCodeOneForExceptionCodeZero()
- {
- $passedRightValue = false;
-
- $dispatcher = new EventDispatcher();
- $dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) use (&$passedRightValue) {
- $passedRightValue = (1 === $event->getExitCode());
- });
- $application = new Application();
- $application->setDispatcher($dispatcher);
- $application->setAutoExit(false);
- $application->register('test')->setCode(function (InputInterface $input, OutputInterface $output) {
- throw new \Exception();
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'test'));
- $this->assertTrue($passedRightValue, '-> exit code 1 was passed in the console.terminate event');
- }
-
- public function testAddingOptionWithDuplicateShortcut()
- {
- $dispatcher = new EventDispatcher();
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->setDispatcher($dispatcher);
- $application->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'Environment'));
- $application
- ->register('foo')
- ->setAliases(array('f'))
- ->setDefinition(array(new InputOption('survey', 'e', InputOption::VALUE_REQUIRED, 'My option with a shortcut.')))
- ->setCode(function (InputInterface $input, OutputInterface $output) {})
- ;
- $input = new ArrayInput(array('command' => 'foo'));
- $output = new NullOutput();
- $application->run($input, $output);
- }
-
- public function testAddingAlreadySetDefinitionElementData($def)
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application
- ->register('foo')
- ->setDefinition(array($def))
- ->setCode(function (InputInterface $input, OutputInterface $output) {})
- ;
- $input = new ArrayInput(array('command' => 'foo'));
- $output = new NullOutput();
- $application->run($input, $output);
- }
- public function getAddingAlreadySetDefinitionElementData()
- {
- return array(
- array(new InputArgument('command', InputArgument::REQUIRED)),
- array(new InputOption('quiet', '', InputOption::VALUE_NONE)),
- array(new InputOption('query', 'q', InputOption::VALUE_NONE)),
- );
- }
- public function testGetDefaultHelperSetReturnsDefaultValues()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $helperSet = $application->getHelperSet();
- $this->assertTrue($helperSet->has('formatter'));
- $this->assertTrue($helperSet->has('dialog'));
- $this->assertTrue($helperSet->has('progress'));
- }
- public function testAddingSingleHelperSetOverwritesDefaultValues()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->setHelperSet(new HelperSet(array(new FormatterHelper())));
- $helperSet = $application->getHelperSet();
- $this->assertTrue($helperSet->has('formatter'));
-
- $this->assertFalse($helperSet->has('dialog'));
- $this->assertFalse($helperSet->has('progress'));
- }
- public function testOverwritingDefaultHelperSetOverwritesDefaultValues()
- {
- $application = new CustomApplication();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->setHelperSet(new HelperSet(array(new FormatterHelper())));
- $helperSet = $application->getHelperSet();
- $this->assertTrue($helperSet->has('formatter'));
-
- $this->assertFalse($helperSet->has('dialog'));
- $this->assertFalse($helperSet->has('progress'));
- }
- public function testGetDefaultInputDefinitionReturnsDefaultValues()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $inputDefinition = $application->getDefinition();
- $this->assertTrue($inputDefinition->hasArgument('command'));
- $this->assertTrue($inputDefinition->hasOption('help'));
- $this->assertTrue($inputDefinition->hasOption('quiet'));
- $this->assertTrue($inputDefinition->hasOption('verbose'));
- $this->assertTrue($inputDefinition->hasOption('version'));
- $this->assertTrue($inputDefinition->hasOption('ansi'));
- $this->assertTrue($inputDefinition->hasOption('no-ansi'));
- $this->assertTrue($inputDefinition->hasOption('no-interaction'));
- }
- public function testOverwritingDefaultInputDefinitionOverwritesDefaultValues()
- {
- $application = new CustomApplication();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $inputDefinition = $application->getDefinition();
-
- $this->assertFalse($inputDefinition->hasArgument('command'));
- $this->assertFalse($inputDefinition->hasOption('help'));
- $this->assertFalse($inputDefinition->hasOption('quiet'));
- $this->assertFalse($inputDefinition->hasOption('verbose'));
- $this->assertFalse($inputDefinition->hasOption('version'));
- $this->assertFalse($inputDefinition->hasOption('ansi'));
- $this->assertFalse($inputDefinition->hasOption('no-ansi'));
- $this->assertFalse($inputDefinition->hasOption('no-interaction'));
- $this->assertTrue($inputDefinition->hasOption('custom'));
- }
- public function testSettingCustomInputDefinitionOverwritesDefaultValues()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->setDefinition(new InputDefinition(array(new InputOption('--custom', '-c', InputOption::VALUE_NONE, 'Set the custom input definition.'))));
- $inputDefinition = $application->getDefinition();
-
- $this->assertFalse($inputDefinition->hasArgument('command'));
- $this->assertFalse($inputDefinition->hasOption('help'));
- $this->assertFalse($inputDefinition->hasOption('quiet'));
- $this->assertFalse($inputDefinition->hasOption('verbose'));
- $this->assertFalse($inputDefinition->hasOption('version'));
- $this->assertFalse($inputDefinition->hasOption('ansi'));
- $this->assertFalse($inputDefinition->hasOption('no-ansi'));
- $this->assertFalse($inputDefinition->hasOption('no-interaction'));
- $this->assertTrue($inputDefinition->hasOption('custom'));
- }
- public function testRunWithDispatcher()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setDispatcher($this->getDispatcher());
- $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('foo.');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'));
- $this->assertEquals('before.foo.after.'.PHP_EOL, $tester->getDisplay());
- }
-
- public function testRunWithExceptionAndDispatcher()
- {
- $application = new Application();
- $application->setDispatcher($this->getDispatcher());
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
- throw new \RuntimeException('foo');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'));
- }
- public function testRunDispatchesAllEventsWithException()
- {
- $application = new Application();
- $application->setDispatcher($this->getDispatcher());
- $application->setAutoExit(false);
- $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('foo.');
- throw new \RuntimeException('foo');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'));
- $this->assertContains('before.foo.caught.after.', $tester->getDisplay());
- }
- public function testRunDispatchesAllEventsWithExceptionInListener()
- {
- $dispatcher = $this->getDispatcher();
- $dispatcher->addListener('console.command', function () {
- throw new \RuntimeException('foo');
- });
- $application = new Application();
- $application->setDispatcher($dispatcher);
- $application->setAutoExit(false);
- $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('foo.');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo'));
- $this->assertContains('before.caught.after.', $tester->getDisplay());
- }
- public function testRunWithError()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('dym.');
- throw new \Error('dymerr');
- });
- $tester = new ApplicationTester($application);
- try {
- $tester->run(array('command' => 'dym'));
- $this->fail('Error expected.');
- } catch (\Error $e) {
- $this->assertSame('dymerr', $e->getMessage());
- }
- }
-
- public function testRunWithErrorAndDispatcher()
- {
- $application = new Application();
- $application->setDispatcher($this->getDispatcher());
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('dym.');
- throw new \Error('dymerr');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'dym'));
- $this->assertContains('before.dym.caught.after.', $tester->getDisplay(), 'The PHP Error did not dispached events');
- }
- public function testRunDispatchesAllEventsWithError()
- {
- $application = new Application();
- $application->setDispatcher($this->getDispatcher());
- $application->setAutoExit(false);
- $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('dym.');
- throw new \Error('dymerr');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'dym'));
- $this->assertContains('before.dym.caught.after.', $tester->getDisplay(), 'The PHP Error did not dispached events');
- }
- public function testRunWithErrorFailingStatusCode()
- {
- $application = new Application();
- $application->setDispatcher($this->getDispatcher());
- $application->setAutoExit(false);
- $application->register('dus')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('dus.');
- throw new \Error('duserr');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'dus'));
- $this->assertSame(1, $tester->getStatusCode(), 'Status code should be 1');
- }
- public function testRunWithDispatcherSkippingCommand()
- {
- $application = new Application();
- $application->setDispatcher($this->getDispatcher(true));
- $application->setAutoExit(false);
- $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('foo.');
- });
- $tester = new ApplicationTester($application);
- $exitCode = $tester->run(array('command' => 'foo'));
- $this->assertContains('before.after.', $tester->getDisplay());
- $this->assertEquals(ConsoleCommandEvent::RETURN_CODE_DISABLED, $exitCode);
- }
- public function testRunWithDispatcherAccessingInputOptions()
- {
- $noInteractionValue = null;
- $quietValue = null;
- $dispatcher = $this->getDispatcher();
- $dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use (&$noInteractionValue, &$quietValue) {
- $input = $event->getInput();
- $noInteractionValue = $input->getOption('no-interaction');
- $quietValue = $input->getOption('quiet');
- });
- $application = new Application();
- $application->setDispatcher($dispatcher);
- $application->setAutoExit(false);
- $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('foo.');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo', '--no-interaction' => true));
- $this->assertTrue($noInteractionValue);
- $this->assertFalse($quietValue);
- }
- public function testRunWithDispatcherAddingInputOptions()
- {
- $extraValue = null;
- $dispatcher = $this->getDispatcher();
- $dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use (&$extraValue) {
- $definition = $event->getCommand()->getDefinition();
- $input = $event->getInput();
- $definition->addOption(new InputOption('extra', null, InputOption::VALUE_REQUIRED));
- $input->bind($definition);
- $extraValue = $input->getOption('extra');
- });
- $application = new Application();
- $application->setDispatcher($dispatcher);
- $application->setAutoExit(false);
- $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
- $output->write('foo.');
- });
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'foo', '--extra' => 'some test value'));
- $this->assertEquals('some test value', $extraValue);
- }
- public function testTerminalDimensions()
- {
- $application = new Application();
- $originalDimensions = $application->getTerminalDimensions();
- $this->assertCount(2, $originalDimensions);
- $width = 80;
- if ($originalDimensions[0] == $width) {
- $width = 100;
- }
- $application->setTerminalDimensions($width, 80);
- $this->assertSame(array($width, 80), $application->getTerminalDimensions());
- }
- public function testSetRunCustomDefaultCommand()
- {
- $command = new \FooCommand();
- $application = new Application();
- $application->setAutoExit(false);
- $application->add($command);
- $application->setDefaultCommand($command->getName());
- $tester = new ApplicationTester($application);
- $tester->run(array(), array('interactive' => false));
- $this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
- $application = new CustomDefaultCommandApplication();
- $application->setAutoExit(false);
- $tester = new ApplicationTester($application);
- $tester->run(array(), array('interactive' => false));
- $this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
- }
- public function testSetRunCustomDefaultCommandWithOption()
- {
- $command = new \FooOptCommand();
- $application = new Application();
- $application->setAutoExit(false);
- $application->add($command);
- $application->setDefaultCommand($command->getName());
- $tester = new ApplicationTester($application);
- $tester->run(array('--fooopt' => 'opt'), array('interactive' => false));
- $this->assertEquals('called'.PHP_EOL.'opt'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
- }
-
- public function testCanCheckIfTerminalIsInteractive()
- {
- $application = new CustomDefaultCommandApplication();
- $application->setAutoExit(false);
- $tester = new ApplicationTester($application);
- $tester->run(array('command' => 'help'));
- $this->assertFalse($tester->getInput()->hasParameterOption(array('--no-interaction', '-n')));
- $inputStream = $application->getHelperSet()->get('question')->getInputStream();
- $this->assertEquals($tester->getInput()->isInteractive(), @posix_isatty($inputStream));
- }
- protected function getDispatcher($skipCommand = false)
- {
- $dispatcher = new EventDispatcher();
- $dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use ($skipCommand) {
- $event->getOutput()->write('before.');
- if ($skipCommand) {
- $event->disableCommand();
- }
- });
- $dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) use ($skipCommand) {
- $event->getOutput()->writeln('after.');
- if (!$skipCommand) {
- $event->setExitCode(ConsoleCommandEvent::RETURN_CODE_DISABLED);
- }
- });
- $dispatcher->addListener('console.exception', function (ConsoleExceptionEvent $event) {
- $event->getOutput()->write('caught.');
- $event->setException(new \LogicException('caught.', $event->getExitCode(), $event->getException()));
- });
- return $dispatcher;
- }
-
- public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEnabled()
- {
- $application = new Application();
- $application->setAutoExit(false);
- $application->setDispatcher(new EventDispatcher());
- $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
- new \UnknownClass();
- });
- $tester = new ApplicationTester($application);
- try {
- $tester->run(array('command' => 'dym'));
- $this->fail('->run() should rethrow PHP errors if not handled via ConsoleErrorEvent.');
- } catch (\Error $e) {
- $this->assertSame($e->getMessage(), 'Class \'UnknownClass\' not found');
- }
- }
- }
- class CustomApplication extends Application
- {
-
- protected function getDefaultInputDefinition()
- {
- return new InputDefinition(array(new InputOption('--custom', '-c', InputOption::VALUE_NONE, 'Set the custom input definition.')));
- }
-
- protected function getDefaultHelperSet()
- {
- return new HelperSet(array(new FormatterHelper()));
- }
- }
- class CustomDefaultCommandApplication extends Application
- {
-
- public function __construct()
- {
- parent::__construct();
- $command = new \FooCommand();
- $this->add($command);
- $this->setDefaultCommand($command->getName());
- }
- }
|