控制台测试

简介

除了简单 HTTP 测试,Laravel 还提供了一个简单的 API 用于测试应用程序的自定义控制台命令。

成功/失败预期

让我们首先探讨如何断言 Artisan 命令的退出代码。为此,我们将使用 artisan 方法在测试中调用 Artisan 命令。然后,我们将使用 assertExitCode 方法断言命令是否以给定的退出代码完成:

  • Pest

  • PHPUnit

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

输入/输出预期

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

您可以使用以下测试来测试此命令:

  • Pest

  • PHPUnit

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 提供的 searchmultisearch 功能,您可以使用 expectsSearch 断言来模拟用户输入、搜索结果和选择:

  • Pest

  • PHPUnit

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 方法:

  • Pest

  • PHPUnit

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 方法可用于对输出的部分内容进行断言:

  • Pest

  • PHPUnit

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

确认预期

当编写一个需要 “yes” 或 “no” 确认回答的命令时,您可以使用 expectsConfirmation 方法:

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

表格预期

如果您的命令使用 Artisan 的 table 方法显示信息表格,那么为整个表格编写输出预期可能会很繁琐。相反,您可以使用 expectsTable 方法。该方法接受表格的头部作为第一个参数,表格的数据作为第二个参数:

$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 添加到测试类中,来为特定的测试类启用这些事件:

  • Pest

  • PHPUnit

<?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;

    // ...
}