TestUrsRequestCommand.php 4.7 KB

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