TestFarmIntegrationCommand.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\Farm\Events\CropHarvestedEvent;
  5. use App\Module\Farm\Models\FarmLand;
  6. use App\Module\Farm\Models\FarmCrop;
  7. use App\Module\Farm\Models\FarmHarvestLog;
  8. use App\Module\UrsPromotion\Models\UrsUserReferral;
  9. use App\Module\UrsPromotion\Models\UrsUserTalent;
  10. use App\Module\UrsPromotion\Models\UrsTalentConfig;
  11. use App\Module\UrsPromotion\Models\UrsProfit;
  12. use App\Module\UrsPromotion\Enums\UrsProfitType;
  13. use Illuminate\Support\Facades\DB;
  14. /**
  15. * 测试Farm模块与UrsPromotion模块集成
  16. */
  17. class TestFarmIntegrationCommand extends Command
  18. {
  19. /**
  20. * 命令签名
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'urs:test-farm-integration {--clean : 清理测试数据}';
  25. /**
  26. * 命令描述
  27. *
  28. * @var string
  29. */
  30. protected $description = '测试Farm模块与UrsPromotion模块的事件集成';
  31. /**
  32. * 执行命令
  33. */
  34. public function handle(): int
  35. {
  36. if ($this->option('clean')) {
  37. $this->cleanTestData();
  38. $this->info('测试数据清理完成');
  39. return 0;
  40. }
  41. $this->info('开始测试Farm模块与UrsPromotion模块集成...');
  42. try {
  43. // 1. 准备测试数据
  44. $this->info('1. 准备测试数据...');
  45. $testData = $this->prepareTestData();
  46. // 2. 模拟Farm收获事件
  47. $this->info('2. 模拟Farm收获事件...');
  48. $this->simulateHarvestEvent($testData);
  49. // 3. 验证URS收益分发
  50. $this->info('3. 验证URS收益分发...');
  51. $this->verifyProfitDistribution($testData);
  52. $this->info('✅ Farm模块与UrsPromotion模块集成测试通过!');
  53. return 0;
  54. } catch (\Exception $e) {
  55. $this->error('❌ 集成测试失败: ' . $e->getMessage());
  56. $this->error('错误详情: ' . $e->getTraceAsString());
  57. return 1;
  58. }
  59. }
  60. /**
  61. * 准备测试数据
  62. */
  63. private function prepareTestData(): array
  64. {
  65. DB::beginTransaction();
  66. try {
  67. // 创建测试用户推荐关系
  68. $userId = 3001; // 收获用户
  69. $directReferrerId = 3002; // 直推
  70. $indirectReferrerId = 3003; // 间推
  71. // 清理可能存在的测试数据
  72. UrsUserReferral::whereIn('user_id', [$userId, $directReferrerId])->delete();
  73. UrsUserTalent::whereIn('user_id', [$userId, $directReferrerId, $indirectReferrerId])->delete();
  74. UrsProfit::where('source_type', 'farm_harvest_test')->delete();
  75. // 创建推荐关系:3003 -> 3002 -> 3001
  76. UrsUserReferral::create([
  77. 'user_id' => $directReferrerId,
  78. 'referrer_id' => $indirectReferrerId,
  79. 'referral_code' => 'TEST_' . $indirectReferrerId,
  80. 'direct_count' => 1,
  81. 'indirect_count' => 0,
  82. 'third_count' => 0,
  83. ]);
  84. UrsUserReferral::create([
  85. 'user_id' => $userId,
  86. 'referrer_id' => $directReferrerId,
  87. 'referral_code' => 'TEST_' . $directReferrerId,
  88. 'direct_count' => 0,
  89. 'indirect_count' => 0,
  90. 'third_count' => 0,
  91. ]);
  92. // 创建达人等级
  93. UrsUserTalent::create([
  94. 'user_id' => $directReferrerId,
  95. 'talent_level' => 1, // 初级达人
  96. 'direct_count' => 1,
  97. 'indirect_count' => 0,
  98. 'third_count' => 0,
  99. 'total_count' => 1,
  100. ]);
  101. UrsUserTalent::create([
  102. 'user_id' => $indirectReferrerId,
  103. 'talent_level' => 2, // 中级达人
  104. 'direct_count' => 1,
  105. 'indirect_count' => 1,
  106. 'third_count' => 0,
  107. 'total_count' => 2,
  108. ]);
  109. // 创建模拟的Farm数据
  110. $mockLand = new FarmLand();
  111. $mockLand->id = 9999;
  112. $mockLand->user_id = $userId;
  113. $mockCrop = new FarmCrop();
  114. $mockCrop->id = 9999;
  115. $mockCrop->user_id = $userId;
  116. $mockHarvestLog = new FarmHarvestLog();
  117. $mockHarvestLog->id = 9999;
  118. $mockHarvestLog->user_id = $userId;
  119. $mockHarvestLog->crop_id = $mockCrop->id;
  120. DB::commit();
  121. $this->info(" - 创建推荐关系: {$indirectReferrerId} -> {$directReferrerId} -> {$userId}");
  122. $this->info(" - 设置达人等级: 用户{$directReferrerId}=初级达人, 用户{$indirectReferrerId}=中级达人");
  123. return [
  124. 'user_id' => $userId,
  125. 'direct_referrer_id' => $directReferrerId,
  126. 'indirect_referrer_id' => $indirectReferrerId,
  127. 'mock_land' => $mockLand,
  128. 'mock_crop' => $mockCrop,
  129. 'mock_harvest_log' => $mockHarvestLog,
  130. 'output_amount' => 1000,
  131. ];
  132. } catch (\Exception $e) {
  133. DB::rollBack();
  134. throw $e;
  135. }
  136. }
  137. /**
  138. * 模拟Farm收获事件
  139. */
  140. private function simulateHarvestEvent(array $testData): void
  141. {
  142. $event = new CropHarvestedEvent(
  143. $testData['user_id'],
  144. $testData['mock_land'],
  145. $testData['mock_crop'],
  146. $testData['mock_harvest_log'],
  147. 1001, // 输出物品ID
  148. $testData['output_amount']
  149. );
  150. // 触发事件
  151. event($event);
  152. $this->info(" - 触发收获事件: 用户{$testData['user_id']}收获{$testData['output_amount']}个物品");
  153. }
  154. /**
  155. * 验证收益分发
  156. */
  157. private function verifyProfitDistribution(array $testData): void
  158. {
  159. // 等待事件处理完成
  160. sleep(1);
  161. // 查询生成的收益记录
  162. $profits = UrsProfit::where('source_type', 'farm_harvest')
  163. ->where('source_id', $testData['mock_harvest_log']->id)
  164. ->where('profit_type', UrsProfitType::PLANTING_REWARD->value)
  165. ->get();
  166. $this->info(" - 生成收益记录数量: " . $profits->count());
  167. if ($profits->isEmpty()) {
  168. throw new \Exception('未生成任何收益记录');
  169. }
  170. foreach ($profits as $profit) {
  171. $this->info(" - 收益记录: 用户{$profit->user_id}, 层级{$profit->relation_level}, 金额{$profit->profit_amount}, 比例{$profit->profit_rate}");
  172. }
  173. // 验证收益记录的正确性
  174. $directProfit = $profits->where('user_id', $testData['direct_referrer_id'])->first();
  175. $indirectProfit = $profits->where('user_id', $testData['indirect_referrer_id'])->first();
  176. if (!$directProfit) {
  177. throw new \Exception('直推收益记录不存在');
  178. }
  179. if (!$indirectProfit) {
  180. throw new \Exception('间推收益记录不存在');
  181. }
  182. $this->info(" ✅ 直推收益: {$directProfit->profit_amount} (比例: {$directProfit->profit_rate})");
  183. $this->info(" ✅ 间推收益: {$indirectProfit->profit_amount} (比例: {$indirectProfit->profit_rate})");
  184. }
  185. /**
  186. * 清理测试数据
  187. */
  188. private function cleanTestData(): void
  189. {
  190. UrsUserReferral::whereIn('user_id', [3001, 3002])->delete();
  191. UrsUserTalent::whereIn('user_id', [3001, 3002, 3003])->delete();
  192. UrsProfit::where('source_type', 'farm_harvest')->where('source_id', 9999)->delete();
  193. }
  194. }