| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace App\Module\AppGame\Tests\Land;
- use App\Module\AppGame\Tests\TestConfig;
- use Google\Protobuf\Internal\Message;
- use Tests\Unit\ProtoRequestTest;
- use Uraus\Kku\Request;
- use Uraus\Kku\Response;
- /**
- * 灾害去除测试基类
- *
- * 提供灾害去除相关测试的通用方法和断言
- */
- 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);
- // 检查响应码
- $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;
- }
|