Skip to content

コンソールテスト

はじめに

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で終了し、失敗した場合にはゼロ以外の終了コードで終了します。したがって、簡単に、assertSuccessfulassertFailedアサーションを使用して、指定されたコマンドが成功した終了コードで終了したかどうかをアサートできます。

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

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

入力 / 出力のテスト

Laravelでは、expectsQuestionメソッドを使用してコンソールコマンドのユーザー入力を簡単に「モック」できます。さらに、assertExitCodeexpectsOutputメソッドを使用して、コンソールコマンドによって出力されると予想される終了コードとテキストを指定できます。例えば、次のコンソールコマンドを考えてみましょう。

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);
});
/**
 * コンソールコマンドのテスト
 */
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が提供する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);
});
/**
 * コンソールコマンドのテスト
 */
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);
});
/**
 * コンソールコマンドのテスト
 */
public function test_console_command(): void
{
    $this->artisan('example')
            ->doesntExpectOutput()
            ->assertExitCode(0);
}

expectsOutputToContaindoesntExpectOutputToContainメソッドを使用して、出力の一部に対してアサーションを行うこともできます。

test('console command', function () {
    $this->artisan('example')
         ->expectsOutputToContain('Taylor')
         ->assertExitCode(0);
});
/**
 * コンソールコマンドのテスト
 */
public function test_console_command(): void
{
    $this->artisan('example')
            ->expectsOutputToContain('Taylor')
            ->assertExitCode(0);
}

確認の期待値

"yes"または"no"の回答形式で確認を期待するコマンドを書く場合、expectsConfirmationメソッドを使用できます。

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

テーブルの期待値

コマンドがArtisanのtableメソッドを使用して情報のテーブルを表示する場合、テーブル全体の出力期待値を書くのは面倒です。代わりに、expectsTableメソッドを使用できます。このメソッドは、テーブルのヘッダーを最初の引数として、テーブルのデータを2番目の引数として受け取ります。

$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トレイトをクラスに追加することで、特定のテストクラスに対してこれらのイベントを有効にできます。

<?php

use Illuminate\Foundation\Testing\WithConsoleEvents;

uses(WithConsoleEvents::class);

// ...
<?php

namespace Tests\Feature;

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

class ConsoleEventTest extends TestCase
{
    use WithConsoleEvents;

    // ...
}

ユーザーノート