ApplicationTest.php 16 KB

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