DisasterRemovalBaseTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Module\AppGame\Tests\Land;
  3. use App\Module\AppGame\Tests\TestConfig;
  4. use Google\Protobuf\Internal\Message;
  5. use Tests\Unit\ProtoRequestTest;
  6. use Uraus\Kku\Request;
  7. use Uraus\Kku\Response;
  8. /**
  9. * 灾害去除测试基类
  10. *
  11. * 提供灾害去除相关测试的通用方法和断言
  12. */
  13. abstract class DisasterRemovalBaseTest extends ProtoRequestTest
  14. {
  15. /**
  16. * 测试用户ID
  17. */
  18. protected int $testUserId;
  19. /**
  20. * 设置测试环境
  21. */
  22. public function setUp(): void
  23. {
  24. parent::setUp();
  25. $this->testUserId = TestConfig::getTestUserId();
  26. }
  27. /**
  28. * 验证成功响应
  29. */
  30. protected function assertSuccessResponse(Response $response, ?string $expectedMessage = null): void
  31. {
  32. $this->assertInstanceOf(Response::class, $response);
  33. // 检查响应码
  34. $this->assertEquals(0, $response->getCode(), '响应码应该为0表示成功');
  35. // 检查消息
  36. if ($expectedMessage) {
  37. $this->assertStringContainsString($expectedMessage, $response->getMsg());
  38. }
  39. // 输出响应内容用于调试
  40. dump('成功响应: ' . $response->serializeToJsonString());
  41. }
  42. /**
  43. * 验证失败响应
  44. */
  45. protected function assertFailureResponse(Response $response, ?string $expectedError = null): void
  46. {
  47. $this->assertInstanceOf(Response::class, $response);
  48. // 检查响应码(非0表示失败)
  49. $this->assertNotEquals(0, $response->getCode(), '响应码应该非0表示失败');
  50. // 检查错误消息
  51. if ($expectedError) {
  52. $this->assertStringContainsString($expectedError, $response->getMsg());
  53. }
  54. // 输出响应内容用于调试
  55. dump('失败响应: ' . $response->serializeToJsonString());
  56. }
  57. /**
  58. * 验证验证失败响应(400错误)
  59. */
  60. protected function assertValidationFailureResponse(Response $response, ?string $expectedError = null): void
  61. {
  62. $this->assertInstanceOf(Response::class, $response);
  63. // 检查响应码(400表示验证失败)
  64. $this->assertEquals(400, $response->getCode(), '响应码应该为400表示验证失败');
  65. // 检查错误消息
  66. if ($expectedError) {
  67. $this->assertStringContainsString($expectedError, $response->getMsg());
  68. }
  69. // 输出响应内容用于调试
  70. dump('验证失败响应: ' . $response->serializeToJsonString());
  71. }
  72. /**
  73. * 创建基础请求对象
  74. */
  75. protected function createBaseRequest(): Request
  76. {
  77. $request = new Request();
  78. // 这里可以添加通用的请求头设置,如用户认证等
  79. return $request;
  80. }
  81. /**
  82. * 测试成功场景的通用方法
  83. */
  84. protected function runSuccessScenario(string $scenarioName, callable $requestCreator, ?string $expectedMessage = null): void
  85. {
  86. dump("开始测试成功场景: {$scenarioName}");
  87. // 创建请求
  88. $this->create_request_protobuf = $requestCreator;
  89. // 发送请求并验证响应
  90. $response = $this->protobufRequest();
  91. $this->assertSuccessResponse($response, $expectedMessage);
  92. dump("成功场景测试完成: {$scenarioName}");
  93. }
  94. /**
  95. * 测试失败场景的通用方法
  96. */
  97. protected function runFailureScenario(string $scenarioName, callable $requestCreator, ?string $expectedError = null): void
  98. {
  99. dump("开始测试失败场景: {$scenarioName}");
  100. // 创建请求
  101. $this->create_request_protobuf = $requestCreator;
  102. // 发送请求并验证响应
  103. $response = $this->protobufRequest();
  104. $this->assertFailureResponse($response, $expectedError);
  105. dump("失败场景测试完成: {$scenarioName}");
  106. }
  107. /**
  108. * 测试验证失败场景的通用方法
  109. */
  110. protected function runValidationFailureScenario(string $scenarioName, callable $requestCreator, ?string $expectedError = null): void
  111. {
  112. dump("开始测试验证失败场景: {$scenarioName}");
  113. // 创建请求
  114. $this->create_request_protobuf = $requestCreator;
  115. // 发送请求并验证响应
  116. $response = $this->protobufRequest();
  117. $this->assertValidationFailureResponse($response, $expectedError);
  118. dump("验证失败场景测试完成: {$scenarioName}");
  119. }
  120. /**
  121. * 输出测试配置信息
  122. */
  123. protected function dumpTestConfig(array $config): void
  124. {
  125. dump('测试配置:', $config);
  126. }
  127. /**
  128. * 输出测试开始信息
  129. */
  130. protected function dumpTestStart(string $testName): void
  131. {
  132. dump("=== 开始测试: {$testName} ===");
  133. }
  134. /**
  135. * 输出测试结束信息
  136. */
  137. protected function dumpTestEnd(string $testName): void
  138. {
  139. dump("=== 测试完成: {$testName} ===");
  140. }
  141. /**
  142. * 创建protobuf请求的抽象方法
  143. * 子类必须实现此方法
  144. */
  145. abstract public function create_request_protobuf(): Message;
  146. }