Skip to content
全部文档

控制台测试

简介

除了简化 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 退出,失败时则以非零退出码退出。因此为方便起见,你可以使用 assertSuccessfulassertFailed 断言来判断给定命令是否以成功退出码退出:

php
$this->artisan('inspire')->assertSuccessful();

$this->artisan('inspire')->assertFailed();

输入 / 输出断言

Laravel 允许你使用 expectsQuestion 方法轻松「模拟」控制台命令的用户输入。此外,你可以用 assertExitCodeexpectsOutput 方法指定期望的退出码与输出文本。例如,考虑以下控制台命令:

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 提供的 searchmultisearch 函数,可使用 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);
}

expectsOutputToContaindoesntExpectOutputToContain 方法可用于对输出的一部分做断言:

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\CommandStartingIlluminate\Console\Events\CommandFinished 事件。不过,可通过在给定测试类上添加 Illuminate\Foundation\Testing\WithConsoleEvents trait 来启用这些事件:

php
<?php

use Illuminate\Foundation\Testing\WithConsoleEvents;

pest()->use(WithConsoleEvents::class);

// ...
php
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\WithConsoleEvents;
use Tests\TestCase;

class ConsoleEventTest extends TestCase
{
    use WithConsoleEvents;

    // ...
}