testUserId = TestConfig::getTestUserId(); } /** * 验证成功响应 */ protected function assertSuccessResponse(Response $response, ?string $expectedMessage = null): void { $this->assertInstanceOf(Response::class, $response); // 检查响应码 $this->assertEquals(0, $response->getCode(), '响应码应该为0表示成功'); // 检查消息 if ($expectedMessage) { $this->assertStringContainsString($expectedMessage, $response->getMsg()); } // 输出响应内容用于调试 dump('成功响应: ' . $response->serializeToJsonString()); } /** * 验证失败响应 */ protected function assertFailureResponse(Response $response, ?string $expectedError = null): void { $this->assertInstanceOf(Response::class, $response); // 检查响应码(非0表示失败) $this->assertNotEquals(0, $response->getCode(), '响应码应该非0表示失败'); // 检查错误消息 if ($expectedError) { $this->assertStringContainsString($expectedError, $response->getMsg()); } // 输出响应内容用于调试 dump('失败响应: ' . $response->serializeToJsonString()); } /** * 验证验证失败响应(400错误) */ protected function assertValidationFailureResponse(Response $response, ?string $expectedError = null): void { $this->assertInstanceOf(Response::class, $response); // 检查响应码(400表示验证失败) $this->assertEquals(400, $response->getCode(), '响应码应该为400表示验证失败'); // 检查错误消息 if ($expectedError) { $this->assertStringContainsString($expectedError, $response->getMsg()); } // 输出响应内容用于调试 dump('验证失败响应: ' . $response->serializeToJsonString()); } /** * 创建基础请求对象 */ protected function createBaseRequest(): Request { $request = new Request(); // 这里可以添加通用的请求头设置,如用户认证等 return $request; } /** * 测试成功场景的通用方法 */ protected function runSuccessScenario(string $scenarioName, callable $requestCreator, ?string $expectedMessage = null): void { dump("开始测试成功场景: {$scenarioName}"); // 创建请求 $this->create_request_protobuf = $requestCreator; // 发送请求并验证响应 $response = $this->protobufRequest(); $this->assertSuccessResponse($response, $expectedMessage); dump("成功场景测试完成: {$scenarioName}"); } /** * 测试失败场景的通用方法 */ protected function runFailureScenario(string $scenarioName, callable $requestCreator, ?string $expectedError = null): void { dump("开始测试失败场景: {$scenarioName}"); // 创建请求 $this->create_request_protobuf = $requestCreator; // 发送请求并验证响应 $response = $this->protobufRequest(); $this->assertFailureResponse($response, $expectedError); dump("失败场景测试完成: {$scenarioName}"); } /** * 测试验证失败场景的通用方法 */ protected function runValidationFailureScenario(string $scenarioName, callable $requestCreator, ?string $expectedError = null): void { dump("开始测试验证失败场景: {$scenarioName}"); // 创建请求 $this->create_request_protobuf = $requestCreator; // 发送请求并验证响应 $response = $this->protobufRequest(); $this->assertValidationFailureResponse($response, $expectedError); dump("验证失败场景测试完成: {$scenarioName}"); } /** * 输出测试配置信息 */ protected function dumpTestConfig(array $config): void { dump('测试配置:', $config); } /** * 输出测试开始信息 */ protected function dumpTestStart(string $testName): void { dump("=== 开始测试: {$testName} ==="); } /** * 输出测试结束信息 */ protected function dumpTestEnd(string $testName): void { dump("=== 测试完成: {$testName} ==="); } /** * 创建protobuf请求的抽象方法 * 子类必须实现此方法 */ abstract public function create_request_protobuf(): Message; }