ApplicationTest.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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\Tests\Component\Console;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Output\Output;
  13. use Symfony\Component\Console\Tester\ApplicationTester;
  14. class ApplicationTest extends \PHPUnit_Framework_TestCase
  15. {
  16. static protected $fixturesPath;
  17. static public function setUpBeforeClass()
  18. {
  19. self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
  20. require_once self::$fixturesPath.'/FooCommand.php';
  21. require_once self::$fixturesPath.'/Foo1Command.php';
  22. require_once self::$fixturesPath.'/Foo2Command.php';
  23. require_once self::$fixturesPath.'/Foo3Command.php';
  24. }
  25. protected function normalize($text)
  26. {
  27. return str_replace(PHP_EOL, "\n", $text);
  28. }
  29. public function testConstructor()
  30. {
  31. $application = new Application('foo', 'bar');
  32. $this->assertEquals('foo', $application->getName(), '__construct() takes the application name as its first argument');
  33. $this->assertEquals('bar', $application->getVersion(), '__construct() takes the application version as its first argument');
  34. $this->assertEquals(array('help', 'list'), array_keys($application->all()), '__construct() registered the help and list commands by default');
  35. }
  36. public function testSetGetName()
  37. {
  38. $application = new Application();
  39. $application->setName('foo');
  40. $this->assertEquals('foo', $application->getName(), '->setName() sets the name of the application');
  41. }
  42. public function testSetGetVersion()
  43. {
  44. $application = new Application();
  45. $application->setVersion('bar');
  46. $this->assertEquals('bar', $application->getVersion(), '->setVersion() sets the version of the application');
  47. }
  48. public function testGetLongVersion()
  49. {
  50. $application = new Application('foo', 'bar');
  51. $this->assertEquals('<info>foo</info> version <comment>bar</comment>', $application->getLongVersion(), '->getLongVersion() returns the long version of the application');
  52. }
  53. public function testHelp()
  54. {
  55. $application = new Application();
  56. $this->assertStringEqualsFile(self::$fixturesPath.'/application_gethelp.txt', $application->getHelp(), '->setHelp() returns a help message');
  57. }
  58. public function testAll()
  59. {
  60. $application = new Application();
  61. $commands = $application->all();
  62. $this->assertEquals('Symfony\\Component\\Console\\Command\\HelpCommand', get_class($commands['help']), '->all() returns the registered commands');
  63. $application->add(new \FooCommand());
  64. $commands = $application->all('foo');
  65. $this->assertEquals(1, count($commands), '->all() takes a namespace as its first argument');
  66. }
  67. public function testRegister()
  68. {
  69. $application = new Application();
  70. $command = $application->register('foo');
  71. $this->assertEquals('foo', $command->getName(), '->register() registers a new command');
  72. }
  73. public function testAdd()
  74. {
  75. $application = new Application();
  76. $application->add($foo = new \FooCommand());
  77. $commands = $application->all();
  78. $this->assertEquals($foo, $commands['foo:bar'], '->add() registers a command');
  79. $application = new Application();
  80. $application->addCommands(array($foo = new \FooCommand(), $foo1 = new \Foo1Command()));
  81. $commands = $application->all();
  82. $this->assertEquals(array($foo, $foo1), array($commands['foo:bar'], $commands['foo:bar1']), '->addCommands() registers an array of commands');
  83. }
  84. public function testHasGet()
  85. {
  86. $application = new Application();
  87. $this->assertTrue($application->has('list'), '->has() returns true if a named command is registered');
  88. $this->assertFalse($application->has('afoobar'), '->has() returns false if a named command is not registered');
  89. $application->add($foo = new \FooCommand());
  90. $this->assertTrue($application->has('afoobar'), '->has() returns true if an alias is registered');
  91. $this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name');
  92. $this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias');
  93. try {
  94. $application->get('foofoo');
  95. $this->fail('->get() throws an \InvalidArgumentException if the command does not exist');
  96. } catch (\Exception $e) {
  97. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the command does not exist');
  98. $this->assertEquals('The command "foofoo" does not exist.', $e->getMessage(), '->get() throws an \InvalidArgumentException if the command does not exist');
  99. }
  100. $application = new Application();
  101. $application->add($foo = new \FooCommand());
  102. // simulate --help
  103. $r = new \ReflectionObject($application);
  104. $p = $r->getProperty('wantHelps');
  105. $p->setAccessible(true);
  106. $p->setValue($application, true);
  107. $command = $application->get('foo:bar');
  108. $this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($command), '->get() returns the help command if --help is provided as the input');
  109. }
  110. public function testGetNamespaces()
  111. {
  112. $application = new Application();
  113. $application->add(new \FooCommand());
  114. $application->add(new \Foo1Command());
  115. $this->assertEquals(array('foo'), $application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces');
  116. }
  117. public function testFindNamespace()
  118. {
  119. $application = new Application();
  120. $application->add(new \FooCommand());
  121. $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
  122. $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
  123. $application->add(new \Foo2Command());
  124. $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
  125. try {
  126. $application->findNamespace('f');
  127. $this->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  128. } catch (\Exception $e) {
  129. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  130. $this->assertEquals('The namespace "f" is ambiguous (foo, foo1).', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  131. }
  132. try {
  133. $application->findNamespace('bar');
  134. $this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  135. } catch (\Exception $e) {
  136. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  137. $this->assertEquals('There are no commands defined in the "bar" namespace.', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  138. }
  139. }
  140. public function testFind()
  141. {
  142. $application = new Application();
  143. $application->add(new \FooCommand());
  144. $this->assertEquals('FooCommand', get_class($application->find('foo:bar')), '->find() returns a command if its name exists');
  145. $this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($application->find('h')), '->find() returns a command if its name exists');
  146. $this->assertEquals('FooCommand', get_class($application->find('f:bar')), '->find() returns a command if the abbreviation for the namespace exists');
  147. $this->assertEquals('FooCommand', get_class($application->find('f:b')), '->find() returns a command if the abbreviation for the namespace and the command name exist');
  148. $this->assertEquals('FooCommand', get_class($application->find('a')), '->find() returns a command if the abbreviation exists for an alias');
  149. $application->add(new \Foo1Command());
  150. $application->add(new \Foo2Command());
  151. try {
  152. $application->find('f');
  153. $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  154. } catch (\Exception $e) {
  155. $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  156. $this->assertEquals('Command "f" is not defined.', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  157. }
  158. try {
  159. $application->find('a');
  160. $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  161. } catch (\Exception $e) {
  162. $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  163. $this->assertEquals('Command "a" is ambiguous (afoobar, afoobar1 and 1 more).', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  164. }
  165. try {
  166. $application->find('foo:b');
  167. $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
  168. } catch (\Exception $e) {
  169. $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
  170. $this->assertEquals('Command "foo:b" is ambiguous (foo:bar, foo:bar1).', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
  171. }
  172. }
  173. public function testSetCatchExceptions()
  174. {
  175. $application = new Application();
  176. $application->setAutoExit(false);
  177. $tester = new ApplicationTester($application);
  178. $application->setCatchExceptions(true);
  179. $tester->run(array('command' => 'foo'), array('decorated' => false));
  180. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalize($tester->getDisplay()), '->setCatchExceptions() sets the catch exception flag');
  181. $application->setCatchExceptions(false);
  182. try {
  183. $tester->run(array('command' => 'foo'), array('decorated' => false));
  184. $this->fail('->setCatchExceptions() sets the catch exception flag');
  185. } catch (\Exception $e) {
  186. $this->assertInstanceOf('\Exception', $e, '->setCatchExceptions() sets the catch exception flag');
  187. $this->assertEquals('Command "foo" is not defined.', $e->getMessage(), '->setCatchExceptions() sets the catch exception flag');
  188. }
  189. }
  190. public function testAsText()
  191. {
  192. $application = new Application();
  193. $application->add(new \FooCommand);
  194. $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext1.txt', str_replace(PHP_EOL, "\n", $application->asText()), '->asText() returns a text representation of the application');
  195. $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext2.txt', str_replace(PHP_EOL, "\n", $application->asText('foo')), '->asText() returns a text representation of the application');
  196. }
  197. public function testAsXml()
  198. {
  199. $application = new Application();
  200. $application->add(new \FooCommand);
  201. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml1.txt', $application->asXml(), '->asXml() returns an XML representation of the application');
  202. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml2.txt', $application->asXml('foo'), '->asXml() returns an XML representation of the application');
  203. }
  204. public function testRenderException()
  205. {
  206. $application = new Application();
  207. $application->setAutoExit(false);
  208. $tester = new ApplicationTester($application);
  209. $tester->run(array('command' => 'foo'), array('decorated' => false));
  210. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exception');
  211. $tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
  212. $this->assertRegExp('/Exception trace/', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
  213. $tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false));
  214. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $this->normalize($tester->getDisplay()), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
  215. $application->add(new \Foo3Command);
  216. $tester = new ApplicationTester($application);
  217. $tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
  218. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions');
  219. }
  220. public function testRun()
  221. {
  222. $application = new Application();
  223. $application->setAutoExit(false);
  224. $application->setCatchExceptions(false);
  225. $application->add($command = new \Foo1Command());
  226. $_SERVER['argv'] = array('cli.php', 'foo:bar1');
  227. ob_start();
  228. $application->run();
  229. ob_end_clean();
  230. $this->assertEquals('Symfony\Component\Console\Input\ArgvInput', get_class($command->input), '->run() creates an ArgvInput by default if none is given');
  231. $this->assertEquals('Symfony\Component\Console\Output\ConsoleOutput', get_class($command->output), '->run() creates a ConsoleOutput by default if none is given');
  232. $application = new Application();
  233. $application->setAutoExit(false);
  234. $application->setCatchExceptions(false);
  235. $tester = new ApplicationTester($application);
  236. $tester->run(array(), array('decorated' => false));
  237. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $this->normalize($tester->getDisplay()), '->run() runs the list command if no argument is passed');
  238. $tester->run(array('--help' => true), array('decorated' => false));
  239. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalize($tester->getDisplay()), '->run() runs the help command if --help is passed');
  240. $tester->run(array('-h' => true), array('decorated' => false));
  241. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalize($tester->getDisplay()), '->run() runs the help command if -h is passed');
  242. $application = new Application();
  243. $application->setAutoExit(false);
  244. $application->setCatchExceptions(false);
  245. $tester = new ApplicationTester($application);
  246. $tester->run(array('command' => 'list', '--help' => true), array('decorated' => false));
  247. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalize($tester->getDisplay()), '->run() displays the help if --help is passed');
  248. $tester->run(array('command' => 'list', '-h' => true), array('decorated' => false));
  249. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalize($tester->getDisplay()), '->run() displays the help if -h is passed');
  250. $application = new Application();
  251. $application->setAutoExit(false);
  252. $application->setCatchExceptions(false);
  253. $tester = new ApplicationTester($application);
  254. $tester->run(array('--ansi' => true));
  255. $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --ansi is passed');
  256. $tester->run(array('--no-ansi' => true));
  257. $this->assertFalse($tester->getOutput()->isDecorated(), '->run() forces color output to be disabled if --no-ansi is passed');
  258. $application = new Application();
  259. $application->setAutoExit(false);
  260. $application->setCatchExceptions(false);
  261. $tester = new ApplicationTester($application);
  262. $tester->run(array('--version' => true), array('decorated' => false));
  263. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalize($tester->getDisplay()), '->run() displays the program version if --version is passed');
  264. $tester->run(array('-V' => true), array('decorated' => false));
  265. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalize($tester->getDisplay()), '->run() displays the program version if -v is passed');
  266. $application = new Application();
  267. $application->setAutoExit(false);
  268. $application->setCatchExceptions(false);
  269. $tester = new ApplicationTester($application);
  270. $tester->run(array('command' => 'list', '--quiet' => true));
  271. $this->assertEquals('', $this->normalize($tester->getDisplay()), '->run() removes all output if --quiet is passed');
  272. $tester->run(array('command' => 'list', '-q' => true));
  273. $this->assertEquals('', $this->normalize($tester->getDisplay()), '->run() removes all output if -q is passed');
  274. $application = new Application();
  275. $application->setAutoExit(false);
  276. $application->setCatchExceptions(false);
  277. $tester = new ApplicationTester($application);
  278. $tester->run(array('command' => 'list', '--verbose' => true));
  279. $this->assertEquals(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose is --verbose is passed');
  280. $tester->run(array('command' => 'list', '-v' => true));
  281. $this->assertEquals(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose is -v is passed');
  282. $application = new Application();
  283. $application->setAutoExit(false);
  284. $application->setCatchExceptions(false);
  285. $application->add(new \FooCommand());
  286. $tester = new ApplicationTester($application);
  287. $tester->run(array('command' => 'foo:bar', '--no-interaction' => true), array('decorated' => false));
  288. $this->assertEquals("called\n", $this->normalize($tester->getDisplay()), '->run() does not called interact() if --no-interaction is passed');
  289. $tester->run(array('command' => 'foo:bar', '-n' => true), array('decorated' => false));
  290. $this->assertEquals("called\n", $this->normalize($tester->getDisplay()), '->run() does not called interact() if -n is passed');
  291. }
  292. }