TestCropPlantingBugFix.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\Farm\Enums\LAND_STATUS;
  5. use App\Module\Farm\Models\FarmCrop;
  6. use App\Module\Farm\Models\FarmLand;
  7. use App\Module\Farm\Models\FarmSeed;
  8. use App\Module\Farm\Services\CropService;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. /**
  13. * 测试作物种植bug修复的命令
  14. */
  15. class TestCropPlantingBugFix extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'farm:test-planting-bug-fix {user_id} {land_id} {item_id}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '测试农场作物种植bug修复';
  29. /**
  30. * Execute the console command.
  31. */
  32. public function handle()
  33. {
  34. $userId = (int) $this->argument('user_id');
  35. $landId = (int) $this->argument('land_id');
  36. $itemId = (int) $this->argument('item_id');
  37. $this->info("开始测试作物种植bug修复...");
  38. $this->info("用户ID: {$userId}, 土地ID: {$landId}, 种子物品ID: {$itemId}");
  39. // 1. 检查初始状态
  40. $this->info("\n=== 1. 检查初始状态 ===");
  41. $land = FarmLand::find($landId);
  42. if (!$land) {
  43. $this->error("土地不存在");
  44. return 1;
  45. }
  46. $existingCrop = FarmCrop::where('land_id', $landId)->first();
  47. $this->info("土地状态: " . LAND_STATUS::getName($land->status) . " ({$land->status})");
  48. $this->info("是否有作物: " . ($land->has_crop ? '是' : '否'));
  49. $this->info("作物记录: " . ($existingCrop ? "存在 (ID: {$existingCrop->id}, 阶段: {$existingCrop->growth_stage})" : '不存在'));
  50. // 2. 模拟bug场景:土地状态为空闲但有作物记录
  51. $this->info("\n=== 2. 模拟bug场景 ===");
  52. if ($existingCrop) {
  53. $this->info("已存在作物记录,模拟土地状态错误重置为空闲的情况");
  54. $land->status = LAND_STATUS::IDLE->value;
  55. $land->has_crop = false;
  56. $land->save();
  57. $this->info("已将土地状态重置为空闲,但保留作物记录");
  58. } else {
  59. $this->info("创建模拟的枯萎作物记录");
  60. $seed = FarmSeed::where('item_id', $itemId)->first();
  61. if (!$seed) {
  62. $this->error("种子配置不存在");
  63. return 1;
  64. }
  65. $mockCrop = new FarmCrop();
  66. $mockCrop->land_id = $landId;
  67. $mockCrop->user_id = $userId;
  68. $mockCrop->seed_id = $seed->id;
  69. $mockCrop->plant_time = now()->subHours(2);
  70. $mockCrop->growth_stage = GROWTH_STAGE::WITHERED;
  71. $mockCrop->stage_start_time = now()->subHours(1);
  72. $mockCrop->stage_end_time = null;
  73. $mockCrop->disasters = [];
  74. $mockCrop->fertilized = false;
  75. $mockCrop->last_disaster_check_time = now()->subHours(2);
  76. $mockCrop->can_disaster = false;
  77. $mockCrop->save();
  78. $land->status = LAND_STATUS::IDLE->value;
  79. $land->has_crop = false;
  80. $land->save();
  81. $this->info("已创建模拟枯萎作物记录 (ID: {$mockCrop->id})");
  82. }
  83. // 3. 测试修复后的种植逻辑
  84. $this->info("\n=== 3. 测试修复后的种植逻辑 ===");
  85. DB::beginTransaction();
  86. try {
  87. $result = CropService::plantCrop($userId, $landId, $itemId);
  88. if ($result) {
  89. $this->error("❌ 种植成功了!这表明bug修复可能有问题");
  90. $this->info("返回结果: " . json_encode($result, JSON_UNESCAPED_UNICODE));
  91. } else {
  92. $this->info("✅ 种植失败,符合预期");
  93. }
  94. DB::rollBack();
  95. } catch (\Exception $e) {
  96. DB::rollBack();
  97. $this->info("✅ 抛出异常,符合预期");
  98. $this->info("异常信息: " . $e->getMessage());
  99. }
  100. // 4. 清理并测试正常种植
  101. $this->info("\n=== 4. 测试正常种植流程 ===");
  102. FarmCrop::where('land_id', $landId)->delete();
  103. $land->status = LAND_STATUS::IDLE->value;
  104. $land->has_crop = false;
  105. $land->save();
  106. $this->info("已清理作物记录,土地状态重置为空闲");
  107. DB::beginTransaction();
  108. try {
  109. $result = CropService::plantCrop($userId, $landId, $itemId);
  110. if ($result) {
  111. $this->info("✅ 正常种植成功");
  112. $this->info("新作物ID: " . $result['crop']->id);
  113. $this->info("种植日志ID: " . $result['log_id']);
  114. } else {
  115. $this->error("❌ 正常种植失败");
  116. }
  117. DB::rollBack();
  118. } catch (\Exception $e) {
  119. DB::rollBack();
  120. $this->error("❌ 正常种植抛出异常: " . $e->getMessage());
  121. }
  122. // 5. 清理测试数据
  123. $this->info("\n=== 5. 清理测试数据 ===");
  124. FarmCrop::where('land_id', $landId)->delete();
  125. $land->status = LAND_STATUS::IDLE->value;
  126. $land->has_crop = false;
  127. $land->save();
  128. $this->info("测试数据已清理");
  129. $this->info("\n测试完成!");
  130. return 0;
  131. }
  132. }