WateringHandlerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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\RequestLandWatering;
  7. /**
  8. * 浇水Handler E2E测试
  9. *
  10. * 测试浇水功能的各种场景,包括成功、失败和边界情况
  11. */
  12. class WateringHandlerTest extends DisasterRemovalBaseTest
  13. {
  14. /**
  15. * 当前测试配置
  16. */
  17. private array $currentTestConfig;
  18. /**
  19. * 测试浇水成功场景
  20. */
  21. public function testWateringSuccess()
  22. {
  23. $this->dumpTestStart('浇水成功测试');
  24. $config = TestConfig::getWateringTestConfig();
  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 testWateringWithInvalidItem()
  35. {
  36. $this->dumpTestStart('无效物品浇水测试');
  37. $config = TestConfig::getWateringTestConfig();
  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 testWateringWithWrongItemType()
  49. {
  50. $this->dumpTestStart('错误物品类型浇水测试');
  51. $config = TestConfig::getWateringTestConfig();
  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 testWateringOnOtherUserLand()
  63. {
  64. $this->dumpTestStart('其他用户土地浇水测试');
  65. $config = TestConfig::getWateringTestConfig();
  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 testWateringOnNormalLand()
  77. {
  78. $this->dumpTestStart('无干旱土地浇水测试');
  79. $config = TestConfig::getWateringTestConfig();
  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 testWateringWithInvalidItemId()
  91. {
  92. $this->dumpTestStart('无效物品ID测试');
  93. $config = TestConfig::getWateringTestConfig();
  94. $config['item_id'] = 0; // 无效的物品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 testWateringProbability()
  105. {
  106. $this->dumpTestStart('浇水概率机制测试');
  107. $config = TestConfig::getWateringTestConfig();
  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. // 浇水通常有较高的成功率,允许一定的误差范围(±20%)
  127. $this->assertGreaterThanOrEqual($expectedRate - 20, $successRate, '实际成功率不应低于预期太多');
  128. $this->assertLessThanOrEqual($expectedRate + 20, $successRate, '实际成功率不应高于预期太多');
  129. $this->dumpTestEnd('浇水概率机制测试');
  130. }
  131. /**
  132. * 测试边界情况 - 100%成功率物品
  133. */
  134. public function testWateringWithHundredPercentSuccessRate()
  135. {
  136. $this->dumpTestStart('100%成功率浇水测试');
  137. $config = TestConfig::getWateringTestConfig();
  138. // 假设有一个100%成功率的浇水道具用于测试
  139. $config['item_id'] = 1030; // 假设的100%成功率浇水道具ID
  140. $this->dumpTestConfig($config);
  141. $this->currentTestConfig = $config;
  142. $response = $this->protobufRequest();
  143. // 100%成功率应该总是成功
  144. $this->assertSuccessResponse($response, '浇水成功');
  145. $this->dumpTestEnd('100%成功率浇水测试');
  146. }
  147. /**
  148. * 测试重复浇水同一块土地
  149. */
  150. public function testRepeatedWateringOnSameLand()
  151. {
  152. $this->dumpTestStart('重复浇水测试');
  153. $config = TestConfig::getWateringTestConfig();
  154. $this->dumpTestConfig($config);
  155. // 第一次浇水
  156. dump("第一次浇水");
  157. $this->currentTestConfig = $config;
  158. $firstResponse = $this->protobufRequest();
  159. dump("第一次浇水响应: " . $firstResponse->serializeToJsonString());
  160. // 第二次浇水(如果第一次成功,第二次应该失败)
  161. dump("第二次浇水");
  162. $this->currentTestConfig = $config;
  163. $secondResponse = $this->protobufRequest();
  164. dump("第二次浇水响应: " . $secondResponse->serializeToJsonString());
  165. // 如果第一次成功,第二次应该失败(因为已经没有干旱了)
  166. if ($firstResponse->getCode() === 0) {
  167. $this->assertFailureResponse($secondResponse, '灾害清理失败');
  168. dump("符合预期:第一次成功后,第二次失败");
  169. } else {
  170. dump("第一次失败,第二次结果不确定");
  171. }
  172. $this->dumpTestEnd('重复浇水测试');
  173. }
  174. /**
  175. * 创建浇水请求的protobuf消息
  176. */
  177. public function create_request_protobuf(): Message
  178. {
  179. $request = $this->createBaseRequest();
  180. $wateringRequest = new RequestLandWatering();
  181. // 使用当前测试配置
  182. $config = $this->currentTestConfig ?? TestConfig::getWateringTestConfig();
  183. $wateringRequest->setLandId($config['land_id']);
  184. $wateringRequest->setUserItemId($config['item_id']);
  185. $request->setLandWatering($wateringRequest);
  186. dump('创建浇水请求:', [
  187. 'land_id' => $config['land_id'],
  188. 'user_item_id' => $config['item_id']
  189. ]);
  190. return $request;
  191. }
  192. /**
  193. * 测试所有预定义场景
  194. */
  195. public function testAllWateringScenarios()
  196. {
  197. $this->dumpTestStart('所有浇水场景测试');
  198. // 测试成功场景
  199. $successScenarios = TestConfig::getTestScenario('success_scenarios', 'watering_success');
  200. if (!empty($successScenarios)) {
  201. dump('测试成功场景:', $successScenarios);
  202. $this->currentTestConfig = [
  203. 'land_id' => $successScenarios['land_id'],
  204. 'item_id' => $successScenarios['item_id']
  205. ];
  206. $response = $this->protobufRequest();
  207. $this->assertSuccessResponse($response, '浇水成功');
  208. }
  209. $this->dumpTestEnd('所有浇水场景测试');
  210. }
  211. /**
  212. * 测试性能 - 大量浇水请求
  213. */
  214. public function testWateringPerformance()
  215. {
  216. $this->dumpTestStart('浇水性能测试');
  217. $config = TestConfig::getWateringTestConfig();
  218. $this->dumpTestConfig($config);
  219. $startTime = microtime(true);
  220. $requestCount = 5; // 测试5个请求
  221. dump("开始性能测试,发送 {$requestCount} 个浇水请求");
  222. for ($i = 1; $i <= $requestCount; $i++) {
  223. $requestStartTime = microtime(true);
  224. $this->currentTestConfig = $config;
  225. $response = $this->protobufRequest();
  226. $requestEndTime = microtime(true);
  227. $requestDuration = ($requestEndTime - $requestStartTime) * 1000; // 转换为毫秒
  228. dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode());
  229. }
  230. $endTime = microtime(true);
  231. $totalDuration = ($endTime - $startTime) * 1000; // 转换为毫秒
  232. $averageDuration = $totalDuration / $requestCount;
  233. dump("性能测试结果: 总耗时 {$totalDuration}ms,平均耗时 {$averageDuration}ms");
  234. // 验证平均响应时间不超过5秒
  235. $this->assertLessThan(5000, $averageDuration, '平均响应时间不应超过5秒');
  236. $this->dumpTestEnd('浇水性能测试');
  237. }
  238. }