| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?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 Uraus\Kku\Request;
- use Uraus\Kku\Request\RequestLandHarvest;
- use Uraus\Kku\Common\RESPONSE_CODE;
- /**
- * 收获Handler E2E测试
- *
- * 测试收获功能的各种场景,包括成功、失败和边界情况
- */
- class HarvestHandlerTest extends DisasterRemovalBaseTest
- {
- /**
- * 当前测试配置
- */
- private array $currentTestConfig;
- /**
- * 测试收获成功场景
- */
- public function testHarvestSuccess()
- {
- $this->dumpTestStart('收获成功测试');
-
- $config = TestEnvironment::getHarvestConfig();
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertSuccessResponse($response, '收获成功');
-
- $this->dumpTestEnd('收获成功测试');
- }
- /**
- * 测试收获未成熟作物
- */
- public function testHarvestImmatureCrop()
- {
- $this->dumpTestStart('收获未成熟作物测试');
-
- $config = TestEnvironment::getHarvestConfig();
- // 使用一个未成熟的作物土地
- $config['land_id'] = TestEnvironment::get('TEST_IMMATURE_CROP_LAND_ID', 20);
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response, '作物未成熟');
-
- $this->dumpTestEnd('收获未成熟作物测试');
- }
- /**
- * 测试收获其他用户土地
- */
- public function testHarvestOtherUserLand()
- {
- $this->dumpTestStart('收获其他用户土地测试');
-
- $config = TestEnvironment::getHarvestConfig();
- $config['land_id'] = TestEnvironment::get('TEST_OTHER_USER_LAND_ID', 5);
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response, '土地不存在或不属于当前用户');
-
- $this->dumpTestEnd('收获其他用户土地测试');
- }
- /**
- * 测试收获空土地
- */
- public function testHarvestEmptyLand()
- {
- $this->dumpTestStart('收获空土地测试');
-
- $config = TestEnvironment::getHarvestConfig();
- $config['land_id'] = TestEnvironment::get('TEST_EMPTY_LAND_ID', 30);
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response, '土地上没有作物');
-
- $this->dumpTestEnd('收获空土地测试');
- }
- /**
- * 测试收获性能
- */
- public function testHarvestPerformance()
- {
- $this->dumpTestStart('收获性能测试');
-
- $config = TestEnvironment::getHarvestConfig();
- $performanceConfig = TestEnvironment::getPerformanceConfig();
- $requestCount = $performanceConfig['request_count'];
- $maxResponseTime = $performanceConfig['max_response_time'];
-
- $this->dumpTestConfig([
- 'harvest_config' => $config,
- 'request_count' => $requestCount,
- 'max_response_time' => $maxResponseTime
- ]);
-
- $startTime = microtime(true);
- $responseTimes = [];
-
- for ($i = 1; $i <= $requestCount; $i++) {
- $requestStartTime = microtime(true);
- $this->currentTestConfig = $config;
-
- $response = $this->executeTestRequest();
-
- $requestEndTime = microtime(true);
- $requestDuration = ($requestEndTime - $requestStartTime) * 1000;
- $responseTimes[] = $requestDuration;
-
- $this->assertLessThan($maxResponseTime, $requestDuration,
- "第 {$i} 个请求响应时间 {$requestDuration}ms 超过限制 {$maxResponseTime}ms");
-
- if (TestEnvironment::isDebugMode()) {
- dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode());
- }
- }
-
- $totalTime = (microtime(true) - $startTime) * 1000;
- $avgTime = array_sum($responseTimes) / count($responseTimes);
- $maxTime = max($responseTimes);
- $minTime = min($responseTimes);
-
- if (TestEnvironment::isDebugMode()) {
- dump("收获性能测试结果:", [
- 'total_requests' => $requestCount,
- 'total_time' => $totalTime . 'ms',
- 'average_time' => $avgTime . 'ms',
- 'max_time' => $maxTime . 'ms',
- 'min_time' => $minTime . 'ms',
- 'max_allowed_time' => $maxResponseTime . 'ms'
- ]);
- }
-
- $this->dumpTestEnd('收获性能测试');
- }
- /**
- * 测试重复收获
- */
- public function testRepeatedHarvest()
- {
- $this->dumpTestStart('重复收获测试');
-
- $config = TestEnvironment::getHarvestConfig();
- $this->currentTestConfig = $config;
-
- // 第一次收获
- $response1 = $this->executeTestRequest();
- $this->dumpResponse($response1, '第一次收获响应');
-
- // 第二次收获(应该失败,因为已经收获过了)
- $response2 = $this->executeTestRequest();
- $this->dumpResponse($response2, '第二次收获响应');
-
- // 验证第一次成功,第二次失败
- if ($response1->getCode() === RESPONSE_CODE::OK) {
- $this->assertNotEquals(RESPONSE_CODE::OK, $response2->getCode(),
- '第二次收获应该失败,因为作物已被收获');
- }
-
- $this->dumpTestEnd('重复收获测试');
- }
- /**
- * 测试批量收获
- */
- public function testBatchHarvest()
- {
- $this->dumpTestStart('批量收获测试');
-
- $landIds = [
- TestEnvironment::get('TEST_HARVEST_LAND_ID', 11),
- TestEnvironment::get('TEST_HARVEST_LAND_ID_2', 21),
- TestEnvironment::get('TEST_HARVEST_LAND_ID_3', 22)
- ];
-
- $successCount = 0;
- $failureCount = 0;
-
- foreach ($landIds as $index => $landId) {
- $this->currentTestConfig = [
- 'land_id' => $landId
- ];
-
- $response = $this->executeTestRequest();
-
- if ($response->getCode() === RESPONSE_CODE::OK) {
- $successCount++;
- if (TestEnvironment::isDebugMode()) {
- dump("土地 {$landId} 收获成功");
- }
- } else {
- $failureCount++;
- if (TestEnvironment::isDebugMode()) {
- dump("土地 {$landId} 收获失败: " . $response->getMsg());
- }
- }
- }
-
- if (TestEnvironment::isDebugMode()) {
- dump("批量收获结果:", [
- 'total_lands' => count($landIds),
- 'success_count' => $successCount,
- 'failure_count' => $failureCount
- ]);
- }
-
- // 至少应该有一个成功
- $this->assertGreaterThan(0, $successCount, '批量收获中至少应有一个成功');
-
- $this->dumpTestEnd('批量收获测试');
- }
- /**
- * 测试收获奖励验证
- */
- public function testHarvestRewardValidation()
- {
- $this->dumpTestStart('收获奖励验证测试');
-
- $config = TestEnvironment::getHarvestConfig();
- $this->currentTestConfig = $config;
-
- $response = $this->executeTestRequest();
-
- if ($response->getCode() === RESPONSE_CODE::OK) {
- // 验证响应中包含奖励信息
- $responseJson = $response->serializeToJsonString();
- $responseData = json_decode($responseJson, true);
-
- // 检查是否有奖励数据
- if (isset($responseData['reward'])) {
- $this->assertNotEmpty($responseData['reward'], '收获成功应该包含奖励信息');
- if (TestEnvironment::isDebugMode()) {
- dump('收获奖励:', $responseData['reward']);
- }
- }
-
- // 检查是否有扣除数据(如工具消耗)
- if (isset($responseData['deduct'])) {
- if (TestEnvironment::isDebugMode()) {
- dump('收获消耗:', $responseData['deduct']);
- }
- }
- }
-
- $this->dumpTestEnd('收获奖励验证测试');
- }
- /**
- * 创建收获请求
- */
- public function create_request_protobuf(): Message
- {
- $request = new Request();
- $harvestRequest = new RequestLandHarvest();
-
- // 使用当前测试配置或默认配置
- $config = $this->currentTestConfig ?? TestEnvironment::getHarvestConfig();
-
- $harvestRequest->setLandId($config['land_id']);
-
- $request->setLandHarvest($harvestRequest);
-
- return $this->createBaseRequest($request);
- }
- /**
- * 测试收获参数验证
- */
- public function testHarvestParameterValidation()
- {
- $this->dumpTestStart('收获参数验证测试');
-
- // 测试无效土地ID
- $this->currentTestConfig = [
- 'land_id' => -1
- ];
-
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response);
-
- // 测试不存在的土地ID
- $this->currentTestConfig = [
- 'land_id' => 99999
- ];
-
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response, '土地不存在');
-
- $this->dumpTestEnd('收获参数验证测试');
- }
- /**
- * 测试所有预定义场景
- */
- public function testAllHarvestScenarios()
- {
- $this->dumpTestStart('所有收获场景测试');
-
- // 测试成功场景
- $successScenarios = TestConfig::getTestScenario('success_scenarios', 'harvest_success');
- if (!empty($successScenarios)) {
- if (TestEnvironment::isDebugMode()) {
- dump('测试成功场景:', $successScenarios);
- }
- $this->currentTestConfig = [
- 'land_id' => $successScenarios['land_id']
- ];
-
- $response = $this->executeTestRequest();
- $this->assertSuccessResponse($response, '收获成功');
- }
-
- $this->dumpTestEnd('所有收获场景测试');
- }
- }
|