PetAutoSkillMultipleDisasterTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Tests\Unit\Pet;
  3. use Tests\TestCase;
  4. use App\Module\Pet\Logic\PetAutoSkillLogic;
  5. use App\Module\Pet\Models\PetActiveSkill;
  6. use App\Module\Pet\Models\Pet;
  7. use App\Module\Farm\Models\FarmLand;
  8. use App\Module\Farm\Models\FarmCrop;
  9. use App\Module\Farm\Enums\DISASTER_TYPE;
  10. use App\Module\Farm\Enums\LAND_STATUS;
  11. use App\Module\Farm\Enums\GROWTH_STAGE;
  12. use App\Module\GameItems\Models\Item;
  13. use App\Module\GameItems\Models\UserItem;
  14. use Illuminate\Foundation\Testing\RefreshDatabase;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. /**
  18. * 宠物自动技能处理多个灾害测试
  19. */
  20. class PetAutoSkillMultipleDisasterTest extends TestCase
  21. {
  22. use RefreshDatabase;
  23. private $userId = 1001;
  24. private $petId = 2001;
  25. private $landId = 3001;
  26. private $cropId = 4001;
  27. private $seedId = 5001;
  28. protected function setUp(): void
  29. {
  30. parent::setUp();
  31. // 创建测试数据
  32. $this->createTestData();
  33. }
  34. /**
  35. * 创建测试数据
  36. */
  37. private function createTestData(): void
  38. {
  39. // 创建宠物
  40. Pet::create([
  41. 'id' => $this->petId,
  42. 'user_id' => $this->userId,
  43. 'name' => '测试宠物',
  44. 'level' => 1,
  45. 'exp' => 0,
  46. 'status' => 'active'
  47. ]);
  48. // 创建土地
  49. FarmLand::create([
  50. 'id' => $this->landId,
  51. 'user_id' => $this->userId,
  52. 'position' => 1,
  53. 'status' => LAND_STATUS::PLANTING->value,
  54. 'has_crop' => true,
  55. 'land_type' => 1
  56. ]);
  57. // 创建作物(带有多个杂草灾害)
  58. FarmCrop::create([
  59. 'id' => $this->cropId,
  60. 'land_id' => $this->landId,
  61. 'user_id' => $this->userId,
  62. 'seed_id' => $this->seedId,
  63. 'land_level' => 1,
  64. 'plant_time' => now()->subHours(2),
  65. 'growth_stage' => GROWTH_STAGE::GROWTH->value,
  66. 'stage_start_time' => now()->subHour(),
  67. 'stage_end_time' => now()->addHour(),
  68. 'disasters' => [
  69. [
  70. 'id' => 1,
  71. 'type' => DISASTER_TYPE::WEED->value,
  72. 'status' => 'active',
  73. 'created_at' => now()->subMinutes(30)->toDateTimeString()
  74. ],
  75. [
  76. 'id' => 2,
  77. 'type' => DISASTER_TYPE::WEED->value,
  78. 'status' => 'active',
  79. 'created_at' => now()->subMinutes(20)->toDateTimeString()
  80. ],
  81. [
  82. 'id' => 3,
  83. 'type' => DISASTER_TYPE::WEED->value,
  84. 'status' => 'active',
  85. 'created_at' => now()->subMinutes(10)->toDateTimeString()
  86. ]
  87. ],
  88. 'fertilized' => false,
  89. 'can_disaster' => true,
  90. 'final_output_item_id' => 1001,
  91. 'final_output_amount' => 10
  92. ]);
  93. // 创建除草剂道具
  94. Item::create([
  95. 'id' => 22,
  96. 'name' => '除草剂',
  97. 'type' => 'tool',
  98. 'category' => 'farm_tool'
  99. ]);
  100. // 给用户足够的除草剂
  101. UserItem::create([
  102. 'user_id' => $this->userId,
  103. 'item_id' => 22,
  104. 'quantity' => 10,
  105. 'frozen_quantity' => 0
  106. ]);
  107. // 创建激活的自动除草技能
  108. PetActiveSkill::create([
  109. 'pet_id' => $this->petId,
  110. 'skill_name' => \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WEEDING->value,
  111. 'activated_at' => now()->subMinutes(10),
  112. 'expires_at' => now()->addHours(3),
  113. 'last_processed_at' => now()->subMinutes(5)
  114. ]);
  115. }
  116. /**
  117. * 测试自动除草处理多个杂草灾害
  118. */
  119. public function testAutoWeedingHandlesMultipleWeeds(): void
  120. {
  121. // 获取激活的技能
  122. $activeSkill = PetActiveSkill::where('pet_id', $this->petId)->first();
  123. $this->assertNotNull($activeSkill);
  124. // 确认作物有3个活跃的杂草灾害
  125. $crop = FarmCrop::find($this->cropId);
  126. $activeWeeds = array_filter($crop->disasters, function($disaster) {
  127. return $disaster['type'] == DISASTER_TYPE::WEED->value && $disaster['status'] === 'active';
  128. });
  129. $this->assertCount(3, $activeWeeds);
  130. // 执行自动除草
  131. DB::beginTransaction();
  132. $autoSkillLogic = new PetAutoSkillLogic();
  133. $autoSkillLogic->processAutoWeeding($activeSkill);
  134. DB::commit();
  135. // 验证所有杂草灾害都被清除
  136. $crop->refresh();
  137. $remainingActiveWeeds = array_filter($crop->disasters, function($disaster) {
  138. return $disaster['type'] == DISASTER_TYPE::WEED->value && $disaster['status'] === 'active';
  139. });
  140. $this->assertCount(0, $remainingActiveWeeds, '所有杂草灾害应该被清除');
  141. // 验证道具消耗(应该消耗3个除草剂)
  142. $userItem = UserItem::where('user_id', $this->userId)
  143. ->where('item_id', 22)
  144. ->first();
  145. $this->assertEquals(7, $userItem->quantity, '应该消耗3个除草剂');
  146. }
  147. /**
  148. * 测试道具不足时的处理
  149. */
  150. public function testAutoWeedingWithInsufficientItems(): void
  151. {
  152. // 将用户的除草剂数量设置为只有1个
  153. UserItem::where('user_id', $this->userId)
  154. ->where('item_id', 22)
  155. ->update(['quantity' => 1]);
  156. $activeSkill = PetActiveSkill::where('pet_id', $this->petId)->first();
  157. // 执行自动除草
  158. DB::beginTransaction();
  159. $autoSkillLogic = new PetAutoSkillLogic();
  160. $autoSkillLogic->processAutoWeeding($activeSkill);
  161. DB::commit();
  162. // 验证只清除了1个杂草灾害
  163. $crop = FarmCrop::find($this->cropId);
  164. $remainingActiveWeeds = array_filter($crop->disasters, function($disaster) {
  165. return $disaster['type'] == DISASTER_TYPE::WEED->value && $disaster['status'] === 'active';
  166. });
  167. $this->assertCount(2, $remainingActiveWeeds, '应该还剩2个杂草灾害');
  168. // 验证道具全部消耗完
  169. $userItem = UserItem::where('user_id', $this->userId)
  170. ->where('item_id', 22)
  171. ->first();
  172. $this->assertEquals(0, $userItem->quantity, '除草剂应该全部消耗完');
  173. }
  174. }