ParserTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/cli-parser.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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 SebastianBergmann\CliParser;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @covers \SebastianBergmann\CliParser\Parser
  14. * @covers \SebastianBergmann\CliParser\AmbiguousOptionException
  15. * @covers \SebastianBergmann\CliParser\OptionDoesNotAllowArgumentException
  16. * @covers \SebastianBergmann\CliParser\RequiredOptionArgumentMissingException
  17. * @covers \SebastianBergmann\CliParser\UnknownOptionException
  18. */
  19. final class ParserTest extends TestCase
  20. {
  21. public function testParsesShortOptionsWithOptionalValues(): void
  22. {
  23. $this->assertSame(
  24. [
  25. [
  26. [
  27. 'f',
  28. null,
  29. ],
  30. ],
  31. [
  32. 'myArgument',
  33. ],
  34. ],
  35. (new Parser)->parse(
  36. [
  37. 'command',
  38. 'myArgument',
  39. '-f',
  40. ],
  41. 'f::'
  42. )
  43. );
  44. }
  45. public function testParsesLongOptionsWithValues(): void
  46. {
  47. $this->assertSame(
  48. [
  49. [
  50. ['--exec', null],
  51. ['--conf', 'config.xml'],
  52. ['--optn', null],
  53. ['--optn', 'content-of-o'],
  54. ],
  55. [
  56. 'parameter-0',
  57. 'parameter-1',
  58. 'parameter-2',
  59. 'parameter-n',
  60. ],
  61. ],
  62. (new Parser)->parse(
  63. [
  64. 'command',
  65. 'parameter-0',
  66. '--exec',
  67. 'parameter-1',
  68. '--conf',
  69. 'config.xml',
  70. '--optn',
  71. 'parameter-2',
  72. '--optn=content-of-o',
  73. 'parameter-n',
  74. ],
  75. '',
  76. ['exec', 'conf=', 'optn==']
  77. )
  78. );
  79. }
  80. public function testParsesShortongOptionsWithValues(): void
  81. {
  82. $this->assertSame(
  83. [
  84. [
  85. ['x', null],
  86. ['c', 'config.xml'],
  87. ['o', null],
  88. ['o', 'content-of-o'],
  89. ],
  90. [
  91. 'parameter-0',
  92. 'parameter-1',
  93. 'parameter-2',
  94. 'parameter-n',
  95. ],
  96. ],
  97. (new Parser)->parse(
  98. [
  99. 'command',
  100. 'parameter-0',
  101. '-x',
  102. 'parameter-1',
  103. '-c',
  104. 'config.xml',
  105. '-o',
  106. 'parameter-2',
  107. '-ocontent-of-o',
  108. 'parameter-n',
  109. ],
  110. 'xc:o::'
  111. )
  112. );
  113. }
  114. public function testParsesLongOptionsAfterArguments(): void
  115. {
  116. $this->assertSame(
  117. [
  118. [
  119. [
  120. '--colors',
  121. null,
  122. ],
  123. ],
  124. [
  125. 'myArgument',
  126. ],
  127. ],
  128. (new Parser)->parse(
  129. [
  130. 'command',
  131. 'myArgument',
  132. '--colors',
  133. ],
  134. '',
  135. ['colors==']
  136. )
  137. );
  138. }
  139. public function testParsesShortOptionsAfterArguments(): void
  140. {
  141. $this->assertSame(
  142. [
  143. [
  144. [
  145. 'v',
  146. null,
  147. ],
  148. ],
  149. [
  150. 'myArgument',
  151. ],
  152. ],
  153. (new Parser)->parse(
  154. [
  155. 'command',
  156. 'myArgument',
  157. '-v',
  158. ],
  159. 'v'
  160. )
  161. );
  162. }
  163. public function testReturnsEmptyResultWhenNotOptionsArePassed(): void
  164. {
  165. $this->assertSame(
  166. [
  167. [],
  168. [],
  169. ],
  170. (new Parser)->parse(
  171. [],
  172. 'v'
  173. )
  174. );
  175. }
  176. public function testRaisesAnExceptionForUnknownLongOption(): void
  177. {
  178. $this->expectException(UnknownOptionException::class);
  179. $this->expectExceptionMessage('Unknown option "--foo"');
  180. /* @noinspection UnusedFunctionResultInspection */
  181. (new Parser)->parse(
  182. [
  183. 'command',
  184. '--foo',
  185. ],
  186. '',
  187. ['colors']
  188. );
  189. }
  190. public function testRaisesAnExceptionForUnknownShortOption(): void
  191. {
  192. $this->expectException(UnknownOptionException::class);
  193. $this->expectExceptionMessage('Unknown option "-v"');
  194. /* @noinspection UnusedFunctionResultInspection */
  195. (new Parser)->parse(
  196. [
  197. 'command',
  198. 'myArgument',
  199. '-v',
  200. ],
  201. ''
  202. );
  203. }
  204. public function testRaisesAnExceptionWhenRequiredArgumentForLongOptionIsMissing(): void
  205. {
  206. $this->expectException(RequiredOptionArgumentMissingException::class);
  207. $this->expectExceptionMessage('Required argument for option "--foo" is missing');
  208. /* @noinspection UnusedFunctionResultInspection */
  209. (new Parser)->parse(
  210. [
  211. 'command',
  212. '--foo',
  213. ],
  214. '',
  215. ['foo=']
  216. );
  217. }
  218. public function testRaisesAnExceptionWhenRequiredArgumentForShortOptionIsMissing(): void
  219. {
  220. $this->expectException(RequiredOptionArgumentMissingException::class);
  221. $this->expectExceptionMessage('Required argument for option "-f" is missing');
  222. /* @noinspection UnusedFunctionResultInspection */
  223. (new Parser)->parse(
  224. [
  225. 'command',
  226. 'myArgument',
  227. '-f',
  228. ],
  229. 'f:'
  230. );
  231. }
  232. public function testRaisesAnExceptionWhenLongOptionIsAmbiguous(): void
  233. {
  234. $this->expectException(AmbiguousOptionException::class);
  235. $this->expectExceptionMessage('Option "--col" is ambiguous');
  236. /* @noinspection UnusedFunctionResultInspection */
  237. (new Parser)->parse(
  238. [
  239. 'command',
  240. '--col',
  241. ],
  242. '',
  243. ['columns', 'colors']
  244. );
  245. }
  246. public function testRaisesAnExceptionWhenAnArgumentIsGivenForLongOptionThatDoesNotAllowAnArgument(): void
  247. {
  248. $this->expectException(OptionDoesNotAllowArgumentException::class);
  249. $this->expectExceptionMessage('Option "--foo" does not allow an argument');
  250. /* @noinspection UnusedFunctionResultInspection */
  251. (new Parser)->parse(
  252. [
  253. 'command',
  254. '--foo=bar',
  255. ],
  256. '',
  257. ['foo']
  258. );
  259. }
  260. }