ArgumentTest.php 12 KB

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