InputDefinitionTest.php 18 KB

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