MailTest.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Tests\Feature;
  3. use Tests\TestCase;
  4. use Illuminate\Support\Facades\Mail;
  5. use App\Mail\InviteMail;
  6. use Illuminate\Support\Str;
  7. /**
  8. * # 只运行 MailTest
  9. php artisan test tests/Feature/MailTest.php
  10. # 运行特定测试方法
  11. php artisan test --filter test_invite_mail_can_be_sent
  12. */
  13. class MailTest extends TestCase
  14. {
  15. /**
  16. * 测试邮件发送功能(使用 fake 模拟)
  17. */
  18. public function test_invite_mail_can_be_sent(): void
  19. {
  20. // 使用 Mail fake 来模拟邮件发送
  21. Mail::fake();
  22. // 发送邮件
  23. Mail::to('visuddhinanda@gmail.com')->send(new InviteMail(Str::uuid()));
  24. // 断言邮件已发送
  25. Mail::assertSent(InviteMail::class, function ($mail) {
  26. return $mail->hasTo('visuddhinanda@gmail.com');
  27. });
  28. // 断言只发送了一封邮件
  29. Mail::assertSent(InviteMail::class, 1);
  30. }
  31. /**
  32. * 测试真实邮件发送
  33. * 注意:需要正确配置 .env 中的邮件参数
  34. */
  35. public function test_invite_mail_actual_sending(): void
  36. {
  37. // 真实发送邮件
  38. Mail::to('visuddhinanda@gmail.com')->send(new InviteMail(Str::uuid()));
  39. // 断言测试通过(需要手动检查邮箱)
  40. $this->assertTrue(true);
  41. }
  42. }