ApplicationTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. require_once __DIR__.'/../../../bootstrap.php';
  10. use Symfony\Components\Console\Application;
  11. use Symfony\Components\Console\Input\ArrayInput;
  12. use Symfony\Components\Console\Output\Output;
  13. use Symfony\Components\Console\Output\StreamOutput;
  14. use Symfony\Components\Console\Tester\ApplicationTester;
  15. $fixtures = __DIR__.'/../../../../fixtures/Symfony/Components/Console';
  16. require_once $fixtures.'/FooCommand.php';
  17. require_once $fixtures.'/Foo1Command.php';
  18. require_once $fixtures.'/Foo2Command.php';
  19. $t = new LimeTest(52);
  20. // __construct()
  21. $t->diag('__construct()');
  22. $application = new Application('foo', 'bar');
  23. $t->is($application->getName(), 'foo', '__construct() takes the application name as its first argument');
  24. $t->is($application->getVersion(), 'bar', '__construct() takes the application version as its first argument');
  25. $t->is(array_keys($application->getCommands()), array('help', 'list'), '__construct() registered the help and list commands by default');
  26. // ->setName() ->getName()
  27. $t->diag('->setName() ->getName()');
  28. $application = new Application();
  29. $application->setName('foo');
  30. $t->is($application->getName(), 'foo', '->setName() sets the name of the application');
  31. // ->getVersion() ->getVersion()
  32. $t->diag('->getVersion() ->getVersion()');
  33. $application = new Application();
  34. $application->setVersion('bar');
  35. $t->is($application->getVersion(), 'bar', '->setVersion() sets the version of the application');
  36. // ->getLongVersion()
  37. $t->diag('->getLongVersion()');
  38. $application = new Application('foo', 'bar');
  39. $t->is($application->getLongVersion(), '<info>foo</info> version <comment>bar</comment>', '->getLongVersion() returns the long version of the application');
  40. // ->getHelp()
  41. $t->diag('->getHelp()');
  42. $application = new Application();
  43. $t->is($application->getHelp(), file_get_contents($fixtures.'/application_gethelp.txt'), '->setHelp() returns a help message');
  44. // ->getCommands()
  45. $t->diag('->getCommands()');
  46. $application = new Application();
  47. $commands = $application->getCommands();
  48. $t->is(get_class($commands['help']), 'Symfony\\Components\\Console\\Command\\HelpCommand', '->getCommands() returns the registered commands');
  49. $application->addCommand(new FooCommand());
  50. $commands = $application->getCommands('foo');
  51. $t->is(count($commands), 1, '->getCommands() takes a namespace as its first argument');
  52. // ->register()
  53. $t->diag('->register()');
  54. $application = new Application();
  55. $command = $application->register('foo');
  56. $t->is($command->getName(), 'foo', '->register() regiters a new command');
  57. // ->addCommand() ->addCommands()
  58. $t->diag('->addCommand() ->addCommands()');
  59. $application = new Application();
  60. $application->addCommand($foo = new FooCommand());
  61. $commands = $application->getCommands();
  62. $t->is($commands['foo:bar'], $foo, '->addCommand() registers a command');
  63. $application = new Application();
  64. $application->addCommands(array($foo = new FooCommand(), $foo1 = new Foo1Command()));
  65. $commands = $application->getCommands();
  66. $t->is(array($commands['foo:bar'], $commands['foo:bar1']), array($foo, $foo1), '->addCommands() registers an array of commands');
  67. // ->hasCommand() ->getCommand()
  68. $t->diag('->hasCommand() ->getCommand()');
  69. $application = new Application();
  70. $t->ok($application->hasCommand('list'), '->hasCommand() returns true if a named command is registered');
  71. $t->ok(!$application->hasCommand('afoobar'), '->hasCommand() returns false if a named command is not registered');
  72. $application->addCommand($foo = new FooCommand());
  73. $t->ok($application->hasCommand('afoobar'), '->hasCommand() returns true if an alias is registered');
  74. $t->is($application->getCommand('foo:bar'), $foo, '->getCommand() returns a command by name');
  75. $t->is($application->getCommand('afoobar'), $foo, '->getCommand() returns a command by alias');
  76. try
  77. {
  78. $application->getCommand('foofoo');
  79. $t->fail('->getCommand() throws an \InvalidArgumentException if the command does not exist');
  80. }
  81. catch (\InvalidArgumentException $e)
  82. {
  83. $t->pass('->getCommand() throws an \InvalidArgumentException if the command does not exist');
  84. }
  85. class TestApplication extends Application
  86. {
  87. public function setWantHelps()
  88. {
  89. $this->wantHelps = true;
  90. }
  91. }
  92. $application = new TestApplication();
  93. $application->addCommand($foo = new FooCommand());
  94. $application->setWantHelps();
  95. $command = $application->getCommand('foo:bar');
  96. $t->is(get_class($command), 'Symfony\Components\Console\Command\HelpCommand', '->getCommand() returns the help command if --help is provided as the input');
  97. // ->getNamespaces()
  98. $t->diag('->getNamespaces()');
  99. $application = new TestApplication();
  100. $application->addCommand(new FooCommand());
  101. $application->addCommand(new Foo1Command());
  102. $t->is($application->getNamespaces(), array('foo'), '->getNamespaces() returns an array of unique used namespaces');
  103. // ->findNamespace()
  104. $t->diag('->findNamespace()');
  105. $application = new TestApplication();
  106. $application->addCommand(new FooCommand());
  107. $t->is($application->findNamespace('foo'), 'foo', '->findNamespace() returns the given namespace if it exists');
  108. $t->is($application->findNamespace('f'), 'foo', '->findNamespace() finds a namespace given an abbreviation');
  109. $application->addCommand(new Foo2Command());
  110. $t->is($application->findNamespace('foo'), 'foo', '->findNamespace() returns the given namespace if it exists');
  111. try
  112. {
  113. $application->findNamespace('f');
  114. $t->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  115. }
  116. catch (\InvalidArgumentException $e)
  117. {
  118. $t->pass('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  119. }
  120. try
  121. {
  122. $application->findNamespace('bar');
  123. $t->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  124. }
  125. catch (\InvalidArgumentException $e)
  126. {
  127. $t->pass('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  128. }
  129. // ->findCommand()
  130. $t->diag('->findCommand()');
  131. $application = new TestApplication();
  132. $application->addCommand(new FooCommand());
  133. $t->is(get_class($application->findCommand('foo:bar')), 'FooCommand', '->findCommand() returns a command if its name exists');
  134. $t->is(get_class($application->findCommand('h')), 'Symfony\Components\Console\Command\HelpCommand', '->findCommand() returns a command if its name exists');
  135. $t->is(get_class($application->findCommand('f:bar')), 'FooCommand', '->findCommand() returns a command if the abbreviation for the namespace exists');
  136. $t->is(get_class($application->findCommand('f:b')), 'FooCommand', '->findCommand() returns a command if the abbreviation for the namespace and the command name exist');
  137. $t->is(get_class($application->findCommand('a')), 'FooCommand', '->findCommand() returns a command if the abbreviation exists for an alias');
  138. $application->addCommand(new Foo1Command());
  139. $application->addCommand(new Foo2Command());
  140. try
  141. {
  142. $application->findCommand('f');
  143. $t->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  144. }
  145. catch (\InvalidArgumentException $e)
  146. {
  147. $t->pass('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  148. }
  149. try
  150. {
  151. $application->findCommand('a');
  152. $t->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  153. }
  154. catch (\InvalidArgumentException $e)
  155. {
  156. $t->pass('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  157. }
  158. try
  159. {
  160. $application->findCommand('foo:b');
  161. $t->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
  162. }
  163. catch (\InvalidArgumentException $e)
  164. {
  165. $t->pass('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a taks');
  166. }
  167. // ->setCatchExceptions()
  168. $t->diag('->setCatchExceptions()');
  169. $application = new Application();
  170. $application->setAutoExit(false);
  171. $tester = new ApplicationTester($application);
  172. $application->setCatchExceptions(true);
  173. $tester->run(array('command' => 'foo'));
  174. $t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_renderexception1.txt'), '->setCatchExceptions() sets the catch exception flag');
  175. $application->setCatchExceptions(false);
  176. try
  177. {
  178. $tester->run(array('command' => 'foo'));
  179. $t->fail('->setCatchExceptions() sets the catch exception flag');
  180. }
  181. catch (\Exception $e)
  182. {
  183. $t->pass('->setCatchExceptions() sets the catch exception flag');
  184. }
  185. // ->asText()
  186. $t->diag('->asText()');
  187. $application = new Application();
  188. $application->addCommand(new FooCommand);
  189. $t->is($application->asText(), file_get_contents($fixtures.'/application_astext1.txt'), '->asText() returns a text representation of the application');
  190. $t->is($application->asText('foo'), file_get_contents($fixtures.'/application_astext2.txt'), '->asText() returns a text representation of the application');
  191. // ->asXml()
  192. $t->diag('->asXml()');
  193. $application = new Application();
  194. $application->addCommand(new FooCommand);
  195. $t->is($application->asXml(), file_get_contents($fixtures.'/application_asxml1.txt'), '->asXml() returns an XML representation of the application');
  196. $t->is($application->asXml('foo'), file_get_contents($fixtures.'/application_asxml2.txt'), '->asXml() returns an XML representation of the application');
  197. // ->renderException()
  198. $t->diag('->renderException()');
  199. $application = new Application();
  200. $application->setAutoExit(false);
  201. $tester = new ApplicationTester($application);
  202. $tester->run(array('command' => 'foo'));
  203. $t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_renderexception1.txt'), '->renderException() renders a pretty exception');
  204. $tester->run(array('command' => 'foo'), array('verbosity' => Output::VERBOSITY_VERBOSE));
  205. $t->like($tester->getDisplay(), '/Exception trace/', '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
  206. $tester->run(array('command' => 'list', '--foo' => true));
  207. $t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_renderexception2.txt'), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
  208. // ->run()
  209. $t->diag('->run()');
  210. $application = new Application();
  211. $application->setAutoExit(false);
  212. $application->setCatchExceptions(false);
  213. $application->addCommand($command = new Foo1Command());
  214. $_SERVER['argv'] = array('cli.php', 'foo:bar1');
  215. ob_start();
  216. $application->run();
  217. ob_end_clean();
  218. $t->is(get_class($command->input), 'Symfony\Components\Console\Input\ArgvInput', '->run() creates an ArgvInput by default if none is given');
  219. $t->is(get_class($command->output), 'Symfony\Components\Console\Output\ConsoleOutput', '->run() creates a ConsoleOutput by default if none is given');
  220. $application = new Application();
  221. $application->setAutoExit(false);
  222. $application->setCatchExceptions(false);
  223. $tester = new ApplicationTester($application);
  224. $tester->run(array());
  225. $t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run1.txt'), '->run() runs the list command if no argument is passed');
  226. $tester->run(array('--help' => true));
  227. $t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run2.txt'), '->run() runs the help command if --help is passed');
  228. $application = new Application();
  229. $application->setAutoExit(false);
  230. $application->setCatchExceptions(false);
  231. $tester = new ApplicationTester($application);
  232. $tester->run(array('command' => 'list', '--help' => true));
  233. $t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run3.txt'), '->run() displays the help if --help is passed');
  234. $application = new Application();
  235. $application->setAutoExit(false);
  236. $application->setCatchExceptions(false);
  237. $tester = new ApplicationTester($application);
  238. $tester->run(array('--color' => true));
  239. $t->ok($tester->getOutput()->isDecorated(), '->run() forces color output if --color is passed');
  240. $application = new Application();
  241. $application->setAutoExit(false);
  242. $application->setCatchExceptions(false);
  243. $tester = new ApplicationTester($application);
  244. $tester->run(array('--version' => true));
  245. $t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run4.txt'), '->run() displays the program version if --version is passed');
  246. $application = new Application();
  247. $application->setAutoExit(false);
  248. $application->setCatchExceptions(false);
  249. $tester = new ApplicationTester($application);
  250. $tester->run(array('command' => 'list', '--quiet' => true));
  251. $t->is($tester->getDisplay(), '', '->run() removes all output if --quiet is passed');
  252. $application = new Application();
  253. $application->setAutoExit(false);
  254. $application->setCatchExceptions(false);
  255. $tester = new ApplicationTester($application);
  256. $tester->run(array('command' => 'list', '--verbose' => true));
  257. $t->is($tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->run() sets the output to verbose is --verbose is passed');
  258. $application = new Application();
  259. $application->setAutoExit(false);
  260. $application->setCatchExceptions(false);
  261. $application->addCommand(new FooCommand());
  262. $tester = new ApplicationTester($application);
  263. $tester->run(array('command' => 'foo:bar', '--no-interaction' => true));
  264. $t->is($tester->getDisplay(), "called\n", '->run() does not called interact() if --no-interaction is passed');