rules(); // 验证必填字段规则 $this->assertNotEmpty($rules); // 检查是否包含必填字段验证 $requiredRule = $rules[0]; $this->assertEquals('user_id,land_id,item_id,disaster_type', $requiredRule[0]); $this->assertEquals('required', $requiredRule[1]); } /** * 测试缺少必填字段 */ public function testMissingRequiredFields() { $this->expectException(ValidateException::class); $validation = new DisasterRemovalValidation([ 'user_id' => 1, // 缺少其他必填字段 ]); $validation->validated(); } /** * 测试字段类型验证 */ public function testFieldTypeValidation() { $this->expectException(ValidateException::class); $validation = new DisasterRemovalValidation([ 'user_id' => 'invalid', // 应该是整数 'land_id' => 1, 'item_id' => 1, 'disaster_type' => DISASTER_TYPE::PEST->value ]); $validation->validated(); } /** * 测试土地归属验证器 */ public function testLandOwnershipValidator() { // 创建测试用户和土地 $userId = 1; $landId = 1; // Mock FarmLand 模型 $this->mock(FarmLand::class, function ($mock) use ($userId, $landId) { $mock->shouldReceive('where') ->with('id', $landId) ->andReturnSelf(); $mock->shouldReceive('where') ->with('user_id', $userId) ->andReturnSelf(); $mock->shouldReceive('first') ->andReturn(new FarmLand()); // 返回土地实例表示验证通过 }); $validator = new LandOwnershipValidator(new DisasterRemovalValidation([]), ['user_id']); $result = $validator->validate($landId, ['user_id' => $userId]); $this->assertTrue($result); } /** * 测试土地归属验证失败 */ public function testLandOwnershipValidatorFail() { $userId = 1; $landId = 999; // 不存在的土地 // Mock FarmLand 模型 $this->mock(FarmLand::class, function ($mock) use ($userId, $landId) { $mock->shouldReceive('where') ->with('id', $landId) ->andReturnSelf(); $mock->shouldReceive('where') ->with('user_id', $userId) ->andReturnSelf(); $mock->shouldReceive('first') ->andReturn(null); // 返回null表示土地不存在 }); $validator = new LandOwnershipValidator(new DisasterRemovalValidation([]), ['user_id']); $result = $validator->validate($landId, ['user_id' => $userId]); $this->assertFalse($result); } /** * 测试灾害去除物品验证器 */ public function testDisasterRemovalItemValidator() { $userId = 1; $itemId = 123; $disasterType = DISASTER_TYPE::PEST->value; // Mock ItemService $this->mock(ItemService::class, function ($mock) use ($userId, $itemId) { $mock->shouldReceive('getUserItems') ->with($userId, ['item_id' => $itemId]) ->andReturn(collect([new \stdClass()])); // 用户拥有该物品 $mock->shouldReceive('getItemNumericAttribute') ->with($itemId, 'fram_pesticide_rate') ->andReturn(80); // 物品具有除虫属性 }); $validator = new DisasterRemovalItemValidator( new DisasterRemovalValidation([]), ['user_id', 'disaster_type'] ); $result = $validator->validate($itemId, [ 'user_id' => $userId, 'disaster_type' => $disasterType ]); $this->assertTrue($result); } /** * 测试物品验证失败 - 用户没有物品 */ public function testDisasterRemovalItemValidatorFailNoItem() { $userId = 1; $itemId = 123; $disasterType = DISASTER_TYPE::PEST->value; // Mock ItemService $this->mock(ItemService::class, function ($mock) use ($userId, $itemId) { $mock->shouldReceive('getUserItems') ->with($userId, ['item_id' => $itemId]) ->andReturn(collect([])); // 用户没有该物品 }); $validator = new DisasterRemovalItemValidator( new DisasterRemovalValidation([]), ['user_id', 'disaster_type'] ); $result = $validator->validate($itemId, [ 'user_id' => $userId, 'disaster_type' => $disasterType ]); $this->assertFalse($result); } /** * 测试物品验证失败 - 物品没有对应属性 */ public function testDisasterRemovalItemValidatorFailNoAttribute() { $userId = 1; $itemId = 123; $disasterType = DISASTER_TYPE::PEST->value; // Mock ItemService $this->mock(ItemService::class, function ($mock) use ($userId, $itemId) { $mock->shouldReceive('getUserItems') ->with($userId, ['item_id' => $itemId]) ->andReturn(collect([new \stdClass()])); // 用户拥有该物品 $mock->shouldReceive('getItemNumericAttribute') ->with($itemId, 'fram_pesticide_rate') ->andReturn(0); // 物品没有除虫属性 }); $validator = new DisasterRemovalItemValidator( new DisasterRemovalValidation([]), ['user_id', 'disaster_type'] ); $result = $validator->validate($itemId, [ 'user_id' => $userId, 'disaster_type' => $disasterType ]); $this->assertFalse($result); } /** * 测试不支持的灾害类型 */ public function testUnsupportedDisasterType() { $userId = 1; $itemId = 123; $disasterType = 999; // 不支持的灾害类型 $validator = new DisasterRemovalItemValidator( new DisasterRemovalValidation([]), ['user_id', 'disaster_type'] ); $result = $validator->validate($itemId, [ 'user_id' => $userId, 'disaster_type' => $disasterType ]); $this->assertFalse($result); } }