控制台測試
📝 此頁面為 Laravel 官方文檔的繁體中文翻譯。查看原始英文版本
控制台測試
簡介
除了簡化 HTTP 測試之外,Laravel 還提供了一個簡單的 API 來測試您應用程式的 自訂控制台命令。
成功/失敗預期
首先,讓我們探索如何對 Artisan 命令的退出代碼進行斷言。為了實現這一點,我們將使用 artisan 方法從我們的測試中調用 Artisan 命令。然後,我們將使用 assertExitCode 方法來斷言命令已完成並返回給定的退出代碼:
test('console command', function () {
$this->artisan('inspire')->assertExitCode(0);
});
/**
* 測試控制台命令。
*/
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('您的名字是什麼?');
$language = $this->choice('您偏好哪種語言?', [
'PHP',
'Ruby',
'Python',
]);
$this->line('您的名字是 '.$name.',您偏好 '.$language.'。');
});
您可以使用以下測試來測試此命令:
test('console command', function () {
$this->artisan('question')
->expectsQuestion('您的名字是什麼?', 'Taylor Otwell')
->expectsQuestion('您偏好哪種語言?', 'PHP')
->expectsOutput('您的名字是 Taylor Otwell,您偏好 PHP。')
->doesntExpectOutput('您的名字是 Taylor Otwell,您偏好 Ruby。')
->assertExitCode(0);
});
/**
* 測試控制台命令。
*/
public function test_console_command(): void
{
$this->artisan('question')
->expectsQuestion('您的名字是什麼?', 'Taylor Otwell')
->expectsQuestion('您偏好哪種語言?', 'PHP')
->expectsOutput('您的名字是 Taylor Otwell,您偏好 PHP。')
->doesntExpectOutput('您的名字是 Taylor Otwell,您偏好 Ruby。')
->assertExitCode(0);
}
如果您正在使用 Laravel Prompts 提供的 search 或 multisearch 函數,您可以使用 expectsSearch 斷言來模擬使用者的輸入、搜尋結果和選擇:
test('console command', function () {
$this->artisan('example')
->expectsSearch('您的名字是什麼?', search: 'Tay', answers: [
'Taylor Otwell',
'Taylor Swift',
'Darian Taylor'
], answer: 'Taylor Otwell')
->assertExitCode(0);
});
/**
* 測試控制台命令。
*/
public function test_console_command(): void
{
$this->artisan('example')
->expectsSearch('您的名字是什麼?', 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);
});
/**
* 測試控制台命令。
*/
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);
});
/**
* 測試控制台命令。
*/
public function test_console_command(): void
{
$this->artisan('example')
->expectsOutputToContain('Taylor')
->assertExitCode(0);
}
確認預期
編寫期望以「是」或「否」形式確認的命令時,您可以使用 expectsConfirmation 方法:
$this->artisan('module:import')
->expectsConfirmation('您真的要執行此命令嗎?', 'no')
->assertExitCode(1);
表格預期
如果您的命令使用 Artisan 的 table 方法顯示資訊表格,為整個表格編寫輸出預期可能很繁瑣。相反,您可以使用 expectsTable 方法。此方法接受表格的標題作為第一個參數,表格的資料作為第二個參數:
$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
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;
// ...
}