运行命令代码

Testing Is Documentation

tests/Console/RunCommandTest.php运行命令代码 - 图1

有时候我们需要在非命令行调用命令,比如在控制器等地方直接运行命令行代码,系统对这种场景进行了简单封装。

Uses

  1. <?php
  2. use Leevel\Console\Application;
  3. use Leevel\Console\RunCommand;
  4. use Leevel\Di\Container;
  5. use Tests\Console\Command\CallOtherCommand;
  6. use Tests\Console\Load1\Test1;

运行命令代码基本使用方法

fixture 定义

Tests\Console\Load1\Test1

  1. namespace Tests\Console\Load1;
  2. use Leevel\Console\Command;
  3. class Test1 extends Command
  4. {
  5. protected string $name = 'load1:test1';
  6. protected string $description = 'load1 test1 for command';
  7. public function handle(): int
  8. {
  9. $this->info('load1 test1');
  10. return 0;
  11. }
  12. }

Tests\Console\Command\CallOtherCommand

  1. namespace Tests\Console\Command;
  2. use Leevel\Console\Command;
  3. class CallOtherCommand extends Command
  4. {
  5. protected string $name = 'call:other';
  6. protected string $description = 'call other command for test.';
  7. public function handle(): int
  8. {
  9. $this->info('call other command test.');
  10. $this->info('argument is '.json_encode($this->getArgument()));
  11. $this->info('option is '.json_encode($this->getOption()));
  12. $this->table([
  13. 'Item',
  14. 'Value',
  15. ], [
  16. ['hello', 'world'],
  17. ['foo', 'bar'],
  18. ]);
  19. $this->info($this->time('test time'));
  20. $this->question('a question');
  21. $this->error('a error message');
  22. $this->error('a error message');
  23. $this->call('load1:test1');
  24. return 0;
  25. }
  26. }
  1. public function testBaseUse(): void
  2. {
  3. $application = new Application(new Container(), '1.0');
  4. $runCommand = new RunCommand($application);
  5. $runCommand->normalizeCommand(Test1::class);
  6. $result = $runCommand->handle(new CallOtherCommand(), [
  7. 'command' => 'call:other',
  8. ]);
  9. $result = $this->normalizeContent($result);
  10. $this->assertStringContainsString($this->normalizeContent('call other command test.'), $result);
  11. $this->assertStringContainsString($this->normalizeContent('load1 test1'), $result);
  12. // argument and option
  13. $this->assertStringContainsString($this->normalizeContent('argument is {"command":"call:other"}'), $result);
  14. $this->assertStringContainsString($this->normalizeContent('option is {"help":false,"quiet":false,"verbose":false,"version":false,"ansi":false,"no-ansi":false,"no-interaction":false}'), $result);
  15. // table
  16. $this->assertStringContainsString($this->normalizeContent('| Item | Value |'), $result);
  17. $this->assertStringContainsString($this->normalizeContent('| hello | world |'), $result);
  18. $this->assertStringContainsString($this->normalizeContent('| foo | bar |'), $result);
  19. // time
  20. $this->assertStringContainsString($this->normalizeContent(']test time'), $result);
  21. // question
  22. $this->assertStringContainsString($this->normalizeContent('a question'), $result);
  23. // error
  24. $this->assertStringContainsString($this->normalizeContent('a error message'), $result);
  25. }

TIP

normalizeCommand 格式化命令,主要用于一个命令可能会调用其它命令,需要预先加载。