ApplicationTest.php 19 KB

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