| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace Tests\Unit\GameItems;
- use App\Module\GameItems\Casts\NumericAttributesCast;
- use Tests\TestCase;
- /**
- * 物品数值属性Cast类测试
- */
- class NumericAttributesCastTest extends TestCase
- {
- /**
- * 测试神像种类属性
- */
- public function testGodTypeAttribute()
- {
- $cast = new NumericAttributesCast();
-
- // 测试默认值
- $this->assertEquals(0, $cast->god_type);
-
- // 测试设置值
- $cast->god_type = 1; // 丰收之神
- $this->assertEquals(1, $cast->god_type);
-
- $cast->god_type = 2; // 雨露之神
- $this->assertEquals(2, $cast->god_type);
-
- $cast->god_type = 3; // 屠草之神
- $this->assertEquals(3, $cast->god_type);
-
- $cast->god_type = 4; // 拭虫之神
- $this->assertEquals(4, $cast->god_type);
- }
-
- /**
- * 测试神像时间属性
- */
- public function testGodDurationAttribute()
- {
- $cast = new NumericAttributesCast();
-
- // 测试默认值
- $this->assertEquals(0, $cast->god_duration_seconds);
-
- // 测试设置值
- $cast->god_duration_seconds = 3600; // 1小时
- $this->assertEquals(3600, $cast->god_duration_seconds);
-
- $cast->god_duration_seconds = 86400; // 24小时
- $this->assertEquals(86400, $cast->god_duration_seconds);
- }
-
- /**
- * 测试神像物品识别逻辑
- */
- public function testGodItemIdentification()
- {
- $cast = new NumericAttributesCast();
-
- // 非神像物品
- $cast->god_duration_seconds = 0;
- $cast->god_type = 0;
- $this->assertFalse($this->isGodItem($cast));
-
- // 只有时间没有类型
- $cast->god_duration_seconds = 3600;
- $cast->god_type = 0;
- $this->assertFalse($this->isGodItem($cast));
-
- // 只有类型没有时间
- $cast->god_duration_seconds = 0;
- $cast->god_type = 1;
- $this->assertFalse($this->isGodItem($cast));
-
- // 完整的神像物品
- $cast->god_duration_seconds = 3600;
- $cast->god_type = 1;
- $this->assertTrue($this->isGodItem($cast));
- }
-
- /**
- * 测试JSON序列化和反序列化
- */
- public function testJsonSerialization()
- {
- $cast = new NumericAttributesCast();
- $cast->god_duration_seconds = 86400;
- $cast->god_type = 2;
- $cast->pet_power = 50;
-
- // 转换为数组
- $array = $cast->toArray();
-
- $this->assertEquals(86400, $array['god_duration_seconds']);
- $this->assertEquals(2, $array['god_type']);
- $this->assertEquals(50, $array['pet_power']);
-
- // 从数组创建新实例
- $newCast = new NumericAttributesCast();
- $newCast->fromArray($array);
-
- $this->assertEquals(86400, $newCast->god_duration_seconds);
- $this->assertEquals(2, $newCast->god_type);
- $this->assertEquals(50, $newCast->pet_power);
- }
-
- /**
- * 辅助方法:判断是否为神像物品
- */
- private function isGodItem(NumericAttributesCast $cast): bool
- {
- return $cast->god_duration_seconds > 0 && $cast->god_type > 0;
- }
- }
|