CommandTest.php 18 KB

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