CommandTest.php 14 KB

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