testUserId = TestConfig::getTestUserId(); } /** * 验证成功响应 */ protected function assertSuccessResponse(Response $response, ?string $expectedMessage = null): void { $this->assertInstanceOf(Response::class, $response); // 检查响应码 - OK = 1 表示成功 $this->assertEquals(RESPONSE_CODE::OK, $response->getCode(), '响应码应该为OK(1)表示成功'); // 检查消息 if ($expectedMessage) { $this->assertStringContainsString($expectedMessage, $response->getMsg()); } // 输出响应内容用于调试 if (TestEnvironment::isDebugMode()) { dump('成功响应: ' . $response->serializeToJsonString()); } // 总是输出响应详情用于调试 dump('响应详情:', [ 'code' => $response->getCode(), 'code_name' => RESPONSE_CODE::name($response->getCode()), 'msg' => $response->getMsg(), 'callpath' => $response->getCallpath(), 'run_ms' => $response->getRunMs(), 'full_response' => $response->serializeToJsonString() ]); } /** * 验证失败响应 */ protected function assertFailureResponse(Response $response, ?string $expectedError = null): void { $this->assertInstanceOf(Response::class, $response); // 检查响应码(非OK表示失败) $this->assertNotEquals(RESPONSE_CODE::OK, $response->getCode(), '响应码应该非OK表示失败'); // 检查错误消息 if ($expectedError) { $this->assertStringContainsString($expectedError, $response->getMsg()); } // 输出响应内容用于调试 if (TestEnvironment::isDebugMode()) { dump('失败响应: ' . $response->serializeToJsonString()); } } /** * 验证验证失败响应 */ protected function assertValidationFailureResponse(Response $response, ?string $expectedError = null): void { $this->assertInstanceOf(Response::class, $response); // 检查响应码(VALIDATE_ERROR = 2 表示验证失败) $this->assertEquals(RESPONSE_CODE::VALIDATE_ERROR, $response->getCode(), '响应码应该为VALIDATE_ERROR(2)表示验证失败'); // 检查错误消息 if ($expectedError) { $this->assertStringContainsString($expectedError, $response->getMsg()); } // 输出响应内容用于调试 if (TestEnvironment::isDebugMode()) { dump('验证失败响应: ' . $response->serializeToJsonString()); } } /** * 用户登录token缓存 */ private static ?string $userToken = null; /** * 获取用户登录token */ protected function getUserToken(): string { if (self::$userToken === null) { self::$userToken = $this->performLogin(); } return self::$userToken; } /** * 执行用户登录,获取token */ private function performLogin(): string { // 暂时返回一个模拟的token,实际应用中需要真正的登录流程 $testUser = TestEnvironment::getTestUser(); $token = 'test_token_' . $testUser['user_id'] . '_' . time(); if (TestEnvironment::isDebugMode()) { dump('模拟用户登录,token: ' . $token); } return $token; } /** * 创建基础请求对象,包含认证信息 */ protected function createBaseRequest(?Request $request = null): Request { if ($request === null) { $request = new Request(); } // 获取用户token $token = $this->getUserToken(); // 设置token到请求中 $tokenRequest = new \Uraus\Kku\Request\RequestPublicToken(); $tokenRequest->setTimes(time()); $request->setPublicToken($tokenRequest); // 这里需要设置session或其他认证方式 // 由于protobuf的限制,我们可能需要通过其他方式传递token if (TestEnvironment::isDebugMode()) { dump('创建基础请求,token: ' . $token); } return $request; } /** * 执行测试请求 */ protected function executeTestRequest(): Response { $response = $this->protobufRequest(); // 确保返回的是Response类型 if (!$response instanceof Response) { throw new \RuntimeException('protobufRequest应该返回Response对象'); } return $response; } /** * 测试成功场景的通用方法 */ protected function runSuccessScenario(string $scenarioName, ?string $expectedMessage = null): void { if (TestEnvironment::isDebugMode()) { dump("开始测试成功场景: {$scenarioName}"); } // 发送请求并验证响应 $response = $this->executeTestRequest(); $this->assertSuccessResponse($response, $expectedMessage); if (TestEnvironment::isDebugMode()) { dump("成功场景测试完成: {$scenarioName}"); } } /** * 测试失败场景的通用方法 */ protected function runFailureScenario(string $scenarioName, ?string $expectedError = null): void { if (TestEnvironment::isDebugMode()) { dump("开始测试失败场景: {$scenarioName}"); } // 发送请求并验证响应 $response = $this->executeTestRequest(); $this->assertFailureResponse($response, $expectedError); if (TestEnvironment::isDebugMode()) { dump("失败场景测试完成: {$scenarioName}"); } } /** * 测试验证失败场景的通用方法 */ protected function runValidationFailureScenario(string $scenarioName, ?string $expectedError = null): void { if (TestEnvironment::isDebugMode()) { dump("开始测试验证失败场景: {$scenarioName}"); } // 发送请求并验证响应 $response = $this->executeTestRequest(); $this->assertValidationFailureResponse($response, $expectedError); if (TestEnvironment::isDebugMode()) { dump("验证失败场景测试完成: {$scenarioName}"); } } /** * 输出测试配置信息 */ protected function dumpTestConfig(array $config): void { if (TestEnvironment::isDebugMode()) { dump('测试配置:', $config); } } /** * 输出测试开始信息 */ protected function dumpTestStart(string $testName): void { if (TestEnvironment::isDebugMode()) { dump("=== 开始测试: {$testName} ==="); } } /** * 输出测试结束信息 */ protected function dumpTestEnd(string $testName): void { if (TestEnvironment::isDebugMode()) { dump("=== 测试完成: {$testName} ==="); } } /** * 输出响应详情 */ protected function dumpResponse(Response $response, string $label = '响应'): void { if (TestEnvironment::isDebugMode()) { dump("{$label}详情:", [ 'code' => $response->getCode(), 'msg' => $response->getMsg(), 'callpath' => $response->getCallpath(), 'run_ms' => $response->getRunMs(), 'json' => $response->serializeToJsonString() ]); } } /** * 验证概率测试结果 */ protected function assertProbabilityResult(int $successCount, int $totalAttempts, int $expectedRate, int $errorMargin = 30): void { $actualRate = ($successCount / $totalAttempts) * 100; $minRate = $expectedRate - $errorMargin; $maxRate = $expectedRate + $errorMargin; $this->assertGreaterThanOrEqual($minRate, $actualRate, "成功率 {$actualRate}% 低于预期范围 {$minRate}%-{$maxRate}%"); $this->assertLessThanOrEqual($maxRate, $actualRate, "成功率 {$actualRate}% 高于预期范围 {$minRate}%-{$maxRate}%"); if (TestEnvironment::isDebugMode()) { dump("概率测试结果:", [ 'success_count' => $successCount, 'total_attempts' => $totalAttempts, 'actual_rate' => $actualRate, 'expected_rate' => $expectedRate, 'error_margin' => $errorMargin, 'result' => '通过' ]); } } /** * 创建protobuf请求的抽象方法 * 子类必须实现此方法 */ abstract public function create_request_protobuf(): Message; }