WateringResponseTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Module\Farm\Models\FarmCrop;
  4. use App\Module\Farm\Models\FarmLand;
  5. use App\Module\Farm\Services\CropService;
  6. use App\Module\GameItems\Services\ItemService;
  7. use App\Module\User\Models\User;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Support\Facades\Cache;
  10. use Tests\TestCase;
  11. /**
  12. * 浇水响应测试
  13. *
  14. * 测试浇水成功后Response的LastData中是否包含DataLands
  15. */
  16. class WateringResponseTest extends TestCase
  17. {
  18. use RefreshDatabase;
  19. /**
  20. * 测试浇水成功后LastData包含土地信息
  21. */
  22. public function testWateringSuccessIncludesLandData()
  23. {
  24. // 创建测试用户
  25. $user = User::factory()->create();
  26. // 创建测试土地
  27. $land = FarmLand::factory()->create([
  28. 'user_id' => $user->id,
  29. 'status' => 3, // DISASTER状态
  30. ]);
  31. // 创建测试作物(带有干旱灾害)
  32. $crop = FarmCrop::factory()->create([
  33. 'land_id' => $land->id,
  34. 'user_id' => $user->id,
  35. 'disasters' => [
  36. [
  37. 'type' => 1, // 干旱
  38. 'status' => 'active',
  39. 'start_time' => now()->timestamp,
  40. 'penalty' => 0.05
  41. ]
  42. ]
  43. ]);
  44. // 创建浇水道具
  45. $wateringItem = \App\Module\GameItems\Models\Item::factory()->create([
  46. 'fram_drought_rate' => 100 // 100%成功率
  47. ]);
  48. // 给用户添加浇水道具
  49. ItemService::addItem($user->id, $wateringItem->id, 1);
  50. // 清空缓存,确保测试环境干净
  51. Cache::flush();
  52. // 模拟浇水请求
  53. $response = $this->actingAs($user)
  54. ->postJson('/api/land/watering', [
  55. 'land_id' => $land->id,
  56. 'item_id' => $wateringItem->id
  57. ]);
  58. // 验证响应成功
  59. $response->assertStatus(200);
  60. // 验证响应中包含LastData
  61. $responseData = $response->json();
  62. $this->assertArrayHasKey('last_data', $responseData);
  63. // 验证LastData中包含lands信息
  64. $lastData = $responseData['last_data'];
  65. $this->assertArrayHasKey('lands', $lastData);
  66. $this->assertNotEmpty($lastData['lands']);
  67. // 验证土地信息正确
  68. $landData = collect($lastData['lands'])->firstWhere('id', $land->id);
  69. $this->assertNotNull($landData);
  70. // 验证灾害状态已更新
  71. $this->assertFalse($landData['need_watering'] ?? true);
  72. // 验证数据库中的灾害状态已更新
  73. $crop->refresh();
  74. $disasters = $crop->disasters ?? [];
  75. $droughtDisaster = collect($disasters)->firstWhere('type', 1);
  76. $this->assertEquals('cleared', $droughtDisaster['status'] ?? 'active');
  77. }
  78. /**
  79. * 测试浇水失败时的响应
  80. */
  81. public function testWateringFailureResponse()
  82. {
  83. // 创建测试用户
  84. $user = User::factory()->create();
  85. // 创建测试土地
  86. $land = FarmLand::factory()->create([
  87. 'user_id' => $user->id,
  88. 'status' => 3, // DISASTER状态
  89. ]);
  90. // 创建测试作物(带有干旱灾害)
  91. $crop = FarmCrop::factory()->create([
  92. 'land_id' => $land->id,
  93. 'user_id' => $user->id,
  94. 'disasters' => [
  95. [
  96. 'type' => 1, // 干旱
  97. 'status' => 'active',
  98. 'start_time' => now()->timestamp,
  99. 'penalty' => 0.05
  100. ]
  101. ]
  102. ]);
  103. // 创建浇水道具(低成功率)
  104. $wateringItem = \App\Module\GameItems\Models\Item::factory()->create([
  105. 'fram_drought_rate' => 0 // 0%成功率,确保失败
  106. ]);
  107. // 给用户添加浇水道具
  108. ItemService::addItem($user->id, $wateringItem->id, 1);
  109. // 清空缓存,确保测试环境干净
  110. Cache::flush();
  111. // 模拟浇水请求
  112. $response = $this->actingAs($user)
  113. ->postJson('/api/land/watering', [
  114. 'land_id' => $land->id,
  115. 'item_id' => $wateringItem->id
  116. ]);
  117. // 验证响应失败
  118. $response->assertStatus(400);
  119. // 验证错误消息
  120. $responseData = $response->json();
  121. $this->assertStringContains('浇水失败', $responseData['msg'] ?? '');
  122. }
  123. }