No Description

CommandTest.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\FormatterHelper;
  13. use Symfony\Component\Console\Application;
  14. use Symfony\Component\Console\Input\InputDefinition;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\StringInput;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Output\NullOutput;
  21. use Symfony\Component\Console\Tester\CommandTester;
  22. class CommandTest extends \PHPUnit_Framework_TestCase
  23. {
  24. protected static $fixturesPath;
  25. public static function setUpBeforeClass()
  26. {
  27. self::$fixturesPath = __DIR__.'/../Fixtures/';
  28. require_once self::$fixturesPath.'/TestCommand.php';
  29. }
  30. public function testConstructor()
  31. {
  32. $command = new Command('foo:bar');
  33. $this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
  34. }
  35. /**
  36. * @expectedException \LogicException
  37. * @expectedExceptionMessage The command defined in "Symfony\Component\Console\Command\Command" cannot have an empty name.
  38. */
  39. public function testCommandNameCannotBeEmpty()
  40. {
  41. new Command();
  42. }
  43. public function testSetApplication()
  44. {
  45. $application = new Application();
  46. $command = new \TestCommand();
  47. $command->setApplication($application);
  48. $this->assertEquals($application, $command->getApplication(), '->setApplication() sets the current application');
  49. $this->assertEquals($application->getHelperSet(), $command->getHelperSet());
  50. }
  51. public function testSetApplicationNull()
  52. {
  53. $command = new \TestCommand();
  54. $command->setApplication(null);
  55. $this->assertNull($command->getHelperSet());
  56. }
  57. public function testSetGetDefinition()
  58. {
  59. $command = new \TestCommand();
  60. $ret = $command->setDefinition($definition = new InputDefinition());
  61. $this->assertEquals($command, $ret, '->setDefinition() implements a fluent interface');
  62. $this->assertEquals($definition, $command->getDefinition(), '->setDefinition() sets the current InputDefinition instance');
  63. $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar')));
  64. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  65. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  66. $command->setDefinition(new InputDefinition());
  67. }
  68. public function testAddArgument()
  69. {
  70. $command = new \TestCommand();
  71. $ret = $command->addArgument('foo');
  72. $this->assertEquals($command, $ret, '->addArgument() implements a fluent interface');
  73. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
  74. }
  75. public function testAddOption()
  76. {
  77. $command = new \TestCommand();
  78. $ret = $command->addOption('foo');
  79. $this->assertEquals($command, $ret, '->addOption() implements a fluent interface');
  80. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
  81. }
  82. public function testSetHidden()
  83. {
  84. $command = new \TestCommand();
  85. $command->setHidden(true);
  86. $this->assertTrue($command->isHidden());
  87. }
  88. public function testGetNamespaceGetNameSetName()
  89. {
  90. $command = new \TestCommand();
  91. $this->assertEquals('namespace:name', $command->getName(), '->getName() returns the command name');
  92. $command->setName('foo');
  93. $this->assertEquals('foo', $command->getName(), '->setName() sets the command name');
  94. $ret = $command->setName('foobar:bar');
  95. $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
  96. $this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
  97. }
  98. /**
  99. * @dataProvider provideInvalidCommandNames
  100. */
  101. public function testInvalidCommandNames($name)
  102. {
  103. $this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
  104. $command = new \TestCommand();
  105. $command->setName($name);
  106. }
  107. public function provideInvalidCommandNames()
  108. {
  109. return array(
  110. array(''),
  111. array('foo:'),
  112. );
  113. }
  114. public function testGetSetDescription()
  115. {
  116. $command = new \TestCommand();
  117. $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
  118. $ret = $command->setDescription('description1');
  119. $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
  120. $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
  121. }
  122. public function testGetSetHelp()
  123. {
  124. $command = new \TestCommand();
  125. $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
  126. $ret = $command->setHelp('help1');
  127. $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
  128. $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
  129. $command->setHelp('');
  130. $this->assertEquals('', $command->getHelp(), '->getHelp() does not fall back to the description');
  131. }
  132. public function testGetProcessedHelp()
  133. {
  134. $command = new \TestCommand();
  135. $command->setHelp('The %command.name% command does... Example: php %command.full_name%.');
  136. $this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly');
  137. $this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name%');
  138. $command = new \TestCommand();
  139. $command->setHelp('');
  140. $this->assertContains('description', $command->getProcessedHelp(), '->getProcessedHelp() falls back to the description');
  141. }
  142. public function testGetSetAliases()
  143. {
  144. $command = new \TestCommand();
  145. $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
  146. $ret = $command->setAliases(array('name1'));
  147. $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
  148. $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
  149. }
  150. public function testSetAliasesNull()
  151. {
  152. $command = new \TestCommand();
  153. $this->setExpectedException('InvalidArgumentException');
  154. $command->setAliases(null);
  155. }
  156. public function testGetSynopsis()
  157. {
  158. $command = new \TestCommand();
  159. $command->addOption('foo');
  160. $command->addArgument('bar');
  161. $this->assertEquals('namespace:name [--foo] [--] [<bar>]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
  162. }
  163. public function testAddGetUsages()
  164. {
  165. $command = new \TestCommand();
  166. $command->addUsage('foo1');
  167. $command->addUsage('foo2');
  168. $this->assertContains('namespace:name foo1', $command->getUsages());
  169. $this->assertContains('namespace:name foo2', $command->getUsages());
  170. }
  171. public function testGetHelper()
  172. {
  173. $application = new Application();
  174. $command = new \TestCommand();
  175. $command->setApplication($application);
  176. $formatterHelper = new FormatterHelper();
  177. $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
  178. }
  179. /**
  180. * @expectedException \LogicException
  181. * @expectedExceptionMessage Cannot retrieve helper "formatter" because there is no HelperSet defined.
  182. */
  183. public function testGetHelperWithoutHelperSet()
  184. {
  185. $command = new \TestCommand();
  186. $command->getHelper('formatter');
  187. }
  188. public function testMergeApplicationDefinition()
  189. {
  190. $application1 = new Application();
  191. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  192. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  193. $command = new \TestCommand();
  194. $command->setApplication($application1);
  195. $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
  196. $r = new \ReflectionObject($command);
  197. $m = $r->getMethod('mergeApplicationDefinition');
  198. $m->setAccessible(true);
  199. $m->invoke($command);
  200. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  201. $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  202. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
  203. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
  204. $m->invoke($command);
  205. $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  206. }
  207. public function testMergeApplicationDefinitionWithoutArgsThenWithArgsAddsArgs()
  208. {
  209. $application1 = new Application();
  210. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  211. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  212. $command = new \TestCommand();
  213. $command->setApplication($application1);
  214. $command->setDefinition($definition = new InputDefinition(array()));
  215. $r = new \ReflectionObject($command);
  216. $m = $r->getMethod('mergeApplicationDefinition');
  217. $m->setAccessible(true);
  218. $m->invoke($command, false);
  219. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition(false) merges the application and the command options');
  220. $this->assertFalse($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(false) does not merge the application arguments');
  221. $m->invoke($command, true);
  222. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(true) merges the application arguments and the command arguments');
  223. $m->invoke($command);
  224. $this->assertEquals(2, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments');
  225. }
  226. public function testRunInteractive()
  227. {
  228. $tester = new CommandTester(new \TestCommand());
  229. $tester->execute(array(), array('interactive' => true));
  230. $this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
  231. }
  232. public function testRunNonInteractive()
  233. {
  234. $tester = new CommandTester(new \TestCommand());
  235. $tester->execute(array(), array('interactive' => false));
  236. $this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
  237. }
  238. /**
  239. * @expectedException \LogicException
  240. * @expectedExceptionMessage You must override the execute() method in the concrete command class.
  241. */
  242. public function testExecuteMethodNeedsToBeOverridden()
  243. {
  244. $command = new Command('foo');
  245. $command->run(new StringInput(''), new NullOutput());
  246. }
  247. /**
  248. * @expectedException \Symfony\Component\Console\Exception\InvalidOptionException
  249. * @expectedExceptionMessage The "--bar" option does not exist.
  250. */
  251. public function testRunWithInvalidOption()
  252. {
  253. $command = new \TestCommand();
  254. $tester = new CommandTester($command);
  255. $tester->execute(array('--bar' => true));
  256. }
  257. public function testRunReturnsIntegerExitCode()
  258. {
  259. $command = new \TestCommand();
  260. $exitCode = $command->run(new StringInput(''), new NullOutput());
  261. $this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');
  262. $command = $this->getMockBuilder('TestCommand')->setMethods(array('execute'))->getMock();
  263. $command->expects($this->once())
  264. ->method('execute')
  265. ->will($this->returnValue('2.3'));
  266. $exitCode = $command->run(new StringInput(''), new NullOutput());
  267. $this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
  268. }
  269. public function testRunWithApplication()
  270. {
  271. $command = new \TestCommand();
  272. $command->setApplication(new Application());
  273. $exitCode = $command->run(new StringInput(''), new NullOutput());
  274. $this->assertSame(0, $exitCode, '->run() returns an integer exit code');
  275. }
  276. public function testRunReturnsAlwaysInteger()
  277. {
  278. $command = new \TestCommand();
  279. $this->assertSame(0, $command->run(new StringInput(''), new NullOutput()));
  280. }
  281. public function testRunWithProcessTitle()
  282. {
  283. $command = new \TestCommand();
  284. $command->setApplication(new Application());
  285. $command->setProcessTitle('foo');
  286. $this->assertSame(0, $command->run(new StringInput(''), new NullOutput()));
  287. if (function_exists('cli_set_process_title')) {
  288. if (null === @cli_get_process_title() && 'Darwin' === PHP_OS) {
  289. $this->markTestSkipped('Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.');
  290. }
  291. $this->assertEquals('foo', cli_get_process_title());
  292. }
  293. }
  294. public function testSetCode()
  295. {
  296. $command = new \TestCommand();
  297. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output) {
  298. $output->writeln('from the code...');
  299. });
  300. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  301. $tester = new CommandTester($command);
  302. $tester->execute(array());
  303. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  304. }
  305. public function getSetCodeBindToClosureTests()
  306. {
  307. return array(
  308. array(true, 'not bound to the command'),
  309. array(false, 'bound to the command'),
  310. );
  311. }
  312. /**
  313. * @dataProvider getSetCodeBindToClosureTests
  314. */
  315. public function testSetCodeBindToClosure($previouslyBound, $expected)
  316. {
  317. $code = createClosure();
  318. if ($previouslyBound) {
  319. $code = $code->bindTo($this);
  320. }
  321. $command = new \TestCommand();
  322. $command->setCode($code);
  323. $tester = new CommandTester($command);
  324. $tester->execute(array());
  325. $this->assertEquals('interact called'.PHP_EOL.$expected.PHP_EOL, $tester->getDisplay());
  326. }
  327. public function testSetCodeWithStaticClosure()
  328. {
  329. $command = new \TestCommand();
  330. $command->setCode(self::createClosure());
  331. $tester = new CommandTester($command);
  332. $tester->execute(array());
  333. if (PHP_VERSION_ID < 70000) {
  334. // Cannot bind static closures in PHP 5
  335. $this->assertEquals('interact called'.PHP_EOL.'not bound'.PHP_EOL, $tester->getDisplay());
  336. } else {
  337. // Can bind static closures in PHP 7
  338. $this->assertEquals('interact called'.PHP_EOL.'bound'.PHP_EOL, $tester->getDisplay());
  339. }
  340. }
  341. private static function createClosure()
  342. {
  343. return function (InputInterface $input, OutputInterface $output) {
  344. $output->writeln(isset($this) ? 'bound' : 'not bound');
  345. };
  346. }
  347. public function testSetCodeWithNonClosureCallable()
  348. {
  349. $command = new \TestCommand();
  350. $ret = $command->setCode(array($this, 'callableMethodCommand'));
  351. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  352. $tester = new CommandTester($command);
  353. $tester->execute(array());
  354. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  355. }
  356. public function callableMethodCommand(InputInterface $input, OutputInterface $output)
  357. {
  358. $output->writeln('from the code...');
  359. }
  360. }
  361. // In order to get an unbound closure, we should create it outside a class
  362. // scope.
  363. function createClosure()
  364. {
  365. return function (InputInterface $input, OutputInterface $output) {
  366. $output->writeln($this instanceof Command ? 'bound to the command' : 'not bound to the command');
  367. };
  368. }