InputDefinitionTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\Console\Input;
  10. use Symfony\Components\Console\Input\InputDefinition;
  11. use Symfony\Components\Console\Input\InputArgument;
  12. use Symfony\Components\Console\Input\InputOption;
  13. use Symfony\Components\Console\Exception;
  14. class InputDefinitionTest extends \PHPUnit_Framework_TestCase
  15. {
  16. static protected $fixtures;
  17. protected $foo, $bar, $foo1, $foo2;
  18. static public function setUpBeforeClass()
  19. {
  20. self::$fixtures = __DIR__.'/../../../../../fixtures/Symfony/Components/Console';
  21. }
  22. public function testConstructor()
  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. $this->initializeOptions();
  30. $definition = new InputDefinition();
  31. $this->assertEquals(array(), $definition->getOptions(), '__construct() creates a new InputDefinition object');
  32. $definition = new InputDefinition(array($this->foo, $this->bar));
  33. $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '__construct() takes an array of InputOption objects as its first argument');
  34. }
  35. public function testSetArguments()
  36. {
  37. $this->initializeArguments();
  38. $definition = new InputDefinition();
  39. $definition->setArguments(array($this->foo));
  40. $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->setArguments() sets the array of InputArgument objects');
  41. $definition->setArguments(array($this->bar));
  42. $this->assertEquals(array('bar' => $this->bar), $definition->getArguments(), '->setArguments() clears all InputArgument objects');
  43. }
  44. public function testAddArguments()
  45. {
  46. $this->initializeArguments();
  47. $definition = new InputDefinition();
  48. $definition->addArguments(array($this->foo));
  49. $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArguments() adds an array of InputArgument objects');
  50. $definition->addArguments(array($this->bar));
  51. $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArguments() does not clear existing InputArgument objects');
  52. }
  53. public function testAddArgument()
  54. {
  55. $this->initializeArguments();
  56. $definition = new InputDefinition();
  57. $definition->addArgument($this->foo);
  58. $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArgument() adds a InputArgument object');
  59. $definition->addArgument($this->bar);
  60. $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArgument() adds a InputArgument object');
  61. // arguments must have different names
  62. try
  63. {
  64. $definition->addArgument($this->foo1);
  65. $this->fail('->addArgument() throws a Exception if another argument is already registered with the same name');
  66. }
  67. catch (\Exception $e)
  68. {
  69. $this->assertType('\Exception', $e, '->addArgument() throws a Exception if another argument is already registered with the same name');
  70. $this->assertEquals('An argument with name "foo" already exist.', $e->getMessage());
  71. }
  72. // cannot add a parameter after an array parameter
  73. $definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
  74. try
  75. {
  76. $definition->addArgument(new InputArgument('anotherbar'));
  77. $this->fail('->addArgument() throws a Exception if there is an array parameter already registered');
  78. }
  79. catch (\Exception $e)
  80. {
  81. $this->assertType('\Exception', $e, '->addArgument() throws a Exception if there is an array parameter already registered');
  82. $this->assertEquals('Cannot add an argument after an array argument.', $e->getMessage());
  83. }
  84. // cannot add a required argument after an optional one
  85. $definition = new InputDefinition();
  86. $definition->addArgument($this->foo);
  87. try
  88. {
  89. $definition->addArgument($this->foo2);
  90. $this->fail('->addArgument() throws an exception if you try to add a required argument after an optional one');
  91. }
  92. catch (\Exception $e)
  93. {
  94. $this->assertType('\Exception', $e, '->addArgument() throws an exception if you try to add a required argument after an optional one');
  95. $this->assertEquals('Cannot add a required argument after an optional one.', $e->getMessage());
  96. }
  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. try
  105. {
  106. $definition->getArgument('bar');
  107. $this->fail('->getArgument() throws an exception if the InputArgument name does not exist');
  108. }
  109. catch (\Exception $e)
  110. {
  111. $this->assertType('\Exception', $e, '->getArgument() throws an exception if the InputArgument name does not exist');
  112. $this->assertEquals('The "bar" argument does not exist.', $e->getMessage());
  113. }
  114. }
  115. public function testHasArgument()
  116. {
  117. $this->initializeArguments();
  118. $definition = new InputDefinition();
  119. $definition->addArguments(array($this->foo));
  120. $this->assertTrue($definition->hasArgument('foo'), '->hasArgument() returns true if a InputArgument exists for the given name');
  121. $this->assertFalse($definition->hasArgument('bar'), '->hasArgument() returns false if a InputArgument exists for the given name');
  122. }
  123. public function testGetArgumentRequiredCount()
  124. {
  125. $this->initializeArguments();
  126. $definition = new InputDefinition();
  127. $definition->addArgument($this->foo2);
  128. $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
  129. $definition->addArgument($this->foo);
  130. $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
  131. }
  132. public function testGetArgumentCount()
  133. {
  134. $this->initializeArguments();
  135. $definition = new InputDefinition();
  136. $definition->addArgument($this->foo2);
  137. $this->assertEquals(1, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
  138. $definition->addArgument($this->foo);
  139. $this->assertEquals(2, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
  140. }
  141. public function testGetArgumentDefaults()
  142. {
  143. $definition = new InputDefinition(array(
  144. new InputArgument('foo1', InputArgument::OPTIONAL),
  145. new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'),
  146. new InputArgument('foo3', InputArgument::OPTIONAL | InputArgument::IS_ARRAY),
  147. // new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
  148. ));
  149. $this->assertEquals(array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
  150. $definition = new InputDefinition(array(
  151. new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
  152. ));
  153. $this->assertEquals(array('foo4' => array(1, 2)), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
  154. }
  155. public function testSetOptions()
  156. {
  157. $this->initializeOptions();
  158. $definition = new InputDefinition(array($this->foo));
  159. $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->setOptions() sets the array of InputOption objects');
  160. $definition->setOptions(array($this->bar));
  161. $this->assertEquals(array('bar' => $this->bar), $definition->getOptions(), '->setOptions() clears all InputOption objects');
  162. try
  163. {
  164. $definition->getOptionForShortcut('f');
  165. $this->fail('->setOptions() clears all InputOption objects');
  166. }
  167. catch (\Exception $e)
  168. {
  169. $this->assertType('\Exception', $e, '->setOptions() clears all InputOption objects');
  170. $this->assertEquals('The "-f" option does not exist.', $e->getMessage());
  171. }
  172. }
  173. public function testAddOptions()
  174. {
  175. $this->initializeOptions();
  176. $definition = new InputDefinition(array($this->foo));
  177. $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOptions() adds an array of InputOption objects');
  178. $definition->addOptions(array($this->bar));
  179. $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOptions() does not clear existing InputOption objects');
  180. }
  181. public function testAddOption()
  182. {
  183. $this->initializeOptions();
  184. $definition = new InputDefinition();
  185. $definition->addOption($this->foo);
  186. $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOption() adds a InputOption object');
  187. $definition->addOption($this->bar);
  188. $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOption() adds a InputOption object');
  189. try
  190. {
  191. $definition->addOption($this->foo2);
  192. $this->fail('->addOption() throws a Exception if the another option is already registered with the same name');
  193. }
  194. catch (\Exception $e)
  195. {
  196. $this->assertType('\Exception', $e, '->addOption() throws a Exception if the another option is already registered with the same name');
  197. $this->assertEquals('An option named "foo" already exist.', $e->getMessage());
  198. }
  199. try
  200. {
  201. $definition->addOption($this->foo1);
  202. $this->fail('->addOption() throws a Exception if the another option is already registered with the same shortcut');
  203. }
  204. catch (\Exception $e)
  205. {
  206. $this->assertType('\Exception', $e, '->addOption() throws a Exception if the another option is already registered with the same shortcut');
  207. $this->assertEquals('An option with shortcut "f" already exist.', $e->getMessage());
  208. }
  209. }
  210. public function testGetOption()
  211. {
  212. $this->initializeOptions();
  213. $definition = new InputDefinition(array($this->foo));
  214. $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
  215. try
  216. {
  217. $definition->getOption('bar');
  218. $this->fail('->getOption() throws an exception if the option name does not exist');
  219. }
  220. catch (\Exception $e)
  221. {
  222. $this->assertType('\Exception', $e, '->getOption() throws an exception if the option name does not exist');
  223. $this->assertEquals('The "--bar" option does not exist.', $e->getMessage());
  224. }
  225. }
  226. public function testHasOption()
  227. {
  228. $this->initializeOptions();
  229. $definition = new InputDefinition(array($this->foo));
  230. $this->assertTrue($definition->hasOption('foo'), '->hasOption() returns true if a InputOption exists for the given name');
  231. $this->assertFalse($definition->hasOption('bar'), '->hasOption() returns false if a InputOption exists for the given name');
  232. }
  233. public function testHasShortcut()
  234. {
  235. $this->initializeOptions();
  236. $definition = new InputDefinition(array($this->foo));
  237. $this->assertTrue($definition->hasShortcut('f'), '->hasShortcut() returns true if a InputOption exists for the given shortcut');
  238. $this->assertFalse($definition->hasShortcut('b'), '->hasShortcut() returns false if a InputOption exists for the given shortcut');
  239. }
  240. public function testGetOptionForShortcut()
  241. {
  242. $this->initializeOptions();
  243. $definition = new InputDefinition(array($this->foo));
  244. $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
  245. try
  246. {
  247. $definition->getOptionForShortcut('l');
  248. $this->fail('->getOption() throws an exception if the shortcut does not exist');
  249. }
  250. catch (\Exception $e)
  251. {
  252. $this->assertType('\Exception', $e, '->getOption() throws an exception if the shortcut does not exist');
  253. $this->assertEquals('The "-l" option does not exist.', $e->getMessage());
  254. }
  255. }
  256. public function testGetOptionDefaults()
  257. {
  258. $definition = new InputDefinition(array(
  259. new InputOption('foo1', null, InputOption::PARAMETER_NONE),
  260. new InputOption('foo2', null, InputOption::PARAMETER_REQUIRED),
  261. new InputOption('foo3', null, InputOption::PARAMETER_REQUIRED, '', 'default'),
  262. new InputOption('foo4', null, InputOption::PARAMETER_OPTIONAL),
  263. new InputOption('foo5', null, InputOption::PARAMETER_OPTIONAL, '', 'default'),
  264. new InputOption('foo6', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY),
  265. new InputOption('foo7', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, '', array(1, 2)),
  266. ));
  267. $defaults = array(
  268. 'foo1' => null,
  269. 'foo2' => null,
  270. 'foo3' => 'default',
  271. 'foo4' => null,
  272. 'foo5' => 'default',
  273. 'foo6' => array(),
  274. 'foo7' => array(1, 2),
  275. );
  276. $this->assertEquals($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
  277. }
  278. public function testGetSynopsis()
  279. {
  280. $definition = new InputDefinition(array(new InputOption('foo')));
  281. $this->assertEquals('[--foo]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
  282. $definition = new InputDefinition(array(new InputOption('foo', 'f')));
  283. $this->assertEquals('[-f|--foo]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
  284. $definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)));
  285. $this->assertEquals('[-f|--foo="..."]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
  286. $definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL)));
  287. $this->assertEquals('[-f|--foo[="..."]]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
  288. $definition = new InputDefinition(array(new InputArgument('foo')));
  289. $this->assertEquals('[foo]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
  290. $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED)));
  291. $this->assertEquals('foo', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
  292. $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY)));
  293. $this->assertEquals('[foo1] ... [fooN]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
  294. $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY)));
  295. $this->assertEquals('foo1 ... [fooN]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
  296. }
  297. public function testAsText()
  298. {
  299. $definition = new InputDefinition(array(
  300. new InputArgument('foo', InputArgument::OPTIONAL, 'The bar argument'),
  301. new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The foo argument', array('bar')),
  302. new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED, 'The foo option'),
  303. new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL, 'The foo option', 'bar'),
  304. ));
  305. $this->assertStringEqualsFile(self::$fixtures.'/definition_astext.txt', $definition->asText(), '->asText() returns a textual representation of the InputDefinition');
  306. }
  307. public function testAsXml()
  308. {
  309. $definition = new InputDefinition(array(
  310. new InputArgument('foo', InputArgument::OPTIONAL, 'The bar argument'),
  311. new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The foo argument', array('bar')),
  312. new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED, 'The foo option'),
  313. new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL, 'The foo option', 'bar'),
  314. ));
  315. $this->assertXmlStringEqualsXmlFile(self::$fixtures.'/definition_asxml.txt', $definition->asXml(), '->asText() returns a textual representation of the InputDefinition');
  316. }
  317. protected function initializeArguments()
  318. {
  319. $this->foo = new InputArgument('foo');
  320. $this->bar = new InputArgument('bar');
  321. $this->foo1 = new InputArgument('foo');
  322. $this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
  323. }
  324. protected function initializeOptions()
  325. {
  326. $this->foo = new InputOption('foo', 'f');
  327. $this->bar = new InputOption('bar', 'b');
  328. $this->foo1 = new InputOption('fooBis', 'f');
  329. $this->foo2 = new InputOption('foo', 'p');
  330. }
  331. }