CommandTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Illuminate\Tests\Console;
  3. use Illuminate\Console\Application;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Console\OutputStyle;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. use Symfony\Component\Console\Input\ArrayInput;
  9. use Symfony\Component\Console\Input\InputArgument;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\NullOutput;
  13. use Symfony\Component\Console\Question\ChoiceQuestion;
  14. class CommandTest extends TestCase
  15. {
  16. protected function tearDown(): void
  17. {
  18. m::close();
  19. }
  20. public function testCallingClassCommandResolveCommandViaApplicationResolution()
  21. {
  22. $command = new class extends Command
  23. {
  24. public function handle()
  25. {
  26. }
  27. };
  28. $application = m::mock(Application::class);
  29. $command->setLaravel($application);
  30. $input = new ArrayInput([]);
  31. $output = new NullOutput;
  32. $application->shouldReceive('make')->with(OutputStyle::class, ['input' => $input, 'output' => $output])->andReturn(m::mock(OutputStyle::class));
  33. $application->shouldReceive('call')->with([$command, 'handle'])->andReturnUsing(function () use ($command, $application) {
  34. $commandCalled = m::mock(Command::class);
  35. $application->shouldReceive('make')->once()->with(Command::class)->andReturn($commandCalled);
  36. $commandCalled->shouldReceive('setApplication')->once()->with(null);
  37. $commandCalled->shouldReceive('setLaravel')->once()->with($application);
  38. $commandCalled->shouldReceive('run')->once();
  39. $command->call(Command::class);
  40. });
  41. $command->run($input, $output);
  42. }
  43. public function testGettingCommandArgumentsAndOptionsByClass()
  44. {
  45. $command = new class extends Command
  46. {
  47. public function handle()
  48. {
  49. }
  50. protected function getArguments()
  51. {
  52. return [
  53. new InputArgument('argument-one', InputArgument::REQUIRED, 'first test argument'),
  54. ['argument-two', InputArgument::OPTIONAL, 'a second test argument'],
  55. ];
  56. }
  57. protected function getOptions()
  58. {
  59. return [
  60. new InputOption('option-one', 'o', InputOption::VALUE_OPTIONAL, 'first test option'),
  61. ['option-two', 't', InputOption::VALUE_REQUIRED, 'second test option'],
  62. ];
  63. }
  64. };
  65. $application = app();
  66. $command->setLaravel($application);
  67. $input = new ArrayInput([
  68. 'argument-one' => 'test-first-argument',
  69. 'argument-two' => 'test-second-argument',
  70. '--option-one' => 'test-first-option',
  71. '--option-two' => 'test-second-option',
  72. ]);
  73. $output = new NullOutput;
  74. $command->run($input, $output);
  75. $this->assertSame('test-first-argument', $command->argument('argument-one'));
  76. $this->assertSame('test-second-argument', $command->argument('argument-two'));
  77. $this->assertSame('test-first-option', $command->option('option-one'));
  78. $this->assertSame('test-second-option', $command->option('option-two'));
  79. }
  80. public function testTheInputSetterOverwrite()
  81. {
  82. $input = m::mock(InputInterface::class);
  83. $input->shouldReceive('hasArgument')->once()->with('foo')->andReturn(false);
  84. $command = new Command;
  85. $command->setInput($input);
  86. $this->assertFalse($command->hasArgument('foo'));
  87. }
  88. public function testTheOutputSetterOverwrite()
  89. {
  90. $output = m::mock(OutputStyle::class);
  91. $output->shouldReceive('writeln')->once()->withArgs(function (...$args) {
  92. return $args[0] === '<info>foo</info>';
  93. });
  94. $command = new Command;
  95. $command->setOutput($output);
  96. $command->info('foo');
  97. }
  98. public function testChoiceIsSingleSelectByDefault()
  99. {
  100. $output = m::mock(OutputStyle::class);
  101. $output->shouldReceive('askQuestion')->once()->withArgs(function (ChoiceQuestion $question) {
  102. return $question->isMultiselect() === false;
  103. });
  104. $command = new Command;
  105. $command->setOutput($output);
  106. $command->choice('Do you need further help?', ['yes', 'no']);
  107. }
  108. public function testChoiceWithMultiselect()
  109. {
  110. $output = m::mock(OutputStyle::class);
  111. $output->shouldReceive('askQuestion')->once()->withArgs(function (ChoiceQuestion $question) {
  112. return $question->isMultiselect() === true;
  113. });
  114. $command = new Command;
  115. $command->setOutput($output);
  116. $command->choice('Select all that apply.', ['option-1', 'option-2', 'option-3'], null, null, true);
  117. }
  118. }