CommandTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. require_once __DIR__.'/../../../../bootstrap.php';
  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. $fixtures = __DIR__.'/../../../../../fixtures/Symfony/Components/Console';
  22. $t = new LimeTest(46);
  23. require_once $fixtures.'/TestCommand.php';
  24. $application = new Application();
  25. // __construct()
  26. $t->diag('__construct()');
  27. try
  28. {
  29. $command = new Command();
  30. $t->fail('__construct() throws a \LogicException if the name is null');
  31. }
  32. catch (\LogicException $e)
  33. {
  34. $t->pass('__construct() throws a \LogicException if the name is null');
  35. }
  36. $command = new Command('foo:bar');
  37. $t->is($command->getFullName(), 'foo:bar', '__construct() takes the command name as its first argument');
  38. // ->setApplication()
  39. $t->diag('->setApplication()');
  40. $command = new TestCommand();
  41. $command->setApplication($application);
  42. $t->is($command->getApplication(), $application, '->setApplication() sets the current application');
  43. // ->setDefinition() ->getDefinition()
  44. $t->diag('->setDefinition() ->getDefinition()');
  45. $ret = $command->setDefinition($definition = new InputDefinition());
  46. $t->is($ret, $command, '->setDefinition() implements a fluent interface');
  47. $t->is($command->getDefinition(), $definition, '->setDefinition() sets the current InputDefinition instance');
  48. $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar')));
  49. $t->ok($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  50. $t->ok($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
  51. $command->setDefinition(new InputDefinition());
  52. // ->addArgument()
  53. $t->diag('->addArgument()');
  54. $ret = $command->addArgument('foo');
  55. $t->is($ret, $command, '->addArgument() implements a fluent interface');
  56. $t->ok($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
  57. // ->addOption()
  58. $t->diag('->addOption()');
  59. $ret = $command->addOption('foo');
  60. $t->is($ret, $command, '->addOption() implements a fluent interface');
  61. $t->ok($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
  62. // ->getNamespace() ->getName() ->getFullName() ->setName()
  63. $t->diag('->getNamespace() ->getName() ->getFullName()');
  64. $t->is($command->getNamespace(), 'namespace', '->getNamespace() returns the command namespace');
  65. $t->is($command->getName(), 'name', '->getName() returns the command name');
  66. $t->is($command->getFullName(), 'namespace:name', '->getNamespace() returns the full command name');
  67. $command->setName('foo');
  68. $t->is($command->getName(), 'foo', '->setName() sets the command name');
  69. $command->setName(':bar');
  70. $t->is($command->getName(), 'bar', '->setName() sets the command name');
  71. $t->is($command->getNamespace(), '', '->setName() can set the command namespace');
  72. $ret = $command->setName('foobar:bar');
  73. $t->is($ret, $command, '->setName() implements a fluent interface');
  74. $t->is($command->getName(), 'bar', '->setName() sets the command name');
  75. $t->is($command->getNamespace(), 'foobar', '->setName() can set the command namespace');
  76. try
  77. {
  78. $command->setName('');
  79. $t->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  80. }
  81. catch (\InvalidArgumentException $e)
  82. {
  83. $t->pass('->setName() throws an \InvalidArgumentException if the name is empty');
  84. }
  85. try
  86. {
  87. $command->setName('foo:');
  88. $t->fail('->setName() throws an \InvalidArgumentException if the name is empty');
  89. }
  90. catch (\InvalidArgumentException $e)
  91. {
  92. $t->pass('->setName() throws an \InvalidArgumentException if the name is empty');
  93. }
  94. // ->getDescription() ->setDescription()
  95. $t->diag('->getDescription() ->setDescription()');
  96. $t->is($command->getDescription(), 'description', '->getDescription() returns the description');
  97. $ret = $command->setDescription('description1');
  98. $t->is($ret, $command, '->setDescription() implements a fluent interface');
  99. $t->is($command->getDescription(), 'description1', '->setDescription() sets the description');
  100. // ->getHelp() ->setHelp()
  101. $t->diag('->getHelp() ->setHelp()');
  102. $t->is($command->getHelp(), 'help', '->getHelp() returns the help');
  103. $ret = $command->setHelp('help1');
  104. $t->is($ret, $command, '->setHelp() implements a fluent interface');
  105. $t->is($command->getHelp(), 'help1', '->setHelp() sets the help');
  106. // ->getAliases() ->setAliases()
  107. $t->diag('->getAliases() ->setAliases()');
  108. $t->is($command->getAliases(), array('name'), '->getAliases() returns the aliases');
  109. $ret = $command->setAliases(array('name1'));
  110. $t->is($ret, $command, '->setAliases() implements a fluent interface');
  111. $t->is($command->getAliases(), array('name1'), '->setAliases() sets the aliases');
  112. // ->getSynopsis()
  113. $t->diag('->getSynopsis()');
  114. $t->is($command->getSynopsis(), '%s foobar:bar [--foo] [foo]', '->getSynopsis() returns the synopsis');
  115. // ->mergeApplicationDefinition()
  116. $t->diag('->mergeApplicationDefinition()');
  117. $application1 = new Application();
  118. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  119. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  120. $command = new TestCommand();
  121. $command->setApplication($application1);
  122. $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
  123. $command->mergeApplicationDefinition();
  124. $t->ok($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  125. $t->ok($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
  126. $t->ok($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
  127. $t->ok($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
  128. $command->mergeApplicationDefinition();
  129. $t->is($command->getDefinition()->getArgumentCount(), 3, '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  130. $command = new TestCommand();
  131. $command->mergeApplicationDefinition();
  132. $t->pass('->mergeApplicationDefinition() does nothing if application is not set');
  133. // ->run()
  134. $t->diag('->run()');
  135. $command = new TestCommand();
  136. $command->setApplication($application);
  137. $tester = new CommandTester($command);
  138. try
  139. {
  140. $tester->execute(array('--bar' => true));
  141. $t->fail('->run() throws a \RuntimeException when the input does not validate the current InputDefinition');
  142. }
  143. catch (\RuntimeException $e)
  144. {
  145. $t->pass('->run() throws a \RuntimeException when the input does not validate the current InputDefinition');
  146. }
  147. $t->is($tester->execute(array(), array('interactive' => true)), "interact called\nexecute called\n", '->run() calls the interact() method if the input is interactive');
  148. $t->is($tester->execute(array(), array('interactive' => false)), "execute called\n", '->run() does not call the interact() method if the input is not interactive');
  149. $command = new Command('foo');
  150. try
  151. {
  152. $command->run(new StringInput(''), new NullOutput());
  153. $t->fail('->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided');
  154. }
  155. catch (\LogicException $e)
  156. {
  157. $t->pass('->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided');
  158. }
  159. // ->setCode()
  160. $t->diag('->setCode()');
  161. $command = new TestCommand();
  162. $command->setApplication($application);
  163. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output)
  164. {
  165. $output->write('from the code...');
  166. });
  167. $t->is($ret, $command, '->setCode() implements a fluent interface');
  168. $tester = new CommandTester($command);
  169. $tester->execute(array());
  170. $t->is($tester->getDisplay(), "interact called\nfrom the code...\n");
  171. // ->asText()
  172. $t->diag('->asText()');
  173. $t->is($command->asText(), file_get_contents($fixtures.'/command_astext.txt'), '->asText() returns a text representation of the command');
  174. // ->asXml()
  175. $t->diag('->asXml()');
  176. $t->is($command->asXml(), file_get_contents($fixtures.'/command_asxml.txt'), '->asXml() returns an XML representation of the command');