主控台測試
簡介
在 Laravel 中除了能簡單地測試 HTTP 外,Laravel 還提供了一個簡單的 API 來測試專案的自訂主控台指令。
預期成功 / 失敗
若要開始,讓我們先來看看如何針對 Artisan 指令的結束代碼 (Exit Code) 作 Assertion (判斷提示)。為此,我們將使用 artisan
方法來在測試中叫用 Artisan 指令。接著,我們會使用 assertExitCode
方法來判斷該指令是否以給定的結束代碼完成:
1/**2 * Test a console command.3 *4 * @return void5 */6public function test_console_command()7{8 $this->artisan('inspire')->assertExitCode(0);9}
1/**2 * Test a console command.3 *4 * @return void5 */6public function test_console_command()7{8 $this->artisan('inspire')->assertExitCode(0);9}
可以使用 assertNotExitCode
方法來判斷該指令是否不以給定結束代碼終止:
1$this->artisan('inspire')->assertNotExitCode(1);
1$this->artisan('inspire')->assertNotExitCode(1);
當然,一般來說,所有以狀態碼 0
結束的終端機指令通常都代表成功,而非 0 的結束代碼則代表不成功。因此,為了方便起見,我們可以使用 assertSuccessful
與 assertFailed
Assertion 來判斷給定的指令是否以成功結束碼退出:
1$this->artisan('inspire')->assertSuccessful();23$this->artisan('inspire')->assertFailed();
1$this->artisan('inspire')->assertSuccessful();23$this->artisan('inspire')->assertFailed();
預期的輸入/輸出
Laravel 能讓你輕鬆地通過 expectsQuestion
方法來為主控台指令「模擬(Mock)」使用者輸入。此外,還可以通過 assertExitCode
與 expectsOutput
來指定主控台預期的結束代碼與輸出文字。舉例來說,假設有下列主控台指令:
1Artisan::command('question', function () {2 $name = $this->ask('What is your name?');34 $language = $this->choice('Which language do you prefer?', [5 'PHP',6 'Ruby',7 'Python',8 ]);910 $this->line('Your name is '.$name.' and you prefer '.$language.'.');11});
1Artisan::command('question', function () {2 $name = $this->ask('What is your name?');34 $language = $this->choice('Which language do you prefer?', [5 'PHP',6 'Ruby',7 'Python',8 ]);910 $this->line('Your name is '.$name.' and you prefer '.$language.'.');11});
可以通過下列這個使用了 expectsQuestion
, expectsOutput
, doesntExpectOutput
, expectsOutputToContain
, doesntExpectOutputToContain
與 assertExitCode
方法的測試來測試該指令:
1/**2 * Test a console command.3 *4 * @return void5 */6public function test_console_command()7{8 $this->artisan('question')9 ->expectsQuestion('What is your name?', 'Taylor Otwell')10 ->expectsQuestion('Which language do you prefer?', 'PHP')11 ->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')12 ->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')13 ->expectsOutputToContain('Taylor Otwell')14 ->doesntExpectOutputToContain('you prefer Ruby')15 ->assertExitCode(0);16}
1/**2 * Test a console command.3 *4 * @return void5 */6public function test_console_command()7{8 $this->artisan('question')9 ->expectsQuestion('What is your name?', 'Taylor Otwell')10 ->expectsQuestion('Which language do you prefer?', 'PHP')11 ->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')12 ->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')13 ->expectsOutputToContain('Taylor Otwell')14 ->doesntExpectOutputToContain('you prefer Ruby')15 ->assertExitCode(0);16}
預期確認
當撰寫預期答案為「yes」或「no」確認的指令時,可以使用 expectsConfirmation
方法:
1$this->artisan('module:import')2 ->expectsConfirmation('Do you really wish to run this command?', 'no')3 ->assertExitCode(1);
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 ]);
1$this->artisan('users:all')2 ->expectsTable([3 'ID',4 'Email',5 ], [8 ]);