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()); } }