InputDefinitionTest.php 13 KB

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