ArgumentTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\Argument\Argument;
  4. class ArgumentTest extends TestBase
  5. {
  6. /** @test */
  7. public function it_throws_an_exception_when_setting_an_unknown_cast_type()
  8. {
  9. $this->setExpectedException(
  10. 'Exception',
  11. "An argument may only be cast to the data type 'string', 'int', 'float', or 'bool'."
  12. );
  13. Argument::createFromArray('invalid-cast-type', [
  14. 'castTo' => 'invalid',
  15. ]);
  16. }
  17. /** @test */
  18. public function it_throws_an_exception_when_building_arguments_from_an_unknown_type()
  19. {
  20. $this->setExpectedException(
  21. 'Exception',
  22. 'Please provide an argument name or object.'
  23. );
  24. $this->cli->arguments->add(new \stdClass);
  25. }
  26. public function provide_cast_types_and_values()
  27. {
  28. return [
  29. 'to string' => ['string', 'a string',],
  30. 'to int' => ['int', '1234',],
  31. 'to float' => ['float', '12.34',],
  32. 'to boolean' => ['bool', '1'],
  33. ];
  34. }
  35. protected function getFullArguments()
  36. {
  37. return [
  38. 'only-short-prefix' => [
  39. 'prefix' => 's',
  40. 'description' => 'Only short prefix',
  41. ],
  42. 'only-long-prefix' => [
  43. 'longPrefix' => 'long',
  44. 'description' => 'Only long prefix',
  45. ],
  46. 'both-prefixes' => [
  47. 'prefix' => 'b',
  48. 'longPrefix' => 'both',
  49. 'description' => 'Both short and long prefixes',
  50. ],
  51. 'no-prefix' => [
  52. 'description' => 'Not defined by a prefix',
  53. ],
  54. 'defined-only' => [
  55. 'prefix' => 'd',
  56. 'longPrefix' => 'defined',
  57. 'description' => 'True when defined',
  58. 'noValue' => true,
  59. ],
  60. 'required' => [
  61. 'prefix' => 'r',
  62. 'description' => 'Required',
  63. 'required' => true,
  64. ],
  65. 'default-value' => [
  66. 'prefix' => 'v',
  67. 'description' => 'Has a default value',
  68. 'defaultValue' => 'test',
  69. ],
  70. 'default-value2' => [
  71. 'prefix' => 'x',
  72. 'description' => 'Has also a default value',
  73. 'defaultValue' => ['test2', 'test3'],
  74. ],
  75. ];
  76. }
  77. /**
  78. * @test
  79. * @dataProvider provide_cast_types_and_values
  80. * @param string $castTo
  81. * @param string $value
  82. */
  83. public function it_can_cast_different_value_data_types($castTo, $value)
  84. {
  85. $argument = Argument::createFromArray('test', [
  86. 'castTo' => $castTo,
  87. ]);
  88. $argument->setValue($value);
  89. $this->assertInternalType($castTo, $argument->value());
  90. }
  91. /** @test */
  92. public function it_casts_to_bool_when_defined_only()
  93. {
  94. $argument = Argument::createFromArray('invalid-cast-type', [
  95. 'noValue' => true,
  96. ]);
  97. $this->assertEquals('bool', $argument->castTo());
  98. }
  99. /** @test */
  100. public function it_builds_arguments_from_a_single_array()
  101. {
  102. // Test Description
  103. //
  104. // Usage: test-script [-b both-prefixes, --both both-prefixes] [-d, --defined] [--long only-long-prefix] [-r required] [-s only-short-prefix] [-v default-value (default: test)] [-x default-value2 (defaults: test2, test3)] [no-prefix]
  105. //
  106. // Required Arguments:
  107. // -r required
  108. // Required
  109. //
  110. // Optional Arguments:
  111. // -b both-prefixes, --both both-prefixes
  112. // Both short and long prefixes
  113. // -d, --defined
  114. // True when defined
  115. // -s only-short-prefix
  116. // Only short prefix
  117. // --long only-long-prefix
  118. // Only long prefix
  119. // -v default-value (default: test)
  120. // Has a default value
  121. // -x default-value2 (defaults: test2, test3)
  122. // Has also a default value
  123. // no-prefix
  124. // Not defined by a prefix
  125. $this->output->shouldReceive("sameLine");
  126. $this->shouldWrite("\e[mTest Description\e[0m");
  127. $this->shouldWrite("\e[m\e[0m");
  128. $this->shouldWrite("\e[mUsage: test-script "
  129. . "[-b both-prefixes, --both both-prefixes] [-d, --defined] "
  130. . "[--long only-long-prefix] [-r required] [-s only-short-prefix] "
  131. . "[-v default-value (default: test)] [-x default-value2 (defaults: test2, test3)] [no-prefix]\e[0m");
  132. $this->shouldWrite("\e[m\e[0m");
  133. $this->shouldWrite("\e[mRequired Arguments:\e[0m");
  134. $this->shouldWrite("\e[m\t\e[0m");
  135. $this->shouldWrite("\e[m-r required\e[0m");
  136. $this->shouldWrite("\e[m\t\t\e[0m");
  137. $this->shouldWrite("\e[mRequired\e[0m");
  138. $this->shouldWrite("\e[m\e[0m");
  139. $this->shouldWrite("\e[mOptional Arguments:\e[0m");
  140. $this->shouldWrite("\e[m\t\e[0m");
  141. $this->shouldWrite("\e[m-b both-prefixes, --both both-prefixes\e[0m");
  142. $this->shouldWrite("\e[m\t\t\e[0m");
  143. $this->shouldWrite("\e[mBoth short and long prefixes\e[0m");
  144. $this->shouldWrite("\e[m\t\e[0m");
  145. $this->shouldWrite("\e[m-d, --defined\e[0m");
  146. $this->shouldWrite("\e[m\t\t\e[0m");
  147. $this->shouldWrite("\e[mTrue when defined\e[0m");
  148. $this->shouldWrite("\e[m\t\e[0m");
  149. $this->shouldWrite("\e[m--long only-long-prefix\e[0m");
  150. $this->shouldWrite("\e[m\t\t\e[0m");
  151. $this->shouldWrite("\e[mOnly long prefix\e[0m");
  152. $this->shouldWrite("\e[m\t\e[0m");
  153. $this->shouldWrite("\e[m-s only-short-prefix\e[0m");
  154. $this->shouldWrite("\e[m\t\t\e[0m");
  155. $this->shouldWrite("\e[mOnly short prefix\e[0m");
  156. $this->shouldWrite("\e[m\t\e[0m");
  157. $this->shouldWrite("\e[m-v default-value (default: test)\e[0m");
  158. $this->shouldWrite("\e[m\t\t\e[0m");
  159. $this->shouldWrite("\e[mHas a default value\e[0m");
  160. $this->shouldWrite("\e[m\t\e[0m");
  161. $this->shouldWrite("\e[m-x default-value2 (defaults: test2, test3)\e[0m");
  162. $this->shouldWrite("\e[m\t\t\e[0m");
  163. $this->shouldWrite("\e[mHas also a default value\e[0m");
  164. $this->shouldWrite("\e[m\t\e[0m");
  165. $this->shouldWrite("\e[mno-prefix\e[0m");
  166. $this->shouldWrite("\e[m\t\t\e[0m");
  167. $this->shouldWrite("\e[mNot defined by a prefix\e[0m");
  168. $this->shouldHavePersisted(39);
  169. $this->cli->description('Test Description');
  170. $this->cli->arguments->add($this->getFullArguments());
  171. $command = 'test-script';
  172. $this->cli->usage([$command]);
  173. }
  174. /** @test */
  175. public function it_can_parse_arguments()
  176. {
  177. $this->cli->arguments->add([
  178. 'only-short-prefix' => [
  179. 'prefix' => 's',
  180. ],
  181. 'only-long-prefix' => [
  182. 'longPrefix' => 'long',
  183. ],
  184. 'both-prefixes' => [
  185. 'prefix' => 'b',
  186. 'longPrefix' => 'both',
  187. ],
  188. 'both-equals' => [
  189. 'longPrefix' => 'both-equals',
  190. ],
  191. 'no-prefix' => [],
  192. 'defined-only' => [
  193. 'prefix' => 'd',
  194. 'longPrefix' => 'defined',
  195. 'noValue' => true,
  196. ],
  197. ]);
  198. $argv = [
  199. 'test-script',
  200. '-s=baz',
  201. '-s',
  202. 'foo',
  203. '--long',
  204. 'bar',
  205. '-b=both',
  206. '-d',
  207. '--both-equals=both_equals',
  208. 'no_prefix_value',
  209. '-unknown',
  210. 'after_non_prefixed'
  211. ];
  212. $this->cli->arguments->parse($argv);
  213. $processed = $this->cli->arguments->toArray();
  214. $this->assertCount(6, $processed);
  215. $this->assertEquals('foo', $processed['only-short-prefix']);
  216. $this->assertEquals('bar', $processed['only-long-prefix']);
  217. $this->assertEquals('both', $processed['both-prefixes']);
  218. $this->assertEquals('both_equals', $processed['both-equals']);
  219. $this->assertEquals('no_prefix_value', $processed['no-prefix']);
  220. $this->assertTrue($processed['defined-only']);
  221. $this->assertEquals('foo', $this->cli->arguments->get('only-short-prefix'));
  222. $this->assertEquals(['baz', 'foo'], $this->cli->arguments->getArray('only-short-prefix'));
  223. }
  224. /** @test */
  225. public function it_will_get_a_default_value_for_a_long_prefix_with_no_value()
  226. {
  227. $this->cli->arguments->add([
  228. 'only-long-prefix' => [
  229. 'longPrefix' => 'long',
  230. 'defaultValue' => 'HEY',
  231. ],
  232. ]);
  233. $argv = [
  234. 'test-script',
  235. '--long',
  236. ];
  237. $this->cli->arguments->parse($argv);
  238. $processed = $this->cli->arguments->toArray();
  239. $this->assertEquals('HEY', $this->cli->arguments->get('only-long-prefix'));
  240. }
  241. /** @test */
  242. public function it_throws_an_exception_when_required_arguments_are_not_defined()
  243. {
  244. $this->setExpectedException(
  245. 'Exception',
  246. 'The following arguments are required: [-r required-value] [-r1 required-value-1].'
  247. );
  248. $this->cli->arguments->add([
  249. 'required-value' => [
  250. 'prefix' => 'r',
  251. 'required' => true,
  252. ],
  253. 'required-value-1' => [
  254. 'prefix' => 'r1',
  255. 'required' => true,
  256. ],
  257. 'optional-value' => [
  258. 'prefix' => 'o',
  259. ],
  260. ]);
  261. $argv = ['test-script', '-o', 'foo'];
  262. $this->cli->arguments->parse($argv);
  263. }
  264. /** @test */
  265. public function it_can_detect_when_arguments_are_defined()
  266. {
  267. $this->cli->arguments->add([
  268. 'argument' => [
  269. 'prefix' => 'a',
  270. ],
  271. 'another-argument' => [
  272. 'prefix' => 'b',
  273. ],
  274. 'long-argument' => [
  275. 'longPrefix' => 'c',
  276. ],
  277. ]);
  278. $argv = ['test-script', '-a', 'foo', '--c=bar'];
  279. $this->assertTrue($this->cli->arguments->defined('argument', $argv));
  280. $this->assertTrue($this->cli->arguments->defined('long-argument', $argv));
  281. $this->assertFalse($this->cli->arguments->defined('another-argument', $argv));
  282. $this->assertFalse($this->cli->arguments->defined('nonexistent', $argv));
  283. }
  284. /** @test */
  285. public function it_can_grab_the_trailing_arguments()
  286. {
  287. $this->cli->arguments->add([
  288. 'argument' => [
  289. 'prefix' => 'a',
  290. ],
  291. 'another-argument' => [
  292. 'prefix' => 'b',
  293. ],
  294. 'long-argument' => [
  295. 'longPrefix' => 'c',
  296. ],
  297. ]);
  298. $argv = ['test-script', '-a', 'foo', '--c=bar', '--', '-the', 'trailing', '--arguments=here'];
  299. $this->cli->arguments->parse($argv);
  300. $this->assertTrue($this->cli->arguments->defined('argument', $argv));
  301. $this->assertTrue($this->cli->arguments->defined('long-argument', $argv));
  302. $this->assertFalse($this->cli->arguments->defined('another-argument', $argv));
  303. $this->assertFalse($this->cli->arguments->defined('nonexistent', $argv));
  304. $this->assertSame($this->cli->arguments->trailing(), '-the trailing --arguments=here');
  305. }
  306. }