UserConfigOptionsParserTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\TestCase;
  4. require_once dirname(__DIR__, 2) . '/cli/CliOption.php';
  5. require_once dirname(__DIR__, 2) . '/cli/CliOptionsParser.php';
  6. final class UserConfigCliOptionsTest extends CliOptionsParser {
  7. public string $user;
  8. public string $key;
  9. public string $value;
  10. public bool $list;
  11. public bool $set;
  12. public bool $unset;
  13. public bool $valueStdin;
  14. public bool $force;
  15. public bool $showSecrets;
  16. public function __construct() {
  17. $this->addRequiredOption('user', (new CliOption('user')));
  18. $this->addOption('key', (new CliOption('key')));
  19. $this->addOption('value', (new CliOption('value')));
  20. $this->addOption('list', (new CliOption('list'))->withValueNone());
  21. $this->addOption('set', (new CliOption('set'))->withValueNone());
  22. $this->addOption('unset', (new CliOption('unset'))->withValueNone());
  23. $this->addOption('valueStdin', (new CliOption('value-stdin'))->withValueNone());
  24. $this->addOption('force', (new CliOption('force'))->withValueNone());
  25. $this->addOption('showSecrets', (new CliOption('show-secrets'))->withValueNone());
  26. parent::__construct();
  27. }
  28. }
  29. class UserConfigOptionsParserTest extends TestCase {
  30. public static function testUserIsRequired(): void {
  31. $result = self::runOptions('');
  32. self::assertArrayHasKey('user', $result->errors);
  33. }
  34. public static function testUserProvided(): void {
  35. $result = self::runOptions('--user=alice');
  36. self::assertEmpty($result->errors);
  37. self::assertSame('alice', $result->user);
  38. }
  39. public static function testListFlag(): void {
  40. $result = self::runOptions('--user=alice --list');
  41. self::assertTrue($result->list);
  42. self::assertFalse($result->set);
  43. self::assertFalse($result->unset);
  44. }
  45. public static function testShowSecretsFlag(): void {
  46. $result = self::runOptions('--user=alice --list --show-secrets');
  47. self::assertTrue($result->list);
  48. self::assertTrue($result->showSecrets);
  49. }
  50. public static function testSetFlagWithValue(): void {
  51. $result = self::runOptions('--user=alice --key=language --set --value=fr');
  52. self::assertTrue($result->set);
  53. self::assertFalse($result->list);
  54. self::assertFalse($result->unset);
  55. self::assertSame('language', $result->key);
  56. self::assertSame('fr', $result->value);
  57. }
  58. public static function testUnsetFlag(): void {
  59. $result = self::runOptions('--user=alice --key=language --unset');
  60. self::assertTrue($result->unset);
  61. self::assertFalse($result->set);
  62. }
  63. public static function testValueStdinFlag(): void {
  64. $result = self::runOptions('--user=alice --key=token --set --value-stdin');
  65. self::assertTrue($result->set);
  66. self::assertTrue($result->valueStdin);
  67. }
  68. public static function testForceFlag(): void {
  69. $result = self::runOptions('--user=alice --key=custom --set --value=hello --force');
  70. self::assertTrue($result->set);
  71. self::assertTrue($result->force);
  72. }
  73. public static function testGetKey(): void {
  74. $result = self::runOptions('--user=alice --key=language');
  75. self::assertEmpty($result->errors);
  76. self::assertSame('language', $result->key);
  77. self::assertFalse($result->set);
  78. self::assertFalse($result->unset);
  79. self::assertFalse($result->list);
  80. }
  81. public static function testUnknownOptionReturnsError(): void {
  82. $result = self::runOptions('--user=alice --unknown');
  83. self::assertArrayHasKey('unknown', $result->errors);
  84. }
  85. private static function runOptions(string $cliOptions = ''): UserConfigCliOptionsTest {
  86. $command = __DIR__ . '/cli-parser-test.php';
  87. $className = UserConfigCliOptionsTest::class;
  88. $result = shell_exec("CLI_PARSER_TEST_OPTIONS_CLASS='$className' $command $cliOptions 2>/dev/null");
  89. $result = is_string($result) ?
  90. unserialize($result, ['allowed_classes' => [UserConfigCliOptionsTest::class]]) :
  91. new UserConfigCliOptionsTest();
  92. /** @var UserConfigCliOptionsTest $result */
  93. return $result;
  94. }
  95. }