介绍
除了简化 HTTP 测试之外,Laravel 还提供了一个简单的 API 来测试应用程序的 自定义控制台命令。
期望成功/失败
首先,让我们探索如何对 Artisan 命令的退出代码进行断言。为此,我们将使用 php artisan 方法从我们的测试中调用 Artisan 命令。然后,我们将使用 php assertExitCode 方法断言该命令以给定的退出代码完成:
test('console command', function () {$this->artisan('inspire')->assertExitCode(0);});
/*** 测试控制台命令。*/public function test_console_command(): void{$this->artisan('inspire')->assertExitCode(0);}
你可以使用 php assertNotExitCode 方法断言命令没有以给定的退出代码退出:
$this->artisan('inspire')->assertNotExitCode(1);
当然,所有终端命令通常在成功时以 php 0 状态码退出,在不成功时以非零退出码退出。因此,为方便起见,你可以使用 php assertSuccessful 和 php assertFailed 断言来断言给定命令是否以成功的退出代码退出:
$this->artisan('inspire')->assertSuccessful();$this->artisan('inspire')->assertFailed();
期望输入/输出
Laravel 允许你使用 php expectsQuestion 方法轻松 「mock」控制台命令的用户输入。此外,你可以使用 php assertExitCode 和 php expectsOutput 方法指定你希望通过控制台命令输出的退出代码和文本。例如,考虑以下控制台命令:
Artisan::command('question', function () {$name = $this->ask('What is your name?');$language = $this->choice('Which language do you prefer?', ['PHP','Ruby','Python',]);$this->line('Your name is '.$name.' and you prefer '.$language.'.');});
你可以通过以下测试来测试该命令:
test('console command', function () {$this->artisan('question')->expectsQuestion('What is your name?', 'Taylor Otwell')->expectsQuestion('Which language do you prefer?', 'PHP')->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')->assertExitCode(0);});
/*** Test a console command.*/public function test_console_command(): void{$this->artisan('question')->expectsQuestion('What is your name?', 'Taylor Otwell')->expectsQuestion('Which language do you prefer?', 'PHP')->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')->assertExitCode(0);}
你还可以使用php doesntExpectOutput方法断言控制台命令不会生成任何输出:
test('console command', function () {$this->artisan('example')->doesntExpectOutput()->assertExitCode(0);});
/*** Test a console command.*/public function test_console_command(): void{$this->artisan('example')->doesntExpectOutput()->assertExitCode(0);}
php expectsOutputToContain 和 php doesntExpectOutputToContain 方法可用于对输出的一部分进行断言
test('console command', function () {$this->artisan('example')->expectsOutputToContain('Taylor')->assertExitCode(0);});
/*** Test a console command.*/public function test_console_command(): void{$this->artisan('example')->expectsOutputToContain('Taylor')->assertExitCode(0);}
确认期望
当编写一个期望以「是」或「否」答案形式确认的命令时,你可以使用 php expectsConfirmation 方法
$this->artisan('module:import')->expectsConfirmation('Do you really wish to run this command?', 'no')->assertExitCode(1);
表期望值
如果命令使用 Artisan 的 php table 方法显示信息表,则为整个表编写输出期望值可能会很麻烦。相反,您可以使用php expectsTable方法。此方法接受表的标头作为其第一个参数,并将表的数据作为其第二个参数:
$this->artisan('users:all')->expectsTable(['ID','Email',], [[1, '[email protected]'],[2, '[email protected]'],]);
控制台事件
默认情况下,在运行应用程序的测试时,不会调度php Illuminate\Console\Events\CommandStarting和php Illuminate\Console\Events\CommandFinished事件。但是,您可以通过将php Illuminate\Foundation\Testing\WithConsoleEvents特征添加到该类来为给定测试类启用这些事件:
<?phpuse Illuminate\Foundation\Testing\WithConsoleEvents;uses(WithConsoleEvents::class);// ...
<?phpnamespace Tests\Feature;use Illuminate\Foundation\Testing\WithConsoleEvents;use Tests\TestCase;class ConsoleEventTest extends TestCase{use WithConsoleEvents;// ...}