LaravelDocs(中文)

Console 測試 (console-tests)

介紹 (Introduction)

除了簡化 HTTP 測試外,Laravel 還提供了一個簡單的 API 來測試你應用程式的自訂 console 命令

成功 / 失敗預期 (Success Failure Expectations)

首先,讓我們探索如何對 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 退出,失敗時以非零結束碼退出。因此,為了方便起見,你可以使用 assertSuccessfulassertFailed 斷言來驗證指定的命令是否以成功的結束碼退出:

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

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

輸入 / 輸出預期 (Input Output Expectations)

Laravel 允許你使用 expectsQuestion 方法輕鬆地「模擬」console 命令的使用者輸入。此外,你可以使用 assertExitCodeexpectsOutput 方法來指定你預期由 console 命令輸出的結束碼和文字。例如,考慮以下 console 命令:

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 Prompts 提供的 searchmultisearch 函式,你可以使用 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 方法斷言 console 命令沒有產生任何輸出:

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);
}

expectsOutputToContaindoesntExpectOutputToContain 方法可用於對部分輸出進行斷言:

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);
}

確認預期 (Confirmation Expectations)

當撰寫一個預期以「是」或「否」形式進行確認的命令時,你可以使用 expectsConfirmation 方法:

$this->artisan('module:import')
    ->expectsConfirmation('Do you really wish to run this command?', 'no')
    ->assertExitCode(1);

表格預期 (Table Expectations)

如果你的命令使用 Artisan 的 table 方法顯示資訊表格,為整個表格撰寫輸出預期可能會很繁瑣。相反地,你可以使用 expectsTable 方法。這個方法接受表格的標題作為第一個引數,表格的資料作為第二個引數:

$this->artisan('users:all')
    ->expectsTable([
        'ID',
        'Email',
    ], [
        [1, 'taylor@example.com'],
        [2, 'abigail@example.com'],
    ]);

Console 事件 (Console Events)

預設情況下,在執行應用程式的測試時不會派發 Illuminate\Console\Events\CommandStartingIlluminate\Console\Events\CommandFinished 事件。然而,你可以透過在類別中加入 Illuminate\Foundation\Testing\WithConsoleEvents trait 來為指定的測試類別啟用這些事件:

<?php

use Illuminate\Foundation\Testing\WithConsoleEvents;

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

// ...
<?php

namespace Tests\Feature;

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

class ConsoleEventTest extends TestCase
{
    use WithConsoleEvents;

    // ...
}