dumpTestStart('除虫成功测试'); $config = TestConfig::getPesticideTestConfig(); $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertSuccessResponse($response, '除虫成功'); $this->dumpTestEnd('除虫成功测试'); } /** * 测试使用无效物品除虫 */ public function testPesticideWithInvalidItem() { $this->dumpTestStart('无效物品除虫测试'); $config = TestConfig::getPesticideTestConfig(); $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 testPesticideOnOtherUserLand() { $this->dumpTestStart('其他用户土地除虫测试'); $config = TestConfig::getPesticideTestConfig(); $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 testPesticideOnNormalLand() { $this->dumpTestStart('无虫害土地除虫测试'); $config = TestConfig::getPesticideTestConfig(); $config['land_id'] = TestConfig::getTestLand('normal_land')['land_id']; $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertFailureResponse($response, '灾害清理失败'); $this->dumpTestEnd('无虫害土地除虫测试'); } /** * 测试参数验证 - 缺少必填字段 */ public function testPesticideWithMissingFields() { $this->dumpTestStart('缺少必填字段测试'); $config = TestConfig::getPesticideTestConfig(); $config['land_id'] = 0; // 无效的土地ID $this->dumpTestConfig($config); $this->currentTestConfig = $config; $response = $this->protobufRequest(); $this->assertValidationFailureResponse($response); $this->dumpTestEnd('缺少必填字段测试'); } /** * 测试概率机制 - 多次测试验证概率 */ public function testPesticideProbability() { $this->dumpTestStart('除虫概率机制测试'); $config = TestConfig::getPesticideTestConfig(); $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}%"); // 允许一定的误差范围(±30%) $this->assertGreaterThanOrEqual($expectedRate - 30, $successRate, '实际成功率不应低于预期太多'); $this->assertLessThanOrEqual($expectedRate + 30, $successRate, '实际成功率不应高于预期太多'); $this->dumpTestEnd('除虫概率机制测试'); } /** * 创建除虫请求的protobuf消息 */ public function create_request_protobuf(): Message { $request = $this->createBaseRequest(); $pesticideRequest = new RequestLandPesticide(); // 使用当前测试配置 $config = $this->currentTestConfig ?? TestConfig::getPesticideTestConfig(); $pesticideRequest->setLandId($config['land_id']); $pesticideRequest->setItemId($config['item_id']); $request->setLandPesticide($pesticideRequest); dump('创建除虫请求:', [ 'land_id' => $config['land_id'], 'item_id' => $config['item_id'] ]); return $request; } /** * 测试所有预定义场景 */ public function testAllPesticideScenarios() { $this->dumpTestStart('所有除虫场景测试'); // 测试成功场景 $successScenarios = TestConfig::getTestScenario('success_scenarios', 'pesticide_success'); if (!empty($successScenarios)) { dump('测试成功场景:', $successScenarios); $this->currentTestConfig = [ 'land_id' => $successScenarios['land_id'], 'item_id' => $successScenarios['item_id'] ]; $response = $this->protobufRequest(); $this->assertSuccessResponse($response, '除虫成功'); } // 测试失败场景 $failureScenarios = TestConfig::getTestScenario('failure_scenarios', 'invalid_item'); if (!empty($failureScenarios)) { dump('测试失败场景:', $failureScenarios); $this->currentTestConfig = [ 'land_id' => $failureScenarios['land_id'], 'item_id' => $failureScenarios['item_id'] ]; $response = $this->protobufRequest(); $this->assertValidationFailureResponse($response, $failureScenarios['expected_error']); } $this->dumpTestEnd('所有除虫场景测试'); } }