ManagerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace League\CLImate\Tests\Argument;
  3. use League\CLImate\Argument\Argument;
  4. use League\CLImate\Argument\Manager;
  5. use PHPUnit\Framework\TestCase;
  6. class ManagerTest extends TestCase
  7. {
  8. private $manager;
  9. public function setUp(): void
  10. {
  11. $this->manager = new Manager();
  12. }
  13. public function testDefined1()
  14. {
  15. $argument = Argument::createFromArray("test", [
  16. "prefix" => "t",
  17. "longPrefix" => "test",
  18. ]);
  19. $this->manager->add($argument);
  20. $result = $this->manager->defined("test", ["command", "--test"]);
  21. $this->assertTrue($result);
  22. }
  23. public function testDefined2()
  24. {
  25. $result = $this->manager->defined("test");
  26. $this->assertFalse($result);
  27. }
  28. public function testDefined3()
  29. {
  30. $argument = Argument::createFromArray("lorem", [
  31. "prefix" => "l",
  32. "longPrefix" => "Лорем",
  33. ]);
  34. $this->manager->add($argument);
  35. $result = $this->manager->defined("lorem", ["command", "--Лорем"]);
  36. $this->assertTrue($result);
  37. }
  38. public function testItParsesAnOptionalArgument()
  39. {
  40. $this->manager->add([
  41. 'foo' => ['prefix' => 'f'],
  42. 'bar' => ['prefix' => 'b']
  43. ]);
  44. $this->manager->parse(['command', '-f', '-b', 'abc']);
  45. $this->assertEquals('', $this->manager->get('foo'));
  46. }
  47. public function testItStoresTrailingInArray()
  48. {
  49. $this->manager->add([
  50. 'foo' => ['prefix' => 'f']
  51. ]);
  52. $this->manager->parse(['command', '-f', '--', 'test', 'trailing with spaces']);
  53. $this->assertEquals('test trailing with spaces', $this->manager->trailing());
  54. $this->assertEquals(['test', 'trailing with spaces'], $this->manager->trailingArray());
  55. }
  56. public function testItSuggestAlternativesToUnknowArguments()
  57. {
  58. $this->manager->add([
  59. 'user' => [
  60. 'longPrefix' => 'user',
  61. ],
  62. 'password' => [
  63. 'longPrefix' => 'password',
  64. ],
  65. 'flag' => [
  66. 'longPrefix' => 'flag',
  67. 'noValue' => true,
  68. ],
  69. ]);
  70. $argv = [
  71. 'test-script',
  72. '--user=baz',
  73. '--pass=123',
  74. '--fag',
  75. '--xyz',
  76. ];
  77. $this->manager->parse($argv);
  78. $processed = $this->manager->getUnknowPrefixedArgumentsAndSuggestions();
  79. $this->assertCount(3, $processed);
  80. $this->assertEquals('password', $processed['pass']);
  81. $this->assertEquals('flag', $processed['fag']);
  82. $this->assertEquals('', $processed['xyz']);
  83. }
  84. }