CropSoftDeleteTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Tests\Feature\Farm;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\Farm\Enums\LAND_STATUS;
  5. use App\Module\Farm\Models\FarmCrop;
  6. use App\Module\Farm\Models\FarmLand;
  7. use App\Module\Farm\Models\FarmSeed;
  8. use App\Module\Farm\Services\CropService;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Support\Facades\DB;
  11. use Tests\TestCase;
  12. /**
  13. * 农场作物软删除功能测试
  14. */
  15. class CropSoftDeleteTest extends TestCase
  16. {
  17. /**
  18. * 测试作物软删除功能
  19. */
  20. public function test_crop_soft_delete()
  21. {
  22. // 准备测试数据
  23. $userId = 39077;
  24. $landId = 296;
  25. $itemId = 1;
  26. // 清理可能存在的作物记录
  27. FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
  28. // 确保土地状态为空闲
  29. $land = FarmLand::find($landId);
  30. $this->assertNotNull($land, '土地不存在');
  31. $land->status = LAND_STATUS::IDLE->value;
  32. $land->has_crop = false;
  33. $land->save();
  34. // 确保种子配置存在
  35. $seed = FarmSeed::where('item_id', $itemId)->first();
  36. $this->assertNotNull($seed, '种子配置不存在');
  37. DB::beginTransaction();
  38. try {
  39. // 1. 种植作物
  40. $plantResult = CropService::plantCrop($userId, $landId, $itemId);
  41. $this->assertNotNull($plantResult, '种植应该成功');
  42. $this->assertArrayHasKey('crop', $plantResult);
  43. $cropId = $plantResult['crop']->id;
  44. // 2. 验证作物存在
  45. $crop = FarmCrop::find($cropId);
  46. $this->assertNotNull($crop, '作物应该存在');
  47. $this->assertNull($crop->deleted_at, '作物不应该被软删除');
  48. // 3. 铲除作物(软删除)
  49. $removeResult = CropService::removeCrop($userId, $landId);
  50. $this->assertTrue($removeResult['success'], '铲除应该成功');
  51. // 4. 验证作物被软删除
  52. $crop->refresh();
  53. $this->assertNotNull($crop->deleted_at, '作物应该被软删除');
  54. // 5. 验证正常查询不会返回软删除的作物
  55. $activeCrop = FarmCrop::where('land_id', $landId)->first();
  56. $this->assertNull($activeCrop, '正常查询不应该返回软删除的作物');
  57. // 6. 验证可以查询到软删除的作物
  58. $trashedCrop = FarmCrop::onlyTrashed()->where('land_id', $landId)->first();
  59. $this->assertNotNull($trashedCrop, '应该能查询到软删除的作物');
  60. $this->assertEquals($cropId, $trashedCrop->id);
  61. // 7. 验证土地状态重置为空闲
  62. $land->refresh();
  63. $this->assertEquals(LAND_STATUS::IDLE->value, $land->status);
  64. $this->assertFalse($land->has_crop);
  65. // 8. 验证可以在同一块土地上重新种植
  66. $newPlantResult = CropService::plantCrop($userId, $landId, $itemId);
  67. $this->assertNotNull($newPlantResult, '应该可以重新种植');
  68. $this->assertArrayHasKey('crop', $newPlantResult);
  69. $this->assertNotEquals($cropId, $newPlantResult['crop']->id, '应该是新的作物记录');
  70. DB::rollBack();
  71. } catch (\Exception $e) {
  72. DB::rollBack();
  73. $this->fail('软删除测试失败: ' . $e->getMessage());
  74. }
  75. // 清理测试数据
  76. FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
  77. }
  78. /**
  79. * 测试恢复软删除的作物
  80. */
  81. public function test_restore_soft_deleted_crop()
  82. {
  83. // 准备测试数据
  84. $userId = 39077;
  85. $landId = 296;
  86. $itemId = 1;
  87. // 清理可能存在的作物记录
  88. FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
  89. // 确保土地状态为空闲
  90. $land = FarmLand::find($landId);
  91. $land->status = LAND_STATUS::IDLE->value;
  92. $land->has_crop = false;
  93. $land->save();
  94. // 确保种子配置存在
  95. $seed = FarmSeed::where('item_id', $itemId)->first();
  96. $this->assertNotNull($seed, '种子配置不存在');
  97. DB::beginTransaction();
  98. try {
  99. // 1. 种植并铲除作物
  100. $plantResult = CropService::plantCrop($userId, $landId, $itemId);
  101. $cropId = $plantResult['crop']->id;
  102. CropService::removeCrop($userId, $landId);
  103. // 2. 验证作物被软删除
  104. $trashedCrop = FarmCrop::onlyTrashed()->where('land_id', $landId)->first();
  105. $this->assertNotNull($trashedCrop, '作物应该被软删除');
  106. // 3. 恢复作物
  107. $restoreResult = CropService::restoreCrop($userId, $landId);
  108. $this->assertTrue($restoreResult['success'], '恢复应该成功');
  109. // 4. 验证作物被恢复
  110. $restoredCrop = FarmCrop::find($cropId);
  111. $this->assertNotNull($restoredCrop, '作物应该被恢复');
  112. $this->assertNull($restoredCrop->deleted_at, '作物不应该处于软删除状态');
  113. // 5. 验证土地状态更新
  114. $land->refresh();
  115. $this->assertEquals(LAND_STATUS::PLANTING->value, $land->status);
  116. $this->assertTrue($land->has_crop);
  117. DB::rollBack();
  118. } catch (\Exception $e) {
  119. DB::rollBack();
  120. $this->fail('恢复软删除作物测试失败: ' . $e->getMessage());
  121. }
  122. // 清理测试数据
  123. FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
  124. }
  125. /**
  126. * 测试强制删除作物
  127. */
  128. public function test_force_delete_crop()
  129. {
  130. // 准备测试数据
  131. $userId = 39077;
  132. $landId = 296;
  133. $itemId = 1;
  134. // 清理可能存在的作物记录
  135. FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
  136. // 确保土地状态为空闲
  137. $land = FarmLand::find($landId);
  138. $land->status = LAND_STATUS::IDLE->value;
  139. $land->has_crop = false;
  140. $land->save();
  141. DB::beginTransaction();
  142. try {
  143. // 1. 种植并铲除作物(软删除)
  144. $plantResult = CropService::plantCrop($userId, $landId, $itemId);
  145. $cropId = $plantResult['crop']->id;
  146. CropService::removeCrop($userId, $landId);
  147. // 2. 验证作物被软删除
  148. $trashedCrop = FarmCrop::onlyTrashed()->where('land_id', $landId)->first();
  149. $this->assertNotNull($trashedCrop, '作物应该被软删除');
  150. // 3. 强制删除作物
  151. $forceDeleteResult = CropService::forceDeleteCrop($userId, $landId, '测试强制删除');
  152. $this->assertTrue($forceDeleteResult['success'], '强制删除应该成功');
  153. // 4. 验证作物被物理删除
  154. $deletedCrop = FarmCrop::withTrashed()->find($cropId);
  155. $this->assertNull($deletedCrop, '作物应该被物理删除');
  156. // 5. 验证无法查询到任何记录
  157. $anyCrop = FarmCrop::withTrashed()->where('land_id', $landId)->first();
  158. $this->assertNull($anyCrop, '不应该查询到任何作物记录');
  159. DB::rollBack();
  160. } catch (\Exception $e) {
  161. DB::rollBack();
  162. $this->fail('强制删除作物测试失败: ' . $e->getMessage());
  163. }
  164. // 清理测试数据
  165. FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
  166. }
  167. }