InputDefinitionTest.php 15 KB

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