WeedicideHandlerTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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\RequestLandWeedicide;
  7. /**
  8. * 除草Handler E2E测试
  9. *
  10. * 测试除草功能的各种场景,包括成功、失败和边界情况
  11. */
  12. class WeedicideHandlerTest extends DisasterRemovalBaseTest
  13. {
  14. /**
  15. * 当前测试配置
  16. */
  17. private array $currentTestConfig;
  18. /**
  19. * 测试除草成功场景
  20. */
  21. public function testWeedicideSuccess()
  22. {
  23. $this->dumpTestStart('除草成功测试');
  24. $config = TestConfig::getWeedicideTestConfig();
  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 testWeedicideWithInvalidItem()
  35. {
  36. $this->dumpTestStart('无效物品除草测试');
  37. $config = TestConfig::getWeedicideTestConfig();
  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 testWeedicideWithWrongItemType()
  49. {
  50. $this->dumpTestStart('错误物品类型除草测试');
  51. $config = TestConfig::getWeedicideTestConfig();
  52. $config['item_id'] = TestConfig::getTestItem('pesticide')['item_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 testWeedicideOnOtherUserLand()
  63. {
  64. $this->dumpTestStart('其他用户土地除草测试');
  65. $config = TestConfig::getWeedicideTestConfig();
  66. $config['land_id'] = TestConfig::getTestLand('other_user_land')['land_id'];
  67. $this->dumpTestConfig($config);
  68. $this->currentTestConfig = $config;
  69. $response = $this->protobufRequest();
  70. $this->assertValidationFailureResponse($response, '土地不存在或不属于当前用户');
  71. $this->dumpTestEnd('其他用户土地除草测试');
  72. }
  73. /**
  74. * 测试对无杂草土地除草
  75. */
  76. public function testWeedicideOnNormalLand()
  77. {
  78. $this->dumpTestStart('无杂草土地除草测试');
  79. $config = TestConfig::getWeedicideTestConfig();
  80. $config['land_id'] = TestConfig::getTestLand('normal_land')['land_id'];
  81. $this->dumpTestConfig($config);
  82. $this->currentTestConfig = $config;
  83. $response = $this->protobufRequest();
  84. $this->assertFailureResponse($response, '灾害清理失败');
  85. $this->dumpTestEnd('无杂草土地除草测试');
  86. }
  87. /**
  88. * 测试参数验证 - 无效的土地ID
  89. */
  90. public function testWeedicideWithInvalidLandId()
  91. {
  92. $this->dumpTestStart('无效土地ID测试');
  93. $config = TestConfig::getWeedicideTestConfig();
  94. $config['land_id'] = -1; // 无效的土地ID
  95. $this->dumpTestConfig($config);
  96. $this->currentTestConfig = $config;
  97. $response = $this->protobufRequest();
  98. $this->assertValidationFailureResponse($response);
  99. $this->dumpTestEnd('无效土地ID测试');
  100. }
  101. /**
  102. * 测试概率机制 - 多次测试验证概率
  103. */
  104. public function testWeedicideProbability()
  105. {
  106. $this->dumpTestStart('除草概率机制测试');
  107. $config = TestConfig::getWeedicideTestConfig();
  108. $this->dumpTestConfig($config);
  109. $successCount = 0;
  110. $totalAttempts = 10; // 测试10次
  111. dump("开始进行 {$totalAttempts} 次除草测试,验证概率机制");
  112. for ($i = 1; $i <= $totalAttempts; $i++) {
  113. dump("第 {$i} 次测试");
  114. $this->currentTestConfig = $config;
  115. $response = $this->protobufRequest();
  116. if ($response->getCode() === 0) {
  117. $successCount++;
  118. dump("第 {$i} 次测试成功");
  119. } else {
  120. dump("第 {$i} 次测试失败: " . $response->getMsg());
  121. }
  122. }
  123. $successRate = ($successCount / $totalAttempts) * 100;
  124. $expectedRate = $config['expected_success_rate'];
  125. dump("测试结果: {$successCount}/{$totalAttempts} 成功,实际成功率: {$successRate}%,预期成功率: {$expectedRate}%");
  126. // 允许一定的误差范围(±30%)
  127. $this->assertGreaterThanOrEqual($expectedRate - 30, $successRate, '实际成功率不应低于预期太多');
  128. $this->assertLessThanOrEqual($expectedRate + 30, $successRate, '实际成功率不应高于预期太多');
  129. $this->dumpTestEnd('除草概率机制测试');
  130. }
  131. /**
  132. * 测试边界情况 - 0%成功率物品
  133. */
  134. public function testWeedicideWithZeroSuccessRate()
  135. {
  136. $this->dumpTestStart('0%成功率除草测试');
  137. $config = TestConfig::getWeedicideTestConfig();
  138. // 假设有一个0%成功率的除草剂用于测试
  139. $config['item_id'] = 1020; // 假设的0%成功率除草剂ID
  140. $this->dumpTestConfig($config);
  141. $this->currentTestConfig = $config;
  142. $response = $this->protobufRequest();
  143. // 0%成功率应该总是失败,但仍会消耗物品
  144. $this->assertFailureResponse($response, '除草失败,请再次尝试');
  145. $this->dumpTestEnd('0%成功率除草测试');
  146. }
  147. /**
  148. * 创建除草请求的protobuf消息
  149. */
  150. public function create_request_protobuf(): Message
  151. {
  152. $request = $this->createBaseRequest();
  153. $weedicideRequest = new RequestLandWeedicide();
  154. // 使用当前测试配置
  155. $config = $this->currentTestConfig ?? TestConfig::getWeedicideTestConfig();
  156. $weedicideRequest->setLandId($config['land_id']);
  157. $weedicideRequest->setUserItemId($config['item_id']);
  158. $request->setLandWeedicide($weedicideRequest);
  159. dump('创建除草请求:', [
  160. 'land_id' => $config['land_id'],
  161. 'user_item_id' => $config['item_id']
  162. ]);
  163. return $request;
  164. }
  165. /**
  166. * 测试所有预定义场景
  167. */
  168. public function testAllWeedicideScenarios()
  169. {
  170. $this->dumpTestStart('所有除草场景测试');
  171. // 测试成功场景
  172. $successScenarios = TestConfig::getTestScenario('success_scenarios', 'weedicide_success');
  173. if (!empty($successScenarios)) {
  174. dump('测试成功场景:', $successScenarios);
  175. $this->currentTestConfig = [
  176. 'land_id' => $successScenarios['land_id'],
  177. 'item_id' => $successScenarios['item_id']
  178. ];
  179. $response = $this->protobufRequest();
  180. $this->assertSuccessResponse($response, '除草成功');
  181. }
  182. $this->dumpTestEnd('所有除草场景测试');
  183. }
  184. /**
  185. * 测试并发除草请求
  186. */
  187. public function testConcurrentWeedicideRequests()
  188. {
  189. $this->dumpTestStart('并发除草请求测试');
  190. $config = TestConfig::getWeedicideTestConfig();
  191. $this->dumpTestConfig($config);
  192. // 模拟快速连续的请求
  193. $responses = [];
  194. for ($i = 1; $i <= 3; $i++) {
  195. dump("发送第 {$i} 个并发请求");
  196. $this->currentTestConfig = $config;
  197. $responses[] = $this->protobufRequest();
  198. }
  199. // 验证响应
  200. $successCount = 0;
  201. foreach ($responses as $index => $response) {
  202. dump("第 " . ($index + 1) . " 个响应: " . $response->serializeToJsonString());
  203. if ($response->getCode() === 0) {
  204. $successCount++;
  205. }
  206. }
  207. dump("并发请求结果: {$successCount}/3 成功");
  208. // 至少应该有一个成功(假设有足够的物品)
  209. $this->assertGreaterThanOrEqual(1, $successCount, '并发请求中至少应该有一个成功');
  210. $this->dumpTestEnd('并发除草请求测试');
  211. }
  212. }