ApplicationTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. }
  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 TestApplication();
  101. $application->add($foo = new \FooCommand());
  102. $application->setWantHelps();
  103. $command = $application->get('foo:bar');
  104. $this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($command), '->get() returns the help command if --help is provided as the input');
  105. }
  106. public function testGetNamespaces()
  107. {
  108. $application = new TestApplication();
  109. $application->add(new \FooCommand());
  110. $application->add(new \Foo1Command());
  111. $this->assertEquals(array('foo'), $application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces');
  112. }
  113. public function testFindNamespace()
  114. {
  115. $application = new TestApplication();
  116. $application->add(new \FooCommand());
  117. $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
  118. $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
  119. $application->add(new \Foo2Command());
  120. $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
  121. try {
  122. $application->findNamespace('f');
  123. $this->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  124. } catch (\Exception $e) {
  125. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  126. $this->assertEquals('The namespace "f" is ambiguous (foo, foo1).', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  127. }
  128. try {
  129. $application->findNamespace('bar');
  130. $this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  131. } catch (\Exception $e) {
  132. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  133. $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');
  134. }
  135. }
  136. public function testFind()
  137. {
  138. $application = new TestApplication();
  139. $application->add(new \FooCommand());
  140. $this->assertEquals('FooCommand', get_class($application->find('foo:bar')), '->find() returns a command if its name exists');
  141. $this->assertEquals('Symfony\Component\Console\Command\HelpCommand', get_class($application->find('h')), '->find() returns a command if its name exists');
  142. $this->assertEquals('FooCommand', get_class($application->find('f:bar')), '->find() returns a command if the abbreviation for the namespace exists');
  143. $this->assertEquals('FooCommand', get_class($application->find('f:b')), '->find() returns a command if the abbreviation for the namespace and the command name exist');
  144. $this->assertEquals('FooCommand', get_class($application->find('a')), '->find() returns a command if the abbreviation exists for an alias');
  145. $application->add(new \Foo1Command());
  146. $application->add(new \Foo2Command());
  147. try {
  148. $application->find('f');
  149. $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  150. } catch (\Exception $e) {
  151. $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  152. $this->assertEquals('Command "f" is not defined.', $e->getMessage(), '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  153. }
  154. try {
  155. $application->find('a');
  156. $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  157. } catch (\Exception $e) {
  158. $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  159. $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');
  160. }
  161. try {
  162. $application->find('foo:b');
  163. $this->fail('->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
  164. } catch (\Exception $e) {
  165. $this->assertInstanceOf('\InvalidArgumentException', $e, '->find() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
  166. $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');
  167. }
  168. }
  169. public function testSetCatchExceptions()
  170. {
  171. $application = new Application();
  172. $application->setAutoExit(false);
  173. $tester = new ApplicationTester($application);
  174. $application->setCatchExceptions(true);
  175. $tester->run(array('command' => 'foo'));
  176. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalize($tester->getDisplay()), '->setCatchExceptions() sets the catch exception flag');
  177. $application->setCatchExceptions(false);
  178. try {
  179. $tester->run(array('command' => 'foo'));
  180. $this->fail('->setCatchExceptions() sets the catch exception flag');
  181. } catch (\Exception $e) {
  182. $this->assertInstanceOf('\Exception', $e, '->setCatchExceptions() sets the catch exception flag');
  183. $this->assertEquals('Command "foo" is not defined.', $e->getMessage(), '->setCatchExceptions() sets the catch exception flag');
  184. }
  185. }
  186. public function testAsText()
  187. {
  188. $application = new Application();
  189. $application->add(new \FooCommand);
  190. $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext1.txt', $application->asText(), '->asText() returns a text representation of the application');
  191. $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext2.txt', $application->asText('foo'), '->asText() returns a text representation of the application');
  192. }
  193. public function testAsXml()
  194. {
  195. $application = new Application();
  196. $application->add(new \FooCommand);
  197. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml1.txt', $application->asXml(), '->asXml() returns an XML representation of the application');
  198. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml2.txt', $application->asXml('foo'), '->asXml() returns an XML representation of the application');
  199. }
  200. public function testRenderException()
  201. {
  202. $application = new Application();
  203. $application->setAutoExit(false);
  204. $tester = new ApplicationTester($application);
  205. $tester->run(array('command' => 'foo'));
  206. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exception');
  207. $tester->run(array('command' => 'foo'), array('verbosity' => Output::VERBOSITY_VERBOSE));
  208. $this->assertRegExp('/Exception trace/', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
  209. $tester->run(array('command' => 'list', '--foo' => true));
  210. $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');
  211. }
  212. public function testRun()
  213. {
  214. $application = new Application();
  215. $application->setAutoExit(false);
  216. $application->setCatchExceptions(false);
  217. $application->add($command = new \Foo1Command());
  218. $_SERVER['argv'] = array('cli.php', 'foo:bar1');
  219. ob_start();
  220. $application->run();
  221. ob_end_clean();
  222. $this->assertEquals('Symfony\Component\Console\Input\ArgvInput', get_class($command->input), '->run() creates an ArgvInput by default if none is given');
  223. $this->assertEquals('Symfony\Component\Console\Output\ConsoleOutput', get_class($command->output), '->run() creates a ConsoleOutput by default if none is given');
  224. $application = new Application();
  225. $application->setAutoExit(false);
  226. $application->setCatchExceptions(false);
  227. $tester = new ApplicationTester($application);
  228. $tester->run(array());
  229. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $this->normalize($tester->getDisplay()), '->run() runs the list command if no argument is passed');
  230. $tester->run(array('--help' => true));
  231. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalize($tester->getDisplay()), '->run() runs the help command if --help is passed');
  232. $tester->run(array('-h' => true));
  233. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalize($tester->getDisplay()), '->run() runs the help command if -h is passed');
  234. $application = new Application();
  235. $application->setAutoExit(false);
  236. $application->setCatchExceptions(false);
  237. $tester = new ApplicationTester($application);
  238. $tester->run(array('command' => 'list', '--help' => true));
  239. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalize($tester->getDisplay()), '->run() displays the help if --help is passed');
  240. $tester->run(array('command' => 'list', '-h' => true));
  241. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalize($tester->getDisplay()), '->run() displays the help 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('--ansi' => true));
  247. $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --ansi is passed');
  248. $tester->run(array('-a' => true));
  249. $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if -a is passed');
  250. $application = new Application();
  251. $application->setAutoExit(false);
  252. $application->setCatchExceptions(false);
  253. $tester = new ApplicationTester($application);
  254. $tester->run(array('--version' => true));
  255. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalize($tester->getDisplay()), '->run() displays the program version if --version is passed');
  256. $tester->run(array('-V' => true));
  257. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalize($tester->getDisplay()), '->run() displays the program version if -v is passed');
  258. $application = new Application();
  259. $application->setAutoExit(false);
  260. $application->setCatchExceptions(false);
  261. $tester = new ApplicationTester($application);
  262. $tester->run(array('command' => 'list', '--quiet' => true));
  263. $this->assertEquals('', $this->normalize($tester->getDisplay()), '->run() removes all output if --quiet is passed');
  264. $tester->run(array('command' => 'list', '-q' => true));
  265. $this->assertEquals('', $this->normalize($tester->getDisplay()), '->run() removes all output if -q 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', '--verbose' => true));
  271. $this->assertEquals(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose is --verbose is passed');
  272. $tester->run(array('command' => 'list', '-v' => true));
  273. $this->assertEquals(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose is -v is passed');
  274. $application = new Application();
  275. $application->setAutoExit(false);
  276. $application->setCatchExceptions(false);
  277. $application->add(new \FooCommand());
  278. $tester = new ApplicationTester($application);
  279. $tester->run(array('command' => 'foo:bar', '--no-interaction' => true));
  280. $this->assertEquals("called\n", $this->normalize($tester->getDisplay()), '->run() does not called interact() if --no-interaction is passed');
  281. $tester->run(array('command' => 'foo:bar', '-n' => true));
  282. $this->assertEquals("called\n", $this->normalize($tester->getDisplay()), '->run() does not called interact() if -n is passed');
  283. }
  284. }
  285. class TestApplication extends Application
  286. {
  287. public function setWantHelps()
  288. {
  289. $this->wantHelps = true;
  290. }
  291. }