CommandTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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('description', $command->getHelp(), '->getHelp() fallback 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. }
  126. public function testGetSetAliases()
  127. {
  128. $command = new \TestCommand();
  129. $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
  130. $ret = $command->setAliases(array('name1'));
  131. $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
  132. $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
  133. }
  134. public function testGetSynopsis()
  135. {
  136. $command = new \TestCommand();
  137. $command->addOption('foo');
  138. $command->addArgument('bar');
  139. $this->assertEquals('namespace:name [--foo] [--] [<bar>]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
  140. }
  141. public function testGetHelper()
  142. {
  143. $application = new Application();
  144. $command = new \TestCommand();
  145. $command->setApplication($application);
  146. $formatterHelper = new FormatterHelper();
  147. $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
  148. }
  149. public function testMergeApplicationDefinition()
  150. {
  151. $application1 = new Application();
  152. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  153. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  154. $command = new \TestCommand();
  155. $command->setApplication($application1);
  156. $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
  157. $r = new \ReflectionObject($command);
  158. $m = $r->getMethod('mergeApplicationDefinition');
  159. $m->setAccessible(true);
  160. $m->invoke($command);
  161. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  162. $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  163. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
  164. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
  165. $m->invoke($command);
  166. $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  167. }
  168. public function testMergeApplicationDefinitionWithoutArgsThenWithArgsAddsArgs()
  169. {
  170. $application1 = new Application();
  171. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  172. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  173. $command = new \TestCommand();
  174. $command->setApplication($application1);
  175. $command->setDefinition($definition = new InputDefinition(array()));
  176. $r = new \ReflectionObject($command);
  177. $m = $r->getMethod('mergeApplicationDefinition');
  178. $m->setAccessible(true);
  179. $m->invoke($command, false);
  180. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition(false) merges the application and the command options');
  181. $this->assertFalse($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(false) does not merge the application arguments');
  182. $m->invoke($command, true);
  183. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(true) merges the application arguments and the command arguments');
  184. $m->invoke($command);
  185. $this->assertEquals(2, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments');
  186. }
  187. public function testRunInteractive()
  188. {
  189. $tester = new CommandTester(new \TestCommand());
  190. $tester->execute(array(), array('interactive' => true));
  191. $this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
  192. }
  193. public function testRunNonInteractive()
  194. {
  195. $tester = new CommandTester(new \TestCommand());
  196. $tester->execute(array(), array('interactive' => false));
  197. $this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
  198. }
  199. /**
  200. * @expectedException \LogicException
  201. * @expectedExceptionMessage You must override the execute() method in the concrete command class.
  202. */
  203. public function testExecuteMethodNeedsToBeOverridden()
  204. {
  205. $command = new Command('foo');
  206. $command->run(new StringInput(''), new NullOutput());
  207. }
  208. /**
  209. * @expectedException \InvalidArgumentException
  210. * @expectedExceptionMessage The "--bar" option does not exist.
  211. */
  212. public function testRunWithInvalidOption()
  213. {
  214. $command = new \TestCommand();
  215. $tester = new CommandTester($command);
  216. $tester->execute(array('--bar' => true));
  217. }
  218. public function testRunReturnsIntegerExitCode()
  219. {
  220. $command = new \TestCommand();
  221. $exitCode = $command->run(new StringInput(''), new NullOutput());
  222. $this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');
  223. $command = $this->getMock('TestCommand', array('execute'));
  224. $command->expects($this->once())
  225. ->method('execute')
  226. ->will($this->returnValue('2.3'));
  227. $exitCode = $command->run(new StringInput(''), new NullOutput());
  228. $this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
  229. }
  230. public function testRunReturnsAlwaysInteger()
  231. {
  232. $command = new \TestCommand();
  233. $this->assertSame(0, $command->run(new StringInput(''), new NullOutput()));
  234. }
  235. public function testSetCode()
  236. {
  237. $command = new \TestCommand();
  238. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output) {
  239. $output->writeln('from the code...');
  240. });
  241. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  242. $tester = new CommandTester($command);
  243. $tester->execute(array());
  244. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  245. }
  246. public function testSetCodeWithNonClosureCallable()
  247. {
  248. $command = new \TestCommand();
  249. $ret = $command->setCode(array($this, 'callableMethodCommand'));
  250. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  251. $tester = new CommandTester($command);
  252. $tester->execute(array());
  253. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  254. }
  255. /**
  256. * @expectedException \InvalidArgumentException
  257. * @expectedExceptionMessage Invalid callable provided to Command::setCode.
  258. */
  259. public function testSetCodeWithNonCallable()
  260. {
  261. $command = new \TestCommand();
  262. $command->setCode(array($this, 'nonExistentMethod'));
  263. }
  264. public function callableMethodCommand(InputInterface $input, OutputInterface $output)
  265. {
  266. $output->writeln('from the code...');
  267. }
  268. /**
  269. * @group legacy
  270. */
  271. public function testLegacyAsText()
  272. {
  273. $command = new \TestCommand();
  274. $command->setApplication(new Application());
  275. $tester = new CommandTester($command);
  276. $tester->execute(array('command' => $command->getName()));
  277. $this->assertStringEqualsFile(self::$fixturesPath.'/command_astext.txt', $command->asText(), '->asText() returns a text representation of the command');
  278. }
  279. /**
  280. * @group legacy
  281. */
  282. public function testLegacyAsXml()
  283. {
  284. $command = new \TestCommand();
  285. $command->setApplication(new Application());
  286. $tester = new CommandTester($command);
  287. $tester->execute(array('command' => $command->getName()));
  288. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/command_asxml.txt', $command->asXml(), '->asXml() returns an XML representation of the command');
  289. }
  290. }