dumpTestStart('收获成功测试'); $config = TestEnvironment::getHarvestConfig(); $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->executeTestRequest(); $this->assertSuccessResponse($response, '收获成功'); $this->dumpTestEnd('收获成功测试'); } /** * 测试收获未成熟作物 */ public function testHarvestImmatureCrop() { $this->dumpTestStart('收获未成熟作物测试'); $config = TestEnvironment::getHarvestConfig(); // 使用一个未成熟的作物土地 $config['land_id'] = TestEnvironment::get('TEST_IMMATURE_CROP_LAND_ID', 20); $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->executeTestRequest(); $this->assertFailureResponse($response, '作物未成熟'); $this->dumpTestEnd('收获未成熟作物测试'); } /** * 测试收获其他用户土地 */ public function testHarvestOtherUserLand() { $this->dumpTestStart('收获其他用户土地测试'); $config = TestEnvironment::getHarvestConfig(); $config['land_id'] = TestEnvironment::get('TEST_OTHER_USER_LAND_ID', 5); $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->executeTestRequest(); $this->assertFailureResponse($response, '土地不存在或不属于当前用户'); $this->dumpTestEnd('收获其他用户土地测试'); } /** * 测试收获空土地 */ public function testHarvestEmptyLand() { $this->dumpTestStart('收获空土地测试'); $config = TestEnvironment::getHarvestConfig(); $config['land_id'] = TestEnvironment::get('TEST_EMPTY_LAND_ID', 30); $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->executeTestRequest(); $this->assertFailureResponse($response, '土地上没有作物'); $this->dumpTestEnd('收获空土地测试'); } /** * 测试收获性能 */ public function testHarvestPerformance() { $this->dumpTestStart('收获性能测试'); $config = TestEnvironment::getHarvestConfig(); $performanceConfig = TestEnvironment::getPerformanceConfig(); $requestCount = $performanceConfig['request_count']; $maxResponseTime = $performanceConfig['max_response_time']; $this->dumpTestConfig([ 'harvest_config' => $config, 'request_count' => $requestCount, 'max_response_time' => $maxResponseTime ]); $startTime = microtime(true); $responseTimes = []; for ($i = 1; $i <= $requestCount; $i++) { $requestStartTime = microtime(true); $this->currentTestConfig = $config; $response = $this->executeTestRequest(); $requestEndTime = microtime(true); $requestDuration = ($requestEndTime - $requestStartTime) * 1000; $responseTimes[] = $requestDuration; $this->assertLessThan($maxResponseTime, $requestDuration, "第 {$i} 个请求响应时间 {$requestDuration}ms 超过限制 {$maxResponseTime}ms"); if (TestEnvironment::isDebugMode()) { dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode()); } } $totalTime = (microtime(true) - $startTime) * 1000; $avgTime = array_sum($responseTimes) / count($responseTimes); $maxTime = max($responseTimes); $minTime = min($responseTimes); if (TestEnvironment::isDebugMode()) { dump("收获性能测试结果:", [ 'total_requests' => $requestCount, 'total_time' => $totalTime . 'ms', 'average_time' => $avgTime . 'ms', 'max_time' => $maxTime . 'ms', 'min_time' => $minTime . 'ms', 'max_allowed_time' => $maxResponseTime . 'ms' ]); } $this->dumpTestEnd('收获性能测试'); } /** * 测试重复收获 */ public function testRepeatedHarvest() { $this->dumpTestStart('重复收获测试'); $config = TestEnvironment::getHarvestConfig(); $this->currentTestConfig = $config; // 第一次收获 $response1 = $this->executeTestRequest(); $this->dumpResponse($response1, '第一次收获响应'); // 第二次收获(应该失败,因为已经收获过了) $response2 = $this->executeTestRequest(); $this->dumpResponse($response2, '第二次收获响应'); // 验证第一次成功,第二次失败 if ($response1->getCode() === RESPONSE_CODE::OK) { $this->assertNotEquals(RESPONSE_CODE::OK, $response2->getCode(), '第二次收获应该失败,因为作物已被收获'); } $this->dumpTestEnd('重复收获测试'); } /** * 测试批量收获 */ public function testBatchHarvest() { $this->dumpTestStart('批量收获测试'); $landIds = [ TestEnvironment::get('TEST_HARVEST_LAND_ID', 11), TestEnvironment::get('TEST_HARVEST_LAND_ID_2', 21), TestEnvironment::get('TEST_HARVEST_LAND_ID_3', 22) ]; $successCount = 0; $failureCount = 0; foreach ($landIds as $index => $landId) { $this->currentTestConfig = [ 'land_id' => $landId ]; $response = $this->executeTestRequest(); if ($response->getCode() === RESPONSE_CODE::OK) { $successCount++; if (TestEnvironment::isDebugMode()) { dump("土地 {$landId} 收获成功"); } } else { $failureCount++; if (TestEnvironment::isDebugMode()) { dump("土地 {$landId} 收获失败: " . $response->getMsg()); } } } if (TestEnvironment::isDebugMode()) { dump("批量收获结果:", [ 'total_lands' => count($landIds), 'success_count' => $successCount, 'failure_count' => $failureCount ]); } // 至少应该有一个成功 $this->assertGreaterThan(0, $successCount, '批量收获中至少应有一个成功'); $this->dumpTestEnd('批量收获测试'); } /** * 测试收获奖励验证 */ public function testHarvestRewardValidation() { $this->dumpTestStart('收获奖励验证测试'); $config = TestEnvironment::getHarvestConfig(); $this->currentTestConfig = $config; $response = $this->executeTestRequest(); if ($response->getCode() === RESPONSE_CODE::OK) { // 验证响应中包含奖励信息 $responseJson = $response->serializeToJsonString(); $responseData = json_decode($responseJson, true); // 检查是否有奖励数据 if (isset($responseData['reward'])) { $this->assertNotEmpty($responseData['reward'], '收获成功应该包含奖励信息'); if (TestEnvironment::isDebugMode()) { dump('收获奖励:', $responseData['reward']); } } // 检查是否有扣除数据(如工具消耗) if (isset($responseData['deduct'])) { if (TestEnvironment::isDebugMode()) { dump('收获消耗:', $responseData['deduct']); } } } $this->dumpTestEnd('收获奖励验证测试'); } /** * 创建收获请求 */ public function create_request_protobuf(): Message { $request = new Request(); $harvestRequest = new RequestLandHarvest(); // 使用当前测试配置或默认配置 $config = $this->currentTestConfig ?? TestEnvironment::getHarvestConfig(); $harvestRequest->setLandId($config['land_id']); $request->setLandHarvest($harvestRequest); return $this->createBaseRequest($request); } /** * 测试收获参数验证 */ public function testHarvestParameterValidation() { $this->dumpTestStart('收获参数验证测试'); // 测试无效土地ID $this->currentTestConfig = [ 'land_id' => -1 ]; $response = $this->executeTestRequest(); $this->assertFailureResponse($response); // 测试不存在的土地ID $this->currentTestConfig = [ 'land_id' => 99999 ]; $response = $this->executeTestRequest(); $this->assertFailureResponse($response, '土地不存在'); $this->dumpTestEnd('收获参数验证测试'); } /** * 测试所有预定义场景 */ public function testAllHarvestScenarios() { $this->dumpTestStart('所有收获场景测试'); // 测试成功场景 $successScenarios = TestConfig::getTestScenario('success_scenarios', 'harvest_success'); if (!empty($successScenarios)) { if (TestEnvironment::isDebugMode()) { dump('测试成功场景:', $successScenarios); } $this->currentTestConfig = [ 'land_id' => $successScenarios['land_id'] ]; $response = $this->executeTestRequest(); $this->assertSuccessResponse($response, '收获成功'); } $this->dumpTestEnd('所有收获场景测试'); } }