CommandTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\Tests\Component\Console\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. static protected $fixturesPath;
  25. static public function setUpBeforeClass()
  26. {
  27. self::$fixturesPath = __DIR__.'/../Fixtures/';
  28. require_once self::$fixturesPath.'/TestCommand.php';
  29. }
  30. public function testConstructor()
  31. {
  32. try {
  33. $command = new Command();
  34. $this->fail('__construct() throws a \LogicException if the name is null');
  35. } catch (\Exception $e) {
  36. $this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the name is null');
  37. $this->assertEquals('The command name cannot be empty.', $e->getMessage(), '__construct() throws a \LogicException if the name is null');
  38. }
  39. $command = new Command('foo:bar');
  40. $this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
  41. }
  42. public function testSetApplication()
  43. {
  44. $application = new Application();
  45. $command = new \TestCommand();
  46. $command->setApplication($application);
  47. $this->assertEquals($application, $command->getApplication(), '->setApplication() sets the current application');
  48. }
  49. public function testSetGetDefinition()
  50. {
  51. $command = new \TestCommand();
  52. $ret = $command->setDefinition($definition = new InputDefinition());
  53. $this->assertEquals($command, $ret, '->setDefinition() implements a fluent interface');
  54. $this->assertEquals($definition, $command->getDefinition(), '->setDefinition() sets the current InputDefinition instance');
  55. $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar')));
  56. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  57. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  58. $command->setDefinition(new InputDefinition());
  59. }
  60. public function testAddArgument()
  61. {
  62. $command = new \TestCommand();
  63. $ret = $command->addArgument('foo');
  64. $this->assertEquals($command, $ret, '->addArgument() implements a fluent interface');
  65. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
  66. }
  67. public function testAddOption()
  68. {
  69. $command = new \TestCommand();
  70. $ret = $command->addOption('foo');
  71. $this->assertEquals($command, $ret, '->addOption() implements a fluent interface');
  72. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
  73. }
  74. public function testGetNamespaceGetNameSetName()
  75. {
  76. $command = new \TestCommand();
  77. $this->assertEquals('namespace:name', $command->getName(), '->getName() returns the command name');
  78. $command->setName('foo');
  79. $this->assertEquals('foo', $command->getName(), '->setName() sets the command name');
  80. $ret = $command->setName('foobar:bar');
  81. $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
  82. $this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
  83. try {
  84. $command->setName('');
  85. $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  86. } catch (\Exception $e) {
  87. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty');
  88. $this->assertEquals('Command name "" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty');
  89. }
  90. try {
  91. $command->setName('foo:');
  92. $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  93. } catch (\Exception $e) {
  94. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty');
  95. $this->assertEquals('Command name "foo:" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty');
  96. }
  97. }
  98. public function testGetSetDescription()
  99. {
  100. $command = new \TestCommand();
  101. $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
  102. $ret = $command->setDescription('description1');
  103. $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
  104. $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
  105. }
  106. public function testGetSetHelp()
  107. {
  108. $command = new \TestCommand();
  109. $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
  110. $ret = $command->setHelp('help1');
  111. $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
  112. $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
  113. }
  114. public function testGetSetAliases()
  115. {
  116. $command = new \TestCommand();
  117. $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
  118. $ret = $command->setAliases(array('name1'));
  119. $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
  120. $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
  121. }
  122. public function testGetSynopsis()
  123. {
  124. $command = new \TestCommand();
  125. $command->addOption('foo');
  126. $command->addArgument('foo');
  127. $this->assertEquals('namespace:name [--foo] [foo]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
  128. }
  129. public function testGetHelper()
  130. {
  131. $application = new Application();
  132. $command = new \TestCommand();
  133. $command->setApplication($application);
  134. $formatterHelper = new FormatterHelper();
  135. $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
  136. }
  137. public function testGet()
  138. {
  139. $application = new Application();
  140. $command = new \TestCommand();
  141. $command->setApplication($application);
  142. $formatterHelper = new FormatterHelper();
  143. $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->__get() returns the correct helper');
  144. }
  145. public function testMergeApplicationDefinition()
  146. {
  147. $application1 = new Application();
  148. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  149. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  150. $command = new \TestCommand();
  151. $command->setApplication($application1);
  152. $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
  153. $r = new \ReflectionObject($command);
  154. $m = $r->getMethod('mergeApplicationDefinition');
  155. $m->setAccessible(true);
  156. $m->invoke($command);
  157. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  158. $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  159. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
  160. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
  161. $m->invoke($command);
  162. $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  163. }
  164. public function testRun()
  165. {
  166. $command = new \TestCommand();
  167. $tester = new CommandTester($command);
  168. try {
  169. $tester->execute(array('--bar' => true));
  170. $this->fail('->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  171. } catch (\Exception $e) {
  172. $this->assertInstanceOf('\InvalidArgumentException', $e, '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  173. $this->assertEquals('The "--bar" option does not exist.', $e->getMessage(), '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  174. }
  175. $tester->execute(array(), array('interactive' => true));
  176. $this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
  177. $tester->execute(array(), array('interactive' => false));
  178. $this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
  179. $command = new Command('foo');
  180. try {
  181. $command->run(new StringInput(''), new NullOutput());
  182. $this->fail('->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
  183. } catch (\Exception $e) {
  184. $this->assertInstanceOf('\LogicException', $e, '->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
  185. $this->assertEquals('You must override the execute() method in the concrete command class.', $e->getMessage(), '->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
  186. }
  187. }
  188. public function testSetCode()
  189. {
  190. $command = new \TestCommand();
  191. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output)
  192. {
  193. $output->writeln('from the code...');
  194. });
  195. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  196. $tester = new CommandTester($command);
  197. $tester->execute(array());
  198. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  199. }
  200. public function testAsText()
  201. {
  202. $command = new \TestCommand();
  203. $command->setApplication(new Application());
  204. $tester = new CommandTester($command);
  205. $tester->execute(array('command' => $command->getName()));
  206. $this->assertStringEqualsFile(self::$fixturesPath.'/command_astext.txt', $command->asText(), '->asText() returns a text representation of the command');
  207. }
  208. public function testAsXml()
  209. {
  210. $command = new \TestCommand();
  211. $command->setApplication(new Application());
  212. $tester = new CommandTester($command);
  213. $tester->execute(array('command' => $command->getName()));
  214. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/command_asxml.txt', $command->asXml(), '->asXml() returns an XML representation of the command');
  215. }
  216. }