命令行脚本

Testing Is Documentation

tests/Console/CommandTest.php命令行脚本 - 图1

QueryPHP 内置控制台命名,底层采用 Symfony/console 开发,用法与 Symfony 一致,对基础命令进行了简单的封装。 几个简单的封装来自 Laravel,是对 Symfony 的基础命令做了一些常用功能的包装,可以完全满足常用开发需求。

Console 组件是 Symfony 里面的一个控制台命令组件,可以轻松地编写出运行在 CLI 上面的命名。

Uses

  1. <?php
  2. use Tests\Console\Command\CallOtherCommand;
  3. 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. $result = $this->runCommand(new CallOtherCommand(), [
  4. 'command' => 'call:other',
  5. ], function ($container, $application) {
  6. $application->normalizeCommands([Test1::class]);
  7. });
  8. $result = $this->normalizeContent($result);
  9. $this->assertStringContainsString($this->normalizeContent('call other command test.'), $result);
  10. $this->assertStringContainsString($this->normalizeContent('load1 test1'), $result);
  11. // argument and option
  12. $this->assertStringContainsString($this->normalizeContent('argument is {"command":"call:other"}'), $result);
  13. $this->assertStringContainsString($this->normalizeContent('option is {"help":false,"quiet":false,"verbose":false,"version":false,"ansi":false,"no-ansi":false,"no-interaction":false}'), $result);
  14. // table
  15. $this->assertStringContainsString($this->normalizeContent('| Item | Value |'), $result);
  16. $this->assertStringContainsString($this->normalizeContent('| hello | world |'), $result);
  17. $this->assertStringContainsString($this->normalizeContent('| foo | bar |'), $result);
  18. // time
  19. $this->assertStringContainsString($this->normalizeContent(']test time'), $result);
  20. // question
  21. $this->assertStringContainsString($this->normalizeContent('a question'), $result);
  22. // error
  23. $this->assertStringContainsString($this->normalizeContent('a error message'), $result);
  24. }