InputDefinitionTest.php 16 KB

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