CommandTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Output\StreamOutput;
  22. use Symfony\Component\Console\Tester\CommandTester;
  23. class CommandTest extends \PHPUnit_Framework_TestCase
  24. {
  25. static protected $fixturesPath;
  26. static public function setUpBeforeClass()
  27. {
  28. self::$fixturesPath = __DIR__.'/../Fixtures/';
  29. require_once self::$fixturesPath.'/TestCommand.php';
  30. }
  31. public function testConstructor()
  32. {
  33. try {
  34. $command = new Command();
  35. $this->fail('__construct() throws a \LogicException if the name is null');
  36. } catch (\Exception $e) {
  37. $this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the name is null');
  38. $this->assertEquals('The command name cannot be empty.', $e->getMessage(), '__construct() throws a \LogicException if the name is null');
  39. }
  40. $command = new Command('foo:bar');
  41. $this->assertEquals('foo:bar', $command->getFullName(), '__construct() takes the command name as its first argument');
  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 testGetNamespaceGetNameGetFullNameSetName()
  76. {
  77. $command = new \TestCommand();
  78. $this->assertEquals('namespace', $command->getNamespace(), '->getNamespace() returns the command namespace');
  79. $this->assertEquals('name', $command->getName(), '->getName() returns the command name');
  80. $this->assertEquals('namespace:name', $command->getFullName(), '->getNamespace() returns the full command name');
  81. $command->setName('foo');
  82. $this->assertEquals('foo', $command->getName(), '->setName() sets the command name');
  83. $command->setName(':bar');
  84. $this->assertEquals('bar', $command->getName(), '->setName() sets the command name');
  85. $this->assertEquals('', $command->getNamespace(), '->setName() can set the command namespace');
  86. $ret = $command->setName('foobar:bar');
  87. $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
  88. $this->assertEquals('bar', $command->getName(), '->setName() sets the command name');
  89. $this->assertEquals('foobar', $command->getNamespace(), '->setName() can set the command namespace');
  90. try {
  91. $command->setName('');
  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('A command name cannot be empty.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty');
  96. }
  97. try {
  98. $command->setName('foo:');
  99. $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  100. } catch (\Exception $e) {
  101. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty');
  102. $this->assertEquals('A command name cannot be empty.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty');
  103. }
  104. }
  105. public function testGetSetDescription()
  106. {
  107. $command = new \TestCommand();
  108. $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
  109. $ret = $command->setDescription('description1');
  110. $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
  111. $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
  112. }
  113. public function testGetSetHelp()
  114. {
  115. $command = new \TestCommand();
  116. $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
  117. $ret = $command->setHelp('help1');
  118. $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
  119. $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
  120. }
  121. public function testGetSetAliases()
  122. {
  123. $command = new \TestCommand();
  124. $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
  125. $ret = $command->setAliases(array('name1'));
  126. $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
  127. $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
  128. }
  129. public function testGetSynopsis()
  130. {
  131. $command = new \TestCommand();
  132. $command->addOption('foo');
  133. $command->addArgument('foo');
  134. $this->assertEquals('namespace:name [--foo] [foo]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
  135. }
  136. public function testGetHelper()
  137. {
  138. $application = new Application();
  139. $command = new \TestCommand();
  140. $command->setApplication($application);
  141. $formatterHelper = new FormatterHelper();
  142. $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
  143. }
  144. public function testGet()
  145. {
  146. $application = new Application();
  147. $command = new \TestCommand();
  148. $command->setApplication($application);
  149. $formatterHelper = new FormatterHelper();
  150. $this->assertEquals($formatterHelper->getName(), $command->formatter->getName(), '->__get() returns the correct helper');
  151. }
  152. public function testMergeApplicationDefinition()
  153. {
  154. $application1 = new Application();
  155. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  156. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  157. $command = new \TestCommand();
  158. $command->setApplication($application1);
  159. $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
  160. $command->mergeApplicationDefinition();
  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. $command->mergeApplicationDefinition();
  166. $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  167. $command = new \TestCommand();
  168. $command->mergeApplicationDefinition();
  169. }
  170. public function testRun()
  171. {
  172. $command = new \TestCommand();
  173. $tester = new CommandTester($command);
  174. try {
  175. $tester->execute(array('--bar' => true));
  176. $this->fail('->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  177. } catch (\Exception $e) {
  178. $this->assertInstanceOf('\InvalidArgumentException', $e, '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  179. $this->assertEquals('The "--bar" option does not exist.', $e->getMessage(), '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  180. }
  181. $this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->execute(array(), array('interactive' => true)), '->run() calls the interact() method if the input is interactive');
  182. $this->assertEquals('execute called'.PHP_EOL, $tester->execute(array(), array('interactive' => false)), '->run() does not call the interact() method if the input is not interactive');
  183. $command = new Command('foo');
  184. try {
  185. $command->run(new StringInput(''), new NullOutput());
  186. $this->fail('->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
  187. } catch (\Exception $e) {
  188. $this->assertInstanceOf('\LogicException', $e, '->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
  189. $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');
  190. }
  191. }
  192. public function testSetCode()
  193. {
  194. $command = new \TestCommand();
  195. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output)
  196. {
  197. $output->writeln('from the code...');
  198. });
  199. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  200. $tester = new CommandTester($command);
  201. $tester->execute(array());
  202. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  203. }
  204. public function testAsText()
  205. {
  206. $command = new \TestCommand();
  207. $command->setApplication(new Application());
  208. $tester = new CommandTester($command);
  209. $tester->execute(array('command' => $command->getFullName()));
  210. $this->assertStringEqualsFile(self::$fixturesPath.'/command_astext.txt', $command->asText(), '->asText() returns a text representation of the command');
  211. }
  212. public function testAsXml()
  213. {
  214. $command = new \TestCommand();
  215. $command->setApplication(new Application());
  216. $tester = new CommandTester($command);
  217. $tester->execute(array('command' => $command->getFullName()));
  218. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/command_asxml.txt', $command->asXml(), '->asXml() returns an XML representation of the command');
  219. }
  220. }