InputDefinitionTest.php 18 KB

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