ApplicationTest.php 14 KB

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