CommandParserTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Test\Michel\Console;
  3. use Michel\Console\CommandParser;
  4. use Michel\UniTester\TestCase;
  5. class CommandParserTest extends TestCase
  6. {
  7. protected function setUp(): void
  8. {
  9. // TODO: Implement setUp() method.
  10. }
  11. protected function tearDown(): void
  12. {
  13. // TODO: Implement tearDown() method.
  14. }
  15. protected function execute(): void
  16. {
  17. $this->testCommandName();
  18. $this->testOptions();
  19. $this->testArguments();
  20. $this->testHasOption();
  21. $this->testGetOptionValue();
  22. $this->testGetArgumentValue();
  23. }
  24. public function testCommandName()
  25. {
  26. $parser = new CommandParser(self::createArgv(['foo', '--bar=baz']));
  27. $this->assertEquals('foo', $parser->getCommandName());
  28. }
  29. public function testOptions()
  30. {
  31. $parser = new CommandParser(self::createArgv(['foo', '--bar=baz', '--qux']));
  32. $this->assertEquals(['bar' => 'baz', 'qux' => true], $parser->getOptions());
  33. }
  34. public function testArguments()
  35. {
  36. $parser = new CommandParser(self::createArgv(['foo', 'bar', 'baz']));
  37. $this->assertEquals(['bar', 'baz'], $parser->getArguments());
  38. }
  39. public function testHasOption()
  40. {
  41. $parser = new CommandParser(self::createArgv(['foo', '--bar=baz']));
  42. $this->assertTrue($parser->hasOption('bar'));
  43. $this->assertFalse($parser->hasOption('qux'));
  44. }
  45. public function testGetOptionValue()
  46. {
  47. $parser = new CommandParser(self::createArgv(['foo', '--bar=baz']));
  48. $this->assertEquals('baz', $parser->getOptionValue('bar'));
  49. }
  50. public function testGetArgumentValue()
  51. {
  52. $parser = new CommandParser(self::createArgv(['foo', 'bar', 'baz']));
  53. $this->assertEquals('bar', $parser->getArgumentValue(0));
  54. $this->assertEquals('baz', $parser->getArgumentValue(1));
  55. }
  56. public function testHasArgument()
  57. {
  58. $parser = new CommandParser(self::createArgv(['foo', 'bar', 'baz']));
  59. $this->assertTrue($parser->hasArgument(0));
  60. $this->assertTrue($parser->hasArgument(1));
  61. $this->assertFalse($parser->hasArgument(2));
  62. }
  63. private static function createArgv(array $argv): array
  64. {
  65. return array_merge(['bin/console'], $argv);
  66. }
  67. }