| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Module\UrsPromotion\Services\UrsTalentUpstreamUpdateService;
- use App\Module\DelayQueue\Redis as DelayQueueRedis;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Log;
- /**
- * 测试URS达人等级上级更新功能
- */
- class TestUrsTalentUpstream extends Command
- {
- /**
- * The name and signature of the console command.
- */
- protected $signature = 'test:urs-talent-upstream {action=all}';
- /**
- * The console command description.
- */
- protected $description = '测试URS达人等级上级更新功能';
- /**
- * Execute the console command.
- */
- public function handle()
- {
- $action = $this->argument('action');
- switch ($action) {
- case 'redis':
- $this->testRedisConnection();
- break;
- case 'delayqueue':
- $this->testDelayQueue();
- break;
- case 'callback':
- $this->testCallback();
- break;
- case 'all':
- default:
- $this->testRedisConnection();
- $this->testDelayQueue();
- $this->testCallback();
- break;
- }
- }
- /**
- * 测试Redis连接
- */
- private function testRedisConnection()
- {
- $this->info('=== 测试Redis连接 ===');
- try {
- $redis = Redis::client();
- $this->info('✓ Redis连接成功');
- $this->info('Redis类型: ' . get_class($redis));
- // 测试基础操作
- $testKey = 'test_urs_talent_' . time();
- $redis->setex($testKey, 10, 'test_value');
- $value = $redis->get($testKey);
- $this->info("✓ 测试键值: {$testKey} = {$value}");
- // 清理测试数据
- $redis->del($testKey);
- } catch (\Exception $e) {
- $this->error('✗ Redis连接失败: ' . $e->getMessage());
- }
- }
- /**
- * 测试DelayQueue功能
- */
- private function testDelayQueue()
- {
- $this->info('=== 测试DelayQueue功能 ===');
- try {
- // 准备测试数据
- $callback = [UrsTalentUpstreamUpdateService::class, 'updateTalentLevel'];
- $runParam = [
- 'referrer_id' => 999999,
- 'original_user_id' => 999998,
- 'level' => 2,
- 'trigger_time' => time()
- ];
- $delaySeconds = 5;
- $this->info('回调: ' . implode('::', $callback));
- $this->info('延时: ' . $delaySeconds . ' 秒');
- $this->info('参数: ' . json_encode($runParam));
- // 添加DelayQueue任务
- $result = DelayQueueRedis::addQueue($callback, $runParam, $delaySeconds);
-
- if ($result === 0) {
- $this->warn('⚠ 任务已存在,跳过重复添加');
- } else {
- $this->info('✓ DelayQueue任务添加成功');
- }
- // 再次添加相同任务,测试防重复
- $result2 = DelayQueueRedis::addQueue($callback, $runParam, $delaySeconds);
- if ($result2 === 0) {
- $this->info('✓ 防重复机制正常工作');
- } else {
- $this->warn('⚠ 防重复机制可能有问题');
- }
- } catch (\Exception $e) {
- $this->error('✗ DelayQueue测试失败: ' . $e->getMessage());
- }
- }
- /**
- * 测试回调方法
- */
- private function testCallback()
- {
- $this->info('=== 测试回调方法 ===');
- // 测试参数验证
- $this->info('测试参数验证...');
-
- // 空参数测试
- $result = UrsTalentUpstreamUpdateService::updateTalentLevel([]);
- $this->info('空参数测试: ' . ($result ? '✓ 通过' : '✗ 失败(预期)'));
- // 不完整参数测试
- $result = UrsTalentUpstreamUpdateService::updateTalentLevel([
- 'referrer_id' => 999999
- ]);
- $this->info('不完整参数测试: ' . ($result ? '✓ 通过' : '✗ 失败(预期)'));
- // 完整参数测试(用户不存在)
- $runParam = [
- 'referrer_id' => 999999,
- 'original_user_id' => 999998,
- 'level' => 2,
- 'trigger_time' => time()
- ];
-
- $this->info('测试完整参数(不存在的用户)...');
- $result = UrsTalentUpstreamUpdateService::updateTalentLevel($runParam);
- $this->info('完整参数测试: ' . ($result ? '✓ 通过' : '✗ 失败(预期,用户不存在)'));
- // 测试批量更新
- $this->info('测试批量更新...');
- $results = UrsTalentUpstreamUpdateService::batchUpdateTalentLevels([999999, 999998]);
- $this->info('批量更新结果: ' . count($results) . ' 个用户处理完成');
- }
- }
|