| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace App\Module\AppGame\Commands;
- use Illuminate\Console\Command;
- use App\Module\AppGame\Handler\Public\Login4uHandler;
- use App\Module\AppGame\Handler\Public\Login4ursHandler;
- use Illuminate\Support\Facades\Log;
- /**
- * URS Login4urs功能测试命令
- *
- * 用于测试Login4ursHandler和公共静态方法的功能是否正常
- */
- class TestLogin4ursCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'test:login4urs
- {mobile? : 手机号码,默认使用测试账号}
- {password? : 登录密码,默认使用测试密码}
- {--static : 测试静态方法而不是Handler}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试URS Login4urs功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $this->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));
- }
- }
- }
|