TestBothUrsHandlersCommand.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Module\AppGame\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\AppGame\Handler\Public\Login4uHandler;
  5. /**
  6. * 测试两个URS Handler的完整功能
  7. *
  8. * 验证Login4uHandler和Login4ursHandler都能正确工作
  9. */
  10. class TestBothUrsHandlersCommand extends Command
  11. {
  12. /**
  13. * 命令签名
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'test:both-urs-handlers
  18. {userkey? : 用户密钥,默认使用测试密钥}
  19. {mobile? : 手机号码,默认使用测试账号}
  20. {password? : 登录密码,默认使用测试密码}';
  21. /**
  22. * 命令描述
  23. *
  24. * @var string
  25. */
  26. protected $description = '测试两个URS Handler的完整功能';
  27. /**
  28. * 执行命令
  29. *
  30. * @return int
  31. */
  32. public function handle(): int
  33. {
  34. $this->info('=== 两个URS Handler完整功能测试 ===');
  35. $this->info('测试Login4uHandler和Login4ursHandler');
  36. $this->newLine();
  37. try {
  38. // 获取测试参数
  39. $userKey = $this->argument('userkey') ?? '$2y$10$vSeIMeziuwcio10eTvLRiuXwSFtSWmVrOqMbewgrbszFfckwmP1bw';
  40. $mobile = $this->argument('mobile') ?? '18600648353';
  41. $password = $this->argument('password') ?? 'a123123';
  42. $this->info("🔑 测试用户密钥: " . substr($userKey, 0, 20) . '...');
  43. $this->info("📱 测试手机号: {$mobile}");
  44. $this->info("🔐 测试密码: " . str_repeat('*', strlen($password)));
  45. $this->newLine();
  46. // 测试Login4ursHandler(手机号密码登录)
  47. $this->testLogin4ursHandler($mobile, $password);
  48. $this->newLine();
  49. // 测试Login4uHandler(也使用手机号密码登录)
  50. $this->testLogin4uHandlerWithMobilePassword($mobile, $password);
  51. $this->newLine();
  52. $this->info('✅ 两个URS Handler完整功能测试完成');
  53. return 0;
  54. } catch (\Exception $e) {
  55. $this->error('❌ 测试失败: ' . $e->getMessage());
  56. $this->error('错误详情: ' . $e->getTraceAsString());
  57. return 1;
  58. }
  59. }
  60. /**
  61. * 测试Login4uHandler(使用手机号密码)
  62. *
  63. * @param string $mobile
  64. * @param string $password
  65. */
  66. protected function testLogin4uHandlerWithMobilePassword(string $mobile, string $password): void
  67. {
  68. $this->info('🔍 测试Login4uHandler(使用手机号密码静态方法)...');
  69. try {
  70. // 使用静态方法处理登录
  71. $loginResult = Login4uHandler::processUrsLoginByMobilePassword($mobile, $password);
  72. // 验证响应
  73. $this->info(' 📋 Login4uHandler测试结果:');
  74. $this->info(" ✅ 会话ID: {$loginResult['sessionId']}");
  75. $this->info(" ✅ 农场用户ID: {$loginResult['farmUserId']}");
  76. $this->info(" ✅ URS用户ID: {$loginResult['ursUserId']}");
  77. if (isset($loginResult['user'])) {
  78. $user = $loginResult['user'];
  79. $this->info(" ✅ 用户名: {$user->username}");
  80. }
  81. $this->info(' ✅ Login4uHandler静态方法调用成功');
  82. } catch (\Exception $e) {
  83. $this->error(' ❌ Login4uHandler测试失败: ' . $e->getMessage());
  84. throw $e;
  85. }
  86. }
  87. /**
  88. * 测试Login4ursHandler
  89. *
  90. * @param string $mobile
  91. * @param string $password
  92. */
  93. protected function testLogin4ursHandler(string $mobile, string $password): void
  94. {
  95. $this->info('🔍 测试Login4ursHandler(使用静态方法)...');
  96. try {
  97. // 使用静态方法处理登录
  98. $loginResult = Login4uHandler::processUrsLoginByMobilePassword($mobile, $password);
  99. // 验证响应
  100. $this->info(' 📋 Login4ursHandler测试结果:');
  101. $this->info(" ✅ 会话ID: {$loginResult['sessionId']}");
  102. $this->info(" ✅ 农场用户ID: {$loginResult['farmUserId']}");
  103. $this->info(" ✅ URS用户ID: {$loginResult['ursUserId']}");
  104. if (isset($loginResult['user'])) {
  105. $user = $loginResult['user'];
  106. $this->info(" ✅ 用户名: {$user->username}");
  107. }
  108. $this->info(' ✅ Login4ursHandler静态方法调用成功');
  109. } catch (\Exception $e) {
  110. $this->error(' ❌ Login4ursHandler测试失败: ' . $e->getMessage());
  111. throw $e;
  112. }
  113. }
  114. }