PesticideHandlerTest.php 6.8 KB

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