CommandTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. {
  35. $command = new Command();
  36. $this->fail('__construct() throws a \LogicException if the name is null');
  37. }
  38. catch (\LogicException $e)
  39. {
  40. }
  41. $command = new Command('foo:bar');
  42. $this->assertEquals('foo:bar', $command->getFullName(), '__construct() takes the command name as its first argument');
  43. }
  44. public function testSetApplication()
  45. {
  46. $application = new Application();
  47. $command = new \TestCommand();
  48. $command->setApplication($application);
  49. $this->assertEquals($application, $command->getApplication(), '->setApplication() sets the current application');
  50. }
  51. public function testSetGetDefinition()
  52. {
  53. $command = new \TestCommand();
  54. $ret = $command->setDefinition($definition = new InputDefinition());
  55. $this->assertEquals($command, $ret, '->setDefinition() implements a fluent interface');
  56. $this->assertEquals($definition, $command->getDefinition(), '->setDefinition() sets the current InputDefinition instance');
  57. $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar')));
  58. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  59. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  60. $command->setDefinition(new InputDefinition());
  61. }
  62. public function testAddArgument()
  63. {
  64. $command = new \TestCommand();
  65. $ret = $command->addArgument('foo');
  66. $this->assertEquals($command, $ret, '->addArgument() implements a fluent interface');
  67. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
  68. }
  69. public function testAddOption()
  70. {
  71. $command = new \TestCommand();
  72. $ret = $command->addOption('foo');
  73. $this->assertEquals($command, $ret, '->addOption() implements a fluent interface');
  74. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
  75. }
  76. public function testGetNamespaceGetNameGetFullNameSetName()
  77. {
  78. $command = new \TestCommand();
  79. $this->assertEquals('namespace', $command->getNamespace(), '->getNamespace() returns the command namespace');
  80. $this->assertEquals('name', $command->getName(), '->getName() returns the command name');
  81. $this->assertEquals('namespace:name', $command->getFullName(), '->getNamespace() returns the full command name');
  82. $command->setName('foo');
  83. $this->assertEquals('foo', $command->getName(), '->setName() sets the command name');
  84. $command->setName(':bar');
  85. $this->assertEquals('bar', $command->getName(), '->setName() sets the command name');
  86. $this->assertEquals('', $command->getNamespace(), '->setName() can set the command namespace');
  87. $ret = $command->setName('foobar:bar');
  88. $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
  89. $this->assertEquals('bar', $command->getName(), '->setName() sets the command name');
  90. $this->assertEquals('foobar', $command->getNamespace(), '->setName() can set the command namespace');
  91. try
  92. {
  93. $command->setName('');
  94. $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  95. }
  96. catch (\InvalidArgumentException $e)
  97. {
  98. }
  99. try
  100. {
  101. $command->setName('foo:');
  102. $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  103. }
  104. catch (\InvalidArgumentException $e)
  105. {
  106. }
  107. }
  108. public function testGetSetDescription()
  109. {
  110. $command = new \TestCommand();
  111. $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
  112. $ret = $command->setDescription('description1');
  113. $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
  114. $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
  115. }
  116. public function testGetSetHelp()
  117. {
  118. $command = new \TestCommand();
  119. $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
  120. $ret = $command->setHelp('help1');
  121. $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
  122. $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
  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->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. $command->mergeApplicationDefinition();
  164. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  165. $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  166. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
  167. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
  168. $command->mergeApplicationDefinition();
  169. $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  170. $command = new \TestCommand();
  171. $command->mergeApplicationDefinition();
  172. }
  173. public function testRun()
  174. {
  175. $command = new \TestCommand();
  176. $application = new Application();
  177. $command->setApplication($application);
  178. $tester = new CommandTester($command);
  179. try
  180. {
  181. $tester->execute(array('--bar' => true));
  182. $this->fail('->run() throws a \RuntimeException when the input does not validate the current InputDefinition');
  183. }
  184. catch (\RuntimeException $e)
  185. {
  186. }
  187. $this->assertEquals("interact called\nexecute called\n", $tester->execute(array(), array('interactive' => true)), '->run() calls the interact() method if the input is interactive');
  188. $this->assertEquals("execute called\n", $tester->execute(array(), array('interactive' => false)), '->run() does not call the interact() method if the input is not interactive');
  189. $command = new Command('foo');
  190. try
  191. {
  192. $command->run(new StringInput(''), new NullOutput());
  193. $this->fail('->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided');
  194. }
  195. catch (\LogicException $e)
  196. {
  197. }
  198. }
  199. public function testSetCode()
  200. {
  201. $application = new Application();
  202. $command = new \TestCommand();
  203. $command->setApplication($application);
  204. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output)
  205. {
  206. $output->writeln('from the code...');
  207. });
  208. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  209. $tester = new CommandTester($command);
  210. $tester->execute(array());
  211. $this->assertEquals("interact called\nfrom the code...\n", $tester->getDisplay());
  212. }
  213. public function testAsText()
  214. {
  215. $command = new \TestCommand();
  216. $command->setApplication(new Application());
  217. $tester = new CommandTester($command);
  218. $tester->execute(array());
  219. $this->assertEquals(file_get_contents(self::$fixturesPath.'/command_astext.txt'), $command->asText(), '->asText() returns a text representation of the command');
  220. }
  221. public function testAsXml()
  222. {
  223. $command = new \TestCommand();
  224. $command->setApplication(new Application());
  225. $tester = new CommandTester($command);
  226. $tester->execute(array());
  227. $this->assertEquals(file_get_contents(self::$fixturesPath.'/command_asxml.txt'), $command->asXml(), '->asXml() returns an XML representation of the command');
  228. }
  229. }