| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- namespace Tests\Unit\Pet;
- use Tests\TestCase;
- use App\Module\Pet\Logic\PetAutoSkillLogic;
- use App\Module\Pet\Models\PetActiveSkill;
- use App\Module\Pet\Models\Pet;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Enums\DISASTER_TYPE;
- use App\Module\Farm\Enums\LAND_STATUS;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\GameItems\Models\Item;
- use App\Module\GameItems\Models\UserItem;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 宠物自动技能处理多个灾害测试
- */
- class PetAutoSkillMultipleDisasterTest extends TestCase
- {
- use RefreshDatabase;
- private $userId = 1001;
- private $petId = 2001;
- private $landId = 3001;
- private $cropId = 4001;
- private $seedId = 5001;
- protected function setUp(): void
- {
- parent::setUp();
-
- // 创建测试数据
- $this->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, '除草剂应该全部消耗完');
- }
- }
|