CommandTest.php 12 KB

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