CommandTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. }
  50. public function testSetGetDefinition()
  51. {
  52. $command = new \TestCommand();
  53. $ret = $command->setDefinition($definition = new InputDefinition());
  54. $this->assertEquals($command, $ret, '->setDefinition() implements a fluent interface');
  55. $this->assertEquals($definition, $command->getDefinition(), '->setDefinition() sets the current InputDefinition instance');
  56. $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar')));
  57. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  58. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  59. $command->setDefinition(new InputDefinition());
  60. }
  61. public function testAddArgument()
  62. {
  63. $command = new \TestCommand();
  64. $ret = $command->addArgument('foo');
  65. $this->assertEquals($command, $ret, '->addArgument() implements a fluent interface');
  66. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
  67. }
  68. public function testAddOption()
  69. {
  70. $command = new \TestCommand();
  71. $ret = $command->addOption('foo');
  72. $this->assertEquals($command, $ret, '->addOption() implements a fluent interface');
  73. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
  74. }
  75. public function testGetNamespaceGetNameSetName()
  76. {
  77. $command = new \TestCommand();
  78. $this->assertEquals('namespace:name', $command->getName(), '->getName() returns the command name');
  79. $command->setName('foo');
  80. $this->assertEquals('foo', $command->getName(), '->setName() sets the command name');
  81. $ret = $command->setName('foobar:bar');
  82. $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
  83. $this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
  84. }
  85. /**
  86. * @dataProvider provideInvalidCommandNames
  87. */
  88. public function testInvalidCommandNames($name)
  89. {
  90. $this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
  91. $command = new \TestCommand();
  92. $command->setName($name);
  93. }
  94. public function provideInvalidCommandNames()
  95. {
  96. return array(
  97. array(''),
  98. array('foo:'),
  99. );
  100. }
  101. public function testGetSetDescription()
  102. {
  103. $command = new \TestCommand();
  104. $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
  105. $ret = $command->setDescription('description1');
  106. $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
  107. $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
  108. }
  109. public function testGetSetHelp()
  110. {
  111. $command = new \TestCommand();
  112. $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
  113. $ret = $command->setHelp('help1');
  114. $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
  115. $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
  116. $command->setHelp('');
  117. $this->assertEquals('', $command->getHelp(), '->getHelp() does not fall back to the description');
  118. }
  119. public function testGetProcessedHelp()
  120. {
  121. $command = new \TestCommand();
  122. $command->setHelp('The %command.name% command does... Example: php %command.full_name%.');
  123. $this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly');
  124. $this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name%');
  125. $command = new \TestCommand();
  126. $command->setHelp('');
  127. $this->assertContains('description', $command->getProcessedHelp(), '->getProcessedHelp() falls back to the description');
  128. }
  129. public function testGetSetAliases()
  130. {
  131. $command = new \TestCommand();
  132. $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
  133. $ret = $command->setAliases(array('name1'));
  134. $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
  135. $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
  136. }
  137. public function testGetSynopsis()
  138. {
  139. $command = new \TestCommand();
  140. $command->addOption('foo');
  141. $command->addArgument('bar');
  142. $this->assertEquals('namespace:name [--foo] [--] [<bar>]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
  143. }
  144. public function testGetHelper()
  145. {
  146. $application = new Application();
  147. $command = new \TestCommand();
  148. $command->setApplication($application);
  149. $formatterHelper = new FormatterHelper();
  150. $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
  151. }
  152. /**
  153. * @expectedException \LogicException
  154. * @expectedExceptionMessage Cannot retrieve helper "formatter" because there is no HelperSet defined.
  155. */
  156. public function testGetHelperWithoutHelperSet()
  157. {
  158. $command = new \TestCommand();
  159. $command->getHelper('formatter');
  160. }
  161. public function testMergeApplicationDefinition()
  162. {
  163. $application1 = new Application();
  164. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  165. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  166. $command = new \TestCommand();
  167. $command->setApplication($application1);
  168. $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
  169. $r = new \ReflectionObject($command);
  170. $m = $r->getMethod('mergeApplicationDefinition');
  171. $m->setAccessible(true);
  172. $m->invoke($command);
  173. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  174. $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  175. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
  176. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
  177. $m->invoke($command);
  178. $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  179. }
  180. public function testMergeApplicationDefinitionWithoutArgsThenWithArgsAddsArgs()
  181. {
  182. $application1 = new Application();
  183. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  184. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  185. $command = new \TestCommand();
  186. $command->setApplication($application1);
  187. $command->setDefinition($definition = new InputDefinition(array()));
  188. $r = new \ReflectionObject($command);
  189. $m = $r->getMethod('mergeApplicationDefinition');
  190. $m->setAccessible(true);
  191. $m->invoke($command, false);
  192. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition(false) merges the application and the command options');
  193. $this->assertFalse($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(false) does not merge the application arguments');
  194. $m->invoke($command, true);
  195. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(true) merges the application arguments and the command arguments');
  196. $m->invoke($command);
  197. $this->assertEquals(2, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments');
  198. }
  199. public function testRunInteractive()
  200. {
  201. $tester = new CommandTester(new \TestCommand());
  202. $tester->execute(array(), array('interactive' => true));
  203. $this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
  204. }
  205. public function testRunNonInteractive()
  206. {
  207. $tester = new CommandTester(new \TestCommand());
  208. $tester->execute(array(), array('interactive' => false));
  209. $this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
  210. }
  211. /**
  212. * @expectedException \LogicException
  213. * @expectedExceptionMessage You must override the execute() method in the concrete command class.
  214. */
  215. public function testExecuteMethodNeedsToBeOverridden()
  216. {
  217. $command = new Command('foo');
  218. $command->run(new StringInput(''), new NullOutput());
  219. }
  220. /**
  221. * @expectedException Symfony\Component\Console\Exception\InvalidOptionException
  222. * @expectedExceptionMessage The "--bar" option does not exist.
  223. */
  224. public function testRunWithInvalidOption()
  225. {
  226. $command = new \TestCommand();
  227. $tester = new CommandTester($command);
  228. $tester->execute(array('--bar' => true));
  229. }
  230. public function testRunReturnsIntegerExitCode()
  231. {
  232. $command = new \TestCommand();
  233. $exitCode = $command->run(new StringInput(''), new NullOutput());
  234. $this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');
  235. $command = $this->getMock('TestCommand', array('execute'));
  236. $command->expects($this->once())
  237. ->method('execute')
  238. ->will($this->returnValue('2.3'));
  239. $exitCode = $command->run(new StringInput(''), new NullOutput());
  240. $this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
  241. }
  242. public function testRunWithApplication()
  243. {
  244. $command = new \TestCommand();
  245. $command->setApplication(new Application());
  246. $exitCode = $command->run(new StringInput(''), new NullOutput());
  247. $this->assertSame(0, $exitCode, '->run() returns an integer exit code');
  248. }
  249. public function testRunReturnsAlwaysInteger()
  250. {
  251. $command = new \TestCommand();
  252. $this->assertSame(0, $command->run(new StringInput(''), new NullOutput()));
  253. }
  254. public function testSetCode()
  255. {
  256. $command = new \TestCommand();
  257. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output) {
  258. $output->writeln('from the code...');
  259. });
  260. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  261. $tester = new CommandTester($command);
  262. $tester->execute(array());
  263. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  264. }
  265. public function getSetCodeBindToClosureTests()
  266. {
  267. return array(
  268. array(true, 'not bound to the command'),
  269. array(false, 'bound to the command'),
  270. );
  271. }
  272. /**
  273. * @dataProvider getSetCodeBindToClosureTests
  274. */
  275. public function testSetCodeBindToClosure($previouslyBound, $expected)
  276. {
  277. $code = createClosure();
  278. if ($previouslyBound) {
  279. $code = $code->bindTo($this);
  280. }
  281. $command = new \TestCommand();
  282. $command->setCode($code);
  283. $tester = new CommandTester($command);
  284. $tester->execute(array());
  285. $this->assertEquals('interact called'.PHP_EOL.$expected.PHP_EOL, $tester->getDisplay());
  286. }
  287. public function testSetCodeWithNonClosureCallable()
  288. {
  289. $command = new \TestCommand();
  290. $ret = $command->setCode(array($this, 'callableMethodCommand'));
  291. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  292. $tester = new CommandTester($command);
  293. $tester->execute(array());
  294. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  295. }
  296. public function callableMethodCommand(InputInterface $input, OutputInterface $output)
  297. {
  298. $output->writeln('from the code...');
  299. }
  300. }
  301. // In order to get an unbound closure, we should create it outside a class
  302. // scope.
  303. function createClosure()
  304. {
  305. return function (InputInterface $input, OutputInterface $output) {
  306. $output->writeln($this instanceof Command ? 'bound to the command' : 'not bound to the command');
  307. };
  308. }