| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- namespace Tests\Unit\Farm;
- use Tests\TestCase;
- use App\Module\Farm\Validations\DisasterRemovalValidation;
- use App\Module\Farm\Validators\LandOwnershipValidator;
- use App\Module\Farm\Validators\DisasterRemovalItemValidator;
- use App\Module\Farm\Enums\DISASTER_TYPE;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\GameItems\Services\ItemService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use UCore\Exception\ValidateException;
- /**
- * 灾害去除验证测试
- */
- class DisasterRemovalValidationTest extends TestCase
- {
- use RefreshDatabase;
- /**
- * 测试验证规则
- */
- public function testValidationRules()
- {
- $validation = new DisasterRemovalValidation([]);
- $rules = $validation->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);
- }
- }
|