今天來看 Mail Mocking 吧!
Mail::fake()
:當我們希望在執行測試目標行為時, 想驗證是否有觸發到發送 Email ,但又不要真的觸發 Email 的寄送時,可在測試程式碼中呼叫此函數。Mail::assertSent()
:可驗證指定的 Mailable 類別是否會被觸發寄送。需在執行 Mail::fake()
後方可使用。Mail::assertNotSent()
。可驗證指定的 Mailable 類別是否不會被觸發寄送。需在執行 Mail::fake()
後方可使用。Mail::assertNothingSent()
。可驗證是否無 Mailable 類別被觸發寄送。需在執行 Mail::fake()
後方可使用。其中值得注意的是,Mail::assertSent()
還有更細緻的測試方式:
Mail::assertSent(MailableClass::class, function ($mail) use ($user) {
return $mail->hasTo($user->email) &&
$mail->hasCc('...') &&
$mail->hasBcc('...') &&
$mail->hasReplyTo('...') &&
$mail->hasFrom('...') &&
$mail->hasSubject('...');
});
$mail→hasTo
:驗證收件人。$mail→hasCc
:驗證副本收件人。$mail→hasBcc
:驗證密件副本收件人。$mail→hasReplyTo
:驗證該信是否為回覆給指定收件人之回信。$mail->hasFrom
:驗證寄件人。$mail->hasSubject
:驗證信件主旨。接下來讓我們實際演練看看吧!
測試目標:忘記密碼信索取端點
<?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');
$table->string('forget_password_token', 128)->nullable();
$table->rememberToken();
$table->timestamps();
}); } /** * Reverse the migrations. * * @return void */
public function down() {
Schema::dropIfExists('users');
}};
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable{
use HasApiTokens, HasFactory, Notifiable;
/** * The attributes that are mass assignable. * * @var array<int, string> */
protected $fillable = [
'name',
'email',
'password',
'forget_password_token',
]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */
protected $hidden = [
'password',
'remember_token',
]; /** * The attributes that should be cast. * * @var array<string, string> */
protected $casts = [
'email_verified_at' => 'datetime',
];}
<?php
namespace Database\Factories;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
class UserFactory extends Factory{
/** * Define the model's default state. * * @return array */
public function definition(): array {
return [
'name' => $this->faker->name,
'email' => $this->faker->safeEmail,
'email_verified_at' => $this->faker->dateTime(),
'password' => bcrypt($this->faker->password),
'remember_token' => Str::random(10),
'forget_password_token' => Str::random(128),
]; }}
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ForgetPasswordMail extends Mailable{
use Queueable, SerializesModels;
private $user;
/** * Create a new message instance. * * @return void */
public function __construct(User $user) {
$this->user = $user;
} /** * Build the message. * * @return $this */
public function build() {
$resetLink = config('app.url')
. "/password-reset?forget_password_token={$this->user->forget_password_token}";
return $this
->subject('To reset password')
->from('example@example.com', 'Example')
->with(['reset_link' => $resetLink])
->view('mail.forget-password-mail');
}}
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Password Reset Mail</title>
</head>
<body class="antialiased">
<a href="{{ $reset_link }}">Reset Password</a>
</body>
</html>
<?php
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;
use App\Mail\ForgetPasswordMail;
Route::post('/forget-password-mail', function (Request $request) {
$email = $request->input('email');
$user = User::where('email', '=', $email)->first();
if (empty($user)) {
return response()->json([], 404);
} $user->forget_password_token = Str::random(128);
$user->save();
Mail::to($email)
->cc('cc@test.test')
->bcc('bcc@test.test')
->send(new ForgetPasswordMail($user));
return response('', 200);
})->name('retrieve-forget-password-mail');
<?php
namespace Tests\Feature;
use App\Mail\ForgetPasswordMail;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class MailTest extends TestCase{
use RefreshDatabase;
public function testRetriveForgetPasswordMailSuccess() {
$user = User::factory()->create();
$data = [
'email' => $user->email,
]; Mail::fake();
$response = $this->post(route('retrieve-forget-password-mail'), $data);
$response->assertOk();
Mail::assertSent(ForgetPasswordMail::class, function ($mail) use ($user) {
return $mail->hasTo($user->email);
}); } public function testRetriveForgetPasswordMailFailed() {
$user = User::factory()->create();
$data = [
'email' => $user->email . 'x',
]; Mail::fake();
$response = $this->post(route('retrieve-forget-password-mail'), $data);
$response->assertNotFound();
Mail::assertNotSent(ForgetPasswordMail::class);
}}
以上測試程式碼,測試了 2 種測試案例:
testRetriveForgetPasswordMailSuccess()
:在這個測試案例函數中,我們驗證了當忘記密碼信索取端點被請求時,若使用者帳號確實存在時,是否會觸發忘記密碼信 ForgotPasswordMail
寄出。testRetriveForgetPasswordMailFailed()
:在這個測試案例函數中,我們驗證了當忘記密碼信索取端點被請求時,若使用者帳號不存在時,是否不會觸發忘記密碼信 ForgotPasswordMail
寄出。以上就是今天所介紹的 Event Mocking,大家可以多加演練。
下一篇讓我們來看看 Queue Mocking。
如果您喜歡這篇文章,歡迎加入追蹤以接收新文章通知 😄