| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace App\Console\Commands;
- use App\Module\Game\Services\RewardCollectorService;
- use App\Module\Game\Services\DeductCollectorService;
- use App\Module\GameItems\Events\ItemQuantityChanged;
- use App\Module\Fund\Events\FundChangedEvent;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Event;
- /**
- * 测试奖励扣除收集器用户区分功能
- */
- class TestRewardDeductCollectorUserSeparation extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'test:reward-deduct-collector-user-separation';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试奖励扣除收集器的用户区分功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $this->info('开始测试奖励扣除收集器用户区分功能...');
- // 清空之前的数据
- RewardCollectorService::clearRewards();
- DeductCollectorService::clearDeducts();
- // 测试手动添加数据
- $this->testManualCollection();
- // 测试事件自动收集
- $this->testEventCollection();
- // 测试查询功能
- $this->testQueryFunctions();
- $this->info('所有测试完成!');
- return 0;
- }
- /**
- * 测试手动收集功能
- */
- private function testManualCollection(): void
- {
- $this->info('=== 测试手动收集功能 ===');
- // 用户1的数据
- RewardCollectorService::addItemReward(1001, 101, 0, 10);
- RewardCollectorService::addCoinReward(1001, 1, 100);
- RewardCollectorService::addGodReward(1001, 1, 3600, 50);
-
- DeductCollectorService::addItemDeduct(1001, 102, 0, 5);
- DeductCollectorService::addCoinDeduct(1001, 2, 20);
- // 用户2的数据
- RewardCollectorService::addItemReward(1002, 103, 0, 15);
- RewardCollectorService::addCoinReward(1002, 1, 200);
-
- DeductCollectorService::addItemDeduct(1002, 104, 0, 8);
- DeductCollectorService::addCoinDeduct(1002, 2, 30);
- $this->info('✅ 手动收集数据添加完成');
- }
- /**
- * 测试事件自动收集
- */
- private function testEventCollection(): void
- {
- $this->info('=== 测试事件自动收集功能 ===');
- // 触发物品变更事件 - 用户1获得物品
- Event::dispatch(new ItemQuantityChanged(
- 1001, // userId
- 201, // itemId
- null, // instanceId
- 0, // oldQuantity
- 25, // newQuantity
- 1 // userItemId
- ));
- // 触发物品变更事件 - 用户2消耗物品
- Event::dispatch(new ItemQuantityChanged(
- 1002, // userId
- 202, // itemId
- null, // instanceId
- 50, // oldQuantity
- 40, // newQuantity
- 2 // userItemId
- ));
- // 触发资金变更事件 - 用户1获得代币
- Event::dispatch(new FundChangedEvent(
- 1001, // userId
- 3, // fundId
- 500, // amount (正数表示获得)
- 1000, // beforeBalance
- 1500, // afterBalance
- 1, // operateType (整数)
- 1, // operateId
- '测试奖励' // remark
- ));
- // 触发资金变更事件 - 用户2消耗代币
- Event::dispatch(new FundChangedEvent(
- 1002, // userId
- 4, // fundId
- -300, // amount (负数表示消耗)
- 2000, // beforeBalance
- 1700, // afterBalance
- 2, // operateType (整数)
- 2, // operateId
- '测试扣除' // remark
- ));
- $this->info('✅ 事件触发完成');
- }
- /**
- * 测试查询功能
- */
- private function testQueryFunctions(): void
- {
- $this->info('=== 测试查询功能 ===');
- // 测试获取所有用户数据
- $allRewards = RewardCollectorService::getRewards();
- $allDeducts = DeductCollectorService::getDeducts();
- $this->info('所有用户奖励数据:');
- $this->info('- 物品奖励数量:' . count($allRewards['items']));
- $this->info('- 代币奖励数量:' . count($allRewards['coins']));
- $this->info('- 神像奖励数量:' . count($allRewards['gods']));
- $this->info('所有用户扣除数据:');
- $this->info('- 物品扣除数量:' . count($allDeducts['items']));
- $this->info('- 代币扣除数量:' . count($allDeducts['coins']));
- // 测试获取指定用户数据
- $user1Rewards = RewardCollectorService::getRewards(1001);
- $user1Deducts = DeductCollectorService::getDeducts(1001);
- $this->info('用户1001奖励数据:');
- $this->info('- 物品奖励数量:' . count($user1Rewards['items']));
- $this->info('- 代币奖励数量:' . count($user1Rewards['coins']));
- $this->info('- 神像奖励数量:' . count($user1Rewards['gods']));
- $this->info('用户1001扣除数据:');
- $this->info('- 物品扣除数量:' . count($user1Deducts['items']));
- $this->info('- 代币扣除数量:' . count($user1Deducts['coins']));
- $user2Rewards = RewardCollectorService::getRewards(1002);
- $user2Deducts = DeductCollectorService::getDeducts(1002);
- $this->info('用户1002奖励数据:');
- $this->info('- 物品奖励数量:' . count($user2Rewards['items']));
- $this->info('- 代币奖励数量:' . count($user2Rewards['coins']));
- $this->info('- 神像奖励数量:' . count($user2Rewards['gods']));
- $this->info('用户1002扣除数据:');
- $this->info('- 物品扣除数量:' . count($user2Deducts['items']));
- $this->info('- 代币扣除数量:' . count($user2Deducts['coins']));
- // 测试检查功能
- $hasAllRewards = RewardCollectorService::hasRewards();
- $hasUser1Rewards = RewardCollectorService::hasRewards(1001);
- $hasUser3Rewards = RewardCollectorService::hasRewards(9999); // 不存在的用户
- $this->info('检查功能测试:');
- $this->info('- 是否有奖励数据(所有用户):' . ($hasAllRewards ? '是' : '否'));
- $this->info('- 用户1001是否有奖励数据:' . ($hasUser1Rewards ? '是' : '否'));
- $this->info('- 用户9999是否有奖励数据:' . ($hasUser3Rewards ? '是' : '否'));
- // 验证用户数据隔离
- $this->verifyUserDataSeparation($user1Rewards, $user2Rewards, $user1Deducts, $user2Deducts);
- $this->info('✅ 查询功能测试完成');
- }
- /**
- * 验证用户数据隔离
- */
- private function verifyUserDataSeparation($user1Rewards, $user2Rewards, $user1Deducts, $user2Deducts): void
- {
- $this->info('=== 验证用户数据隔离 ===');
- // 检查用户1的数据是否包含正确的用户ID
- foreach ($user1Rewards['items'] as $item) {
- if ($item['user_id'] !== 1001) {
- $this->error('❌ 用户1001的物品奖励数据包含错误的用户ID:' . $item['user_id']);
- return;
- }
- }
- foreach ($user1Rewards['coins'] as $coin) {
- if ($coin['user_id'] !== 1001) {
- $this->error('❌ 用户1001的代币奖励数据包含错误的用户ID:' . $coin['user_id']);
- return;
- }
- }
- // 检查用户2的数据是否包含正确的用户ID
- foreach ($user2Rewards['items'] as $item) {
- if ($item['user_id'] !== 1002) {
- $this->error('❌ 用户1002的物品奖励数据包含错误的用户ID:' . $item['user_id']);
- return;
- }
- }
- $this->info('✅ 用户数据隔离验证通过');
- }
- }
|