TestUrsTalentUpstream.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\UrsPromotion\Services\UrsTalentUpstreamUpdateService;
  5. use App\Module\DelayQueue\Redis as DelayQueueRedis;
  6. use Illuminate\Support\Facades\Redis;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 测试URS达人等级上级更新功能
  10. */
  11. class TestUrsTalentUpstream extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. */
  16. protected $signature = 'test:urs-talent-upstream {action=all}';
  17. /**
  18. * The console command description.
  19. */
  20. protected $description = '测试URS达人等级上级更新功能';
  21. /**
  22. * Execute the console command.
  23. */
  24. public function handle()
  25. {
  26. $action = $this->argument('action');
  27. switch ($action) {
  28. case 'redis':
  29. $this->testRedisConnection();
  30. break;
  31. case 'delayqueue':
  32. $this->testDelayQueue();
  33. break;
  34. case 'callback':
  35. $this->testCallback();
  36. break;
  37. case 'all':
  38. default:
  39. $this->testRedisConnection();
  40. $this->testDelayQueue();
  41. $this->testCallback();
  42. break;
  43. }
  44. }
  45. /**
  46. * 测试Redis连接
  47. */
  48. private function testRedisConnection()
  49. {
  50. $this->info('=== 测试Redis连接 ===');
  51. try {
  52. $redis = Redis::client();
  53. $this->info('✓ Redis连接成功');
  54. $this->info('Redis类型: ' . get_class($redis));
  55. // 测试基础操作
  56. $testKey = 'test_urs_talent_' . time();
  57. $redis->setex($testKey, 10, 'test_value');
  58. $value = $redis->get($testKey);
  59. $this->info("✓ 测试键值: {$testKey} = {$value}");
  60. // 清理测试数据
  61. $redis->del($testKey);
  62. } catch (\Exception $e) {
  63. $this->error('✗ Redis连接失败: ' . $e->getMessage());
  64. }
  65. }
  66. /**
  67. * 测试DelayQueue功能
  68. */
  69. private function testDelayQueue()
  70. {
  71. $this->info('=== 测试DelayQueue功能 ===');
  72. try {
  73. // 准备测试数据
  74. $callback = [UrsTalentUpstreamUpdateService::class, 'updateTalentLevel'];
  75. $runParam = [
  76. 'referrer_id' => 999999,
  77. 'original_user_id' => 999998,
  78. 'level' => 2,
  79. 'trigger_time' => time()
  80. ];
  81. $delaySeconds = 5;
  82. $this->info('回调: ' . implode('::', $callback));
  83. $this->info('延时: ' . $delaySeconds . ' 秒');
  84. $this->info('参数: ' . json_encode($runParam));
  85. // 添加DelayQueue任务
  86. $result = DelayQueueRedis::addQueue($callback, $runParam, $delaySeconds);
  87. if ($result === 0) {
  88. $this->warn('⚠ 任务已存在,跳过重复添加');
  89. } else {
  90. $this->info('✓ DelayQueue任务添加成功');
  91. }
  92. // 再次添加相同任务,测试防重复
  93. $result2 = DelayQueueRedis::addQueue($callback, $runParam, $delaySeconds);
  94. if ($result2 === 0) {
  95. $this->info('✓ 防重复机制正常工作');
  96. } else {
  97. $this->warn('⚠ 防重复机制可能有问题');
  98. }
  99. } catch (\Exception $e) {
  100. $this->error('✗ DelayQueue测试失败: ' . $e->getMessage());
  101. }
  102. }
  103. /**
  104. * 测试回调方法
  105. */
  106. private function testCallback()
  107. {
  108. $this->info('=== 测试回调方法 ===');
  109. // 测试参数验证
  110. $this->info('测试参数验证...');
  111. // 空参数测试
  112. $result = UrsTalentUpstreamUpdateService::updateTalentLevel([]);
  113. $this->info('空参数测试: ' . ($result ? '✓ 通过' : '✗ 失败(预期)'));
  114. // 不完整参数测试
  115. $result = UrsTalentUpstreamUpdateService::updateTalentLevel([
  116. 'referrer_id' => 999999
  117. ]);
  118. $this->info('不完整参数测试: ' . ($result ? '✓ 通过' : '✗ 失败(预期)'));
  119. // 完整参数测试(用户不存在)
  120. $runParam = [
  121. 'referrer_id' => 999999,
  122. 'original_user_id' => 999998,
  123. 'level' => 2,
  124. 'trigger_time' => time()
  125. ];
  126. $this->info('测试完整参数(不存在的用户)...');
  127. $result = UrsTalentUpstreamUpdateService::updateTalentLevel($runParam);
  128. $this->info('完整参数测试: ' . ($result ? '✓ 通过' : '✗ 失败(预期,用户不存在)'));
  129. // 测试批量更新
  130. $this->info('测试批量更新...');
  131. $results = UrsTalentUpstreamUpdateService::batchUpdateTalentLevels([999999, 999998]);
  132. $this->info('批量更新结果: ' . count($results) . ' 个用户处理完成');
  133. }
  134. }