InputDefinitionTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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(): void
  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::class);
  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::class);
  82. $this->expectExceptionMessage('Cannot add a required argument "anotherbar" after an array argument "fooarray".');
  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->initializeArguments();
  91. $this->expectException(\LogicException::class);
  92. $this->expectExceptionMessage(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $this->foo2->getName(), $this->foo->getName()));
  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::class);
  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::class);
  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::class);
  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 testAddDuplicateNegatedOption()
  197. {
  198. $this->expectException(\LogicException::class);
  199. $this->expectExceptionMessage('An option named "no-foo" already exists.');
  200. $definition = new InputDefinition();
  201. $definition->addOption(new InputOption('no-foo'));
  202. $definition->addOption(new InputOption('foo', null, InputOption::VALUE_NEGATABLE));
  203. }
  204. public function testAddDuplicateNegatedReverseOption()
  205. {
  206. $this->expectException(\LogicException::class);
  207. $this->expectExceptionMessage('An option named "no-foo" already exists.');
  208. $definition = new InputDefinition();
  209. $definition->addOption(new InputOption('foo', null, InputOption::VALUE_NEGATABLE));
  210. $definition->addOption(new InputOption('no-foo'));
  211. }
  212. public function testAddDuplicateShortcutOption()
  213. {
  214. $this->expectException(\LogicException::class);
  215. $this->expectExceptionMessage('An option with shortcut "f" already exists.');
  216. $this->initializeOptions();
  217. $definition = new InputDefinition();
  218. $definition->addOption($this->foo);
  219. $definition->addOption($this->foo1);
  220. }
  221. public function testGetOption()
  222. {
  223. $this->initializeOptions();
  224. $definition = new InputDefinition([$this->foo]);
  225. $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
  226. }
  227. public function testGetInvalidOption()
  228. {
  229. $this->expectException(\InvalidArgumentException::class);
  230. $this->expectExceptionMessage('The "--bar" option does not exist.');
  231. $this->initializeOptions();
  232. $definition = new InputDefinition([$this->foo]);
  233. $definition->getOption('bar');
  234. }
  235. public function testHasOption()
  236. {
  237. $this->initializeOptions();
  238. $definition = new InputDefinition([$this->foo]);
  239. $this->assertTrue($definition->hasOption('foo'), '->hasOption() returns true if a InputOption exists for the given name');
  240. $this->assertFalse($definition->hasOption('bar'), '->hasOption() returns false if a InputOption exists for the given name');
  241. }
  242. public function testHasShortcut()
  243. {
  244. $this->initializeOptions();
  245. $definition = new InputDefinition([$this->foo]);
  246. $this->assertTrue($definition->hasShortcut('f'), '->hasShortcut() returns true if a InputOption exists for the given shortcut');
  247. $this->assertFalse($definition->hasShortcut('b'), '->hasShortcut() returns false if a InputOption exists for the given shortcut');
  248. }
  249. public function testGetOptionForShortcut()
  250. {
  251. $this->initializeOptions();
  252. $definition = new InputDefinition([$this->foo]);
  253. $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
  254. }
  255. public function testGetOptionForMultiShortcut()
  256. {
  257. $this->initializeOptions();
  258. $definition = new InputDefinition([$this->multi]);
  259. $this->assertEquals($this->multi, $definition->getOptionForShortcut('m'), '->getOptionForShortcut() returns a InputOption by its shortcut');
  260. $this->assertEquals($this->multi, $definition->getOptionForShortcut('mmm'), '->getOptionForShortcut() returns a InputOption by its shortcut');
  261. }
  262. public function testGetOptionForInvalidShortcut()
  263. {
  264. $this->expectException(\InvalidArgumentException::class);
  265. $this->expectExceptionMessage('The "-l" option does not exist.');
  266. $this->initializeOptions();
  267. $definition = new InputDefinition([$this->foo]);
  268. $definition->getOptionForShortcut('l');
  269. }
  270. public function testGetOptionDefaults()
  271. {
  272. $definition = new InputDefinition([
  273. new InputOption('foo1', null, InputOption::VALUE_NONE),
  274. new InputOption('foo2', null, InputOption::VALUE_REQUIRED),
  275. new InputOption('foo3', null, InputOption::VALUE_REQUIRED, '', 'default'),
  276. new InputOption('foo4', null, InputOption::VALUE_OPTIONAL),
  277. new InputOption('foo5', null, InputOption::VALUE_OPTIONAL, '', 'default'),
  278. new InputOption('foo6', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
  279. new InputOption('foo7', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', [1, 2]),
  280. ]);
  281. $defaults = [
  282. 'foo1' => false,
  283. 'foo2' => null,
  284. 'foo3' => 'default',
  285. 'foo4' => null,
  286. 'foo5' => 'default',
  287. 'foo6' => [],
  288. 'foo7' => [1, 2],
  289. ];
  290. $this->assertSame($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
  291. }
  292. /**
  293. * @dataProvider getGetSynopsisData
  294. */
  295. public function testGetSynopsis(InputDefinition $definition, $expectedSynopsis, $message = null)
  296. {
  297. $this->assertEquals($expectedSynopsis, $definition->getSynopsis(), $message ? '->getSynopsis() '.$message : '');
  298. }
  299. public static function getGetSynopsisData()
  300. {
  301. return [
  302. [new InputDefinition([new InputOption('foo')]), '[--foo]', 'puts optional options in square brackets'],
  303. [new InputDefinition([new InputOption('foo', 'f')]), '[-f|--foo]', 'separates shortcut with a pipe'],
  304. [new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]), '[-f|--foo FOO]', 'uses shortcut as value placeholder'],
  305. [new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)]), '[-f|--foo [FOO]]', 'puts optional values in square brackets'],
  306. [new InputDefinition([new InputArgument('foo', InputArgument::REQUIRED)]), '<foo>', 'puts arguments in angle brackets'],
  307. [new InputDefinition([new InputArgument('foo')]), '[<foo>]', 'puts optional arguments in square brackets'],
  308. [new InputDefinition([new InputArgument('foo'), new InputArgument('bar')]), '[<foo> [<bar>]]', 'chains optional arguments inside brackets'],
  309. [new InputDefinition([new InputArgument('foo', InputArgument::IS_ARRAY)]), '[<foo>...]', 'uses an ellipsis for array arguments'],
  310. [new InputDefinition([new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY)]), '<foo>...', 'uses an ellipsis for required array arguments'],
  311. [new InputDefinition([new InputOption('foo'), new InputArgument('foo', InputArgument::REQUIRED)]), '[--foo] [--] <foo>', 'puts [--] between options and arguments'],
  312. ];
  313. }
  314. public function testGetShortSynopsis()
  315. {
  316. $definition = new InputDefinition([new InputOption('foo'), new InputOption('bar'), new InputArgument('cat')]);
  317. $this->assertEquals('[options] [--] [<cat>]', $definition->getSynopsis(true), '->getSynopsis(true) groups options in [options]');
  318. }
  319. protected function initializeArguments()
  320. {
  321. $this->foo = new InputArgument('foo');
  322. $this->bar = new InputArgument('bar');
  323. $this->foo1 = new InputArgument('foo');
  324. $this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
  325. }
  326. protected function initializeOptions()
  327. {
  328. $this->foo = new InputOption('foo', 'f');
  329. $this->bar = new InputOption('bar', 'b');
  330. $this->foo1 = new InputOption('fooBis', 'f');
  331. $this->foo2 = new InputOption('foo', 'p');
  332. $this->multi = new InputOption('multi', 'm|mm|mmm');
  333. }
  334. }