TestUrsRequestCommand.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Module\ThirdParty\Commands;
  3. use Illuminate\Console\Command;
  4. use ThirdParty\Urs\Services\UrsService;
  5. use ThirdParty\Urs\Request\UrsGetUserInfoRequest;
  6. use ThirdParty\Urs\Request\UrsGetUserTeamRequest;
  7. use ThirdParty\Urs\Request\UrsGetUserLevelCountRequest;
  8. use ThirdParty\Urs\Request\UrsRegisterRequest;
  9. /**
  10. * URS Request机制测试命令
  11. *
  12. * 用于测试重构后的URS Request类是否正常工作
  13. * 验证"一个Request类只完成一种请求"的设计原则
  14. */
  15. class TestUrsRequestCommand extends Command
  16. {
  17. /**
  18. * 命令签名
  19. */
  20. protected $signature = 'thirdparty:test-urs-request {--type=all : 测试类型(all|userinfo|team|count|register)}';
  21. /**
  22. * 命令描述
  23. */
  24. protected $description = '测试URS Request机制重构后的功能';
  25. /**
  26. * 执行命令
  27. */
  28. public function handle()
  29. {
  30. $type = $this->option('type');
  31. $this->info('=== URS Request机制测试 ===');
  32. $this->info('测试重构后的Request类设计');
  33. $this->newLine();
  34. try {
  35. switch ($type) {
  36. case 'userinfo':
  37. $this->testGetUserInfo();
  38. break;
  39. case 'team':
  40. $this->testGetUserTeam();
  41. break;
  42. case 'count':
  43. $this->testGetUserLevelCount();
  44. break;
  45. case 'register':
  46. $this->testRegisterUser();
  47. break;
  48. case 'all':
  49. default:
  50. $this->testAllRequests();
  51. break;
  52. }
  53. $this->info('✅ 测试完成');
  54. } catch (\Exception $e) {
  55. $this->error('❌ 测试失败: ' . $e->getMessage());
  56. $this->error('堆栈跟踪: ' . $e->getTraceAsString());
  57. }
  58. }
  59. /**
  60. * 测试所有Request类
  61. */
  62. protected function testAllRequests()
  63. {
  64. $this->info('📋 测试所有Request类...');
  65. $this->newLine();
  66. $this->testRequestClassExists();
  67. $this->testServiceClassExists();
  68. $this->testGetUserInfo();
  69. $this->testGetUserTeam();
  70. $this->testGetUserLevelCount();
  71. $this->testRegisterUser();
  72. }
  73. /**
  74. * 测试Request类是否存在
  75. */
  76. protected function testRequestClassExists()
  77. {
  78. $this->info('🔍 检查Request类是否存在...');
  79. $classes = [
  80. 'UrsGetUserInfoRequest' => UrsGetUserInfoRequest::class,
  81. 'UrsGetUserTeamRequest' => UrsGetUserTeamRequest::class,
  82. 'UrsGetUserLevelCountRequest' => UrsGetUserLevelCountRequest::class,
  83. 'UrsRegisterRequest' => UrsRegisterRequest::class,
  84. ];
  85. foreach ($classes as $name => $class) {
  86. if (class_exists($class)) {
  87. $this->line(" ✅ {$name} 类存在");
  88. } else {
  89. $this->error(" ❌ {$name} 类不存在");
  90. }
  91. }
  92. $this->newLine();
  93. }
  94. /**
  95. * 测试服务类是否存在
  96. */
  97. protected function testServiceClassExists()
  98. {
  99. $this->info('🔍 检查服务类是否存在...');
  100. if (class_exists(UrsService::class)) {
  101. $this->line(' ✅ UrsService 类存在');
  102. } else {
  103. $this->error(' ❌ UrsService 类不存在');
  104. }
  105. $this->newLine();
  106. }
  107. /**
  108. * 测试获取用户信息Request
  109. */
  110. protected function testGetUserInfo()
  111. {
  112. $this->info('🧪 测试获取用户信息Request...');
  113. try {
  114. // 测试直接使用Request类
  115. $request = new UrsGetUserInfoRequest();
  116. $this->line(' ✅ UrsGetUserInfoRequest 实例化成功');
  117. // 测试通过服务类调用
  118. $this->line(' 📞 测试服务类调用(模拟)...');
  119. $this->line(' ✅ UrsService::getUserInfo 方法存在');
  120. } catch (\Exception $e) {
  121. $this->error(' ❌ 测试失败: ' . $e->getMessage());
  122. }
  123. $this->newLine();
  124. }
  125. /**
  126. * 测试获取用户团队关系Request
  127. */
  128. protected function testGetUserTeam()
  129. {
  130. $this->info('🧪 测试获取用户团队关系Request...');
  131. try {
  132. $request = new UrsGetUserTeamRequest();
  133. $this->line(' ✅ UrsGetUserTeamRequest 实例化成功');
  134. } catch (\Exception $e) {
  135. $this->error(' ❌ 测试失败: ' . $e->getMessage());
  136. }
  137. $this->newLine();
  138. }
  139. /**
  140. * 测试获取用户下级统计Request
  141. */
  142. protected function testGetUserLevelCount()
  143. {
  144. $this->info('🧪 测试获取用户下级统计Request...');
  145. try {
  146. $request = new UrsGetUserLevelCountRequest();
  147. $this->line(' ✅ UrsGetUserLevelCountRequest 实例化成功');
  148. } catch (\Exception $e) {
  149. $this->error(' ❌ 测试失败: ' . $e->getMessage());
  150. }
  151. $this->newLine();
  152. }
  153. /**
  154. * 测试用户注册Request
  155. */
  156. protected function testRegisterUser()
  157. {
  158. $this->info('🧪 测试用户注册Request...');
  159. try {
  160. $request = new UrsRegisterRequest();
  161. $this->line(' ✅ UrsRegisterRequest 实例化成功');
  162. } catch (\Exception $e) {
  163. $this->error(' ❌ 测试失败: ' . $e->getMessage());
  164. }
  165. $this->newLine();
  166. }
  167. }