dumpTestStart('浇水成功测试'); $config = TestConfig::getWateringTestConfig(); $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertSuccessResponse($response, '浇水成功'); $this->dumpTestEnd('浇水成功测试'); } /** * 测试使用无效物品浇水 */ public function testWateringWithInvalidItem() { $this->dumpTestStart('无效物品浇水测试'); $config = TestConfig::getWateringTestConfig(); $config['item_id'] = TestConfig::getTestItem('invalid_item')['item_id']; $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertValidationFailureResponse($response, '不是浇水物品'); $this->dumpTestEnd('无效物品浇水测试'); } /** * 测试使用除虫剂进行浇水(物品类型不匹配) */ public function testWateringWithWrongItemType() { $this->dumpTestStart('错误物品类型浇水测试'); $config = TestConfig::getWateringTestConfig(); $config['item_id'] = TestConfig::getTestItem('pesticide')['item_id']; // 使用除虫剂 $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertValidationFailureResponse($response, '不是浇水物品'); $this->dumpTestEnd('错误物品类型浇水测试'); } /** * 测试对其他用户土地浇水 */ public function testWateringOnOtherUserLand() { $this->dumpTestStart('其他用户土地浇水测试'); $config = TestConfig::getWateringTestConfig(); $config['land_id'] = TestConfig::getTestLand('other_user_land')['land_id']; $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertValidationFailureResponse($response, '土地不存在或不属于当前用户'); $this->dumpTestEnd('其他用户土地浇水测试'); } /** * 测试对无干旱土地浇水 */ public function testWateringOnNormalLand() { $this->dumpTestStart('无干旱土地浇水测试'); $config = TestConfig::getWateringTestConfig(); $config['land_id'] = TestConfig::getTestLand('normal_land')['land_id']; $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertFailureResponse($response, '灾害清理失败'); $this->dumpTestEnd('无干旱土地浇水测试'); } /** * 测试参数验证 - 无效的物品ID */ public function testWateringWithInvalidItemId() { $this->dumpTestStart('无效物品ID测试'); $config = TestConfig::getWateringTestConfig(); $config['item_id'] = 0; // 无效的物品ID $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertValidationFailureResponse($response); $this->dumpTestEnd('无效物品ID测试'); } /** * 测试概率机制 - 多次测试验证概率 */ public function testWateringProbability() { $this->dumpTestStart('浇水概率机制测试'); $config = TestConfig::getWateringTestConfig(); $this->dumpTestConfig($config); $successCount = 0; $totalAttempts = 10; // 测试10次 dump("开始进行 {$totalAttempts} 次浇水测试,验证概率机制"); for ($i = 1; $i <= $totalAttempts; $i++) { dump("第 {$i} 次测试"); $this->currentTestConfig = $config; $response = $this->protobufRequest(); if ($response->getCode() === 0) { $successCount++; dump("第 {$i} 次测试成功"); } else { dump("第 {$i} 次测试失败: " . $response->getMsg()); } } $successRate = ($successCount / $totalAttempts) * 100; $expectedRate = $config['expected_success_rate']; dump("测试结果: {$successCount}/{$totalAttempts} 成功,实际成功率: {$successRate}%,预期成功率: {$expectedRate}%"); // 浇水通常有较高的成功率,允许一定的误差范围(±20%) $this->assertGreaterThanOrEqual($expectedRate - 20, $successRate, '实际成功率不应低于预期太多'); $this->assertLessThanOrEqual($expectedRate + 20, $successRate, '实际成功率不应高于预期太多'); $this->dumpTestEnd('浇水概率机制测试'); } /** * 测试边界情况 - 100%成功率物品 */ public function testWateringWithHundredPercentSuccessRate() { $this->dumpTestStart('100%成功率浇水测试'); $config = TestConfig::getWateringTestConfig(); // 假设有一个100%成功率的浇水道具用于测试 $config['item_id'] = 1030; // 假设的100%成功率浇水道具ID $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); // 100%成功率应该总是成功 $this->assertSuccessResponse($response, '浇水成功'); $this->dumpTestEnd('100%成功率浇水测试'); } /** * 测试重复浇水同一块土地 */ public function testRepeatedWateringOnSameLand() { $this->dumpTestStart('重复浇水测试'); $config = TestConfig::getWateringTestConfig(); $this->dumpTestConfig($config); // 第一次浇水 dump("第一次浇水"); $this->currentTestConfig = $config; $firstResponse = $this->protobufRequest(); dump("第一次浇水响应: " . $firstResponse->serializeToJsonString()); // 第二次浇水(如果第一次成功,第二次应该失败) dump("第二次浇水"); $this->currentTestConfig = $config; $secondResponse = $this->protobufRequest(); dump("第二次浇水响应: " . $secondResponse->serializeToJsonString()); // 如果第一次成功,第二次应该失败(因为已经没有干旱了) if ($firstResponse->getCode() === 0) { $this->assertFailureResponse($secondResponse, '灾害清理失败'); dump("符合预期:第一次成功后,第二次失败"); } else { dump("第一次失败,第二次结果不确定"); } $this->dumpTestEnd('重复浇水测试'); } /** * 创建浇水请求的protobuf消息 */ public function create_request_protobuf(): Message { $request = $this->createBaseRequest(); $wateringRequest = new RequestLandWatering(); // 使用当前测试配置 $config = $this->currentTestConfig ?? TestConfig::getWateringTestConfig(); $wateringRequest->setLandId($config['land_id']); $wateringRequest->setUserItemId($config['item_id']); $request->setLandWatering($wateringRequest); dump('创建浇水请求:', [ 'land_id' => $config['land_id'], 'user_item_id' => $config['item_id'] ]); return $request; } /** * 测试所有预定义场景 */ public function testAllWateringScenarios() { $this->dumpTestStart('所有浇水场景测试'); // 测试成功场景 $successScenarios = TestConfig::getTestScenario('success_scenarios', 'watering_success'); if (!empty($successScenarios)) { dump('测试成功场景:', $successScenarios); $this->currentTestConfig = [ 'land_id' => $successScenarios['land_id'], 'item_id' => $successScenarios['item_id'] ]; $response = $this->protobufRequest(); $this->assertSuccessResponse($response, '浇水成功'); } $this->dumpTestEnd('所有浇水场景测试'); } /** * 测试性能 - 大量浇水请求 */ public function testWateringPerformance() { $this->dumpTestStart('浇水性能测试'); $config = TestConfig::getWateringTestConfig(); $this->dumpTestConfig($config); $startTime = microtime(true); $requestCount = 5; // 测试5个请求 dump("开始性能测试,发送 {$requestCount} 个浇水请求"); for ($i = 1; $i <= $requestCount; $i++) { $requestStartTime = microtime(true); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $requestEndTime = microtime(true); $requestDuration = ($requestEndTime - $requestStartTime) * 1000; // 转换为毫秒 dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode()); } $endTime = microtime(true); $totalDuration = ($endTime - $startTime) * 1000; // 转换为毫秒 $averageDuration = $totalDuration / $requestCount; dump("性能测试结果: 总耗时 {$totalDuration}ms,平均耗时 {$averageDuration}ms"); // 验证平均响应时间不超过5秒 $this->assertLessThan(5000, $averageDuration, '平均响应时间不应超过5秒'); $this->dumpTestEnd('浇水性能测试'); } }