ApplicationTest.php 38 KB

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