fetchAdminRole(); $user = $this->fetchUser(static function ($query) use ($admin_role): void { $query->where('status', 1)->where('role_id', $admin_role->id); }); if (!$user) { $this->markTestSkipped('没有可用的 administrator 用户'); } $auth = make(AuthManager::class); return $auth->guard('jwt')->login($user); } protected function fetchAdminRole(): Role { return $this->runInCoroutine(static function (): Role { return Role::query()->where('name', 'administrator')->firstOrFail(); }); } protected function fetchUser(?callable $callback = null): ?User { return $this->runInCoroutine(static function () use ($callback): ?User { $query = User::query(); if ($callback !== null) { $callback($query); } return $query->first(); }); } protected function authHeaders(): array { return ['Authorization' => 'Bearer ' . $this->getAdminToken()]; } /** * 在协程环境中执行回调,自动处理 Swoole 协程包装 */ protected function runInCoroutine(callable $callback): mixed { if (\Swoole\Coroutine::getCid() > 0) { return $callback(); } $result = null; \Swoole\Coroutine\run(static function () use ($callback, &$result): void { $result = $callback(); }); return $result; } }