CommandTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\Console\Command;
  10. use Symfony\Components\Console\Command\Command;
  11. use Symfony\Components\Console\Helper\FormatterHelper;
  12. use Symfony\Components\Console\Application;
  13. use Symfony\Components\Console\Input\InputDefinition;
  14. use Symfony\Components\Console\Input\InputArgument;
  15. use Symfony\Components\Console\Input\InputOption;
  16. use Symfony\Components\Console\Input\InputInterface;
  17. use Symfony\Components\Console\Input\StringInput;
  18. use Symfony\Components\Console\Output\OutputInterface;
  19. use Symfony\Components\Console\Output\NullOutput;
  20. use Symfony\Components\Console\Output\StreamOutput;
  21. use Symfony\Components\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/Symfony/Components/Console/';
  28. require_once self::$fixturesPath.'/TestCommand.php';
  29. }
  30. public function testConstructor()
  31. {
  32. $application = new Application();
  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. $application = new Application();
  174. $command->setApplication($application);
  175. $tester = new CommandTester($command);
  176. try {
  177. $tester->execute(array('--bar' => true));
  178. $this->fail('->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  179. } catch (\Exception $e) {
  180. $this->assertInstanceOf('\InvalidArgumentException', $e, '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  181. $this->assertEquals('The "--bar" option does not exist.', $e->getMessage(), '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
  182. }
  183. $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');
  184. $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');
  185. $command = new Command('foo');
  186. try {
  187. $command->run(new StringInput(''), new NullOutput());
  188. $this->fail('->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided');
  189. } catch (\Exception $e) {
  190. $this->assertInstanceOf('\LogicException', $e, '->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided');
  191. $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 overriden and no code has been provided');
  192. }
  193. }
  194. public function testSetCode()
  195. {
  196. $application = new Application();
  197. $command = new \TestCommand();
  198. $command->setApplication($application);
  199. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output)
  200. {
  201. $output->writeln('from the code...');
  202. });
  203. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  204. $tester = new CommandTester($command);
  205. $tester->execute(array());
  206. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  207. }
  208. public function testAsText()
  209. {
  210. $command = new \TestCommand();
  211. $command->setApplication(new Application());
  212. $tester = new CommandTester($command);
  213. $tester->execute(array());
  214. $this->assertStringEqualsFile(self::$fixturesPath.'/command_astext.txt', $command->asText(), '->asText() returns a text representation of the command');
  215. }
  216. public function testAsXml()
  217. {
  218. $command = new \TestCommand();
  219. $command->setApplication(new Application());
  220. $tester = new CommandTester($command);
  221. $tester->execute(array());
  222. $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/command_asxml.txt', $command->asXml(), '->asXml() returns an XML representation of the command');
  223. }
  224. }