ApplicationTest.php 19KB

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