| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace Tests\Unit;
- use App\Module\Farm\Enums\BUFF_TYPE;
- use App\Module\Farm\Services\BuffService;
- use App\Module\Farm\Validations\GodActivationValidation;
- use App\Module\GameItems\Services\ItemService;
- use Tests\TestCase;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- /**
- * 神像激活功能测试
- */
- class GodActivationTest extends TestCase
- {
- use RefreshDatabase;
- /**
- * 测试神像时间属性
- */
- public function testGodDurationAttribute()
- {
- // 创建一个测试物品ID
- $itemId = 3001; // 假设这是丰收之神物品
-
- // 测试获取神像时间属性
- $godDuration = ItemService::getItemNumericAttribute($itemId, 'god_duration_seconds');
-
- // 如果物品不存在,应该返回默认值0
- $this->assertIsInt($godDuration);
- $this->assertGreaterThanOrEqual(0, $godDuration);
- }
- /**
- * 测试神像激活验证
- */
- public function testGodActivationValidation()
- {
- $userId = 1;
- $godId = BUFF_TYPE::HARVEST_GOD->value;
- $itemId = 3001;
- // 创建验证对象
- $validation = new GodActivationValidation([
- 'user_id' => $userId,
- 'god_id' => $godId,
- 'item_id' => $itemId
- ]);
- // 验证规则是否正确设置
- $rules = $validation->rules();
- $this->assertIsArray($rules);
- $this->assertNotEmpty($rules);
- }
- /**
- * 测试神像类型枚举
- */
- public function testBuffTypeEnum()
- {
- // 测试所有神像类型
- $this->assertEquals(1, BUFF_TYPE::HARVEST_GOD->value);
- $this->assertEquals(2, BUFF_TYPE::RAIN_GOD->value);
- $this->assertEquals(3, BUFF_TYPE::WEED_KILLER_GOD->value);
- $this->assertEquals(4, BUFF_TYPE::PEST_CLEANER_GOD->value);
- // 测试名称获取
- $this->assertEquals('丰收之神', BUFF_TYPE::getName(1));
- $this->assertEquals('雨露之神', BUFF_TYPE::getName(2));
- $this->assertEquals('屠草之神', BUFF_TYPE::getName(3));
- $this->assertEquals('拭虫之神', BUFF_TYPE::getName(4));
- // 测试效果描述
- $this->assertEquals('确保收获时获得最高产量', BUFF_TYPE::getDescription(1));
- $this->assertEquals('防止干旱灾害', BUFF_TYPE::getDescription(2));
- $this->assertEquals('防止杂草灾害', BUFF_TYPE::getDescription(3));
- $this->assertEquals('防止虫害灾害', BUFF_TYPE::getDescription(4));
- }
- /**
- * 测试神像物品ID映射
- */
- public function testGodItemIdMapping()
- {
- // 测试神像物品ID映射关系
- $godTypes = [
- BUFF_TYPE::HARVEST_GOD->value => 3001,
- BUFF_TYPE::RAIN_GOD->value => 3002,
- BUFF_TYPE::WEED_KILLER_GOD->value => 3003,
- BUFF_TYPE::PEST_CLEANER_GOD->value => 3004,
- ];
- foreach ($godTypes as $godId => $expectedItemId) {
- $calculatedItemId = 3000 + $godId;
- $this->assertEquals($expectedItemId, $calculatedItemId, "神像ID {$godId} 对应的物品ID应该是 {$expectedItemId}");
- }
- }
- /**
- * 测试时间转换逻辑
- */
- public function testTimeConversion()
- {
- // 测试秒转小时的逻辑
- $testCases = [
- 3600 => 1, // 1小时
- 7200 => 2, // 2小时
- 86400 => 24, // 24小时
- 90000 => 25, // 25小时(向上取整)
- 3599 => 1, // 不足1小时,向上取整为1小时
- 0 => 24, // 0秒,使用默认24小时
- ];
- foreach ($testCases as $seconds => $expectedHours) {
- $actualHours = $seconds > 0 ? ceil($seconds / 3600) : 24;
- $this->assertEquals($expectedHours, $actualHours, "秒数 {$seconds} 应该转换为 {$expectedHours} 小时");
- }
- }
- /**
- * 测试数值属性Cast类
- */
- public function testNumericAttributesCast()
- {
- // 创建NumericAttributesCast实例
- $cast = new \App\Module\GameItems\Casts\NumericAttributesCast();
-
- // 验证新增的神像时间属性
- $this->assertObjectHasProperty('god_duration_seconds', $cast);
- $this->assertEquals(0, $cast->god_duration_seconds);
- }
- /**
- * 测试验证器错误消息
- */
- public function testValidatorErrorMessages()
- {
- // 测试无效的神像类型
- $invalidGodIds = [0, -1, 5, 999];
-
- foreach ($invalidGodIds as $invalidGodId) {
- $validation = new GodActivationValidation([
- 'user_id' => 1,
- 'god_id' => $invalidGodId,
- 'item_id' => 3001
- ]);
-
- // 验证应该失败
- try {
- $validation->validated();
- $this->fail("神像ID {$invalidGodId} 应该验证失败");
- } catch (\Exception $e) {
- $this->assertStringContainsString('验证失败', $e->getMessage());
- }
- }
- }
- /**
- * 测试物品查找逻辑
- */
- public function testFindGodItemLogic()
- {
- // 这个测试需要实际的数据库数据,这里只测试逻辑结构
- $this->assertTrue(method_exists(\App\Module\GameItems\Services\ItemService::class, 'getUserItems'));
- $this->assertTrue(method_exists(\App\Module\GameItems\Services\ItemService::class, 'getItemNumericAttribute'));
- }
- }
|