InputDefinitionTest.php 16 KB

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