PesticideHandlerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Module\AppGame\Tests\Land;
  3. use App\Module\AppGame\Tests\TestConfig;
  4. use Google\Protobuf\Internal\Message;
  5. use Uraus\Kku\Request;
  6. use Uraus\Kku\Request\RequestLandPesticide;
  7. /**
  8. * 除虫Handler E2E测试
  9. *
  10. * 测试除虫功能的各种场景,包括成功、失败和边界情况
  11. */
  12. class PesticideHandlerTest extends DisasterRemovalBaseTest
  13. {
  14. /**
  15. * 当前测试配置
  16. */
  17. private array $currentTestConfig;
  18. /**
  19. * 测试除虫成功场景
  20. */
  21. public function testPesticideSuccess()
  22. {
  23. $this->dumpTestStart('除虫成功测试');
  24. $config = TestConfig::getPesticideTestConfig();
  25. $this->dumpTestConfig($config);
  26. $this->currentTestConfig = $config;
  27. $response = $this->protobufRequest();
  28. $this->assertSuccessResponse($response, '除虫成功');
  29. $this->dumpTestEnd('除虫成功测试');
  30. }
  31. /**
  32. * 测试使用无效物品除虫
  33. */
  34. public function testPesticideWithInvalidItem()
  35. {
  36. $this->dumpTestStart('无效物品除虫测试');
  37. $config = TestConfig::getPesticideTestConfig();
  38. $config['item_id'] = TestConfig::getTestItem('invalid_item')['item_id'];
  39. $this->dumpTestConfig($config);
  40. $this->currentTestConfig = $config;
  41. $response = $this->protobufRequest();
  42. $this->assertValidationFailureResponse($response, '不是除虫物品');
  43. $this->dumpTestEnd('无效物品除虫测试');
  44. }
  45. /**
  46. * 测试对其他用户土地除虫
  47. */
  48. public function testPesticideOnOtherUserLand()
  49. {
  50. $this->dumpTestStart('其他用户土地除虫测试');
  51. $config = TestConfig::getPesticideTestConfig();
  52. $config['land_id'] = TestConfig::getTestLand('other_user_land')['land_id'];
  53. $this->dumpTestConfig($config);
  54. $this->currentTestConfig = $config;
  55. $response = $this->protobufRequest();
  56. $this->assertValidationFailureResponse($response, '土地不存在或不属于当前用户');
  57. $this->dumpTestEnd('其他用户土地除虫测试');
  58. }
  59. /**
  60. * 测试对无虫害土地除虫
  61. */
  62. public function testPesticideOnNormalLand()
  63. {
  64. $this->dumpTestStart('无虫害土地除虫测试');
  65. $config = TestConfig::getPesticideTestConfig();
  66. $config['land_id'] = TestConfig::getTestLand('normal_land')['land_id'];
  67. $this->dumpTestConfig($config);
  68. $this->currentTestConfig = $config;
  69. $response = $this->protobufRequest();
  70. $this->assertFailureResponse($response, '灾害清理失败');
  71. $this->dumpTestEnd('无虫害土地除虫测试');
  72. }
  73. /**
  74. * 测试参数验证 - 缺少必填字段
  75. */
  76. public function testPesticideWithMissingFields()
  77. {
  78. $this->dumpTestStart('缺少必填字段测试');
  79. $config = TestConfig::getPesticideTestConfig();
  80. $config['land_id'] = 0; // 无效的土地ID
  81. $this->dumpTestConfig($config);
  82. $this->currentTestConfig = $config;
  83. $response = $this->protobufRequest();
  84. $this->assertValidationFailureResponse($response);
  85. $this->dumpTestEnd('缺少必填字段测试');
  86. }
  87. /**
  88. * 测试概率机制 - 多次测试验证概率
  89. */
  90. public function testPesticideProbability()
  91. {
  92. $this->dumpTestStart('除虫概率机制测试');
  93. $config = TestConfig::getPesticideTestConfig();
  94. $this->dumpTestConfig($config);
  95. $successCount = 0;
  96. $totalAttempts = 10; // 测试10次
  97. dump("开始进行 {$totalAttempts} 次除虫测试,验证概率机制");
  98. for ($i = 1; $i <= $totalAttempts; $i++) {
  99. dump("第 {$i} 次测试");
  100. $this->currentTestConfig = $config;
  101. $response = $this->protobufRequest();
  102. if ($response->getCode() === 0) {
  103. $successCount++;
  104. dump("第 {$i} 次测试成功");
  105. } else {
  106. dump("第 {$i} 次测试失败: " . $response->getMsg());
  107. }
  108. }
  109. $successRate = ($successCount / $totalAttempts) * 100;
  110. $expectedRate = $config['expected_success_rate'];
  111. dump("测试结果: {$successCount}/{$totalAttempts} 成功,实际成功率: {$successRate}%,预期成功率: {$expectedRate}%");
  112. // 允许一定的误差范围(±30%)
  113. $this->assertGreaterThanOrEqual($expectedRate - 30, $successRate, '实际成功率不应低于预期太多');
  114. $this->assertLessThanOrEqual($expectedRate + 30, $successRate, '实际成功率不应高于预期太多');
  115. $this->dumpTestEnd('除虫概率机制测试');
  116. }
  117. /**
  118. * 创建除虫请求的protobuf消息
  119. */
  120. public function create_request_protobuf(): Message
  121. {
  122. $request = $this->createBaseRequest();
  123. $pesticideRequest = new RequestLandPesticide();
  124. // 使用当前测试配置
  125. $config = $this->currentTestConfig ?? TestConfig::getPesticideTestConfig();
  126. $pesticideRequest->setLandId($config['land_id']);
  127. $pesticideRequest->setItemId($config['item_id']);
  128. $request->setLandPesticide($pesticideRequest);
  129. dump('创建除虫请求:', [
  130. 'land_id' => $config['land_id'],
  131. 'item_id' => $config['item_id']
  132. ]);
  133. return $request;
  134. }
  135. /**
  136. * 测试所有预定义场景
  137. */
  138. public function testAllPesticideScenarios()
  139. {
  140. $this->dumpTestStart('所有除虫场景测试');
  141. // 测试成功场景
  142. $successScenarios = TestConfig::getTestScenario('success_scenarios', 'pesticide_success');
  143. if (!empty($successScenarios)) {
  144. dump('测试成功场景:', $successScenarios);
  145. $this->currentTestConfig = [
  146. 'land_id' => $successScenarios['land_id'],
  147. 'item_id' => $successScenarios['item_id']
  148. ];
  149. $response = $this->protobufRequest();
  150. $this->assertSuccessResponse($response, '除虫成功');
  151. }
  152. // 测试失败场景
  153. $failureScenarios = TestConfig::getTestScenario('failure_scenarios', 'invalid_item');
  154. if (!empty($failureScenarios)) {
  155. dump('测试失败场景:', $failureScenarios);
  156. $this->currentTestConfig = [
  157. 'land_id' => $failureScenarios['land_id'],
  158. 'item_id' => $failureScenarios['item_id']
  159. ];
  160. $response = $this->protobufRequest();
  161. $this->assertValidationFailureResponse($response, $failureScenarios['expected_error']);
  162. }
  163. $this->dumpTestEnd('所有除虫场景测试');
  164. }
  165. }