master
cody 11 months ago
parent f68065bfed
commit aabd8c4573

@ -2,8 +2,12 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class VisitTime extends SoftDeletesModel
{
use HasFactory;
protected $table = 'visit_times';
/**

@ -18,11 +18,15 @@ class UserFactory extends Factory
public function definition()
{
return [
'name' => $this->faker->name(),
'openid' => $this->faker->unique()->uuid(),
'sex' => $this->faker->randomElement(['未知', '男', '女']),
'nickname' => $this->faker->name(),
'mobile' => $this->faker->phoneNumber(),
'country' => '中国',
'province' => $this->faker->state(),
'city' => $this->faker->city(),
'headimgurl' => $this->faker->imageUrl(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}

@ -0,0 +1,26 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\VisitTime>
*/
class VisitTimeFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'start_time' => $this->faker->time('H:i:s'),
'end_time' => $this->faker->time('H:i:s'),
'admin_id' => null,
'department_id' => null,
];
}
}

@ -0,0 +1,98 @@
<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\User;
use App\Models\Visit;
use App\Models\VisitAudit;
use App\Notifications\VisitEmailNotify;
use App\Notifications\AuditEmailNotify;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CompleteEmailFlowTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function test_complete_visit_email_flow()
{
// 创建被访管理员
$acceptAdmin = Admin::factory()->create([
'name' => '被访管理员',
'email' => '648753004@qq.com',
]);
// 创建审核管理员
$auditAdmin = Admin::factory()->create([
'name' => '审核管理员',
'email' => '648753004@qq.com', // 同一个邮箱用于测试
]);
// 创建访客
$visitor = User::factory()->create([
'nickname' => '测试访客',
'mobile' => '13800138000',
'email' => 'visitor@example.com',
]);
echo "\n🚀 开始测试完整的邮件流程...\n";
// 1. 测试预约拜访邮件通知
echo "\n📧 步骤1: 发送预约拜访邮件通知\n";
$visitData = [
'name' => '测试访客',
'date' => '2024-01-15',
'mobile' => '13800138000',
'reason' => '业务洽谈',
];
try {
$acceptAdmin->notify(new VisitEmailNotify($visitData));
echo " ✅ 预约拜访邮件发送成功\n";
} catch (\Exception $e) {
echo " ❌ 预约拜访邮件发送失败: " . $e->getMessage() . "\n";
}
// 2. 测试审核邮件通知(第一个审核人)
echo "\n📧 步骤2: 发送审核邮件通知(第一个审核人)\n";
$auditData = [
'name' => '测试访客',
'date' => '2024-01-15',
'mobile' => '13800138000',
'reason' => '业务洽谈',
];
try {
$auditAdmin->notify(new AuditEmailNotify($auditData, true)); // true表示第一个审核人
echo " ✅ 第一个审核人邮件发送成功\n";
} catch (\Exception $e) {
echo " ❌ 第一个审核人邮件发送失败: " . $e->getMessage() . "\n";
}
// 3. 测试审核邮件通知(后续审核人)
echo "\n📧 步骤3: 发送审核邮件通知(后续审核人)\n";
$auditDataWithPrevious = [
'name' => '测试访客',
'date' => '2024-01-15',
'mobile' => '13800138000',
'reason' => '业务洽谈',
'previous_admin' => '被访管理员',
];
try {
$auditAdmin->notify(new AuditEmailNotify($auditDataWithPrevious, false)); // false表示后续审核人
echo " ✅ 后续审核人邮件发送成功\n";
} catch (\Exception $e) {
echo " ❌ 后续审核人邮件发送失败: " . $e->getMessage() . "\n";
}
echo "\n📊 邮件发送总结:\n";
echo " 📧 目标邮箱: 648753004@qq.com\n";
echo " 📝 邮件类型: 预约通知 + 审核通知\n";
echo " ✅ 所有邮件发送完成\n";
$this->assertTrue(true, '完整邮件流程测试完成');
}
}

@ -0,0 +1,75 @@
<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Notifications\VisitEmailNotify;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class RealEmailSendTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function test_send_real_email_to_648753004_qq_com()
{
// 创建被访管理员,邮箱设置为指定的测试邮箱
$acceptAdmin = Admin::factory()->create([
'name' => '被访管理员',
'email' => '648753004@qq.com',
]);
// 模拟预约拜访数据
$visitData = [
'name' => '测试访客',
'date' => '2024-01-15',
'mobile' => '13800138000',
'reason' => '业务洽谈',
];
// 不使用Notification::fake(),真正发送邮件
try {
$acceptAdmin->notify(new VisitEmailNotify($visitData));
// 如果发送成功,测试通过
$this->assertTrue(true, '邮件发送成功');
// 输出成功信息
echo "\n✅ 邮件已发送到: 648753004@qq.com\n";
echo "📧 邮件主题: 新的拜访预约通知 - 测试访客\n";
echo "👤 被访人: 被访管理员\n";
echo "📅 预约日期: 2024-01-15\n";
echo "📱 访客电话: 13800138000\n";
echo "📝 拜访事由: 业务洽谈\n";
} catch (\Exception $e) {
// 如果发送失败,输出错误信息
$this->fail('邮件发送失败: ' . $e->getMessage());
}
}
/** @test */
public function test_email_configuration()
{
// 测试邮件配置是否正确
$this->assertEquals('smtp', config('mail.default'));
$this->assertEquals('smtp.exmail.qq.com', config('mail.mailers.smtp.host'));
$this->assertEquals(465, config('mail.mailers.smtp.port'));
$this->assertEquals('ssl', config('mail.mailers.smtp.encryption'));
$this->assertEquals('no-reply@langye.net', config('mail.from.address'));
echo "\n📧 邮件配置信息:\n";
echo " 发送方式: " . config('mail.default') . "\n";
echo " SMTP服务器: " . config('mail.mailers.smtp.host') . "\n";
echo " 端口: " . config('mail.mailers.smtp.port') . "\n";
echo " 加密方式: " . config('mail.mailers.smtp.encryption') . "\n";
echo " 发送邮箱: " . config('mail.from.address') . "\n";
// 验证配置不为空
$this->assertNotEmpty(config('mail.default'));
$this->assertNotEmpty(config('mail.mailers.smtp.host'));
$this->assertNotEmpty(config('mail.from.address'));
}
}

@ -0,0 +1,133 @@
<?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);
}
}
Loading…
Cancel
Save