InputDefinitionTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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\Input;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputDefinition;
  14. use Symfony\Component\Console\Input\InputOption;
  15. class InputDefinitionTest extends TestCase
  16. {
  17. protected static $fixtures;
  18. protected $multi;
  19. protected $foo;
  20. protected $bar;
  21. protected $foo1;
  22. protected $foo2;
  23. public static function setUpBeforeClass()
  24. {
  25. self::$fixtures = __DIR__.'/../Fixtures/';
  26. }
  27. public function testConstructorArguments()
  28. {
  29. $this->initializeArguments();
  30. $definition = new InputDefinition();
  31. $this->assertEquals([], $definition->getArguments(), '__construct() creates a new InputDefinition object');
  32. $definition = new InputDefinition([$this->foo, $this->bar]);
  33. $this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getArguments(), '__construct() takes an array of InputArgument objects as its first argument');
  34. }
  35. public function testConstructorOptions()
  36. {
  37. $this->initializeOptions();
  38. $definition = new InputDefinition();
  39. $this->assertEquals([], $definition->getOptions(), '__construct() creates a new InputDefinition object');
  40. $definition = new InputDefinition([$this->foo, $this->bar]);
  41. $this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getOptions(), '__construct() takes an array of InputOption objects as its first argument');
  42. }
  43. public function testSetArguments()
  44. {
  45. $this->initializeArguments();
  46. $definition = new InputDefinition();
  47. $definition->setArguments([$this->foo]);
  48. $this->assertEquals(['foo' => $this->foo], $definition->getArguments(), '->setArguments() sets the array of InputArgument objects');
  49. $definition->setArguments([$this->bar]);
  50. $this->assertEquals(['bar' => $this->bar], $definition->getArguments(), '->setArguments() clears all InputArgument objects');
  51. }
  52. public function testAddArguments()
  53. {
  54. $this->initializeArguments();
  55. $definition = new InputDefinition();
  56. $definition->addArguments([$this->foo]);
  57. $this->assertEquals(['foo' => $this->foo], $definition->getArguments(), '->addArguments() adds an array of InputArgument objects');
  58. $definition->addArguments([$this->bar]);
  59. $this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getArguments(), '->addArguments() does not clear existing InputArgument objects');
  60. }
  61. public function testAddArgument()
  62. {
  63. $this->initializeArguments();
  64. $definition = new InputDefinition();
  65. $definition->addArgument($this->foo);
  66. $this->assertEquals(['foo' => $this->foo], $definition->getArguments(), '->addArgument() adds a InputArgument object');
  67. $definition->addArgument($this->bar);
  68. $this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getArguments(), '->addArgument() adds a InputArgument object');
  69. }
  70. public function testArgumentsMustHaveDifferentNames()
  71. {
  72. $this->expectException('LogicException');
  73. $this->expectExceptionMessage('An argument with name "foo" already exists.');
  74. $this->initializeArguments();
  75. $definition = new InputDefinition();
  76. $definition->addArgument($this->foo);
  77. $definition->addArgument($this->foo1);
  78. }
  79. public function testArrayArgumentHasToBeLast()
  80. {
  81. $this->expectException('LogicException');
  82. $this->expectExceptionMessage('Cannot add an argument after an array argument.');
  83. $this->initializeArguments();
  84. $definition = new InputDefinition();
  85. $definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
  86. $definition->addArgument(new InputArgument('anotherbar'));
  87. }
  88. public function testRequiredArgumentCannotFollowAnOptionalOne()
  89. {
  90. $this->expectException('LogicException');
  91. $this->expectExceptionMessage('Cannot add a required argument after an optional one.');
  92. $this->initializeArguments();
  93. $definition = new InputDefinition();
  94. $definition->addArgument($this->foo);
  95. $definition->addArgument($this->foo2);
  96. }
  97. public function testGetArgument()
  98. {
  99. $this->initializeArguments();
  100. $definition = new InputDefinition();
  101. $definition->addArguments([$this->foo]);
  102. $this->assertEquals($this->foo, $definition->getArgument('foo'), '->getArgument() returns a InputArgument by its name');
  103. }
  104. public function testGetInvalidArgument()
  105. {
  106. $this->expectException('InvalidArgumentException');
  107. $this->expectExceptionMessage('The "bar" argument does not exist.');
  108. $this->initializeArguments();
  109. $definition = new InputDefinition();
  110. $definition->addArguments([$this->foo]);
  111. $definition->getArgument('bar');
  112. }
  113. public function testHasArgument()
  114. {
  115. $this->initializeArguments();
  116. $definition = new InputDefinition();
  117. $definition->addArguments([$this->foo]);
  118. $this->assertTrue($definition->hasArgument('foo'), '->hasArgument() returns true if a InputArgument exists for the given name');
  119. $this->assertFalse($definition->hasArgument('bar'), '->hasArgument() returns false if a InputArgument exists for the given name');
  120. }
  121. public function testGetArgumentRequiredCount()
  122. {
  123. $this->initializeArguments();
  124. $definition = new InputDefinition();
  125. $definition->addArgument($this->foo2);
  126. $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
  127. $definition->addArgument($this->foo);
  128. $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
  129. }
  130. public function testGetArgumentCount()
  131. {
  132. $this->initializeArguments();
  133. $definition = new InputDefinition();
  134. $definition->addArgument($this->foo2);
  135. $this->assertEquals(1, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
  136. $definition->addArgument($this->foo);
  137. $this->assertEquals(2, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
  138. }
  139. public function testGetArgumentDefaults()
  140. {
  141. $definition = new InputDefinition([
  142. new InputArgument('foo1', InputArgument::OPTIONAL),
  143. new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'),
  144. new InputArgument('foo3', InputArgument::OPTIONAL | InputArgument::IS_ARRAY),
  145. // new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', [1, 2]),
  146. ]);
  147. $this->assertEquals(['foo1' => null, 'foo2' => 'default', 'foo3' => []], $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
  148. $definition = new InputDefinition([
  149. new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', [1, 2]),
  150. ]);
  151. $this->assertEquals(['foo4' => [1, 2]], $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
  152. }
  153. public function testSetOptions()
  154. {
  155. $this->initializeOptions();
  156. $definition = new InputDefinition([$this->foo]);
  157. $this->assertEquals(['foo' => $this->foo], $definition->getOptions(), '->setOptions() sets the array of InputOption objects');
  158. $definition->setOptions([$this->bar]);
  159. $this->assertEquals(['bar' => $this->bar], $definition->getOptions(), '->setOptions() clears all InputOption objects');
  160. }
  161. public function testSetOptionsClearsOptions()
  162. {
  163. $this->expectException('InvalidArgumentException');
  164. $this->expectExceptionMessage('The "-f" option does not exist.');
  165. $this->initializeOptions();
  166. $definition = new InputDefinition([$this->foo]);
  167. $definition->setOptions([$this->bar]);
  168. $definition->getOptionForShortcut('f');
  169. }
  170. public function testAddOptions()
  171. {
  172. $this->initializeOptions();
  173. $definition = new InputDefinition([$this->foo]);
  174. $this->assertEquals(['foo' => $this->foo], $definition->getOptions(), '->addOptions() adds an array of InputOption objects');
  175. $definition->addOptions([$this->bar]);
  176. $this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getOptions(), '->addOptions() does not clear existing InputOption objects');
  177. }
  178. public function testAddOption()
  179. {
  180. $this->initializeOptions();
  181. $definition = new InputDefinition();
  182. $definition->addOption($this->foo);
  183. $this->assertEquals(['foo' => $this->foo], $definition->getOptions(), '->addOption() adds a InputOption object');
  184. $definition->addOption($this->bar);
  185. $this->assertEquals(['foo' => $this->foo, 'bar' => $this->bar], $definition->getOptions(), '->addOption() adds a InputOption object');
  186. }
  187. public function testAddDuplicateOption()
  188. {
  189. $this->expectException('LogicException');
  190. $this->expectExceptionMessage('An option named "foo" already exists.');
  191. $this->initializeOptions();
  192. $definition = new InputDefinition();
  193. $definition->addOption($this->foo);
  194. $definition->addOption($this->foo2);
  195. }
  196. public function testAddDuplicateShortcutOption()
  197. {
  198. $this->expectException('LogicException');
  199. $this->expectExceptionMessage('An option with shortcut "f" already exists.');
  200. $this->initializeOptions();
  201. $definition = new InputDefinition();
  202. $definition->addOption($this->foo);
  203. $definition->addOption($this->foo1);
  204. }
  205. public function testGetOption()
  206. {
  207. $this->initializeOptions();
  208. $definition = new InputDefinition([$this->foo]);
  209. $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
  210. }
  211. public function testGetInvalidOption()
  212. {
  213. $this->expectException('InvalidArgumentException');
  214. $this->expectExceptionMessage('The "--bar" option does not exist.');
  215. $this->initializeOptions();
  216. $definition = new InputDefinition([$this->foo]);
  217. $definition->getOption('bar');
  218. }
  219. public function testHasOption()
  220. {
  221. $this->initializeOptions();
  222. $definition = new InputDefinition([$this->foo]);
  223. $this->assertTrue($definition->hasOption('foo'), '->hasOption() returns true if a InputOption exists for the given name');
  224. $this->assertFalse($definition->hasOption('bar'), '->hasOption() returns false if a InputOption exists for the given name');
  225. }
  226. public function testHasShortcut()
  227. {
  228. $this->initializeOptions();
  229. $definition = new InputDefinition([$this->foo]);
  230. $this->assertTrue($definition->hasShortcut('f'), '->hasShortcut() returns true if a InputOption exists for the given shortcut');
  231. $this->assertFalse($definition->hasShortcut('b'), '->hasShortcut() returns false if a InputOption exists for the given shortcut');
  232. }
  233. public function testGetOptionForShortcut()
  234. {
  235. $this->initializeOptions();
  236. $definition = new InputDefinition([$this->foo]);
  237. $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
  238. }
  239. public function testGetOptionForMultiShortcut()
  240. {
  241. $this->initializeOptions();
  242. $definition = new InputDefinition([$this->multi]);
  243. $this->assertEquals($this->multi, $definition->getOptionForShortcut('m'), '->getOptionForShortcut() returns a InputOption by its shortcut');
  244. $this->assertEquals($this->multi, $definition->getOptionForShortcut('mmm'), '->getOptionForShortcut() returns a InputOption by its shortcut');
  245. }
  246. public function testGetOptionForInvalidShortcut()
  247. {
  248. $this->expectException('InvalidArgumentException');
  249. $this->expectExceptionMessage('The "-l" option does not exist.');
  250. $this->initializeOptions();
  251. $definition = new InputDefinition([$this->foo]);
  252. $definition->getOptionForShortcut('l');
  253. }
  254. public function testGetOptionDefaults()
  255. {
  256. $definition = new InputDefinition([
  257. new InputOption('foo1', null, InputOption::VALUE_NONE),
  258. new InputOption('foo2', null, InputOption::VALUE_REQUIRED),
  259. new InputOption('foo3', null, InputOption::VALUE_REQUIRED, '', 'default'),
  260. new InputOption('foo4', null, InputOption::VALUE_OPTIONAL),
  261. new InputOption('foo5', null, InputOption::VALUE_OPTIONAL, '', 'default'),
  262. new InputOption('foo6', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
  263. new InputOption('foo7', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', [1, 2]),
  264. ]);
  265. $defaults = [
  266. 'foo1' => false,
  267. 'foo2' => null,
  268. 'foo3' => 'default',
  269. 'foo4' => null,
  270. 'foo5' => 'default',
  271. 'foo6' => [],
  272. 'foo7' => [1, 2],
  273. ];
  274. $this->assertSame($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
  275. }
  276. /**
  277. * @dataProvider getGetSynopsisData
  278. */
  279. public function testGetSynopsis(InputDefinition $definition, $expectedSynopsis, $message = null)
  280. {
  281. $this->assertEquals($expectedSynopsis, $definition->getSynopsis(), $message ? '->getSynopsis() '.$message : '');
  282. }
  283. public function getGetSynopsisData()
  284. {
  285. return [
  286. [new InputDefinition([new InputOption('foo')]), '[--foo]', 'puts optional options in square brackets'],
  287. [new InputDefinition([new InputOption('foo', 'f')]), '[-f|--foo]', 'separates shortcut with a pipe'],
  288. [new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]), '[-f|--foo FOO]', 'uses shortcut as value placeholder'],
  289. [new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)]), '[-f|--foo [FOO]]', 'puts optional values in square brackets'],
  290. [new InputDefinition([new InputArgument('foo', InputArgument::REQUIRED)]), '<foo>', 'puts arguments in angle brackets'],
  291. [new InputDefinition([new InputArgument('foo')]), '[<foo>]', 'puts optional arguments in square brackets'],
  292. [new InputDefinition([new InputArgument('foo', InputArgument::IS_ARRAY)]), '[<foo>]...', 'uses an ellipsis for array arguments'],
  293. [new InputDefinition([new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY)]), '<foo> (<foo>)...', 'uses parenthesis and ellipsis for required array arguments'],
  294. [new InputDefinition([new InputOption('foo'), new InputArgument('foo', InputArgument::REQUIRED)]), '[--foo] [--] <foo>', 'puts [--] between options and arguments'],
  295. ];
  296. }
  297. public function testGetShortSynopsis()
  298. {
  299. $definition = new InputDefinition([new InputOption('foo'), new InputOption('bar'), new InputArgument('cat')]);
  300. $this->assertEquals('[options] [--] [<cat>]', $definition->getSynopsis(true), '->getSynopsis(true) groups options in [options]');
  301. }
  302. protected function initializeArguments()
  303. {
  304. $this->foo = new InputArgument('foo');
  305. $this->bar = new InputArgument('bar');
  306. $this->foo1 = new InputArgument('foo');
  307. $this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
  308. }
  309. protected function initializeOptions()
  310. {
  311. $this->foo = new InputOption('foo', 'f');
  312. $this->bar = new InputOption('bar', 'b');
  313. $this->foo1 = new InputOption('fooBis', 'f');
  314. $this->foo2 = new InputOption('foo', 'p');
  315. $this->multi = new InputOption('multi', 'm|mm|mmm');
  316. }
  317. }