| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace Tests\Unit\Farm;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\Farm\Logics\CropLogic;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmSeed;
- use App\Module\Farm\Models\FarmUser;
- use Tests\TestCase;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- /**
- * 成熟期产量确定功能测试
- */
- class CropMatureOutputTest extends TestCase
- {
- use RefreshDatabase;
- protected $cropLogic;
- protected function setUp(): void
- {
- parent::setUp();
- $this->cropLogic = new CropLogic();
- }
- /**
- * 测试作物进入成熟期时产量计算
- */
- public function test_crop_mature_output_calculation()
- {
- // 创建测试数据
- $crop = $this->createTestCrop();
-
- // 确保作物有产出物品ID但没有产量
- $crop->final_output_item_id = 1001;
- $crop->final_output_amount = null;
- $crop->save();
- // 调用成熟期产量计算
- $calculatedAmount = $this->cropLogic->calculateMatureOutput($crop);
- // 验证产量计算结果
- $this->assertIsInt($calculatedAmount);
- $this->assertGreaterThan(0, $calculatedAmount);
- $this->assertLessThanOrEqual(3000, $calculatedAmount); // 全局最大产量限制
- // 验证产量在合理范围内(种子的最小-最大产出范围)
- $seed = $crop->seed;
- $this->assertGreaterThanOrEqual($seed->min_output, $calculatedAmount);
- }
- /**
- * 测试作物生长阶段更新时自动计算产量
- */
- public function test_auto_calculate_output_on_mature_stage()
- {
- // 创建测试作物(生长期)
- $crop = $this->createTestCrop();
- $crop->growth_stage = GROWTH_STAGE::GROWTH;
- $crop->final_output_item_id = 1001;
- $crop->final_output_amount = null;
- $crop->save();
- // 模拟进入成熟期
- $result = $this->cropLogic->updateGrowthStage($crop->id);
- // 验证更新成功
- $this->assertTrue($result);
- // 重新加载作物数据
- $crop->refresh();
- // 验证进入成熟期后产量已确定
- if ($crop->growth_stage === GROWTH_STAGE::MATURE) {
- $this->assertNotNull($crop->final_output_amount);
- $this->assertGreaterThan(0, $crop->final_output_amount);
- }
- }
- /**
- * 测试收获时使用预确定的产量
- */
- public function test_harvest_uses_predetermined_output()
- {
- // 创建成熟期作物,预设产量
- $crop = $this->createTestCrop();
- $crop->growth_stage = GROWTH_STAGE::MATURE;
- $crop->final_output_item_id = 1001;
- $crop->final_output_amount = 1500; // 预设产量
- $crop->save();
- // 执行收获
- $result = $this->cropLogic->harvestCrop($crop->user_id, $crop->land_id);
- // 验证收获成功
- $this->assertTrue($result->isSuccess());
- // 验证收获记录中的产量
- $harvestLog = \App\Module\Farm\Models\FarmHarvestLog::where('crop_id', $crop->id)->first();
- $this->assertNotNull($harvestLog);
-
- // 注意:最终产量可能会被事件监听器调整,但应该基于预设的1500
- $this->assertGreaterThan(0, $harvestLog->output_amount);
- }
- /**
- * 创建测试作物
- */
- private function createTestCrop(): FarmCrop
- {
- // 创建测试用户
- $user = \App\Models\User::factory()->create();
-
- // 创建农场用户
- $farmUser = FarmUser::create([
- 'user_id' => $user->id,
- 'house_level' => 1,
- ]);
- // 创建土地
- $land = FarmLand::create([
- 'user_id' => $user->id,
- 'position' => 1,
- 'land_type' => 1,
- 'status' => 1,
- ]);
- // 创建种子
- $seed = FarmSeed::create([
- 'name' => '测试种子',
- 'type' => 1,
- 'seed_time' => 3600,
- 'min_output' => 100,
- 'max_output' => 200,
- 'item_id' => 1001,
- ]);
- // 创建作物
- $crop = FarmCrop::create([
- 'land_id' => $land->id,
- 'user_id' => $user->id,
- 'seed_id' => $seed->id,
- 'plant_time' => now(),
- 'growth_stage' => GROWTH_STAGE::SPROUT,
- 'stage_start_time' => now(),
- 'stage_end_time' => now()->addHours(1),
- ]);
- return $crop;
- }
- }
|