|
|
<?php
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
use App\Models\User;
|
|
|
use App\Models\Admin;
|
|
|
use App\Models\Visit;
|
|
|
use App\Models\VisitTime;
|
|
|
use App\Models\VisitArea;
|
|
|
use App\Models\Department;
|
|
|
use App\Notifications\VisitEmailNotify;
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
class VisitEmailSendTest extends TestCase
|
|
|
{
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
protected function setUp(): void
|
|
|
{
|
|
|
parent::setUp();
|
|
|
|
|
|
// 创建测试数据
|
|
|
$this->createTestData();
|
|
|
}
|
|
|
|
|
|
private function createTestData()
|
|
|
{
|
|
|
// 创建部门
|
|
|
$department = Department::factory()->create([
|
|
|
'name' => '测试部门'
|
|
|
]);
|
|
|
|
|
|
// 创建访问区域
|
|
|
$visitArea = VisitArea::factory()->create([
|
|
|
'name' => '测试区域'
|
|
|
]);
|
|
|
|
|
|
// 创建访问时段
|
|
|
$visitTime = VisitTime::factory()->create([
|
|
|
'start_time' => '09:00:00',
|
|
|
'end_time' => '12:00:00'
|
|
|
]);
|
|
|
|
|
|
// 创建被访管理员
|
|
|
$this->acceptAdmin = Admin::factory()->create([
|
|
|
'name' => '被访管理员',
|
|
|
'email' => '648753004@qq.com', // 测试邮箱
|
|
|
'department_id' => $department->id
|
|
|
]);
|
|
|
|
|
|
// 创建访客用户
|
|
|
$this->visitor = User::factory()->create([
|
|
|
'nickname' => '测试访客',
|
|
|
'mobile' => '13800138000'
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/** @test */
|
|
|
public function test_visit_email_notification_sent()
|
|
|
{
|
|
|
// 模拟预约拜访数据
|
|
|
$visitData = [
|
|
|
'name' => '测试访客',
|
|
|
'date' => '2024-01-15',
|
|
|
'mobile' => '13800138000',
|
|
|
'reason' => '业务洽谈',
|
|
|
];
|
|
|
|
|
|
// 使用Notification::fake()来捕获通知
|
|
|
Notification::fake();
|
|
|
|
|
|
// 发送邮件通知
|
|
|
$this->acceptAdmin->notify(new VisitEmailNotify($visitData));
|
|
|
|
|
|
// 验证通知是否发送
|
|
|
Notification::assertSentTo(
|
|
|
$this->acceptAdmin,
|
|
|
VisitEmailNotify::class
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/** @test */
|
|
|
public function test_visit_email_content()
|
|
|
{
|
|
|
$visitData = [
|
|
|
'name' => '测试访客',
|
|
|
'date' => '2024-01-15',
|
|
|
'mobile' => '13800138000',
|
|
|
'reason' => '业务洽谈',
|
|
|
];
|
|
|
|
|
|
$notification = new VisitEmailNotify($visitData);
|
|
|
$mailMessage = $notification->toMail($this->acceptAdmin);
|
|
|
|
|
|
// 验证邮件内容
|
|
|
$this->assertStringContainsString('新的拜访预约通知 - 测试访客', $mailMessage->subject);
|
|
|
$this->assertStringContainsString('您好 被访管理员', $mailMessage->greeting);
|
|
|
$this->assertStringContainsString('访客 测试访客 预约于 2024-01-15 到访', $mailMessage->introLines[0]);
|
|
|
$this->assertStringContainsString('访客联系电话:13800138000', $mailMessage->introLines[1]);
|
|
|
$this->assertStringContainsString('拜访事由:业务洽谈', $mailMessage->introLines[2]);
|
|
|
$this->assertStringContainsString('查看详情', $mailMessage->actionText);
|
|
|
}
|
|
|
|
|
|
/** @test */
|
|
|
public function test_visit_email_notification_to_specific_email()
|
|
|
{
|
|
|
// 测试发送到指定邮箱
|
|
|
$visitData = [
|
|
|
'name' => '测试访客',
|
|
|
'date' => '2024-01-15',
|
|
|
'mobile' => '13800138000',
|
|
|
'reason' => '业务洽谈',
|
|
|
];
|
|
|
|
|
|
// 使用Notification::fake()来捕获通知
|
|
|
Notification::fake();
|
|
|
|
|
|
// 发送邮件通知到指定邮箱
|
|
|
$this->acceptAdmin->notify(new VisitEmailNotify($visitData));
|
|
|
|
|
|
// 验证通知是否发送到正确的邮箱
|
|
|
Notification::assertSentTo(
|
|
|
$this->acceptAdmin,
|
|
|
VisitEmailNotify::class
|
|
|
);
|
|
|
|
|
|
// 验证邮箱地址是否正确
|
|
|
$this->assertEquals('648753004@qq.com', $this->acceptAdmin->email);
|
|
|
}
|
|
|
}
|