跳轉至內容

控制檯測試

簡介

除了簡化 HTTP 測試外,Laravel 還提供了一個簡單的 API,用於測試應用程式的自定義控制檯命令

成功 / 失敗預期

首先,我們來探討如何對 Artisan 命令的退出程式碼進行斷言。為此,我們將使用 artisan 方法在測試中呼叫 Artisan 命令。然後,使用 assertExitCode 方法來斷言命令是否以指定的退出程式碼完成。

1test('console command', function () {
2 $this->artisan('inspire')->assertExitCode(0);
3});
1/**
2 * Test a console command.
3 */
4public function test_console_command(): void
5{
6 $this->artisan('inspire')->assertExitCode(0);
7}

你可以使用 assertNotExitCode 方法來斷言命令沒有以指定的退出程式碼退出。

1$this->artisan('inspire')->assertNotExitCode(1);

當然,所有終端命令通常在成功時會返回狀態碼 0,在失敗時會返回非零退出程式碼。因此,為了方便起見,你可以利用 assertSuccessfulassertFailed 斷言來判斷給定的命令是否以成功狀態碼退出。

1$this->artisan('inspire')->assertSuccessful();
2 
3$this->artisan('inspire')->assertFailed();

輸入 / 輸出預期

Laravel 允許你使用 expectsQuestion 方法輕鬆地為控制檯命令“模擬”使用者輸入。此外,你可以使用 assertExitCodeexpectsOutput 方法來指定你期望控制檯命令輸出的退出程式碼和文字。例如,請看以下控制檯命令:

1Artisan::command('question', function () {
2 $name = $this->ask('What is your name?');
3 
4 $language = $this->choice('Which language do you prefer?', [
5 'PHP',
6 'Ruby',
7 'Python',
8 ]);
9 
10 $this->line('Your name is '.$name.' and you prefer '.$language.'.');
11});

你可以使用以下測試來測試此命令:

1test('console command', function () {
2 $this->artisan('question')
3 ->expectsQuestion('What is your name?', 'Taylor Otwell')
4 ->expectsQuestion('Which language do you prefer?', 'PHP')
5 ->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')
6 ->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')
7 ->assertExitCode(0);
8});
1/**
2 * Test a console command.
3 */
4public function test_console_command(): void
5{
6 $this->artisan('question')
7 ->expectsQuestion('What is your name?', 'Taylor Otwell')
8 ->expectsQuestion('Which language do you prefer?', 'PHP')
9 ->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')
10 ->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')
11 ->assertExitCode(0);
12}

如果你使用的是 Laravel Prompts 提供的 searchmultisearch 功能,可以使用 expectsSearch 斷言來模擬使用者的輸入、搜尋結果和選擇。

1test('console command', function () {
2 $this->artisan('example')
3 ->expectsSearch('What is your name?', search: 'Tay', answers: [
4 'Taylor Otwell',
5 'Taylor Swift',
6 'Darian Taylor'
7 ], answer: 'Taylor Otwell')
8 ->assertExitCode(0);
9});
1/**
2 * Test a console command.
3 */
4public function test_console_command(): void
5{
6 $this->artisan('example')
7 ->expectsSearch('What is your name?', search: 'Tay', answers: [
8 'Taylor Otwell',
9 'Taylor Swift',
10 'Darian Taylor'
11 ], answer: 'Taylor Otwell')
12 ->assertExitCode(0);
13}

你還可以使用 doesntExpectOutput 方法來斷言控制檯命令沒有產生任何輸出。

1test('console command', function () {
2 $this->artisan('example')
3 ->doesntExpectOutput()
4 ->assertExitCode(0);
5});
1/**
2 * Test a console command.
3 */
4public function test_console_command(): void
5{
6 $this->artisan('example')
7 ->doesntExpectOutput()
8 ->assertExitCode(0);
9}

expectsOutputToContaindoesntExpectOutputToContain 方法可用於對輸出的部分內容進行斷言。

1test('console command', function () {
2 $this->artisan('example')
3 ->expectsOutputToContain('Taylor')
4 ->assertExitCode(0);
5});
1/**
2 * Test a console command.
3 */
4public function test_console_command(): void
5{
6 $this->artisan('example')
7 ->expectsOutputToContain('Taylor')
8 ->assertExitCode(0);
9}

確認預期

在編寫需要以“是”或“否”形式進行確認的命令時,你可以利用 expectsConfirmation 方法。

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

表格預期

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

1$this->artisan('users:all')
2 ->expectsTable([
3 'ID',
4 'Email',
5 ], [
8 ]);

控制檯事件

預設情況下,在執行應用程式測試時,不會分發 Illuminate\Console\Events\CommandStartingIlluminate\Console\Events\CommandFinished 事件。不過,你可以透過在測試類中新增 Illuminate\Foundation\Testing\WithConsoleEvents trait,為特定的測試類啟用這些事件。

1<?php
2 
3use Illuminate\Foundation\Testing\WithConsoleEvents;
4 
5pest()->use(WithConsoleEvents::class);
6 
7// ...
1<?php
2 
3namespace Tests\Feature;
4 
5use Illuminate\Foundation\Testing\WithConsoleEvents;
6use Tests\TestCase;
7 
8class ConsoleEventTest extends TestCase
9{
10 use WithConsoleEvents;
11 
12 // ...
13}