HouseOutputBonusTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Module\Farm\Models\FarmHouseConfig;
  4. use PHPUnit\Framework\TestCase;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. /**
  7. * 房屋产出加成测试
  8. *
  9. * 验证房屋等级产出加成的数据一致性和计算正确性
  10. */
  11. class HouseOutputBonusTest extends TestCase
  12. {
  13. use RefreshDatabase;
  14. /**
  15. * 测试房屋配置数据的正确性
  16. */
  17. public function testHouseConfigDataConsistency()
  18. {
  19. // 期望的房屋等级产出加成配置(百分比值)
  20. $expectedConfigs = [
  21. 1 => 0, // 0%
  22. 2 => 5, // 5%
  23. 3 => 10, // 10%
  24. 4 => 15, // 15%
  25. 5 => 20, // 20%
  26. 6 => 25, // 25%
  27. 7 => 30, // 30%
  28. 8 => 35, // 35%
  29. 9 => 40, // 40%
  30. 10 => 45, // 45%
  31. 11 => 50, // 50%
  32. 12 => 60, // 60%
  33. ];
  34. foreach ($expectedConfigs as $level => $expectedBonus) {
  35. $config = FarmHouseConfig::where('level', $level)->first();
  36. $this->assertNotNull($config, "房屋等级 {$level} 的配置不存在");
  37. $this->assertEquals(
  38. $expectedBonus,
  39. (float)$config->output_bonus,
  40. "房屋等级 {$level} 的产出加成应该是 {$expectedBonus},实际是 {$config->output_bonus}"
  41. );
  42. }
  43. }
  44. /**
  45. * 测试产出加成计算逻辑
  46. */
  47. public function testOutputBonusCalculation()
  48. {
  49. // 测试用例:基础产量1000,不同房屋等级的最终产量
  50. $baseOutput = 1000;
  51. $testCases = [
  52. 1 => 1000, // 1000 * (1 + 0.00) = 1000
  53. 2 => 1050, // 1000 * (1 + 0.05) = 1050
  54. 3 => 1100, // 1000 * (1 + 0.10) = 1100
  55. 4 => 1150, // 1000 * (1 + 0.15) = 1150
  56. 5 => 1200, // 1000 * (1 + 0.20) = 1200
  57. 6 => 1250, // 1000 * (1 + 0.25) = 1250
  58. 7 => 1300, // 1000 * (1 + 0.30) = 1300
  59. 8 => 1350, // 1000 * (1 + 0.35) = 1350
  60. 9 => 1400, // 1000 * (1 + 0.40) = 1400
  61. 10 => 1450, // 1000 * (1 + 0.45) = 1450
  62. 11 => 1500, // 1000 * (1 + 0.50) = 1500
  63. 12 => 1600, // 1000 * (1 + 0.60) = 1600
  64. ];
  65. foreach ($testCases as $level => $expectedOutput) {
  66. $config = FarmHouseConfig::where('level', $level)->first();
  67. $actualOutput = (int)($baseOutput * (1 + ($config->output_bonus / 100))); // 将百分比值转换为小数
  68. $this->assertEquals(
  69. $expectedOutput,
  70. $actualOutput,
  71. "房屋等级 {$level},基础产量 {$baseOutput},期望最终产量 {$expectedOutput},实际计算结果 {$actualOutput}"
  72. );
  73. }
  74. }
  75. /**
  76. * 测试百分比显示格式
  77. */
  78. public function testPercentageDisplayFormat()
  79. {
  80. $testCases = [
  81. 1 => '0%',
  82. 2 => '5%',
  83. 3 => '10%',
  84. 4 => '15%',
  85. 5 => '20%',
  86. 6 => '25%',
  87. 7 => '30%',
  88. 8 => '35%',
  89. 9 => '40%',
  90. 10 => '45%',
  91. 11 => '50%',
  92. 12 => '60%',
  93. ];
  94. foreach ($testCases as $level => $expectedPercentage) {
  95. $config = FarmHouseConfig::where('level', $level)->first();
  96. $actualPercentage = round($config->output_bonus) . '%'; // 直接使用百分比值
  97. $this->assertEquals(
  98. $expectedPercentage,
  99. $actualPercentage,
  100. "房屋等级 {$level} 的百分比显示应该是 {$expectedPercentage},实际是 {$actualPercentage}"
  101. );
  102. }
  103. }
  104. /**
  105. * 测试复合加成计算(土地+房屋+灾害)
  106. */
  107. public function testComplexBonusCalculation()
  108. {
  109. $baseOutput = 1000;
  110. $landBonus = 0.25; // 土地25%加成
  111. $houseLevel = 7; // 房屋7级30%加成
  112. $disasterPenalty = 0.10; // 灾害10%减产
  113. $config = FarmHouseConfig::where('level', $houseLevel)->first();
  114. $houseBonus = $config->output_bonus / 100; // 将百分比值转换为小数
  115. // 计算最终产量:基础产量 × (1 + 土地加成) × (1 + 房屋加成) × (1 - 灾害减产)
  116. $expectedOutput = (int)($baseOutput * (1 + $landBonus) * (1 + $houseBonus) * (1 - $disasterPenalty));
  117. // 手动计算验证:1000 * 1.25 * 1.30 * 0.90 = 1462.5 -> 1462
  118. $manualCalculation = (int)(1000 * 1.25 * 1.30 * 0.90);
  119. $this->assertEquals(1462, $manualCalculation, "手动计算结果应该是1462");
  120. $this->assertEquals($manualCalculation, $expectedOutput, "复合加成计算结果不正确");
  121. }
  122. /**
  123. * 测试边界情况
  124. */
  125. public function testEdgeCases()
  126. {
  127. // 测试最低等级(无加成)
  128. $level1Config = FarmHouseConfig::where('level', 1)->first();
  129. $this->assertEquals(0.00, (float)$level1Config->output_bonus, "等级1应该没有产出加成");
  130. // 测试最高等级
  131. $level12Config = FarmHouseConfig::where('level', 12)->first();
  132. $this->assertEquals(0.60, (float)$level12Config->output_bonus, "等级12应该有60%产出加成");
  133. // 测试不存在的等级
  134. $nonExistentConfig = FarmHouseConfig::where('level', 99)->first();
  135. $this->assertNull($nonExistentConfig, "不存在的等级应该返回null");
  136. }
  137. }