TestUrsResponseConsistencyCommand.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Module\AppGame\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\AppGame\Handler\Public\Login4uHandler;
  5. use Uraus\Kku\Response\ResponsePublicLogin4u;
  6. use Uraus\Kku\Response\LastLoginInfo;
  7. /**
  8. * URS响应一致性测试命令
  9. *
  10. * 验证两个Handler返回的Response格式是否一致
  11. */
  12. class TestUrsResponseConsistencyCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'test:urs-response-consistency';
  20. /**
  21. * 命令描述
  22. *
  23. * @var string
  24. */
  25. protected $description = '测试URS登录Handler响应格式一致性';
  26. /**
  27. * 执行命令
  28. *
  29. * @return int
  30. */
  31. public function handle(): int
  32. {
  33. $this->info('=== URS响应一致性测试 ===');
  34. $this->info('验证两个Handler返回的Response格式是否一致');
  35. $this->newLine();
  36. try {
  37. // 测试Response类的结构
  38. $this->testResponseStructure();
  39. // 测试静态方法返回的数据结构
  40. $this->testStaticMethodResponse();
  41. $this->newLine();
  42. $this->info('✅ URS响应一致性测试完成');
  43. return 0;
  44. } catch (\Exception $e) {
  45. $this->error('❌ 测试失败: ' . $e->getMessage());
  46. return 1;
  47. }
  48. }
  49. /**
  50. * 测试Response类的结构
  51. */
  52. protected function testResponseStructure(): void
  53. {
  54. $this->info('🔍 测试Response类结构...');
  55. // 创建Response对象
  56. $response = new ResponsePublicLogin4u();
  57. // 设置所有字段
  58. $response->setToken('test_token_123');
  59. $response->setIsProhibit(false);
  60. $lastLoginInfo = new LastLoginInfo();
  61. $lastLoginInfo->setLastLoginTimes(time());
  62. $response->setLastLoginInfo($lastLoginInfo);
  63. // 验证字段
  64. $this->info(' 📋 Response字段验证:');
  65. $this->info(" ✅ Token: {$response->getToken()}");
  66. $this->info(" ✅ IsProhibit: " . ($response->getIsProhibit() ? 'true' : 'false'));
  67. if ($response->hasLastLoginInfo()) {
  68. $lastLogin = $response->getLastLoginInfo();
  69. $this->info(" ✅ LastLoginTimes: {$lastLogin->getLastLoginTimes()}");
  70. } else {
  71. $this->warn(" ⚠️ LastLoginInfo: 未设置");
  72. }
  73. // 验证JSON序列化
  74. $jsonString = $response->serializeToJsonString();
  75. $this->info(' 📄 JSON序列化成功');
  76. if ($this->getOutput()->isVerbose()) {
  77. $this->line(" JSON: {$jsonString}");
  78. }
  79. $this->newLine();
  80. }
  81. /**
  82. * 测试静态方法返回的数据结构
  83. */
  84. protected function testStaticMethodResponse(): void
  85. {
  86. $this->info('🔍 测试静态方法数据结构...');
  87. // 模拟登录结果数据
  88. $mockLoginResult = [
  89. 'sessionId' => 'mock_session_' . time(),
  90. 'farmUserId' => 12345,
  91. 'ursUserId' => 67890,
  92. 'user' => (object)[
  93. 'id' => 12345,
  94. 'username' => 'test_user'
  95. ],
  96. 'userDto' => (object)[
  97. 'name' => 'Test User'
  98. ]
  99. ];
  100. $this->info(' 📋 静态方法返回数据验证:');
  101. $this->info(" ✅ sessionId: {$mockLoginResult['sessionId']}");
  102. $this->info(" ✅ farmUserId: {$mockLoginResult['farmUserId']}");
  103. $this->info(" ✅ ursUserId: {$mockLoginResult['ursUserId']}");
  104. $this->info(" ✅ user: " . json_encode($mockLoginResult['user']));
  105. // 验证必需字段
  106. $requiredFields = ['sessionId', 'farmUserId', 'ursUserId', 'user'];
  107. $missingFields = [];
  108. foreach ($requiredFields as $field) {
  109. if (!isset($mockLoginResult[$field])) {
  110. $missingFields[] = $field;
  111. }
  112. }
  113. if (empty($missingFields)) {
  114. $this->info(' ✅ 所有必需字段都存在');
  115. } else {
  116. $this->warn(' ⚠️ 缺少字段: ' . implode(', ', $missingFields));
  117. }
  118. // 验证Response创建过程
  119. $this->info(' 🔧 模拟Response创建过程:');
  120. $response = new ResponsePublicLogin4u();
  121. $response->setToken($mockLoginResult['sessionId']);
  122. $response->setIsProhibit(false);
  123. $lastLoginInfo = new LastLoginInfo();
  124. $lastLoginInfo->setLastLoginTimes(time());
  125. $response->setLastLoginInfo($lastLoginInfo);
  126. $this->info(' ✅ Response创建成功');
  127. $this->info(" ✅ Token设置: {$response->getToken()}");
  128. $this->info(" ✅ IsProhibit设置: " . ($response->getIsProhibit() ? 'true' : 'false'));
  129. $this->info(" ✅ LastLoginInfo设置: " . ($response->hasLastLoginInfo() ? '已设置' : '未设置'));
  130. $this->newLine();
  131. }
  132. }