InputDefinitionTest.php 18 KB

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