AutoFertilizingSkillTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace Tests\Feature\Pet;
  3. use App\Module\Pet\Models\PetUser;
  4. use App\Module\Pet\Models\PetSkill;
  5. use App\Module\Pet\Services\PetService;
  6. use App\Module\GameItems\Services\ItemService;
  7. use App\Module\Farm\Models\FarmLand;
  8. use App\Module\Farm\Models\FarmCrop;
  9. use App\Module\Farm\Services\CropService;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Support\Facades\DB;
  12. use Tests\TestCase;
  13. /**
  14. * 宠物自动施肥技能测试
  15. */
  16. class AutoFertilizingSkillTest extends TestCase
  17. {
  18. private int $testUserId = 9999;
  19. private int $testPetId;
  20. private int $testLandId;
  21. private int $fertilizerItemId = 19; // 普通化肥
  22. protected function setUp(): void
  23. {
  24. parent::setUp();
  25. // 清理可能存在的测试数据
  26. $this->cleanupTestData();
  27. // 创建测试数据
  28. $this->createTestData();
  29. }
  30. /**
  31. * 清理测试数据
  32. */
  33. private function cleanupTestData(): void
  34. {
  35. // 按正确顺序清理测试数据,避免外键约束问题
  36. DB::table('pet_skill_logs')->whereIn('pet_id', function($query) {
  37. $query->select('id')->from('pet_users')->where('user_id', $this->testUserId);
  38. })->delete();
  39. DB::table('pet_active_skills')->whereIn('pet_id', function($query) {
  40. $query->select('id')->from('pet_users')->where('user_id', $this->testUserId);
  41. })->delete();
  42. DB::table('farm_crops')->where('user_id', $this->testUserId)->delete();
  43. DB::table('farm_land_users')->where('user_id', $this->testUserId)->delete();
  44. DB::table('item_users')->where('user_id', $this->testUserId)->delete();
  45. DB::table('pet_users')->where('user_id', $this->testUserId)->delete();
  46. }
  47. /**
  48. * 创建测试数据
  49. */
  50. private function createTestData(): void
  51. {
  52. // 创建测试宠物(10级以上)
  53. $pet = PetUser::create([
  54. 'user_id' => $this->testUserId,
  55. 'pet_config_id' => 1,
  56. 'name' => '测试宠物',
  57. 'level' => 15, // 确保等级满足要求
  58. 'experience' => 1000,
  59. 'stamina' => 100,
  60. 'status' => 1,
  61. 'created_at' => now(),
  62. 'updated_at' => now()
  63. ]);
  64. $this->testPetId = $pet->id;
  65. // 创建测试土地
  66. $land = FarmLand::create([
  67. 'user_id' => $this->testUserId,
  68. 'position' => 1, // 土地位置
  69. 'land_type' => 1,
  70. 'status' => 2, // 种植中状态
  71. 'has_crop' => true,
  72. 'created_at' => now(),
  73. 'updated_at' => now()
  74. ]);
  75. $this->testLandId = $land->id;
  76. // 在土地上种植作物
  77. $crop = FarmCrop::create([
  78. 'land_id' => $this->testLandId,
  79. 'user_id' => $this->testUserId,
  80. 'seed_id' => 1,
  81. 'land_level' => 1,
  82. 'plant_time' => now(),
  83. 'growth_stage' => 20, // 发芽期,可以施肥
  84. 'stage_start_time' => now(),
  85. 'stage_end_time' => now()->addHours(3),
  86. 'disasters' => [],
  87. 'fertilized' => false, // 未施肥
  88. 'last_disaster_check_time' => now(),
  89. 'can_disaster' => true,
  90. 'final_output_item_id' => 2,
  91. 'final_output_amount' => 1,
  92. 'created_at' => now(),
  93. 'updated_at' => now()
  94. ]);
  95. // 给用户添加肥料物品
  96. DB::beginTransaction();
  97. ItemService::addItem($this->testUserId, $this->fertilizerItemId, 10, [
  98. 'source' => 'test_setup'
  99. ]);
  100. DB::commit();
  101. }
  102. /**
  103. * 测试自动施肥技能激活
  104. */
  105. public function testActivateAutoFertilizingSkill(): void
  106. {
  107. DB::beginTransaction();
  108. try {
  109. // 获取自动施肥技能
  110. $skill = PetSkill::where('skill_name', '自动施肥')->first();
  111. $this->assertNotNull($skill, '自动施肥技能不存在');
  112. // 激活技能
  113. $result = PetService::useSkill($this->testUserId, $this->testPetId, $skill->id, [
  114. 'duration' => 3600, // 1小时
  115. 'auto_use_items' => true
  116. ]);
  117. $this->assertTrue($result['success'], '技能激活失败: ' . ($result['message'] ?? ''));
  118. $this->assertEquals($skill->id, $result['skill_id']);
  119. // 验证技能激活记录
  120. $activeSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $this->testPetId)
  121. ->where('skill_name', '自动施肥')
  122. ->where('status', 'active')
  123. ->first();
  124. $this->assertNotNull($activeSkill, '技能激活记录不存在');
  125. $this->assertEquals('自动施肥', $activeSkill->skill_name);
  126. DB::commit();
  127. echo "✓ 自动施肥技能激活测试通过\n";
  128. } catch (\Exception $e) {
  129. DB::rollBack();
  130. $this->fail('测试失败: ' . $e->getMessage());
  131. }
  132. }
  133. /**
  134. * 测试自动施肥处理逻辑
  135. */
  136. public function testAutoFertilizingProcess(): void
  137. {
  138. DB::beginTransaction();
  139. try {
  140. // 先激活技能
  141. $skill = PetSkill::where('skill_name', '自动施肥')->first();
  142. PetService::useSkill($this->testUserId, $this->testPetId, $skill->id, [
  143. 'duration' => 3600,
  144. 'auto_use_items' => true
  145. ]);
  146. // 获取激活的技能
  147. $activeSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $this->testPetId)
  148. ->where('skill_name', '自动施肥')
  149. ->where('status', 'active')
  150. ->first();
  151. $this->assertNotNull($activeSkill, '技能激活记录不存在');
  152. // 获取施肥前的作物状态
  153. $cropBefore = FarmCrop::where('land_id', $this->testLandId)->first();
  154. $this->assertFalse($cropBefore->fertilized, '作物应该未施肥');
  155. // 获取施肥前的肥料数量
  156. $fertilizerBefore = ItemService::getUserItems($this->testUserId, ['item_id' => $this->fertilizerItemId])
  157. ->sum('quantity');
  158. // 执行自动施肥处理
  159. $autoSkillLogic = new \App\Module\Pet\Logic\PetAutoSkillLogic();
  160. $autoSkillLogic->processAutoFertilizing($activeSkill);
  161. // 验证施肥结果
  162. $cropAfter = FarmCrop::where('land_id', $this->testLandId)->first();
  163. $this->assertTrue($cropAfter->fertilized, '作物应该已施肥');
  164. // 验证肥料消耗
  165. $fertilizerAfter = ItemService::getUserItems($this->testUserId, ['item_id' => $this->fertilizerItemId])
  166. ->sum('quantity');
  167. $this->assertEquals($fertilizerBefore - 1, $fertilizerAfter, '肥料应该消耗1个');
  168. DB::commit();
  169. echo "✓ 自动施肥处理逻辑测试通过\n";
  170. } catch (\Exception $e) {
  171. DB::rollBack();
  172. $this->fail('测试失败: ' . $e->getMessage());
  173. }
  174. }
  175. /**
  176. * 测试等级限制
  177. */
  178. public function testLevelRequirement(): void
  179. {
  180. DB::beginTransaction();
  181. try {
  182. // 创建低等级宠物
  183. $lowLevelPet = PetUser::create([
  184. 'user_id' => $this->testUserId,
  185. 'pet_config_id' => 1,
  186. 'name' => '低等级宠物',
  187. 'level' => 5, // 低于10级
  188. 'experience' => 100,
  189. 'stamina' => 100,
  190. 'status' => 1,
  191. 'created_at' => now(),
  192. 'updated_at' => now()
  193. ]);
  194. // 获取自动施肥技能
  195. $skill = PetSkill::where('skill_name', '自动施肥')->first();
  196. // 尝试激活技能(应该失败)
  197. $result = PetService::useSkill($this->testUserId, $lowLevelPet->id, $skill->id, []);
  198. $this->assertFalse($result['success'], '低等级宠物不应该能激活自动施肥技能');
  199. DB::commit();
  200. echo "✓ 等级限制测试通过\n";
  201. } catch (\Exception $e) {
  202. DB::rollBack();
  203. $this->fail('测试失败: ' . $e->getMessage());
  204. }
  205. }
  206. /**
  207. * 清理测试数据
  208. */
  209. protected function tearDown(): void
  210. {
  211. // 清理测试数据
  212. DB::table('pet_users')->where('user_id', $this->testUserId)->delete();
  213. DB::table('farm_lands')->where('user_id', $this->testUserId)->delete();
  214. DB::table('farm_crops')->where('user_id', $this->testUserId)->delete();
  215. DB::table('pet_active_skills')->where('pet_id', $this->testPetId)->delete();
  216. DB::table('item_users')->where('user_id', $this->testUserId)->delete();
  217. parent::tearDown();
  218. }
  219. }