Files
chatroom/tests/Feature/AutoSaveExpCommandTest.php
T

102 lines
3.3 KiB
PHP
Raw Normal View History

2026-04-25 02:24:24 +08:00
<?php
/**
* 文件功能:自动存点命令功能测试
*
* 覆盖定时自动存点私信内容,确保用户身份信息展示完整。
*/
namespace Tests\Feature;
use App\Events\MessageSent;
use App\Models\Department;
use App\Models\Position;
use App\Models\Room;
use App\Models\Sysparam;
use App\Models\User;
use App\Models\UserPosition;
use App\Models\VipLevel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Redis;
use Tests\TestCase;
/**
* 类功能:验证自动存点命令推送给用户的状态通知内容。
*/
class AutoSaveExpCommandTest extends TestCase
{
use RefreshDatabase;
/**
* 每个测试前清空 Redis 与配置缓存,避免在线状态和系统参数串扰。
*/
protected function setUp(): void
{
parent::setUp();
Redis::flushall();
Cache::flush();
}
/**
* 测试自动存点私信会展示部门、职务与会员信息。
*/
public function test_auto_save_notice_includes_department_position_and_vip_identity(): void
{
Event::fake([MessageSent::class]);
Sysparam::updateOrCreate(['alias' => 'exp_per_heartbeat'], ['body' => '0']);
Sysparam::updateOrCreate(['alias' => 'jjb_per_heartbeat'], ['body' => '0']);
Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
Cache::flush();
$room = Room::create(['room_name' => 'asave']);
$vipLevel = VipLevel::factory()->create([
'name' => '至尊会员',
'icon' => '👑',
]);
$user = User::factory()->create([
'username' => 'autosave-user',
'user_level' => 100,
'exp_num' => 168762,
'jjb' => 1100017,
'vip_level_id' => $vipLevel->id,
'hy_time' => now()->addDay(),
]);
$department = Department::create([
'name' => '办公厅',
'rank' => 100,
'color' => '#1d4ed8',
'sort_order' => 1,
]);
$position = Position::create([
'department_id' => $department->id,
'name' => '厅长',
'icon' => '🏛️',
'rank' => 100,
'level' => 100,
'sort_order' => 1,
]);
UserPosition::create([
'user_id' => $user->id,
'position_id' => $position->id,
'appointed_at' => now(),
'is_active' => true,
]);
Redis::hset("room:{$room->id}:users", $user->username, json_encode(['username' => $user->username], JSON_UNESCAPED_UNICODE));
$this->artisan('chatroom:auto-save-exp')->assertSuccessful();
$messages = collect(Redis::lrange("room:{$room->id}:messages", 0, -1))
->map(fn (string $message): array => json_decode($message, true));
$notice = $messages->first(fn (array $message): bool => ($message['to_user'] ?? '') === $user->username);
$this->assertIsArray($notice);
$this->assertStringContainsString('部门 办公厅 · 职务 🏛️ 厅长 · 会员 👑 至尊会员', $notice['content']);
$this->assertStringContainsString('LV.100 · 经验 168762 · 金币 1100017 · 已满级 ✓', $notice['content']);
}
}