PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/console/Tests/Command/CommandTest.php

https://gitlab.com/ealexis.t/trends
PHP | 350 lines | 271 code | 51 blank | 28 comment | 1 complexity | fab8aee53dbff677570b1764a5204d81 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\FormatterHelper;
  13. use Symfony\Component\Console\Application;
  14. use Symfony\Component\Console\Input\InputDefinition;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\StringInput;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Output\NullOutput;
  21. use Symfony\Component\Console\Tester\CommandTester;
  22. class CommandTest extends \PHPUnit_Framework_TestCase
  23. {
  24. protected static $fixturesPath;
  25. public static function setUpBeforeClass()
  26. {
  27. self::$fixturesPath = __DIR__.'/../Fixtures/';
  28. require_once self::$fixturesPath.'/TestCommand.php';
  29. }
  30. public function testConstructor()
  31. {
  32. $command = new Command('foo:bar');
  33. $this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
  34. }
  35. /**
  36. * @expectedException \LogicException
  37. * @expectedExceptionMessage The command defined in "Symfony\Component\Console\Command\Command" cannot have an empty name.
  38. */
  39. public function testCommandNameCannotBeEmpty()
  40. {
  41. new Command();
  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 testGetNamespaceGetNameSetName()
  76. {
  77. $command = new \TestCommand();
  78. $this->assertEquals('namespace:name', $command->getName(), '->getName() returns the command name');
  79. $command->setName('foo');
  80. $this->assertEquals('foo', $command->getName(), '->setName() sets the command name');
  81. $ret = $command->setName('foobar:bar');
  82. $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
  83. $this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
  84. }
  85. /**
  86. * @dataProvider provideInvalidCommandNames
  87. */
  88. public function testInvalidCommandNames($name)
  89. {
  90. $this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
  91. $command = new \TestCommand();
  92. $command->setName($name);
  93. }
  94. public function provideInvalidCommandNames()
  95. {
  96. return array(
  97. array(''),
  98. array('foo:'),
  99. );
  100. }
  101. public function testGetSetDescription()
  102. {
  103. $command = new \TestCommand();
  104. $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
  105. $ret = $command->setDescription('description1');
  106. $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
  107. $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
  108. }
  109. public function testGetSetHelp()
  110. {
  111. $command = new \TestCommand();
  112. $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
  113. $ret = $command->setHelp('help1');
  114. $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
  115. $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
  116. $command->setHelp('');
  117. $this->assertEquals('', $command->getHelp(), '->getHelp() does not fall back to the description');
  118. }
  119. public function testGetProcessedHelp()
  120. {
  121. $command = new \TestCommand();
  122. $command->setHelp('The %command.name% command does... Example: php %command.full_name%.');
  123. $this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly');
  124. $this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name%');
  125. $command = new \TestCommand();
  126. $command->setHelp('');
  127. $this->assertContains('description', $command->getProcessedHelp(), '->getProcessedHelp() falls back to the description');
  128. }
  129. public function testGetSetAliases()
  130. {
  131. $command = new \TestCommand();
  132. $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
  133. $ret = $command->setAliases(array('name1'));
  134. $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
  135. $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
  136. }
  137. public function testGetSynopsis()
  138. {
  139. $command = new \TestCommand();
  140. $command->addOption('foo');
  141. $command->addArgument('bar');
  142. $this->assertEquals('namespace:name [--foo] [--] [<bar>]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
  143. }
  144. public function testGetHelper()
  145. {
  146. $application = new Application();
  147. $command = new \TestCommand();
  148. $command->setApplication($application);
  149. $formatterHelper = new FormatterHelper();
  150. $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() 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. $r = new \ReflectionObject($command);
  161. $m = $r->getMethod('mergeApplicationDefinition');
  162. $m->setAccessible(true);
  163. $m->invoke($command);
  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. $m->invoke($command);
  169. $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
  170. }
  171. public function testMergeApplicationDefinitionWithoutArgsThenWithArgsAddsArgs()
  172. {
  173. $application1 = new Application();
  174. $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
  175. $application1->getDefinition()->addOptions(array(new InputOption('bar')));
  176. $command = new \TestCommand();
  177. $command->setApplication($application1);
  178. $command->setDefinition($definition = new InputDefinition(array()));
  179. $r = new \ReflectionObject($command);
  180. $m = $r->getMethod('mergeApplicationDefinition');
  181. $m->setAccessible(true);
  182. $m->invoke($command, false);
  183. $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition(false) merges the application and the command options');
  184. $this->assertFalse($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(false) does not merge the application arguments');
  185. $m->invoke($command, true);
  186. $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(true) merges the application arguments and the command arguments');
  187. $m->invoke($command);
  188. $this->assertEquals(2, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments');
  189. }
  190. public function testRunInteractive()
  191. {
  192. $tester = new CommandTester(new \TestCommand());
  193. $tester->execute(array(), array('interactive' => true));
  194. $this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
  195. }
  196. public function testRunNonInteractive()
  197. {
  198. $tester = new CommandTester(new \TestCommand());
  199. $tester->execute(array(), array('interactive' => false));
  200. $this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
  201. }
  202. /**
  203. * @expectedException \LogicException
  204. * @expectedExceptionMessage You must override the execute() method in the concrete command class.
  205. */
  206. public function testExecuteMethodNeedsToBeOverridden()
  207. {
  208. $command = new Command('foo');
  209. $command->run(new StringInput(''), new NullOutput());
  210. }
  211. /**
  212. * @expectedException Symfony\Component\Console\Exception\InvalidOptionException
  213. * @expectedExceptionMessage The "--bar" option does not exist.
  214. */
  215. public function testRunWithInvalidOption()
  216. {
  217. $command = new \TestCommand();
  218. $tester = new CommandTester($command);
  219. $tester->execute(array('--bar' => true));
  220. }
  221. public function testRunReturnsIntegerExitCode()
  222. {
  223. $command = new \TestCommand();
  224. $exitCode = $command->run(new StringInput(''), new NullOutput());
  225. $this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');
  226. $command = $this->getMock('TestCommand', array('execute'));
  227. $command->expects($this->once())
  228. ->method('execute')
  229. ->will($this->returnValue('2.3'));
  230. $exitCode = $command->run(new StringInput(''), new NullOutput());
  231. $this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
  232. }
  233. public function testRunWithApplication()
  234. {
  235. $command = new \TestCommand();
  236. $command->setApplication(new Application());
  237. $exitCode = $command->run(new StringInput(''), new NullOutput());
  238. $this->assertSame(0, $exitCode, '->run() returns an integer exit code');
  239. }
  240. public function testRunReturnsAlwaysInteger()
  241. {
  242. $command = new \TestCommand();
  243. $this->assertSame(0, $command->run(new StringInput(''), new NullOutput()));
  244. }
  245. public function testSetCode()
  246. {
  247. $command = new \TestCommand();
  248. $ret = $command->setCode(function (InputInterface $input, OutputInterface $output) {
  249. $output->writeln('from the code...');
  250. });
  251. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  252. $tester = new CommandTester($command);
  253. $tester->execute(array());
  254. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  255. }
  256. public function getSetCodeBindToClosureTests()
  257. {
  258. return array(
  259. array(true, 'not bound to the command'),
  260. array(false, 'bound to the command'),
  261. );
  262. }
  263. /**
  264. * @dataProvider getSetCodeBindToClosureTests
  265. */
  266. public function testSetCodeBindToClosure($previouslyBound, $expected)
  267. {
  268. $code = createClosure();
  269. if ($previouslyBound) {
  270. $code = $code->bindTo($this);
  271. }
  272. $command = new \TestCommand();
  273. $command->setCode($code);
  274. $tester = new CommandTester($command);
  275. $tester->execute(array());
  276. $this->assertEquals('interact called'.PHP_EOL.$expected.PHP_EOL, $tester->getDisplay());
  277. }
  278. public function testSetCodeWithNonClosureCallable()
  279. {
  280. $command = new \TestCommand();
  281. $ret = $command->setCode(array($this, 'callableMethodCommand'));
  282. $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
  283. $tester = new CommandTester($command);
  284. $tester->execute(array());
  285. $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
  286. }
  287. public function callableMethodCommand(InputInterface $input, OutputInterface $output)
  288. {
  289. $output->writeln('from the code...');
  290. }
  291. }
  292. // In order to get an unbound closure, we should create it outside a class
  293. // scope.
  294. function createClosure()
  295. {
  296. return function (InputInterface $input, OutputInterface $output) {
  297. $output->writeln($this instanceof Command ? 'bound to the command' : 'not bound to the command');
  298. };
  299. }