ArgumentsTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.6.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Console;
  17. use Cake\Console\Arguments;
  18. use Cake\Console\Exception\ConsoleException;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Arguments test case.
  22. */
  23. class ArgumentsTest extends TestCase
  24. {
  25. /**
  26. * Get all arguments
  27. */
  28. public function testGetArguments(): void
  29. {
  30. $values = ['big', 'brown', 'bear'];
  31. $args = new Arguments($values, [], []);
  32. $this->assertSame($values, $args->getArguments());
  33. }
  34. /**
  35. * Get arguments by index
  36. */
  37. public function testGetArgumentAt(): void
  38. {
  39. $values = ['big', 'brown', 'bear'];
  40. $args = new Arguments($values, [], []);
  41. $this->assertSame($values[0], $args->getArgumentAt(0));
  42. $this->assertSame($values[1], $args->getArgumentAt(1));
  43. $this->assertNull($args->getArgumentAt(3));
  44. }
  45. /**
  46. * check arguments by index
  47. */
  48. public function testHasArgumentAt(): void
  49. {
  50. $values = ['big', 'brown', 'bear'];
  51. $args = new Arguments($values, [], []);
  52. $this->assertTrue($args->hasArgumentAt(0));
  53. $this->assertTrue($args->hasArgumentAt(1));
  54. $this->assertFalse($args->hasArgumentAt(3));
  55. $this->assertFalse($args->hasArgumentAt(-1));
  56. }
  57. /**
  58. * check arguments by name
  59. */
  60. public function testHasArgument(): void
  61. {
  62. $values = ['big', 'brown', 'bear'];
  63. $names = ['size', 'color', 'species', 'odd'];
  64. $args = new Arguments($values, [], $names);
  65. $this->assertTrue($args->hasArgument('size'));
  66. $this->assertTrue($args->hasArgument('color'));
  67. $this->assertFalse($args->hasArgument('odd'));
  68. $this->assertFalse($args->hasArgument('undefined'));
  69. }
  70. /**
  71. * get arguments by name
  72. */
  73. public function testGetArgument(): void
  74. {
  75. $values = ['big', 'brown', 'bear'];
  76. $names = ['size', 'color', 'species', 'odd'];
  77. $args = new Arguments($values, [], $names);
  78. $this->assertSame($values[0], $args->getArgument('size'));
  79. $this->assertSame($values[1], $args->getArgument('color'));
  80. $this->assertNull($args->getArgument('odd'));
  81. }
  82. /**
  83. * get arguments missing value
  84. */
  85. public function testGetArgumentMissing(): void
  86. {
  87. $values = [];
  88. $names = ['size', 'color'];
  89. $args = new Arguments($values, [], $names);
  90. $this->assertNull($args->getArgument('size'));
  91. $this->assertNull($args->getArgument('color'));
  92. }
  93. /**
  94. * get arguments by name
  95. */
  96. public function testGetArgumentInvalid(): void
  97. {
  98. $values = [];
  99. $names = ['size'];
  100. $args = new Arguments($values, [], $names);
  101. $this->expectException(ConsoleException::class);
  102. $args->getArgument('color');
  103. }
  104. /**
  105. * test getOptions()
  106. */
  107. public function testGetOptions(): void
  108. {
  109. $options = [
  110. 'verbose' => true,
  111. 'off' => false,
  112. 'empty' => '',
  113. ];
  114. $args = new Arguments([], $options, []);
  115. $this->assertSame($options, $args->getOptions());
  116. }
  117. /**
  118. * test hasOption()
  119. */
  120. public function testHasOption(): void
  121. {
  122. $options = [
  123. 'verbose' => true,
  124. 'off' => false,
  125. 'zero' => 0,
  126. 'empty' => '',
  127. ];
  128. $args = new Arguments([], $options, []);
  129. $this->assertTrue($args->hasOption('verbose'));
  130. $this->assertTrue($args->hasOption('off'));
  131. $this->assertTrue($args->hasOption('empty'));
  132. $this->assertTrue($args->hasOption('zero'));
  133. $this->assertFalse($args->hasOption('undef'));
  134. }
  135. /**
  136. * test getOption()
  137. */
  138. public function testGetOption(): void
  139. {
  140. $options = [
  141. 'verbose' => true,
  142. 'off' => false,
  143. 'zero' => '0',
  144. 'empty' => '',
  145. ];
  146. $args = new Arguments([], $options, []);
  147. $this->assertTrue($args->getOption('verbose'));
  148. $this->assertFalse($args->getOption('off'));
  149. $this->assertSame('', $args->getOption('empty'));
  150. $this->assertSame('0', $args->getOption('zero'));
  151. $this->assertNull($args->getOption('undef'));
  152. }
  153. /**
  154. * test getOption() checks types
  155. */
  156. public function testGetOptionInvalidType(): void
  157. {
  158. $options = [
  159. 'list' => [1, 2],
  160. ];
  161. $args = new Arguments([], $options, []);
  162. $this->expectException(ConsoleException::class);
  163. $args->getOption('list');
  164. }
  165. public function testGetBooleanOption(): void
  166. {
  167. $options = [
  168. 'verbose' => true,
  169. ];
  170. $args = new Arguments([], $options, []);
  171. $this->assertTrue($args->getBooleanOption('verbose'));
  172. $this->assertNull($args->getBooleanOption('missing'));
  173. }
  174. /**
  175. * test getOption() checks types
  176. */
  177. public function testGetOptionBooleanInvalidType(): void
  178. {
  179. $options = [
  180. 'list' => [1, 2],
  181. ];
  182. $args = new Arguments([], $options, []);
  183. $this->expectException(ConsoleException::class);
  184. $args->getBooleanOption('list');
  185. }
  186. public function testGetMultipleOption(): void
  187. {
  188. $options = [
  189. 'types' => ['one', 'two', 'three'],
  190. ];
  191. $args = new Arguments([], $options, []);
  192. $this->assertSame(['one', 'two', 'three'], $args->getMultipleOption('types'));
  193. $this->assertNull($args->getMultipleOption('missing'));
  194. }
  195. public function testGetMultipleOptionInvalidType(): void
  196. {
  197. $options = [
  198. 'connection' => 'test',
  199. ];
  200. $args = new Arguments([], $options, []);
  201. $this->expectException(ConsoleException::class);
  202. $args->getMultipleOption('connection');
  203. }
  204. }