HarvestHandlerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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\RequestLandHarvest;
  8. use Uraus\Kku\Common\RESPONSE_CODE;
  9. /**
  10. * 收获Handler E2E测试
  11. *
  12. * 测试收获功能的各种场景,包括成功、失败和边界情况
  13. */
  14. class HarvestHandlerTest extends DisasterRemovalBaseTest
  15. {
  16. /**
  17. * 当前测试配置
  18. */
  19. private array $currentTestConfig;
  20. /**
  21. * 测试收获成功场景
  22. */
  23. public function testHarvestSuccess()
  24. {
  25. $this->dumpTestStart('收获成功测试');
  26. $config = TestEnvironment::getHarvestConfig();
  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 testHarvestImmatureCrop()
  37. {
  38. $this->dumpTestStart('收获未成熟作物测试');
  39. $config = TestEnvironment::getHarvestConfig();
  40. // 使用一个未成熟的作物土地
  41. $config['land_id'] = TestEnvironment::get('TEST_IMMATURE_CROP_LAND_ID', 20);
  42. $this->dumpTestConfig($config);
  43. $this->currentTestConfig = $config;
  44. $response = $this->executeTestRequest();
  45. $this->assertFailureResponse($response, '作物未成熟');
  46. $this->dumpTestEnd('收获未成熟作物测试');
  47. }
  48. /**
  49. * 测试收获其他用户土地
  50. */
  51. public function testHarvestOtherUserLand()
  52. {
  53. $this->dumpTestStart('收获其他用户土地测试');
  54. $config = TestEnvironment::getHarvestConfig();
  55. $config['land_id'] = TestEnvironment::get('TEST_OTHER_USER_LAND_ID', 5);
  56. $this->dumpTestConfig($config);
  57. $this->currentTestConfig = $config;
  58. $response = $this->executeTestRequest();
  59. $this->assertFailureResponse($response, '土地不存在或不属于当前用户');
  60. $this->dumpTestEnd('收获其他用户土地测试');
  61. }
  62. /**
  63. * 测试收获空土地
  64. */
  65. public function testHarvestEmptyLand()
  66. {
  67. $this->dumpTestStart('收获空土地测试');
  68. $config = TestEnvironment::getHarvestConfig();
  69. $config['land_id'] = TestEnvironment::get('TEST_EMPTY_LAND_ID', 30);
  70. $this->dumpTestConfig($config);
  71. $this->currentTestConfig = $config;
  72. $response = $this->executeTestRequest();
  73. $this->assertFailureResponse($response, '土地上没有作物');
  74. $this->dumpTestEnd('收获空土地测试');
  75. }
  76. /**
  77. * 测试收获性能
  78. */
  79. public function testHarvestPerformance()
  80. {
  81. $this->dumpTestStart('收获性能测试');
  82. $config = TestEnvironment::getHarvestConfig();
  83. $performanceConfig = TestEnvironment::getPerformanceConfig();
  84. $requestCount = $performanceConfig['request_count'];
  85. $maxResponseTime = $performanceConfig['max_response_time'];
  86. $this->dumpTestConfig([
  87. 'harvest_config' => $config,
  88. 'request_count' => $requestCount,
  89. 'max_response_time' => $maxResponseTime
  90. ]);
  91. $startTime = microtime(true);
  92. $responseTimes = [];
  93. for ($i = 1; $i <= $requestCount; $i++) {
  94. $requestStartTime = microtime(true);
  95. $this->currentTestConfig = $config;
  96. $response = $this->executeTestRequest();
  97. $requestEndTime = microtime(true);
  98. $requestDuration = ($requestEndTime - $requestStartTime) * 1000;
  99. $responseTimes[] = $requestDuration;
  100. $this->assertLessThan($maxResponseTime, $requestDuration,
  101. "第 {$i} 个请求响应时间 {$requestDuration}ms 超过限制 {$maxResponseTime}ms");
  102. if (TestEnvironment::isDebugMode()) {
  103. dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode());
  104. }
  105. }
  106. $totalTime = (microtime(true) - $startTime) * 1000;
  107. $avgTime = array_sum($responseTimes) / count($responseTimes);
  108. $maxTime = max($responseTimes);
  109. $minTime = min($responseTimes);
  110. if (TestEnvironment::isDebugMode()) {
  111. dump("收获性能测试结果:", [
  112. 'total_requests' => $requestCount,
  113. 'total_time' => $totalTime . 'ms',
  114. 'average_time' => $avgTime . 'ms',
  115. 'max_time' => $maxTime . 'ms',
  116. 'min_time' => $minTime . 'ms',
  117. 'max_allowed_time' => $maxResponseTime . 'ms'
  118. ]);
  119. }
  120. $this->dumpTestEnd('收获性能测试');
  121. }
  122. /**
  123. * 测试重复收获
  124. */
  125. public function testRepeatedHarvest()
  126. {
  127. $this->dumpTestStart('重复收获测试');
  128. $config = TestEnvironment::getHarvestConfig();
  129. $this->currentTestConfig = $config;
  130. // 第一次收获
  131. $response1 = $this->executeTestRequest();
  132. $this->dumpResponse($response1, '第一次收获响应');
  133. // 第二次收获(应该失败,因为已经收获过了)
  134. $response2 = $this->executeTestRequest();
  135. $this->dumpResponse($response2, '第二次收获响应');
  136. // 验证第一次成功,第二次失败
  137. if ($response1->getCode() === RESPONSE_CODE::OK) {
  138. $this->assertNotEquals(RESPONSE_CODE::OK, $response2->getCode(),
  139. '第二次收获应该失败,因为作物已被收获');
  140. }
  141. $this->dumpTestEnd('重复收获测试');
  142. }
  143. /**
  144. * 测试批量收获
  145. */
  146. public function testBatchHarvest()
  147. {
  148. $this->dumpTestStart('批量收获测试');
  149. $landIds = [
  150. TestEnvironment::get('TEST_HARVEST_LAND_ID', 11),
  151. TestEnvironment::get('TEST_HARVEST_LAND_ID_2', 21),
  152. TestEnvironment::get('TEST_HARVEST_LAND_ID_3', 22)
  153. ];
  154. $successCount = 0;
  155. $failureCount = 0;
  156. foreach ($landIds as $index => $landId) {
  157. $this->currentTestConfig = [
  158. 'land_id' => $landId
  159. ];
  160. $response = $this->executeTestRequest();
  161. if ($response->getCode() === RESPONSE_CODE::OK) {
  162. $successCount++;
  163. if (TestEnvironment::isDebugMode()) {
  164. dump("土地 {$landId} 收获成功");
  165. }
  166. } else {
  167. $failureCount++;
  168. if (TestEnvironment::isDebugMode()) {
  169. dump("土地 {$landId} 收获失败: " . $response->getMsg());
  170. }
  171. }
  172. }
  173. if (TestEnvironment::isDebugMode()) {
  174. dump("批量收获结果:", [
  175. 'total_lands' => count($landIds),
  176. 'success_count' => $successCount,
  177. 'failure_count' => $failureCount
  178. ]);
  179. }
  180. // 至少应该有一个成功
  181. $this->assertGreaterThan(0, $successCount, '批量收获中至少应有一个成功');
  182. $this->dumpTestEnd('批量收获测试');
  183. }
  184. /**
  185. * 测试收获奖励验证
  186. */
  187. public function testHarvestRewardValidation()
  188. {
  189. $this->dumpTestStart('收获奖励验证测试');
  190. $config = TestEnvironment::getHarvestConfig();
  191. $this->currentTestConfig = $config;
  192. $response = $this->executeTestRequest();
  193. if ($response->getCode() === RESPONSE_CODE::OK) {
  194. // 验证响应中包含奖励信息
  195. $responseJson = $response->serializeToJsonString();
  196. $responseData = json_decode($responseJson, true);
  197. // 检查是否有奖励数据
  198. if (isset($responseData['reward'])) {
  199. $this->assertNotEmpty($responseData['reward'], '收获成功应该包含奖励信息');
  200. if (TestEnvironment::isDebugMode()) {
  201. dump('收获奖励:', $responseData['reward']);
  202. }
  203. }
  204. // 检查是否有扣除数据(如工具消耗)
  205. if (isset($responseData['deduct'])) {
  206. if (TestEnvironment::isDebugMode()) {
  207. dump('收获消耗:', $responseData['deduct']);
  208. }
  209. }
  210. }
  211. $this->dumpTestEnd('收获奖励验证测试');
  212. }
  213. /**
  214. * 创建收获请求
  215. */
  216. public function create_request_protobuf(): Message
  217. {
  218. $request = new Request();
  219. $harvestRequest = new RequestLandHarvest();
  220. // 使用当前测试配置或默认配置
  221. $config = $this->currentTestConfig ?? TestEnvironment::getHarvestConfig();
  222. $harvestRequest->setLandId($config['land_id']);
  223. $request->setLandHarvest($harvestRequest);
  224. return $this->createBaseRequest($request);
  225. }
  226. /**
  227. * 测试收获参数验证
  228. */
  229. public function testHarvestParameterValidation()
  230. {
  231. $this->dumpTestStart('收获参数验证测试');
  232. // 测试无效土地ID
  233. $this->currentTestConfig = [
  234. 'land_id' => -1
  235. ];
  236. $response = $this->executeTestRequest();
  237. $this->assertFailureResponse($response);
  238. // 测试不存在的土地ID
  239. $this->currentTestConfig = [
  240. 'land_id' => 99999
  241. ];
  242. $response = $this->executeTestRequest();
  243. $this->assertFailureResponse($response, '土地不存在');
  244. $this->dumpTestEnd('收获参数验证测试');
  245. }
  246. /**
  247. * 测试所有预定义场景
  248. */
  249. public function testAllHarvestScenarios()
  250. {
  251. $this->dumpTestStart('所有收获场景测试');
  252. // 测试成功场景
  253. $successScenarios = TestConfig::getTestScenario('success_scenarios', 'harvest_success');
  254. if (!empty($successScenarios)) {
  255. if (TestEnvironment::isDebugMode()) {
  256. dump('测试成功场景:', $successScenarios);
  257. }
  258. $this->currentTestConfig = [
  259. 'land_id' => $successScenarios['land_id']
  260. ];
  261. $response = $this->executeTestRequest();
  262. $this->assertSuccessResponse($response, '收获成功');
  263. }
  264. $this->dumpTestEnd('所有收获场景测试');
  265. }
  266. }