控制台测试
成功/失败预期
让我们首先探讨如何断言 Artisan 命令的退出代码。为此,我们将使用 artisan
方法在测试中调用 Artisan 命令。然后,我们将使用 assertExitCode
方法断言命令是否以给定的退出代码完成:
test('console command', function () {
$this->artisan('inspire')->assertExitCode(0);
});
/**
* Test a console command.
*/
public function test_console_command(): void
{
$this->artisan('inspire')->assertExitCode(0);
}
您可以使用 assertNotExitCode
方法来断言命令未以给定的退出代码退出:
$this->artisan('inspire')->assertNotExitCode(1);
当然,所有终端命令通常在成功时会以退出代码 0 退出,而在失败时则会以非零退出代码退出。因此,为了方便,您可以使用 assertSuccessful
和 assertFailed
断言,分别断言给定命令是否以成功的退出代码退出或失败:
$this->artisan('inspire')->assertSuccessful();
$this->artisan('inspire')->assertFailed();
输入/输出预期
Laravel 允许您轻松地“模拟”控制台命令的用户输入,使用 expectsQuestion
方法。此外,您可以使用 assertExitCode
和 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);
}
如果您正在使用 Laravel 提供的 search
或 multisearch
功能,您可以使用 expectsSearch
断言来模拟用户输入、搜索结果和选择:
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);
});
/**
* 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
方法:
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);
}
expectsOutputToContain
和 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);
}