Files
chatroom/tests/TestCase.php
T
2026-04-30 10:02:59 +08:00

33 lines
980 B
PHP

<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Redis;
/**
* 测试基类
* 提供项目 Feature Test 共用的辅助方法。
*/
abstract class TestCase extends BaseTestCase
{
/**
* 清理聊天室测试产生的 Redis 房间状态,避免误删浏览器登录会话。
*/
protected function flushChatRoomRedisState(): void
{
$prefix = config('database.redis.options.prefix', '');
$cursor = '0';
do {
[$cursor, $keys] = Redis::scan($cursor, ['match' => $prefix.'room:*', 'count' => 200]);
foreach ($keys ?? [] as $fullKey) {
// Laravel Redis Facade 写入时会自动追加前缀,删除时要还原成业务短 key。
$shortKey = $prefix ? substr((string) $fullKey, strlen($prefix)) : (string) $fullKey;
Redis::del($shortKey);
}
} while ($cursor !== '0');
}
}