CropMatureOutputTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Tests\Unit\Farm;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\Farm\Logics\CropLogic;
  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\Models\FarmUser;
  9. use Tests\TestCase;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. /**
  12. * 成熟期产量确定功能测试
  13. */
  14. class CropMatureOutputTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. protected $cropLogic;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->cropLogic = new CropLogic();
  22. }
  23. /**
  24. * 测试作物进入成熟期时产量计算
  25. */
  26. public function test_crop_mature_output_calculation()
  27. {
  28. // 创建测试数据
  29. $crop = $this->createTestCrop();
  30. // 确保作物有产出物品ID但没有产量
  31. $crop->final_output_item_id = 1001;
  32. $crop->final_output_amount = null;
  33. $crop->save();
  34. // 调用成熟期产量计算
  35. $calculatedAmount = $this->cropLogic->calculateMatureOutput($crop);
  36. // 验证产量计算结果
  37. $this->assertIsInt($calculatedAmount);
  38. $this->assertGreaterThan(0, $calculatedAmount);
  39. $this->assertLessThanOrEqual(3000, $calculatedAmount); // 全局最大产量限制
  40. // 验证产量在合理范围内(种子的最小-最大产出范围)
  41. $seed = $crop->seed;
  42. $this->assertGreaterThanOrEqual($seed->min_output, $calculatedAmount);
  43. }
  44. /**
  45. * 测试作物生长阶段更新时自动计算产量
  46. */
  47. public function test_auto_calculate_output_on_mature_stage()
  48. {
  49. // 创建测试作物(生长期)
  50. $crop = $this->createTestCrop();
  51. $crop->growth_stage = GROWTH_STAGE::GROWTH;
  52. $crop->final_output_item_id = 1001;
  53. $crop->final_output_amount = null;
  54. $crop->save();
  55. // 模拟进入成熟期
  56. $result = $this->cropLogic->updateGrowthStage($crop->id);
  57. // 验证更新成功
  58. $this->assertTrue($result);
  59. // 重新加载作物数据
  60. $crop->refresh();
  61. // 验证进入成熟期后产量已确定
  62. if ($crop->growth_stage === GROWTH_STAGE::MATURE) {
  63. $this->assertNotNull($crop->final_output_amount);
  64. $this->assertGreaterThan(0, $crop->final_output_amount);
  65. }
  66. }
  67. /**
  68. * 测试收获时使用预确定的产量
  69. */
  70. public function test_harvest_uses_predetermined_output()
  71. {
  72. // 创建成熟期作物,预设产量
  73. $crop = $this->createTestCrop();
  74. $crop->growth_stage = GROWTH_STAGE::MATURE;
  75. $crop->final_output_item_id = 1001;
  76. $crop->final_output_amount = 1500; // 预设产量
  77. $crop->save();
  78. // 执行收获
  79. $result = $this->cropLogic->harvestCrop($crop->user_id, $crop->land_id);
  80. // 验证收获成功
  81. $this->assertTrue($result->isSuccess());
  82. // 验证收获记录中的产量
  83. $harvestLog = \App\Module\Farm\Models\FarmHarvestLog::where('crop_id', $crop->id)->first();
  84. $this->assertNotNull($harvestLog);
  85. // 注意:最终产量可能会被事件监听器调整,但应该基于预设的1500
  86. $this->assertGreaterThan(0, $harvestLog->output_amount);
  87. }
  88. /**
  89. * 创建测试作物
  90. */
  91. private function createTestCrop(): FarmCrop
  92. {
  93. // 创建测试用户
  94. $user = \App\Models\User::factory()->create();
  95. // 创建农场用户
  96. $farmUser = FarmUser::create([
  97. 'user_id' => $user->id,
  98. 'house_level' => 1,
  99. ]);
  100. // 创建土地
  101. $land = FarmLand::create([
  102. 'user_id' => $user->id,
  103. 'position' => 1,
  104. 'land_type' => 1,
  105. 'status' => 1,
  106. ]);
  107. // 创建种子
  108. $seed = FarmSeed::create([
  109. 'name' => '测试种子',
  110. 'type' => 1,
  111. 'seed_time' => 3600,
  112. 'min_output' => 100,
  113. 'max_output' => 200,
  114. 'item_id' => 1001,
  115. ]);
  116. // 创建作物
  117. $crop = FarmCrop::create([
  118. 'land_id' => $land->id,
  119. 'user_id' => $user->id,
  120. 'seed_id' => $seed->id,
  121. 'plant_time' => now(),
  122. 'growth_stage' => GROWTH_STAGE::SPROUT,
  123. 'stage_start_time' => now(),
  124. 'stage_end_time' => now()->addHours(1),
  125. ]);
  126. return $crop;
  127. }
  128. }