CommandTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Test\Michel\Console;
  3. use Michel\Console\Argument\CommandArgument;
  4. use Michel\Console\Input;
  5. use Michel\Console\Option\CommandOption;
  6. use Michel\Console\Output;
  7. use Michel\UniTester\TestCase;
  8. use Test\Michel\Console\Command\FooCommand;
  9. class CommandTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. // TODO: Implement setUp() method.
  14. }
  15. protected function tearDown(): void
  16. {
  17. // TODO: Implement tearDown() method.
  18. }
  19. protected function execute(): void
  20. {
  21. $this->testGetName();
  22. $this->testGetDescription();
  23. $this->testGetOptions();
  24. $this->testGetArguments();
  25. $this->testExecute();
  26. }
  27. public function testGetName(): void
  28. {
  29. $command = new FooCommand();
  30. $this->assertEquals('foo', $command->getName());
  31. }
  32. public function testGetDescription(): void
  33. {
  34. $command = new FooCommand();
  35. $this->assertEquals('Performs the foo operation with optional parameters.', $command->getDescription());
  36. }
  37. public function testGetOptions(): void
  38. {
  39. $command = new FooCommand();
  40. $options = $command->getOptions();
  41. $this->assertEquals(2, count($options));
  42. $this->assertInstanceOf(CommandOption::class, $options[0]);
  43. $this->assertEquals('verbose', $options[0]->getName());
  44. $this->assertEquals('v', $options[0]->getShortcut());
  45. $this->assertEquals('Enable verbose output', $options[0]->getDescription());
  46. $this->assertTrue($options[0]->isFlag());
  47. $this->assertInstanceOf(CommandOption::class, $options[1]);
  48. $this->assertEquals('output', $options[1]->getName());
  49. $this->assertEquals('o', $options[1]->getShortcut());
  50. $this->assertEquals('Specify output file', $options[1]->getDescription());
  51. $this->assertFalse($options[1]->isFlag());
  52. }
  53. public function testGetArguments(): void
  54. {
  55. $command = new FooCommand();
  56. $arguments = $command->getArguments();
  57. $this->assertEquals(1, count($arguments));
  58. $this->assertInstanceOf(CommandArgument::class, $arguments[0]);
  59. $this->assertEquals('input', $arguments[0]->getName());
  60. $this->assertFalse($arguments[0]->isRequired());
  61. }
  62. public function testExecute(): void
  63. {
  64. $input = new Input('foo', ['verbose' => true, 'output' => 'output.txt'], ['input' => 'foo']);
  65. $lines = 0;
  66. $output = new Output(function (string $message) use (&$lines) {
  67. if ($lines === 0) {
  68. $this->assertEquals('Test OK', $message);
  69. }
  70. if ($lines === 2) {
  71. $this->assertEquals('ARGUMENTS: {"input":"foo"}', $message);
  72. }
  73. if ($lines === 4) {
  74. $this->assertEquals('OPTIONS: {"verbose":true,"output":"output.txt"}', $message);
  75. }
  76. $lines++;
  77. });
  78. $command = new FooCommand();
  79. $command->execute($input, $output);
  80. $this->assertEquals(6, $lines);
  81. }
  82. }