FertilizerHandlerTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\RequestLandFertilizer;
  8. use Uraus\Kku\Common\RESPONSE_CODE;
  9. /**
  10. * 施肥Handler E2E测试
  11. *
  12. * 测试施肥功能的各种场景,包括成功、失败和边界情况
  13. */
  14. class FertilizerHandlerTest extends DisasterRemovalBaseTest
  15. {
  16. /**
  17. * 当前测试配置
  18. */
  19. private array $currentTestConfig;
  20. /**
  21. * 测试施肥成功场景
  22. */
  23. public function testFertilizerSuccess()
  24. {
  25. $this->dumpTestStart('施肥成功测试');
  26. $config = TestEnvironment::getFertilizerConfig();
  27. $this->dumpTestConfig($config);
  28. $this->currentTestConfig = $config;
  29. $response = $this->executeTestRequest();
  30. $this->assertSuccessResponse($response, '施肥成功');
  31. $this->dumpTestEnd('施肥成功测试');
  32. }
  33. /**
  34. * 测试使用无效物品施肥
  35. */
  36. public function testFertilizerWithInvalidItem()
  37. {
  38. $this->dumpTestStart('无效物品施肥测试');
  39. $config = TestEnvironment::getFertilizerConfig();
  40. $config['item_id'] = TestEnvironment::get('TEST_INVALID_ITEM_ID', 999);
  41. $this->dumpTestConfig($config);
  42. $this->currentTestConfig = $config;
  43. $response = $this->executeTestRequest();
  44. $this->assertFailureResponse($response, '不是肥料物品');
  45. $this->dumpTestEnd('无效物品施肥测试');
  46. }
  47. /**
  48. * 测试对其他用户土地施肥
  49. */
  50. public function testFertilizerOnOtherUserLand()
  51. {
  52. $this->dumpTestStart('其他用户土地施肥测试');
  53. $config = TestEnvironment::getFertilizerConfig();
  54. $config['land_id'] = TestEnvironment::get('TEST_OTHER_USER_LAND_ID', 5);
  55. $this->dumpTestConfig($config);
  56. $this->currentTestConfig = $config;
  57. $response = $this->executeTestRequest();
  58. $this->assertFailureResponse($response, '土地不存在或不属于当前用户');
  59. $this->dumpTestEnd('其他用户土地施肥测试');
  60. }
  61. /**
  62. * 测试施肥性能
  63. */
  64. public function testFertilizerPerformance()
  65. {
  66. $this->dumpTestStart('施肥性能测试');
  67. $config = TestEnvironment::getFertilizerConfig();
  68. $performanceConfig = TestEnvironment::getPerformanceConfig();
  69. $requestCount = $performanceConfig['request_count'];
  70. $maxResponseTime = $performanceConfig['max_response_time'];
  71. $this->dumpTestConfig([
  72. 'fertilizer_config' => $config,
  73. 'request_count' => $requestCount,
  74. 'max_response_time' => $maxResponseTime
  75. ]);
  76. $startTime = microtime(true);
  77. for ($i = 1; $i <= $requestCount; $i++) {
  78. $requestStartTime = microtime(true);
  79. $this->currentTestConfig = $config;
  80. $response = $this->executeTestRequest();
  81. $requestEndTime = microtime(true);
  82. $requestDuration = ($requestEndTime - $requestStartTime) * 1000;
  83. $this->assertLessThan($maxResponseTime, $requestDuration,
  84. "第 {$i} 个请求响应时间 {$requestDuration}ms 超过限制 {$maxResponseTime}ms");
  85. if (TestEnvironment::isDebugMode()) {
  86. dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode());
  87. }
  88. }
  89. $totalTime = (microtime(true) - $startTime) * 1000;
  90. $avgTime = $totalTime / $requestCount;
  91. if (TestEnvironment::isDebugMode()) {
  92. dump("性能测试结果:", [
  93. 'total_requests' => $requestCount,
  94. 'total_time' => $totalTime . 'ms',
  95. 'average_time' => $avgTime . 'ms',
  96. 'max_allowed_time' => $maxResponseTime . 'ms'
  97. ]);
  98. }
  99. $this->dumpTestEnd('施肥性能测试');
  100. }
  101. /**
  102. * 测试重复施肥
  103. */
  104. public function testRepeatedFertilizer()
  105. {
  106. $this->dumpTestStart('重复施肥测试');
  107. $config = TestEnvironment::getFertilizerConfig();
  108. $this->currentTestConfig = $config;
  109. // 第一次施肥
  110. $response1 = $this->executeTestRequest();
  111. $this->dumpResponse($response1, '第一次施肥响应');
  112. // 第二次施肥(可能失败,因为已经施过肥)
  113. $response2 = $this->executeTestRequest();
  114. $this->dumpResponse($response2, '第二次施肥响应');
  115. // 验证至少有一次成功
  116. $hasSuccess = ($response1->getCode() === RESPONSE_CODE::OK) ||
  117. ($response2->getCode() === RESPONSE_CODE::OK);
  118. $this->assertTrue($hasSuccess, '至少应有一次施肥成功');
  119. $this->dumpTestEnd('重复施肥测试');
  120. }
  121. /**
  122. * 测试所有预定义场景
  123. */
  124. public function testAllFertilizerScenarios()
  125. {
  126. $this->dumpTestStart('所有施肥场景测试');
  127. // 测试成功场景
  128. $successScenarios = TestConfig::getTestScenario('success_scenarios', 'fertilizer_success');
  129. if (!empty($successScenarios)) {
  130. if (TestEnvironment::isDebugMode()) {
  131. dump('测试成功场景:', $successScenarios);
  132. }
  133. $this->currentTestConfig = [
  134. 'land_id' => $successScenarios['land_id'],
  135. 'item_id' => $successScenarios['item_id']
  136. ];
  137. $response = $this->executeTestRequest();
  138. $this->assertSuccessResponse($response, '施肥成功');
  139. }
  140. $this->dumpTestEnd('所有施肥场景测试');
  141. }
  142. /**
  143. * 创建施肥请求
  144. */
  145. public function create_request_protobuf(): Message
  146. {
  147. $request = new Request();
  148. $fertilizerRequest = new RequestLandFertilizer();
  149. // 使用当前测试配置或默认配置
  150. $config = $this->currentTestConfig ?? TestEnvironment::getFertilizerConfig();
  151. $fertilizerRequest->setLandId($config['land_id']);
  152. $fertilizerRequest->setUserItemId($config['item_id']);
  153. $request->setLandFertilizer($fertilizerRequest);
  154. return $this->createBaseRequest($request);
  155. }
  156. /**
  157. * 测试并发施肥请求
  158. */
  159. public function testConcurrentFertilizerRequests()
  160. {
  161. $this->dumpTestStart('并发施肥请求测试');
  162. $config = TestEnvironment::getFertilizerConfig();
  163. $concurrentConfig = TestEnvironment::getConcurrentConfig();
  164. $requestCount = $concurrentConfig['request_count'];
  165. if (TestEnvironment::isDebugMode()) {
  166. dump("开始并发测试,发送 {$requestCount} 个同时请求");
  167. }
  168. $responses = [];
  169. $this->currentTestConfig = $config;
  170. // 模拟并发请求(在测试环境中顺序执行)
  171. for ($i = 0; $i < $requestCount; $i++) {
  172. $responses[] = $this->executeTestRequest();
  173. }
  174. // 验证响应
  175. $successCount = 0;
  176. foreach ($responses as $index => $response) {
  177. if (TestEnvironment::isDebugMode()) {
  178. dump("第 " . ($index + 1) . " 个响应: " . $response->serializeToJsonString());
  179. }
  180. if ($response->getCode() === RESPONSE_CODE::OK) {
  181. $successCount++;
  182. }
  183. }
  184. // 至少应该有一个成功(第一个请求)
  185. $this->assertGreaterThan(0, $successCount, '并发请求中至少应有一个成功');
  186. if (TestEnvironment::isDebugMode()) {
  187. dump("并发测试结果: {$successCount}/{$requestCount} 个请求成功");
  188. }
  189. $this->dumpTestEnd('并发施肥请求测试');
  190. }
  191. /**
  192. * 测试施肥参数验证
  193. */
  194. public function testFertilizerParameterValidation()
  195. {
  196. $this->dumpTestStart('施肥参数验证测试');
  197. // 测试无效土地ID
  198. $this->currentTestConfig = [
  199. 'land_id' => -1,
  200. 'item_id' => TestEnvironment::get('TEST_FERTILIZER_ITEM_ID', 401)
  201. ];
  202. $response = $this->executeTestRequest();
  203. $this->assertFailureResponse($response);
  204. // 测试无效物品ID
  205. $this->currentTestConfig = [
  206. 'land_id' => TestEnvironment::get('TEST_FERTILIZER_LAND_ID', 12),
  207. 'item_id' => -1
  208. ];
  209. $response = $this->executeTestRequest();
  210. $this->assertFailureResponse($response);
  211. $this->dumpTestEnd('施肥参数验证测试');
  212. }
  213. }