TestRewardDeductCollectorUserSeparation.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Module\Game\Services\RewardCollectorService;
  4. use App\Module\Game\Services\DeductCollectorService;
  5. use App\Module\GameItems\Events\ItemQuantityChanged;
  6. use App\Module\Fund\Events\FundChangedEvent;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Event;
  9. /**
  10. * 测试奖励扣除收集器用户区分功能
  11. */
  12. class TestRewardDeductCollectorUserSeparation extends Command
  13. {
  14. /**
  15. * 命令签名
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'test:reward-deduct-collector-user-separation';
  20. /**
  21. * 命令描述
  22. *
  23. * @var string
  24. */
  25. protected $description = '测试奖励扣除收集器的用户区分功能';
  26. /**
  27. * 执行命令
  28. *
  29. * @return int
  30. */
  31. public function handle(): int
  32. {
  33. $this->info('开始测试奖励扣除收集器用户区分功能...');
  34. // 清空之前的数据
  35. RewardCollectorService::clearRewards();
  36. DeductCollectorService::clearDeducts();
  37. // 测试手动添加数据
  38. $this->testManualCollection();
  39. // 测试事件自动收集
  40. $this->testEventCollection();
  41. // 测试查询功能
  42. $this->testQueryFunctions();
  43. $this->info('所有测试完成!');
  44. return 0;
  45. }
  46. /**
  47. * 测试手动收集功能
  48. */
  49. private function testManualCollection(): void
  50. {
  51. $this->info('=== 测试手动收集功能 ===');
  52. // 用户1的数据
  53. RewardCollectorService::addItemReward(1001, 101, 0, 10);
  54. RewardCollectorService::addCoinReward(1001, 1, 100);
  55. RewardCollectorService::addGodReward(1001, 1, 3600, 50);
  56. DeductCollectorService::addItemDeduct(1001, 102, 0, 5);
  57. DeductCollectorService::addCoinDeduct(1001, 2, 20);
  58. // 用户2的数据
  59. RewardCollectorService::addItemReward(1002, 103, 0, 15);
  60. RewardCollectorService::addCoinReward(1002, 1, 200);
  61. DeductCollectorService::addItemDeduct(1002, 104, 0, 8);
  62. DeductCollectorService::addCoinDeduct(1002, 2, 30);
  63. $this->info('✅ 手动收集数据添加完成');
  64. }
  65. /**
  66. * 测试事件自动收集
  67. */
  68. private function testEventCollection(): void
  69. {
  70. $this->info('=== 测试事件自动收集功能 ===');
  71. // 触发物品变更事件 - 用户1获得物品
  72. Event::dispatch(new ItemQuantityChanged(
  73. 1001, // userId
  74. 201, // itemId
  75. null, // instanceId
  76. 0, // oldQuantity
  77. 25, // newQuantity
  78. 1 // userItemId
  79. ));
  80. // 触发物品变更事件 - 用户2消耗物品
  81. Event::dispatch(new ItemQuantityChanged(
  82. 1002, // userId
  83. 202, // itemId
  84. null, // instanceId
  85. 50, // oldQuantity
  86. 40, // newQuantity
  87. 2 // userItemId
  88. ));
  89. // 触发资金变更事件 - 用户1获得代币
  90. Event::dispatch(new FundChangedEvent(
  91. 1001, // userId
  92. 3, // fundId
  93. 500, // amount (正数表示获得)
  94. 1000, // beforeBalance
  95. 1500, // afterBalance
  96. 1, // operateType (整数)
  97. 1, // operateId
  98. '测试奖励' // remark
  99. ));
  100. // 触发资金变更事件 - 用户2消耗代币
  101. Event::dispatch(new FundChangedEvent(
  102. 1002, // userId
  103. 4, // fundId
  104. -300, // amount (负数表示消耗)
  105. 2000, // beforeBalance
  106. 1700, // afterBalance
  107. 2, // operateType (整数)
  108. 2, // operateId
  109. '测试扣除' // remark
  110. ));
  111. $this->info('✅ 事件触发完成');
  112. }
  113. /**
  114. * 测试查询功能
  115. */
  116. private function testQueryFunctions(): void
  117. {
  118. $this->info('=== 测试查询功能 ===');
  119. // 测试获取所有用户数据
  120. $allRewards = RewardCollectorService::getRewards();
  121. $allDeducts = DeductCollectorService::getDeducts();
  122. $this->info('所有用户奖励数据:');
  123. $this->info('- 物品奖励数量:' . count($allRewards['items']));
  124. $this->info('- 代币奖励数量:' . count($allRewards['coins']));
  125. $this->info('- 神像奖励数量:' . count($allRewards['gods']));
  126. $this->info('所有用户扣除数据:');
  127. $this->info('- 物品扣除数量:' . count($allDeducts['items']));
  128. $this->info('- 代币扣除数量:' . count($allDeducts['coins']));
  129. // 测试获取指定用户数据
  130. $user1Rewards = RewardCollectorService::getRewards(1001);
  131. $user1Deducts = DeductCollectorService::getDeducts(1001);
  132. $this->info('用户1001奖励数据:');
  133. $this->info('- 物品奖励数量:' . count($user1Rewards['items']));
  134. $this->info('- 代币奖励数量:' . count($user1Rewards['coins']));
  135. $this->info('- 神像奖励数量:' . count($user1Rewards['gods']));
  136. $this->info('用户1001扣除数据:');
  137. $this->info('- 物品扣除数量:' . count($user1Deducts['items']));
  138. $this->info('- 代币扣除数量:' . count($user1Deducts['coins']));
  139. $user2Rewards = RewardCollectorService::getRewards(1002);
  140. $user2Deducts = DeductCollectorService::getDeducts(1002);
  141. $this->info('用户1002奖励数据:');
  142. $this->info('- 物品奖励数量:' . count($user2Rewards['items']));
  143. $this->info('- 代币奖励数量:' . count($user2Rewards['coins']));
  144. $this->info('- 神像奖励数量:' . count($user2Rewards['gods']));
  145. $this->info('用户1002扣除数据:');
  146. $this->info('- 物品扣除数量:' . count($user2Deducts['items']));
  147. $this->info('- 代币扣除数量:' . count($user2Deducts['coins']));
  148. // 测试检查功能
  149. $hasAllRewards = RewardCollectorService::hasRewards();
  150. $hasUser1Rewards = RewardCollectorService::hasRewards(1001);
  151. $hasUser3Rewards = RewardCollectorService::hasRewards(9999); // 不存在的用户
  152. $this->info('检查功能测试:');
  153. $this->info('- 是否有奖励数据(所有用户):' . ($hasAllRewards ? '是' : '否'));
  154. $this->info('- 用户1001是否有奖励数据:' . ($hasUser1Rewards ? '是' : '否'));
  155. $this->info('- 用户9999是否有奖励数据:' . ($hasUser3Rewards ? '是' : '否'));
  156. // 验证用户数据隔离
  157. $this->verifyUserDataSeparation($user1Rewards, $user2Rewards, $user1Deducts, $user2Deducts);
  158. $this->info('✅ 查询功能测试完成');
  159. }
  160. /**
  161. * 验证用户数据隔离
  162. */
  163. private function verifyUserDataSeparation($user1Rewards, $user2Rewards, $user1Deducts, $user2Deducts): void
  164. {
  165. $this->info('=== 验证用户数据隔离 ===');
  166. // 检查用户1的数据是否包含正确的用户ID
  167. foreach ($user1Rewards['items'] as $item) {
  168. if ($item['user_id'] !== 1001) {
  169. $this->error('❌ 用户1001的物品奖励数据包含错误的用户ID:' . $item['user_id']);
  170. return;
  171. }
  172. }
  173. foreach ($user1Rewards['coins'] as $coin) {
  174. if ($coin['user_id'] !== 1001) {
  175. $this->error('❌ 用户1001的代币奖励数据包含错误的用户ID:' . $coin['user_id']);
  176. return;
  177. }
  178. }
  179. // 检查用户2的数据是否包含正确的用户ID
  180. foreach ($user2Rewards['items'] as $item) {
  181. if ($item['user_id'] !== 1002) {
  182. $this->error('❌ 用户1002的物品奖励数据包含错误的用户ID:' . $item['user_id']);
  183. return;
  184. }
  185. }
  186. $this->info('✅ 用户数据隔离验证通过');
  187. }
  188. }