PHPUnit 自動化測試大作戰【CH18】

閱讀時間約 9 分鐘

今天我們會接續環繞著 「Mocking」 這個主題。

在 Laravel 中,有幾個類別的 Mocking 方式,與前兩天所介紹的方式有所不同,在這次的系列文章中,會和大家介紹 Event、Mail、Queue、Storage、HTTP 這幾個類別的特殊 Mocking 方式。

今天就先來看 Event Mocking 吧!

Event Mocking 函數

  • Event::fake():當我們希望在執行測試目標行為時, 想驗證是否有觸發到 Event ,但又不要真的觸發 Event 的執行時,可在測試程式碼中呼叫此函數。
  • Event::assertDispatched():可驗證指定的 Event 是否會被觸發。需在執行 Event::fake() 後方可使用。
  • Event::assertNotDispatched():可驗證指定的 Event 是否不會被觸發。需在執行 Event::fake() 後方可使用。
  • Event::assertNothingDispatched():可驗正是否無 Event 被觸發。需在執行 Event::fake() 後方可使用。

(實際上 Event 還有其他 Mocking 函數可用,不過以上應該是最常用的幾個了。

接下來讓我們實際演練看看吧!

範例:使用者登入

  • 測試目標:登入端點
  • database/migrations/2014_10_12_000000_create_users_table.php
<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration{

/** * Run the migrations. * * @return void */

public function up() {

Schema::create('users', function (Blueprint $table) {

$table->id();

$table->string('name');

$table->string('email')->unique();

$table->timestamp('email_verified_at')->nullable();

$table->string('password');

// 新增 api_token 欄位

$table->string('api_token', 32)->nullable();

$table->rememberToken();

$table->timestamps();

}); } /** * Reverse the migrations. * * @return void */

public function down() {

Schema::dropIfExists('users');

}};
  • routes\web.php
use App\Events\UserLoggedIn;

use App\Models\User;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Route;

use Illuminate\Support\Str;

Route::post('/login', function (Request $request) {

$email = $request->input('email');

$password = $request->input('password');

$user = User::where([

'email' => $email,

'password' => $password,

])->first();

if (empty($user)) {

return response()->json([], 401);

} event(UserLoggedIn::class);

$user->api_token = Str::random(32);

$user->save();

return response()->json(['token' => $user->api_token]);

})->name('login');
  • 測試程式碼:
<?php

namespace Tests\Feature;

use App\Events\UserLoggedIn;

use App\Models\User;

use Illuminate\Foundation\Testing\RefreshDatabase;

use Illuminate\Support\Facades\Event;

use Illuminate\Support\Str;

use Tests\TestCase;

class EventTest extends TestCase{

use RefreshDatabase;

public function testLoginSuccess() {

$user = User::factory()->create([

'password' => Str::random(10),

]); $payload = [

'email' => $user->email,

'password' => $user->password,

]; Event::fake();

$response = $this->post(route('login'), $payload);

$user->refresh();

$response->assertJson([

'api_token' => $user->api_token

]); Event::assertDispatched(UserLoggedIn::class, 1);

} public function testLoginFailed() {

$user = User::factory()->create([

'password' => Str::random(10),

]); $payload = [

'email' => $user->email,

'password' => $user->password . 'x',

]; Event::fake();

$response = $this->post(route('login'), $payload);

$response->assertUnauthorized();

Event::assertNotDispatched(UserLoggedIn::class);

}}

以上測試程式碼,測試了 2 種測試案例:

  • testLoginSuccess():在這個測試案例函數中,我們驗證了當使用者成功登入時,是否會觸發 UserLoggedIn 這個 Event的發出。
  • testLoginFailed():在這個測試案例函數中,我們驗證了當使用者登入失敗時,是否不會觸發 UserLoggedIn 這個 Event的發出。

以上就是今天所介紹的 Event Mocking,大家可以多加演練。

下一篇讓我們來看看 Mail Mocking。

如果您喜歡這篇文章,歡迎加入追蹤以接收新文章通知 😄

參考資料

本系列文章目錄

3會員
57內容數
歡迎來到 WilliamP 的沙龍天地,在這裡將與各位讀者探討各種主題,包刮高中數學題庫、PHP開發經驗、LINE聊天機器人開發經驗、書摘筆記等,歡迎交流!
留言0
查看全部
發表第一個留言支持創作者!