CommandTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\Application;
  12. use Symfony\Components\Console\Input\InputDefinition;
  13. use Symfony\Components\Console\Input\InputArgument;
  14. use Symfony\Components\Console\Input\InputOption;
  15. use Symfony\Components\Console\Input\InputInterface;
  16. use Symfony\Components\Console\Input\StringInput;
  17. use Symfony\Components\Console\Output\OutputInterface;
  18. use Symfony\Components\Console\Output\NullOutput;
  19. use Symfony\Components\Console\Output\StreamOutput;
  20. use Symfony\Components\Console\Tester\CommandTester;
  21. class CommandTest extends \PHPUnit_Framework_TestCase
  22. {
  23. static protected $fixturesPath;
  24. static public function setUpBeforeClass()
  25. {
  26. self::$fixturesPath = __DIR__.'/../../../../../fixtures/Symfony/Components/Console/';
  27. require_once self::$fixturesPath.'/TestCommand.php';
  28. }
  29. public function testConstructor()
  30. {
  31. $application = new Application();
  32. try
  33. {
  34. $command = new Command();
  35. $this->fail('__construct() throws a \LogicException if the name is null');
  36. }
  37. catch (\LogicException $e)
  38. {
  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. {
  92. $command->setName('');
  93. $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  94. }
  95. catch (\InvalidArgumentException $e)
  96. {
  97. }
  98. try
  99. {
  100. $command->setName('foo:');
  101. $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  102. }
  103. catch (\InvalidArgumentException $e)
  104. {
  105. }
  106. }
  107. public function testGetSetDescription()
  108. {
  109. $command = new \TestCommand();
  110. $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
  111. $ret = $command->setDescription('description1');
  112. $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
  113. $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
  114. }
  115. public function testGetSetHelp()
  116. {
  117. $command = new \TestCommand();
  118. $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
  119. $ret = $command->setHelp('help1');
  120. $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
  121. $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
  122. }
  123. public function testGetSetAliases()
  124. {
  125. $command = new \TestCommand();
  126. $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
  127. $ret = $command->setAliases(array('name1'));
  128. $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
  129. $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
  130. }
  131. public function testGetSynopsis()
  132. {
  133. $command = new \TestCommand();
  134. $command->addOption('foo');
  135. $command->addArgument('foo');
  136. $this->assertEquals('namespace:name [--foo] [foo]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
  137. }
  138. public function testMergeApplicationDefinition()
  139. {
  140. $application1 = new Application();
  141. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  142. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  143. $command = new \TestCommand();
  144. $command->setApplication($application1);
  145. $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
  146. $command->mergeApplicationDefinition();
  147. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  148. $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  149. $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
  150. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
  151. $command->mergeApplicationDefinition();
  152. $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  153. $command = new \TestCommand();
  154. $command->mergeApplicationDefinition();
  155. }
  156. public function testRun()
  157. {
  158. $command = new \TestCommand();
  159. $application = new Application();
  160. $command->setApplication($application);
  161. $tester = new CommandTester($command);
  162. try
  163. {
  164. $tester->execute(array('--bar' => true));
  165. $this->fail('->run() throws a \RuntimeException when the input does not validate the current InputDefinition');
  166. }
  167. catch (\RuntimeException $e)
  168. {
  169. }
  170. $this->assertEquals("interact called\nexecute called\n", $tester->execute(array(), array('interactive' => true)), '->run() calls the interact() method if the input is interactive');
  171. $this->assertEquals("execute called\n", $tester->execute(array(), array('interactive' => false)), '->run() does not call the interact() method if the input is not interactive');
  172. $command = new Command('foo');
  173. try
  174. {
  175. $command->run(new StringInput(''), new NullOutput());
  176. $this->fail('->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided');
  177. }
  178. catch (\LogicException $e)
  179. {
  180. }
  181. }
  182. public function testSetCode()
  183. {
  184. $application = new Application();
  185. $command = new \TestCommand();
  186. $command->setApplication($application);
  187. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output)
  188. {
  189. $output->writeln('from the code...');
  190. });
  191. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  192. $tester = new CommandTester($command);
  193. $tester->execute(array());
  194. $this->assertEquals("interact called\nfrom the code...\n", $tester->getDisplay());
  195. }
  196. public function testAsText()
  197. {
  198. $command = new \TestCommand();
  199. $command->setApplication(new Application());
  200. $tester = new CommandTester($command);
  201. $tester->execute(array());
  202. $this->assertEquals(file_get_contents(self::$fixturesPath.'/command_astext.txt'), $command->asText(), '->asText() returns a text representation of the command');
  203. }
  204. public function testAsXml()
  205. {
  206. $command = new \TestCommand();
  207. $command->setApplication(new Application());
  208. $tester = new CommandTester($command);
  209. $tester->execute(array());
  210. $this->assertEquals(file_get_contents(self::$fixturesPath.'/command_asxml.txt'), $command->asXml(), '->asXml() returns an XML representation of the command');
  211. }
  212. }