| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- namespace App\Module\AppGame\Tests\Land;
- use App\Module\AppGame\Tests\TestConfig;
- use App\Module\AppGame\Tests\TestEnvironment;
- use Google\Protobuf\Internal\Message;
- use Tests\Unit\ProtoRequestTest;
- use Uraus\Kku\Request;
- use Uraus\Kku\Response;
- use Uraus\Kku\Common\RESPONSE_CODE;
- /**
- * 灾害去除测试基类
- *
- * 提供灾害去除相关测试的通用方法和断言
- */
- abstract class DisasterRemovalBaseTest extends ProtoRequestTest
- {
- /**
- * 测试用户ID
- */
- protected int $testUserId;
- /**
- * 设置测试环境
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->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;
- }
|