| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace Tests\Feature\Farm;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\Farm\Enums\LAND_STATUS;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmSeed;
- use App\Module\Farm\Services\CropService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\DB;
- use Tests\TestCase;
- /**
- * 农场作物软删除功能测试
- */
- class CropSoftDeleteTest extends TestCase
- {
- /**
- * 测试作物软删除功能
- */
- public function test_crop_soft_delete()
- {
- // 准备测试数据
- $userId = 39077;
- $landId = 296;
- $itemId = 1;
- // 清理可能存在的作物记录
- FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
- // 确保土地状态为空闲
- $land = FarmLand::find($landId);
- $this->assertNotNull($land, '土地不存在');
-
- $land->status = LAND_STATUS::IDLE->value;
- $land->has_crop = false;
- $land->save();
- // 确保种子配置存在
- $seed = FarmSeed::where('item_id', $itemId)->first();
- $this->assertNotNull($seed, '种子配置不存在');
- DB::beginTransaction();
- try {
- // 1. 种植作物
- $plantResult = CropService::plantCrop($userId, $landId, $itemId);
- $this->assertNotNull($plantResult, '种植应该成功');
- $this->assertArrayHasKey('crop', $plantResult);
-
- $cropId = $plantResult['crop']->id;
- // 2. 验证作物存在
- $crop = FarmCrop::find($cropId);
- $this->assertNotNull($crop, '作物应该存在');
- $this->assertNull($crop->deleted_at, '作物不应该被软删除');
- // 3. 铲除作物(软删除)
- $removeResult = CropService::removeCrop($userId, $landId);
- $this->assertTrue($removeResult['success'], '铲除应该成功');
- // 4. 验证作物被软删除
- $crop->refresh();
- $this->assertNotNull($crop->deleted_at, '作物应该被软删除');
- // 5. 验证正常查询不会返回软删除的作物
- $activeCrop = FarmCrop::where('land_id', $landId)->first();
- $this->assertNull($activeCrop, '正常查询不应该返回软删除的作物');
- // 6. 验证可以查询到软删除的作物
- $trashedCrop = FarmCrop::onlyTrashed()->where('land_id', $landId)->first();
- $this->assertNotNull($trashedCrop, '应该能查询到软删除的作物');
- $this->assertEquals($cropId, $trashedCrop->id);
- // 7. 验证土地状态重置为空闲
- $land->refresh();
- $this->assertEquals(LAND_STATUS::IDLE->value, $land->status);
- $this->assertFalse($land->has_crop);
- // 8. 验证可以在同一块土地上重新种植
- $newPlantResult = CropService::plantCrop($userId, $landId, $itemId);
- $this->assertNotNull($newPlantResult, '应该可以重新种植');
- $this->assertArrayHasKey('crop', $newPlantResult);
- $this->assertNotEquals($cropId, $newPlantResult['crop']->id, '应该是新的作物记录');
- DB::rollBack();
- } catch (\Exception $e) {
- DB::rollBack();
- $this->fail('软删除测试失败: ' . $e->getMessage());
- }
- // 清理测试数据
- FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
- }
- /**
- * 测试恢复软删除的作物
- */
- public function test_restore_soft_deleted_crop()
- {
- // 准备测试数据
- $userId = 39077;
- $landId = 296;
- $itemId = 1;
- // 清理可能存在的作物记录
- FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
- // 确保土地状态为空闲
- $land = FarmLand::find($landId);
- $land->status = LAND_STATUS::IDLE->value;
- $land->has_crop = false;
- $land->save();
- // 确保种子配置存在
- $seed = FarmSeed::where('item_id', $itemId)->first();
- $this->assertNotNull($seed, '种子配置不存在');
- DB::beginTransaction();
- try {
- // 1. 种植并铲除作物
- $plantResult = CropService::plantCrop($userId, $landId, $itemId);
- $cropId = $plantResult['crop']->id;
-
- CropService::removeCrop($userId, $landId);
- // 2. 验证作物被软删除
- $trashedCrop = FarmCrop::onlyTrashed()->where('land_id', $landId)->first();
- $this->assertNotNull($trashedCrop, '作物应该被软删除');
- // 3. 恢复作物
- $restoreResult = CropService::restoreCrop($userId, $landId);
- $this->assertTrue($restoreResult['success'], '恢复应该成功');
- // 4. 验证作物被恢复
- $restoredCrop = FarmCrop::find($cropId);
- $this->assertNotNull($restoredCrop, '作物应该被恢复');
- $this->assertNull($restoredCrop->deleted_at, '作物不应该处于软删除状态');
- // 5. 验证土地状态更新
- $land->refresh();
- $this->assertEquals(LAND_STATUS::PLANTING->value, $land->status);
- $this->assertTrue($land->has_crop);
- DB::rollBack();
- } catch (\Exception $e) {
- DB::rollBack();
- $this->fail('恢复软删除作物测试失败: ' . $e->getMessage());
- }
- // 清理测试数据
- FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
- }
- /**
- * 测试强制删除作物
- */
- public function test_force_delete_crop()
- {
- // 准备测试数据
- $userId = 39077;
- $landId = 296;
- $itemId = 1;
- // 清理可能存在的作物记录
- FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
- // 确保土地状态为空闲
- $land = FarmLand::find($landId);
- $land->status = LAND_STATUS::IDLE->value;
- $land->has_crop = false;
- $land->save();
- DB::beginTransaction();
- try {
- // 1. 种植并铲除作物(软删除)
- $plantResult = CropService::plantCrop($userId, $landId, $itemId);
- $cropId = $plantResult['crop']->id;
-
- CropService::removeCrop($userId, $landId);
- // 2. 验证作物被软删除
- $trashedCrop = FarmCrop::onlyTrashed()->where('land_id', $landId)->first();
- $this->assertNotNull($trashedCrop, '作物应该被软删除');
- // 3. 强制删除作物
- $forceDeleteResult = CropService::forceDeleteCrop($userId, $landId, '测试强制删除');
- $this->assertTrue($forceDeleteResult['success'], '强制删除应该成功');
- // 4. 验证作物被物理删除
- $deletedCrop = FarmCrop::withTrashed()->find($cropId);
- $this->assertNull($deletedCrop, '作物应该被物理删除');
- // 5. 验证无法查询到任何记录
- $anyCrop = FarmCrop::withTrashed()->where('land_id', $landId)->first();
- $this->assertNull($anyCrop, '不应该查询到任何作物记录');
- DB::rollBack();
- } catch (\Exception $e) {
- DB::rollBack();
- $this->fail('强制删除作物测试失败: ' . $e->getMessage());
- }
- // 清理测试数据
- FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
- }
- }
|