InputDefinitionTest.php 15 KB

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