| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace Tests\Unit;
- use App\Module\AppGame\Proto\DisasterConverter;
- use PHPUnit\Framework\TestCase;
- use Uraus\Kku\Common\DataLand;
- use Uraus\Kku\Common\LandDisaster;
- /**
- * 灾害转换器测试
- */
- class DisasterConverterTest extends TestCase
- {
- /**
- * 测试将灾害数组转换为LandDisaster对象数组
- */
- public function testConvertToLandDisasters()
- {
- $disasters = [
- [
- 'type' => 1,
- 'status' => 'active'
- ],
- [
- 'type' => 2,
- 'status' => 'inactive'
- ],
- [
- 'type' => 3,
- 'status' => 'active'
- ]
- ];
- $landDisasters = DisasterConverter::convertToLandDisasters($disasters);
- $this->assertCount(3, $landDisasters);
-
- // 检查第一个灾害
- $this->assertInstanceOf(LandDisaster::class, $landDisasters[0]);
- $this->assertEquals(1, $landDisasters[0]->getType());
- $this->assertTrue($landDisasters[0]->getActive());
-
- // 检查第二个灾害
- $this->assertInstanceOf(LandDisaster::class, $landDisasters[1]);
- $this->assertEquals(2, $landDisasters[1]->getType());
- $this->assertFalse($landDisasters[1]->getActive());
-
- // 检查第三个灾害
- $this->assertInstanceOf(LandDisaster::class, $landDisasters[2]);
- $this->assertEquals(3, $landDisasters[2]->getType());
- $this->assertTrue($landDisasters[2]->getActive());
- }
- /**
- * 测试处理空灾害数组
- */
- public function testConvertEmptyDisasters()
- {
- $landDisasters = DisasterConverter::convertToLandDisasters([]);
- $this->assertEmpty($landDisasters);
- }
- /**
- * 测试处理无效灾害数据
- */
- public function testConvertInvalidDisasters()
- {
- $disasters = [
- ['invalid' => 'data'],
- ['type' => 1, 'status' => 'active'],
- ['status' => 'active'] // 缺少type
- ];
- $landDisasters = DisasterConverter::convertToLandDisasters($disasters);
-
- // 只有一个有效的灾害
- $this->assertCount(1, $landDisasters);
- $this->assertEquals(1, $landDisasters[0]->getType());
- }
- /**
- * 测试设置向后兼容标志
- */
- public function testSetCompatibilityFlags()
- {
- $dataLand = new DataLand();
-
- $disasters = [
- ['type' => 1, 'status' => 'active'], // 干旱
- ['type' => 2, 'status' => 'active'], // 虫害
- ['type' => 3, 'status' => 'active'], // 杂草
- ['type' => 1, 'status' => 'inactive'] // 已处理的干旱
- ];
- DisasterConverter::setCompatibilityFlags($dataLand, $disasters);
- $this->assertTrue($dataLand->getNeedWatering()); // 干旱
- $this->assertTrue($dataLand->getNeedPestControl()); // 虫害
- $this->assertTrue($dataLand->getNeedWeed()); // 杂草
- }
- /**
- * 测试完整的灾害处理流程
- */
- public function testProcessDisasters()
- {
- $dataLand = new DataLand();
-
- $disasters = [
- ['type' => 1, 'status' => 'active'],
- ['type' => 2, 'status' => 'inactive']
- ];
- DisasterConverter::processDisasters($dataLand, $disasters);
- // 检查disasters属性是否正确设置
- $landDisasters = $dataLand->getDisasters();
- $this->assertCount(2, $landDisasters);
-
- // 检查向后兼容标志
- $this->assertTrue($dataLand->getNeedWatering());
- $this->assertFalse($dataLand->getNeedPestControl()); // inactive状态不设置标志
- $this->assertFalse($dataLand->getNeedWeed());
- }
- /**
- * 测试处理空灾害数组
- */
- public function testProcessEmptyDisasters()
- {
- $dataLand = new DataLand();
-
- DisasterConverter::processDisasters($dataLand, []);
- // 检查disasters属性是否设置为空数组
- $landDisasters = $dataLand->getDisasters();
- $this->assertEmpty($landDisasters);
-
- // 检查向后兼容标志都为false
- $this->assertFalse($dataLand->getNeedWatering());
- $this->assertFalse($dataLand->getNeedPestControl());
- $this->assertFalse($dataLand->getNeedWeed());
- }
- }
|