控制台测试
简介
除了简化 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(): void5{6 $this->artisan('inspire')->assertExitCode(0);7}
你可以使用 assertNotExitCode 方法来断言命令没有以指定的退出代码退出。
1$this->artisan('inspire')->assertNotExitCode(1);
当然,所有终端命令通常在成功时会返回状态码 0,在失败时会返回非零退出代码。因此,为了方便起见,你可以利用 assertSuccessful 和 assertFailed 断言来判断给定的命令是否以成功状态码退出。
1$this->artisan('inspire')->assertSuccessful();2 3$this->artisan('inspire')->assertFailed();
输入 / 输出预期
Laravel 允许你使用 expectsQuestion 方法轻松地为控制台命令“模拟”用户输入。此外,你可以使用 assertExitCode 和 expectsOutput 方法来指定你期望控制台命令输出的退出代码和文本。例如,请看以下控制台命令:
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 提供的 search 或 multisearch 功能,可以使用 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(): void5{6 $this->artisan('example')7 ->doesntExpectOutput()8 ->assertExitCode(0);9}
expectsOutputToContain 和 doesntExpectOutputToContain 方法可用于对输出的部分内容进行断言。
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(): void5{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\CommandStarting 和 Illuminate\Console\Events\CommandFinished 事件。不过,你可以通过在测试类中添加 Illuminate\Foundation\Testing\WithConsoleEvents trait,为特定的测试类启用这些事件。
1<?php2 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}