ArgumentTest.php 12 KB

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