ApplicationTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. $application->getCommand('foofoo');
  91. $this->fail('->getCommand() throws an \InvalidArgumentException if the command does not exist');
  92. } catch (\Exception $e) {
  93. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getCommand() throws an \InvalidArgumentException if the command does not exist');
  94. $this->assertEquals('The command "foofoo" does not exist.', $e->getMessage(), '->getCommand() throws an \InvalidArgumentException if the command does not exist');
  95. }
  96. $application = new TestApplication();
  97. $application->addCommand($foo = new \FooCommand());
  98. $application->setWantHelps();
  99. $command = $application->getCommand('foo:bar');
  100. $this->assertEquals('Symfony\Components\Console\Command\HelpCommand', get_class($command), '->getCommand() returns the help command if --help is provided as the input');
  101. }
  102. public function testGetNamespaces()
  103. {
  104. $application = new TestApplication();
  105. $application->addCommand(new \FooCommand());
  106. $application->addCommand(new \Foo1Command());
  107. $this->assertEquals(array('foo'), $application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces');
  108. }
  109. public function testFindNamespace()
  110. {
  111. $application = new TestApplication();
  112. $application->addCommand(new \FooCommand());
  113. $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
  114. $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
  115. $application->addCommand(new \Foo2Command());
  116. $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
  117. try {
  118. $application->findNamespace('f');
  119. $this->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  120. } catch (\Exception $e) {
  121. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  122. $this->assertEquals('The namespace "f" is ambiguous (foo, foo1).', $e->getMessage(), '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous');
  123. }
  124. try {
  125. $application->findNamespace('bar');
  126. $this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  127. } catch (\Exception $e) {
  128. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace');
  129. $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');
  130. }
  131. }
  132. public function testFindCommand()
  133. {
  134. $application = new TestApplication();
  135. $application->addCommand(new \FooCommand());
  136. $this->assertEquals('FooCommand', get_class($application->findCommand('foo:bar')), '->findCommand() returns a command if its name exists');
  137. $this->assertEquals('Symfony\Components\Console\Command\HelpCommand', get_class($application->findCommand('h')), '->findCommand() returns a command if its name exists');
  138. $this->assertEquals('FooCommand', get_class($application->findCommand('f:bar')), '->findCommand() returns a command if the abbreviation for the namespace exists');
  139. $this->assertEquals('FooCommand', get_class($application->findCommand('f:b')), '->findCommand() returns a command if the abbreviation for the namespace and the command name exist');
  140. $this->assertEquals('FooCommand', get_class($application->findCommand('a')), '->findCommand() returns a command if the abbreviation exists for an alias');
  141. $application->addCommand(new \Foo1Command());
  142. $application->addCommand(new \Foo2Command());
  143. try {
  144. $application->findCommand('f');
  145. $this->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  146. } catch (\Exception $e) {
  147. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  148. $this->assertEquals('Command "f" is not defined.', $e->getMessage(), '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace');
  149. }
  150. try {
  151. $application->findCommand('a');
  152. $this->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  153. } catch (\Exception $e) {
  154. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias');
  155. $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');
  156. }
  157. try {
  158. $application->findCommand('foo:b');
  159. $this->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
  160. } catch (\Exception $e) {
  161. $this->assertInstanceOf('\InvalidArgumentException', $e, '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command');
  162. $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');
  163. }
  164. }
  165. public function testSetCatchExceptions()
  166. {
  167. $application = new Application();
  168. $application->setAutoExit(false);
  169. $tester = new ApplicationTester($application);
  170. $application->setCatchExceptions(true);
  171. $tester->run(array('command' => 'foo'));
  172. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getDisplay(), '->setCatchExceptions() sets the catch exception flag');
  173. $application->setCatchExceptions(false);
  174. try {
  175. $tester->run(array('command' => 'foo'));
  176. $this->fail('->setCatchExceptions() sets the catch exception flag');
  177. } catch (\Exception $e) {
  178. $this->assertInstanceOf('\Exception', $e, '->setCatchExceptions() sets the catch exception flag');
  179. $this->assertEquals('Command "foo" is not defined.', $e->getMessage(), '->setCatchExceptions() sets the catch exception flag');
  180. }
  181. }
  182. public function testAsText()
  183. {
  184. $application = new Application();
  185. $application->addCommand(new \FooCommand);
  186. $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext1.txt', $application->asText(), '->asText() returns a text representation of the application');
  187. $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext2.txt', $application->asText('foo'), '->asText() returns a text representation of the application');
  188. }
  189. public function testAsXml()
  190. {
  191. $application = new Application();
  192. $application->addCommand(new \FooCommand);
  193. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml1.txt', $application->asXml(), '->asXml() returns an XML representation of the application');
  194. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/application_asxml2.txt', $application->asXml('foo'), '->asXml() returns an XML representation of the application');
  195. }
  196. public function testRenderException()
  197. {
  198. $application = new Application();
  199. $application->setAutoExit(false);
  200. $tester = new ApplicationTester($application);
  201. $tester->run(array('command' => 'foo'));
  202. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getDisplay(), '->renderException() renders a pretty exception');
  203. $tester->run(array('command' => 'foo'), array('verbosity' => Output::VERBOSITY_VERBOSE));
  204. $this->assertRegExp('/Exception trace/', $tester->getDisplay(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
  205. $tester->run(array('command' => 'list', '--foo' => true));
  206. $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $tester->getDisplay(), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
  207. }
  208. public function testRun()
  209. {
  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. $this->assertEquals('Symfony\Components\Console\Input\ArgvInput', get_class($command->input), '->run() creates an ArgvInput by default if none is given');
  219. $this->assertEquals('Symfony\Components\Console\Output\ConsoleOutput', get_class($command->output), '->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. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $tester->getDisplay(), '->run() runs the list command if no argument is passed');
  226. $tester->run(array('--help' => true));
  227. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $tester->getDisplay(), '->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. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $tester->getDisplay(), '->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. $this->assertTrue($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. $this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $tester->getDisplay(), '->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. $this->assertEquals('', $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. $this->assertEquals(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->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. $this->assertEquals("called\n", $tester->getDisplay(), '->run() does not called interact() if --no-interaction is passed');
  265. }
  266. }
  267. class TestApplication extends Application
  268. {
  269. public function setWantHelps()
  270. {
  271. $this->wantHelps = true;
  272. }
  273. }