SowHandlerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\RequestLandSow;
  8. use Uraus\Kku\Common\RESPONSE_CODE;
  9. /**
  10. * 播种Handler E2E测试
  11. *
  12. * 测试播种功能的各种场景,包括成功、失败和边界情况
  13. */
  14. class SowHandlerTest extends DisasterRemovalBaseTest
  15. {
  16. /**
  17. * 当前测试配置
  18. */
  19. private array $currentTestConfig;
  20. /**
  21. * 测试播种成功场景
  22. */
  23. public function testSowSuccess()
  24. {
  25. $this->dumpTestStart('播种成功测试');
  26. $config = TestEnvironment::getSowConfig();
  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 testSowWithInvalidSeed()
  37. {
  38. $this->dumpTestStart('无效种子播种测试');
  39. $config = TestEnvironment::getSowConfig();
  40. $config['seed_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 testSowOnOtherUserLand()
  51. {
  52. $this->dumpTestStart('其他用户土地播种测试');
  53. $config = TestEnvironment::getSowConfig();
  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 testSowOnOccupiedLand()
  65. {
  66. $this->dumpTestStart('已占用土地播种测试');
  67. $config = TestEnvironment::getSowConfig();
  68. $config['land_id'] = TestEnvironment::get('TEST_OCCUPIED_LAND_ID', 40);
  69. $this->dumpTestConfig($config);
  70. $this->currentTestConfig = $config;
  71. $response = $this->executeTestRequest();
  72. $this->assertFailureResponse($response, '土地已有作物');
  73. $this->dumpTestEnd('已占用土地播种测试');
  74. }
  75. /**
  76. * 测试播种性能
  77. */
  78. public function testSowPerformance()
  79. {
  80. $this->dumpTestStart('播种性能测试');
  81. $config = TestEnvironment::getSowConfig();
  82. $performanceConfig = TestEnvironment::getPerformanceConfig();
  83. $requestCount = $performanceConfig['request_count'];
  84. $maxResponseTime = $performanceConfig['max_response_time'];
  85. $this->dumpTestConfig([
  86. 'sow_config' => $config,
  87. 'request_count' => $requestCount,
  88. 'max_response_time' => $maxResponseTime
  89. ]);
  90. $startTime = microtime(true);
  91. $responseTimes = [];
  92. for ($i = 1; $i <= $requestCount; $i++) {
  93. $requestStartTime = microtime(true);
  94. // 使用不同的土地ID避免冲突
  95. $testConfig = $config;
  96. $testConfig['land_id'] = $config['land_id'] + $i;
  97. $this->currentTestConfig = $testConfig;
  98. $response = $this->executeTestRequest();
  99. $requestEndTime = microtime(true);
  100. $requestDuration = ($requestEndTime - $requestStartTime) * 1000;
  101. $responseTimes[] = $requestDuration;
  102. $this->assertLessThan($maxResponseTime, $requestDuration,
  103. "第 {$i} 个请求响应时间 {$requestDuration}ms 超过限制 {$maxResponseTime}ms");
  104. if (TestEnvironment::isDebugMode()) {
  105. dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode());
  106. }
  107. }
  108. $totalTime = (microtime(true) - $startTime) * 1000;
  109. $avgTime = array_sum($responseTimes) / count($responseTimes);
  110. $maxTime = max($responseTimes);
  111. $minTime = min($responseTimes);
  112. if (TestEnvironment::isDebugMode()) {
  113. dump("播种性能测试结果:", [
  114. 'total_requests' => $requestCount,
  115. 'total_time' => $totalTime . 'ms',
  116. 'average_time' => $avgTime . 'ms',
  117. 'max_time' => $maxTime . 'ms',
  118. 'min_time' => $minTime . 'ms',
  119. 'max_allowed_time' => $maxResponseTime . 'ms'
  120. ]);
  121. }
  122. $this->dumpTestEnd('播种性能测试');
  123. }
  124. /**
  125. * 测试重复播种
  126. */
  127. public function testRepeatedSow()
  128. {
  129. $this->dumpTestStart('重复播种测试');
  130. $config = TestEnvironment::getSowConfig();
  131. $this->currentTestConfig = $config;
  132. // 第一次播种
  133. $response1 = $this->executeTestRequest();
  134. $this->dumpResponse($response1, '第一次播种响应');
  135. // 第二次播种(应该失败,因为土地已有作物)
  136. $response2 = $this->executeTestRequest();
  137. $this->dumpResponse($response2, '第二次播种响应');
  138. // 验证第一次成功,第二次失败
  139. if ($response1->getCode() === RESPONSE_CODE::OK) {
  140. $this->assertNotEquals(RESPONSE_CODE::OK, $response2->getCode(),
  141. '第二次播种应该失败,因为土地已有作物');
  142. }
  143. $this->dumpTestEnd('重复播种测试');
  144. }
  145. /**
  146. * 测试批量播种
  147. */
  148. public function testBatchSow()
  149. {
  150. $this->dumpTestStart('批量播种测试');
  151. $baseConfig = TestEnvironment::getSowConfig();
  152. $landIds = [
  153. TestEnvironment::get('TEST_SOW_LAND_ID', 10),
  154. TestEnvironment::get('TEST_SOW_LAND_ID_2', 50),
  155. TestEnvironment::get('TEST_SOW_LAND_ID_3', 51)
  156. ];
  157. $successCount = 0;
  158. $failureCount = 0;
  159. foreach ($landIds as $index => $landId) {
  160. $this->currentTestConfig = [
  161. 'land_id' => $landId,
  162. 'seed_id' => $baseConfig['seed_id']
  163. ];
  164. $response = $this->executeTestRequest();
  165. if ($response->getCode() === RESPONSE_CODE::OK) {
  166. $successCount++;
  167. if (TestEnvironment::isDebugMode()) {
  168. dump("土地 {$landId} 播种成功");
  169. }
  170. } else {
  171. $failureCount++;
  172. if (TestEnvironment::isDebugMode()) {
  173. dump("土地 {$landId} 播种失败: " . $response->getMsg());
  174. }
  175. }
  176. }
  177. if (TestEnvironment::isDebugMode()) {
  178. dump("批量播种结果:", [
  179. 'total_lands' => count($landIds),
  180. 'success_count' => $successCount,
  181. 'failure_count' => $failureCount
  182. ]);
  183. }
  184. // 至少应该有一个成功
  185. $this->assertGreaterThan(0, $successCount, '批量播种中至少应有一个成功');
  186. $this->dumpTestEnd('批量播种测试');
  187. }
  188. /**
  189. * 测试不同种子类型播种
  190. */
  191. public function testDifferentSeedTypes()
  192. {
  193. $this->dumpTestStart('不同种子类型播种测试');
  194. $seedIds = [
  195. TestEnvironment::get('TEST_SOW_SEED_ID', 201),
  196. TestEnvironment::get('TEST_SOW_SEED_ID_2', 202),
  197. TestEnvironment::get('TEST_SOW_SEED_ID_3', 203)
  198. ];
  199. $baseLandId = TestEnvironment::get('TEST_SOW_LAND_ID', 10);
  200. foreach ($seedIds as $index => $seedId) {
  201. $this->currentTestConfig = [
  202. 'land_id' => $baseLandId + $index + 10, // 使用不同的土地
  203. 'seed_id' => $seedId
  204. ];
  205. $response = $this->executeTestRequest();
  206. if (TestEnvironment::isDebugMode()) {
  207. dump("种子 {$seedId} 播种结果: " . $response->getCode() . " - " . $response->getMsg());
  208. }
  209. // 记录结果但不强制要求成功(因为种子可能不存在)
  210. $this->assertInstanceOf(\Uraus\Kku\Response::class, $response);
  211. }
  212. $this->dumpTestEnd('不同种子类型播种测试');
  213. }
  214. /**
  215. * 测试播种资源消耗验证
  216. */
  217. public function testSowResourceConsumption()
  218. {
  219. $this->dumpTestStart('播种资源消耗验证测试');
  220. $config = TestEnvironment::getSowConfig();
  221. $this->currentTestConfig = $config;
  222. $response = $this->executeTestRequest();
  223. if ($response->getCode() === RESPONSE_CODE::OK) {
  224. // 验证响应中包含消耗信息
  225. $responseJson = $response->serializeToJsonString();
  226. $responseData = json_decode($responseJson, true);
  227. // 检查是否有扣除数据(种子消耗)
  228. if (isset($responseData['deduct'])) {
  229. $this->assertNotEmpty($responseData['deduct'], '播种成功应该包含资源消耗信息');
  230. if (TestEnvironment::isDebugMode()) {
  231. dump('播种消耗:', $responseData['deduct']);
  232. }
  233. }
  234. // 检查是否有最新数据更新
  235. if (isset($responseData['last_data'])) {
  236. if (TestEnvironment::isDebugMode()) {
  237. dump('数据更新:', $responseData['last_data']);
  238. }
  239. }
  240. }
  241. $this->dumpTestEnd('播种资源消耗验证测试');
  242. }
  243. /**
  244. * 创建播种请求
  245. */
  246. public function create_request_protobuf(): Message
  247. {
  248. $request = new Request();
  249. $sowRequest = new RequestLandSow();
  250. // 使用当前测试配置或默认配置
  251. $config = $this->currentTestConfig ?? TestEnvironment::getSowConfig();
  252. $sowRequest->setLandId($config['land_id']);
  253. $sowRequest->setUserItemId($config['seed_id']);
  254. $request->setLandSow($sowRequest);
  255. return $this->createBaseRequest($request);
  256. }
  257. /**
  258. * 测试播种参数验证
  259. */
  260. public function testSowParameterValidation()
  261. {
  262. $this->dumpTestStart('播种参数验证测试');
  263. // 测试无效土地ID
  264. $this->currentTestConfig = [
  265. 'land_id' => -1,
  266. 'seed_id' => TestEnvironment::get('TEST_SOW_SEED_ID', 201)
  267. ];
  268. $response = $this->executeTestRequest();
  269. $this->assertFailureResponse($response);
  270. // 测试无效种子ID
  271. $this->currentTestConfig = [
  272. 'land_id' => TestEnvironment::get('TEST_SOW_LAND_ID', 10),
  273. 'seed_id' => -1
  274. ];
  275. $response = $this->executeTestRequest();
  276. $this->assertFailureResponse($response);
  277. $this->dumpTestEnd('播种参数验证测试');
  278. }
  279. /**
  280. * 测试所有预定义场景
  281. */
  282. public function testAllSowScenarios()
  283. {
  284. $this->dumpTestStart('所有播种场景测试');
  285. // 测试成功场景
  286. $successScenarios = TestConfig::getTestScenario('success_scenarios', 'sow_success');
  287. if (!empty($successScenarios)) {
  288. if (TestEnvironment::isDebugMode()) {
  289. dump('测试成功场景:', $successScenarios);
  290. }
  291. $this->currentTestConfig = [
  292. 'land_id' => $successScenarios['land_id'],
  293. 'seed_id' => $successScenarios['seed_id']
  294. ];
  295. $response = $this->executeTestRequest();
  296. $this->assertSuccessResponse($response, '播种成功');
  297. }
  298. $this->dumpTestEnd('所有播种场景测试');
  299. }
  300. }