ApplicationTest.php 14 KB

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