AutoFertilizingSkillTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. 'exp' => 1000,
  59. 'stamina' => 100,
  60. 'max_stamina' => 100,
  61. 'status' => 1,
  62. 'grade' => 1,
  63. 'created_at' => now(),
  64. 'updated_at' => now()
  65. ]);
  66. $this->testPetId = $pet->id;
  67. // 创建测试土地
  68. $land = FarmLand::create([
  69. 'user_id' => $this->testUserId,
  70. 'position' => 1, // 土地位置
  71. 'land_type' => 1,
  72. 'status' => 2, // 种植中状态
  73. 'has_crop' => true,
  74. 'created_at' => now(),
  75. 'updated_at' => now()
  76. ]);
  77. $this->testLandId = $land->id;
  78. // 在土地上种植作物
  79. $crop = FarmCrop::create([
  80. 'land_id' => $this->testLandId,
  81. 'user_id' => $this->testUserId,
  82. 'seed_id' => 1,
  83. 'land_level' => 1,
  84. 'plant_time' => now(),
  85. 'growth_stage' => 20, // 发芽期,可以施肥
  86. 'stage_start_time' => now(),
  87. 'stage_end_time' => now()->addHours(3),
  88. 'disasters' => [],
  89. 'fertilized' => false, // 未施肥
  90. 'last_disaster_check_time' => now(),
  91. 'can_disaster' => true,
  92. 'final_output_item_id' => 2,
  93. 'final_output_amount' => 1,
  94. 'created_at' => now(),
  95. 'updated_at' => now()
  96. ]);
  97. // 给用户添加肥料物品
  98. DB::beginTransaction();
  99. ItemService::addItem($this->testUserId, $this->fertilizerItemId, 10, [
  100. 'source' => 'test_setup'
  101. ]);
  102. DB::commit();
  103. }
  104. /**
  105. * 测试自动施肥技能激活
  106. */
  107. public function testActivateAutoFertilizingSkill(): void
  108. {
  109. DB::beginTransaction();
  110. try {
  111. // 获取自动施肥技能
  112. $skill = PetSkill::where('skill_name', '自动施肥')->first();
  113. $this->assertNotNull($skill, '自动施肥技能不存在');
  114. // 激活技能
  115. $result = PetService::useSkill($this->testUserId, $this->testPetId, $skill->id, [
  116. 'duration' => 3600, // 1小时
  117. 'auto_use_items' => true
  118. ]);
  119. $this->assertTrue($result['success'], '技能激活失败: ' . ($result['message'] ?? ''));
  120. $this->assertEquals($skill->id, $result['skill_id']);
  121. // 验证技能激活记录
  122. $activeSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $this->testPetId)
  123. ->where('skill_name', '自动施肥')
  124. ->where('status', 'active')
  125. ->first();
  126. $this->assertNotNull($activeSkill, '技能激活记录不存在');
  127. $this->assertEquals('自动施肥', $activeSkill->skill_name);
  128. DB::commit();
  129. echo "✓ 自动施肥技能激活测试通过\n";
  130. } catch (\Exception $e) {
  131. DB::rollBack();
  132. $this->fail('测试失败: ' . $e->getMessage());
  133. }
  134. }
  135. /**
  136. * 测试自动施肥处理逻辑
  137. */
  138. public function testAutoFertilizingProcess(): void
  139. {
  140. DB::beginTransaction();
  141. try {
  142. // 先激活技能
  143. $skill = PetSkill::where('skill_name', '自动施肥')->first();
  144. PetService::useSkill($this->testUserId, $this->testPetId, $skill->id, [
  145. 'duration' => 3600,
  146. 'auto_use_items' => true
  147. ]);
  148. // 获取激活的技能
  149. $activeSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $this->testPetId)
  150. ->where('skill_name', '自动施肥')
  151. ->where('status', 'active')
  152. ->first();
  153. $this->assertNotNull($activeSkill, '技能激活记录不存在');
  154. // 获取施肥前的作物状态
  155. $cropBefore = FarmCrop::where('land_id', $this->testLandId)->first();
  156. $this->assertFalse($cropBefore->fertilized, '作物应该未施肥');
  157. // 获取施肥前的肥料数量
  158. $fertilizerBefore = ItemService::getUserItems($this->testUserId, ['item_id' => $this->fertilizerItemId])
  159. ->sum('quantity');
  160. // 执行自动施肥处理
  161. $autoSkillLogic = new \App\Module\Pet\Logic\PetAutoSkillLogic();
  162. $autoSkillLogic->processAutoFertilizing($activeSkill);
  163. // 验证施肥结果
  164. $cropAfter = FarmCrop::where('land_id', $this->testLandId)->first();
  165. $this->assertTrue($cropAfter->fertilized, '作物应该已施肥');
  166. // 验证肥料消耗
  167. $fertilizerAfter = ItemService::getUserItems($this->testUserId, ['item_id' => $this->fertilizerItemId])
  168. ->sum('quantity');
  169. $this->assertEquals($fertilizerBefore - 1, $fertilizerAfter, '肥料应该消耗1个');
  170. DB::commit();
  171. echo "✓ 自动施肥处理逻辑测试通过\n";
  172. } catch (\Exception $e) {
  173. DB::rollBack();
  174. $this->fail('测试失败: ' . $e->getMessage());
  175. }
  176. }
  177. /**
  178. * 测试等级限制
  179. */
  180. public function testLevelRequirement(): void
  181. {
  182. DB::beginTransaction();
  183. try {
  184. // 创建低等级宠物
  185. $lowLevelPet = PetUser::create([
  186. 'user_id' => $this->testUserId,
  187. 'pet_config_id' => 1,
  188. 'name' => '低等级宠物',
  189. 'level' => 5, // 低于10级
  190. 'exp' => 100,
  191. 'stamina' => 100,
  192. 'max_stamina' => 100,
  193. 'status' => 1,
  194. 'grade' => 1,
  195. 'created_at' => now(),
  196. 'updated_at' => now()
  197. ]);
  198. // 获取自动施肥技能
  199. $skill = PetSkill::where('skill_name', '自动施肥')->first();
  200. // 尝试激活技能(应该失败)
  201. $result = PetService::useSkill($this->testUserId, $lowLevelPet->id, $skill->id, []);
  202. $this->assertFalse($result['success'], '低等级宠物不应该能激活自动施肥技能');
  203. DB::commit();
  204. echo "✓ 等级限制测试通过\n";
  205. } catch (\Exception $e) {
  206. DB::rollBack();
  207. $this->fail('测试失败: ' . $e->getMessage());
  208. }
  209. }
  210. /**
  211. * 清理测试数据
  212. */
  213. protected function tearDown(): void
  214. {
  215. // 清理测试数据
  216. DB::table('pet_users')->where('user_id', $this->testUserId)->delete();
  217. DB::table('farm_lands')->where('user_id', $this->testUserId)->delete();
  218. DB::table('farm_crops')->where('user_id', $this->testUserId)->delete();
  219. DB::table('pet_active_skills')->where('pet_id', $this->testPetId)->delete();
  220. DB::table('item_users')->where('user_id', $this->testUserId)->delete();
  221. parent::tearDown();
  222. }
  223. }