DisasterConverterTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Module\AppGame\Proto\DisasterConverter;
  4. use PHPUnit\Framework\TestCase;
  5. use Uraus\Kku\Common\DataLand;
  6. use Uraus\Kku\Common\LandDisaster;
  7. /**
  8. * 灾害转换器测试
  9. */
  10. class DisasterConverterTest extends TestCase
  11. {
  12. /**
  13. * 测试将灾害数组转换为LandDisaster对象数组
  14. */
  15. public function testConvertToLandDisasters()
  16. {
  17. $disasters = [
  18. [
  19. 'type' => 1,
  20. 'status' => 'active'
  21. ],
  22. [
  23. 'type' => 2,
  24. 'status' => 'inactive'
  25. ],
  26. [
  27. 'type' => 3,
  28. 'status' => 'active'
  29. ]
  30. ];
  31. $landDisasters = DisasterConverter::convertToLandDisasters($disasters);
  32. $this->assertCount(3, $landDisasters);
  33. // 检查第一个灾害
  34. $this->assertInstanceOf(LandDisaster::class, $landDisasters[0]);
  35. $this->assertEquals(1, $landDisasters[0]->getType());
  36. $this->assertTrue($landDisasters[0]->getActive());
  37. // 检查第二个灾害
  38. $this->assertInstanceOf(LandDisaster::class, $landDisasters[1]);
  39. $this->assertEquals(2, $landDisasters[1]->getType());
  40. $this->assertFalse($landDisasters[1]->getActive());
  41. // 检查第三个灾害
  42. $this->assertInstanceOf(LandDisaster::class, $landDisasters[2]);
  43. $this->assertEquals(3, $landDisasters[2]->getType());
  44. $this->assertTrue($landDisasters[2]->getActive());
  45. }
  46. /**
  47. * 测试处理空灾害数组
  48. */
  49. public function testConvertEmptyDisasters()
  50. {
  51. $landDisasters = DisasterConverter::convertToLandDisasters([]);
  52. $this->assertEmpty($landDisasters);
  53. }
  54. /**
  55. * 测试处理无效灾害数据
  56. */
  57. public function testConvertInvalidDisasters()
  58. {
  59. $disasters = [
  60. ['invalid' => 'data'],
  61. ['type' => 1, 'status' => 'active'],
  62. ['status' => 'active'] // 缺少type
  63. ];
  64. $landDisasters = DisasterConverter::convertToLandDisasters($disasters);
  65. // 只有一个有效的灾害
  66. $this->assertCount(1, $landDisasters);
  67. $this->assertEquals(1, $landDisasters[0]->getType());
  68. }
  69. /**
  70. * 测试设置向后兼容标志
  71. */
  72. public function testSetCompatibilityFlags()
  73. {
  74. $dataLand = new DataLand();
  75. $disasters = [
  76. ['type' => 1, 'status' => 'active'], // 干旱
  77. ['type' => 2, 'status' => 'active'], // 虫害
  78. ['type' => 3, 'status' => 'active'], // 杂草
  79. ['type' => 1, 'status' => 'inactive'] // 已处理的干旱
  80. ];
  81. DisasterConverter::setCompatibilityFlags($dataLand, $disasters);
  82. $this->assertTrue($dataLand->getNeedWatering()); // 干旱
  83. $this->assertTrue($dataLand->getNeedPestControl()); // 虫害
  84. $this->assertTrue($dataLand->getNeedWeed()); // 杂草
  85. }
  86. /**
  87. * 测试完整的灾害处理流程
  88. */
  89. public function testProcessDisasters()
  90. {
  91. $dataLand = new DataLand();
  92. $disasters = [
  93. ['type' => 1, 'status' => 'active'],
  94. ['type' => 2, 'status' => 'inactive']
  95. ];
  96. DisasterConverter::processDisasters($dataLand, $disasters);
  97. // 检查disasters属性是否正确设置
  98. $landDisasters = $dataLand->getDisasters();
  99. $this->assertCount(2, $landDisasters);
  100. // 检查向后兼容标志
  101. $this->assertTrue($dataLand->getNeedWatering());
  102. $this->assertFalse($dataLand->getNeedPestControl()); // inactive状态不设置标志
  103. $this->assertFalse($dataLand->getNeedWeed());
  104. }
  105. /**
  106. * 测试处理空灾害数组
  107. */
  108. public function testProcessEmptyDisasters()
  109. {
  110. $dataLand = new DataLand();
  111. DisasterConverter::processDisasters($dataLand, []);
  112. // 检查disasters属性是否设置为空数组
  113. $landDisasters = $dataLand->getDisasters();
  114. $this->assertEmpty($landDisasters);
  115. // 检查向后兼容标志都为false
  116. $this->assertFalse($dataLand->getNeedWatering());
  117. $this->assertFalse($dataLand->getNeedPestControl());
  118. $this->assertFalse($dataLand->getNeedWeed());
  119. }
  120. }