info('=== URS Login4urs功能测试 ==='); $this->info('测试Login4ursHandler和公共静态方法'); $this->newLine(); try { // 获取测试参数 $mobile = $this->argument('mobile') ?? '18600648353'; $password = $this->argument('password') ?? 'a123123'; $useStatic = $this->option('static'); $this->info("📱 测试手机号: {$mobile}"); $this->info("🔑 测试密码: " . str_repeat('*', strlen($password))); $this->info("🔧 测试方式: " . ($useStatic ? '静态方法' : 'Handler类')); $this->newLine(); // 检查类是否存在 $this->checkClassExists(); // 执行测试 if ($useStatic) { $result = $this->testStaticMethod($mobile, $password); } else { $result = $this->testHandler($mobile, $password); } // 显示测试结果 $this->displayResult($result); $this->newLine(); $this->info('✅ URS Login4urs功能测试完成'); return 0; } catch (\Exception $e) { $this->error('❌ 测试失败: ' . $e->getMessage()); $this->error('错误详情: ' . $e->getTraceAsString()); return 1; } } /** * 检查相关类是否存在 */ protected function checkClassExists(): void { $this->info('🔍 检查相关类是否存在...'); $classes = [ 'Login4uHandler' => Login4uHandler::class, 'Login4ursHandler' => Login4ursHandler::class, ]; foreach ($classes as $name => $class) { if (class_exists($class)) { $this->info(" ✅ {$name} 类存在"); } else { $this->error(" ❌ {$name} 类不存在"); throw new \Exception("{$name} 类不存在"); } } // 检查静态方法是否存在 $methods = [ 'processUrsLoginByMobilePassword', 'processUrsLoginByUserKey', 'completeUrsLogin', 'syncReferralRelations', 'triggerUserEnteredFarmEvent' ]; foreach ($methods as $method) { if (method_exists(Login4uHandler::class, $method)) { $this->info(" ✅ Login4uHandler::{$method} 方法存在"); } else { $this->error(" ❌ Login4uHandler::{$method} 方法不存在"); throw new \Exception("Login4uHandler::{$method} 方法不存在"); } } $this->newLine(); } /** * 测试静态方法 * * @param string $mobile * @param string $password * @return array */ protected function testStaticMethod(string $mobile, string $password): array { $this->info('🚀 使用静态方法测试...'); return Login4uHandler::processUrsLoginByMobilePassword($mobile, $password); } /** * 测试Handler类(模拟) * * @param string $mobile * @param string $password * @return array */ protected function testHandler(string $mobile, string $password): array { $this->info('🚀 使用Handler类测试(模拟protobuf调用)...'); // 注意:这里只是模拟测试,实际使用时需要protobuf类 // 我们直接调用静态方法来验证逻辑 $this->warn(' ⚠️ 注意:这是模拟测试,实际需要protobuf类支持'); return Login4uHandler::processUrsLoginByMobilePassword($mobile, $password); } /** * 显示测试结果 * * @param array $result */ protected function displayResult(array $result): void { $this->newLine(); $this->info('📊 登录测试结果:'); if (isset($result['sessionId'])) { $this->info(" 🎫 会话ID: {$result['sessionId']}"); } else { $this->warn(" ⚠️ 会话ID: 未返回"); } if (isset($result['farmUserId'])) { $this->info(" 🚜 农场用户ID: {$result['farmUserId']}"); } else { $this->warn(" ⚠️ 农场用户ID: 未返回"); } if (isset($result['ursUserId'])) { $this->info(" 👤 URS用户ID: {$result['ursUserId']}"); } else { $this->warn(" ⚠️ URS用户ID: 未返回"); } if (isset($result['user'])) { $user = $result['user']; $this->info(" 👨‍🌾 用户名: {$user->username}"); } else { $this->warn(" ⚠️ 用户对象: 未返回"); } // 显示完整结果(调试用) if ($this->getOutput()->isVerbose()) { $this->newLine(); $this->info('🔍 完整响应数据:'); $displayResult = $result; // 移除复杂对象以便显示 if (isset($displayResult['user'])) { $displayResult['user'] = '[User Object]'; } if (isset($displayResult['userDto'])) { $displayResult['userDto'] = '[UserDto Object]'; } $this->line(json_encode($displayResult, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); } // 验证必需字段 $requiredFields = ['sessionId', 'farmUserId', 'ursUserId']; $missingFields = []; foreach ($requiredFields as $field) { if (!isset($result[$field]) || empty($result[$field])) { $missingFields[] = $field; } } if (empty($missingFields)) { $this->info('✅ 所有必需字段都已返回'); } else { $this->warn('⚠️ 缺少必需字段: ' . implode(', ', $missingFields)); } } }