TestLogin4ursCommand.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Module\AppGame\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\AppGame\Handler\Public\Login4uHandler;
  5. use App\Module\AppGame\Handler\Public\Login4ursHandler;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * URS Login4urs功能测试命令
  9. *
  10. * 用于测试Login4ursHandler和公共静态方法的功能是否正常
  11. */
  12. class TestLogin4ursCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'test:login4urs
  20. {mobile? : 手机号码,默认使用测试账号}
  21. {password? : 登录密码,默认使用测试密码}
  22. {--static : 测试静态方法而不是Handler}';
  23. /**
  24. * 命令描述
  25. *
  26. * @var string
  27. */
  28. protected $description = '测试URS Login4urs功能';
  29. /**
  30. * 执行命令
  31. *
  32. * @return int
  33. */
  34. public function handle(): int
  35. {
  36. $this->info('=== URS Login4urs功能测试 ===');
  37. $this->info('测试Login4ursHandler和公共静态方法');
  38. $this->newLine();
  39. try {
  40. // 获取测试参数
  41. $mobile = $this->argument('mobile') ?? '18600648353';
  42. $password = $this->argument('password') ?? 'a123123';
  43. $useStatic = $this->option('static');
  44. $this->info("📱 测试手机号: {$mobile}");
  45. $this->info("🔑 测试密码: " . str_repeat('*', strlen($password)));
  46. $this->info("🔧 测试方式: " . ($useStatic ? '静态方法' : 'Handler类'));
  47. $this->newLine();
  48. // 检查类是否存在
  49. $this->checkClassExists();
  50. // 执行测试
  51. if ($useStatic) {
  52. $result = $this->testStaticMethod($mobile, $password);
  53. } else {
  54. $result = $this->testHandler($mobile, $password);
  55. }
  56. // 显示测试结果
  57. $this->displayResult($result);
  58. $this->newLine();
  59. $this->info('✅ URS Login4urs功能测试完成');
  60. return 0;
  61. } catch (\Exception $e) {
  62. $this->error('❌ 测试失败: ' . $e->getMessage());
  63. $this->error('错误详情: ' . $e->getTraceAsString());
  64. return 1;
  65. }
  66. }
  67. /**
  68. * 检查相关类是否存在
  69. */
  70. protected function checkClassExists(): void
  71. {
  72. $this->info('🔍 检查相关类是否存在...');
  73. $classes = [
  74. 'Login4uHandler' => Login4uHandler::class,
  75. 'Login4ursHandler' => Login4ursHandler::class,
  76. ];
  77. foreach ($classes as $name => $class) {
  78. if (class_exists($class)) {
  79. $this->info(" ✅ {$name} 类存在");
  80. } else {
  81. $this->error(" ❌ {$name} 类不存在");
  82. throw new \Exception("{$name} 类不存在");
  83. }
  84. }
  85. // 检查静态方法是否存在
  86. $methods = [
  87. 'processUrsLoginByMobilePassword',
  88. 'processUrsLoginByUserKey',
  89. 'completeUrsLogin',
  90. 'syncReferralRelations',
  91. 'triggerUserEnteredFarmEvent'
  92. ];
  93. foreach ($methods as $method) {
  94. if (method_exists(Login4uHandler::class, $method)) {
  95. $this->info(" ✅ Login4uHandler::{$method} 方法存在");
  96. } else {
  97. $this->error(" ❌ Login4uHandler::{$method} 方法不存在");
  98. throw new \Exception("Login4uHandler::{$method} 方法不存在");
  99. }
  100. }
  101. $this->newLine();
  102. }
  103. /**
  104. * 测试静态方法
  105. *
  106. * @param string $mobile
  107. * @param string $password
  108. * @return array
  109. */
  110. protected function testStaticMethod(string $mobile, string $password): array
  111. {
  112. $this->info('🚀 使用静态方法测试...');
  113. return Login4uHandler::processUrsLoginByMobilePassword($mobile, $password);
  114. }
  115. /**
  116. * 测试Handler类(模拟)
  117. *
  118. * @param string $mobile
  119. * @param string $password
  120. * @return array
  121. */
  122. protected function testHandler(string $mobile, string $password): array
  123. {
  124. $this->info('🚀 使用Handler类测试(模拟protobuf调用)...');
  125. // 注意:这里只是模拟测试,实际使用时需要protobuf类
  126. // 我们直接调用静态方法来验证逻辑
  127. $this->warn(' ⚠️ 注意:这是模拟测试,实际需要protobuf类支持');
  128. return Login4uHandler::processUrsLoginByMobilePassword($mobile, $password);
  129. }
  130. /**
  131. * 显示测试结果
  132. *
  133. * @param array $result
  134. */
  135. protected function displayResult(array $result): void
  136. {
  137. $this->newLine();
  138. $this->info('📊 登录测试结果:');
  139. if (isset($result['sessionId'])) {
  140. $this->info(" 🎫 会话ID: {$result['sessionId']}");
  141. } else {
  142. $this->warn(" ⚠️ 会话ID: 未返回");
  143. }
  144. if (isset($result['farmUserId'])) {
  145. $this->info(" 🚜 农场用户ID: {$result['farmUserId']}");
  146. } else {
  147. $this->warn(" ⚠️ 农场用户ID: 未返回");
  148. }
  149. if (isset($result['ursUserId'])) {
  150. $this->info(" 👤 URS用户ID: {$result['ursUserId']}");
  151. } else {
  152. $this->warn(" ⚠️ URS用户ID: 未返回");
  153. }
  154. if (isset($result['user'])) {
  155. $user = $result['user'];
  156. $this->info(" 👨‍🌾 用户名: {$user->username}");
  157. } else {
  158. $this->warn(" ⚠️ 用户对象: 未返回");
  159. }
  160. // 显示完整结果(调试用)
  161. if ($this->getOutput()->isVerbose()) {
  162. $this->newLine();
  163. $this->info('🔍 完整响应数据:');
  164. $displayResult = $result;
  165. // 移除复杂对象以便显示
  166. if (isset($displayResult['user'])) {
  167. $displayResult['user'] = '[User Object]';
  168. }
  169. if (isset($displayResult['userDto'])) {
  170. $displayResult['userDto'] = '[UserDto Object]';
  171. }
  172. $this->line(json_encode($displayResult, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
  173. }
  174. // 验证必需字段
  175. $requiredFields = ['sessionId', 'farmUserId', 'ursUserId'];
  176. $missingFields = [];
  177. foreach ($requiredFields as $field) {
  178. if (!isset($result[$field]) || empty($result[$field])) {
  179. $missingFields[] = $field;
  180. }
  181. }
  182. if (empty($missingFields)) {
  183. $this->info('✅ 所有必需字段都已返回');
  184. } else {
  185. $this->warn('⚠️ 缺少必需字段: ' . implode(', ', $missingFields));
  186. }
  187. }
  188. }