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(); } }