Appearance
控制台测试
简介
除了简化 HTTP 测试外,Laravel 还为测试应用程序的自定义控制台命令提供了一个简单的 API。
成功/失败预期
首先,让我们探讨如何对 Artisan 命令的退出代码进行断言。为此,我们将使用 artisan
方法从测试中调用 Artisan 命令。然后,我们将使用 assertExitCode
方法断言命令以给定的退出代码完成:
php
test('console command', function () {
$this->artisan('inspire')->assertExitCode(0);
});
php
/**
* Test a console command.
*/
public function test_console_command(): void
{
$this->artisan('inspire')->assertExitCode(0);
}
您可以使用 assertNotExitCode 方法来断言命令未使用给定的退出代码退出:
php
$this->artisan('inspire')->assertNotExitCode(1);
当然,所有终端命令通常在成功时以状态代码 0 退出,在失败时以非零退出代码退出。因此,为方便起见,您可以使用 assertSuccessful 和 assertFailed 断言来断言给定命令是否以成功的退出代码退出:
php
$this->artisan('inspire')->assertSuccessful();
$this->artisan('inspire')->assertFailed();
输入/输出预期
Laravel 允许您轻松地 "模拟" 用户输入以测试您的控制台命令使用 expectsQuestion
方法。此外,您可以指定控制台命令的退出代码和文本使用 assertExitCode
和 expectsOutput
方法。例如,考虑以下控制台命令:
php
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.'.');
});
您可以使用以下测试来测试此命令:
php
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);
});
php
/**
* 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);
}
如果您正在使用 Laravel Prompts 提供的 search
或 multisearch
函数,则可以使用 expectsSearch
断言来模拟用户输入、搜索结果和选择:
php
test('console command', function () {
$this->artisan('example')
->expectsSearch('What is your name?', search: 'Tay', answers: [
'Taylor Otwell',
'Taylor Swift',
'Darian Taylor'
], answer: 'Taylor Otwell')
->assertExitCode(0);
});
php
/**
* Test a console command.
*/
public function test_console_command(): void
{
$this->artisan('example')
->expectsSearch('What is your name?', search: 'Tay', answers: [
'Taylor Otwell',
'Taylor Swift',
'Darian Taylor'
], answer: 'Taylor Otwell')
->assertExitCode(0);
}
您还可以断言控制台命令不生成任何输出使用 doesntExpectOutput
方法:
php
test('console command', function () {
$this->artisan('example')
->doesntExpectOutput()
->assertExitCode(0);
});
php
/**
* Test a console command.
*/
public function test_console_command(): void
{
$this->artisan('example')
->doesntExpectOutput()
->assertExitCode(0);
}
expectsOutputToContain
和 doesntExpectOutputToContain
方法可以用于对输出的部分进行断言:
php
test('console command', function () {
$this->artisan('example')
->expectsOutputToContain('Taylor')
->assertExitCode(0);
});
php
/**
* Test a console command.
*/
public function test_console_command(): void
{
$this->artisan('example')
->expectsOutputToContain('Taylor')
->assertExitCode(0);
}
确认预期
当编写一个需要确认的命令(以 "yes" 或 "no" 的形式)时,您可以使用 expectsConfirmation
方法:
php
$this->artisan('module:import')
->expectsConfirmation('Do you really wish to run this command?', 'no')
->assertExitCode(1);
表格预期
如果您的命令使用 Artisan 的 table
方法显示信息表格,则写出输出预期可能会很麻烦。相反,您可以使用 expectsTable
方法。该方法接受表格的标题作为其第一个参数和表格的数据作为其第二个参数:
php
$this->artisan('users:all')
->expectsTable([
'ID',
'Email',
], [
[1, 'taylor@example.com'],
[2, 'abigail@example.com'],
]);
控制台事件
默认情况下,Illuminate\Console\Events\CommandStarting
和 Illuminate\Console\Events\CommandFinished
事件在运行应用程序的测试时不会被分派。然而,您可以通过将 Illuminate\Foundation\Testing\WithConsoleEvents
trait 添加到给定测试类中来启用这些事件:
php
<?php
use Illuminate\Foundation\Testing\WithConsoleEvents;
uses(WithConsoleEvents::class);
// ...
php
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\WithConsoleEvents;
use Tests\TestCase;
class ConsoleEventTest extends TestCase
{
use WithConsoleEvents;
// ...
}