createTestData(); } /** * 创建测试数据 */ private function createTestData(): void { // 创建宠物 Pet::create([ 'id' => $this->petId, 'user_id' => $this->userId, 'name' => '测试宠物', 'level' => 1, 'exp' => 0, 'status' => 'active' ]); // 创建土地 FarmLand::create([ 'id' => $this->landId, 'user_id' => $this->userId, 'position' => 1, 'status' => LAND_STATUS::PLANTING->value, 'has_crop' => true, 'land_type' => 1 ]); // 创建作物(带有多个杂草灾害) FarmCrop::create([ 'id' => $this->cropId, 'land_id' => $this->landId, 'user_id' => $this->userId, 'seed_id' => $this->seedId, 'land_level' => 1, 'plant_time' => now()->subHours(2), 'growth_stage' => GROWTH_STAGE::GROWTH->value, 'stage_start_time' => now()->subHour(), 'stage_end_time' => now()->addHour(), 'disasters' => [ [ 'id' => 1, 'type' => DISASTER_TYPE::WEED->value, 'status' => 'active', 'created_at' => now()->subMinutes(30)->toDateTimeString() ], [ 'id' => 2, 'type' => DISASTER_TYPE::WEED->value, 'status' => 'active', 'created_at' => now()->subMinutes(20)->toDateTimeString() ], [ 'id' => 3, 'type' => DISASTER_TYPE::WEED->value, 'status' => 'active', 'created_at' => now()->subMinutes(10)->toDateTimeString() ] ], 'fertilized' => false, 'can_disaster' => true, 'final_output_item_id' => 1001, 'final_output_amount' => 10 ]); // 创建除草剂道具 Item::create([ 'id' => 22, 'name' => '除草剂', 'type' => 'tool', 'category' => 'farm_tool' ]); // 给用户足够的除草剂 UserItem::create([ 'user_id' => $this->userId, 'item_id' => 22, 'quantity' => 10, 'frozen_quantity' => 0 ]); // 创建激活的自动除草技能 PetActiveSkill::create([ 'pet_id' => $this->petId, 'skill_name' => \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WEEDING->value, 'activated_at' => now()->subMinutes(10), 'expires_at' => now()->addHours(3), 'last_processed_at' => now()->subMinutes(5) ]); } /** * 测试自动除草处理多个杂草灾害 */ public function testAutoWeedingHandlesMultipleWeeds(): void { // 获取激活的技能 $activeSkill = PetActiveSkill::where('pet_id', $this->petId)->first(); $this->assertNotNull($activeSkill); // 确认作物有3个活跃的杂草灾害 $crop = FarmCrop::find($this->cropId); $activeWeeds = array_filter($crop->disasters, function($disaster) { return $disaster['type'] == DISASTER_TYPE::WEED->value && $disaster['status'] === 'active'; }); $this->assertCount(3, $activeWeeds); // 执行自动除草 DB::beginTransaction(); $autoSkillLogic = new PetAutoSkillLogic(); $autoSkillLogic->processAutoWeeding($activeSkill); DB::commit(); // 验证所有杂草灾害都被清除 $crop->refresh(); $remainingActiveWeeds = array_filter($crop->disasters, function($disaster) { return $disaster['type'] == DISASTER_TYPE::WEED->value && $disaster['status'] === 'active'; }); $this->assertCount(0, $remainingActiveWeeds, '所有杂草灾害应该被清除'); // 验证道具消耗(应该消耗3个除草剂) $userItem = UserItem::where('user_id', $this->userId) ->where('item_id', 22) ->first(); $this->assertEquals(7, $userItem->quantity, '应该消耗3个除草剂'); } /** * 测试道具不足时的处理 */ public function testAutoWeedingWithInsufficientItems(): void { // 将用户的除草剂数量设置为只有1个 UserItem::where('user_id', $this->userId) ->where('item_id', 22) ->update(['quantity' => 1]); $activeSkill = PetActiveSkill::where('pet_id', $this->petId)->first(); // 执行自动除草 DB::beginTransaction(); $autoSkillLogic = new PetAutoSkillLogic(); $autoSkillLogic->processAutoWeeding($activeSkill); DB::commit(); // 验证只清除了1个杂草灾害 $crop = FarmCrop::find($this->cropId); $remainingActiveWeeds = array_filter($crop->disasters, function($disaster) { return $disaster['type'] == DISASTER_TYPE::WEED->value && $disaster['status'] === 'active'; }); $this->assertCount(2, $remainingActiveWeeds, '应该还剩2个杂草灾害'); // 验证道具全部消耗完 $userItem = UserItem::where('user_id', $this->userId) ->where('item_id', 22) ->first(); $this->assertEquals(0, $userItem->quantity, '除草剂应该全部消耗完'); } }