| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- <?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\RequestLandSow;
- use Uraus\Kku\Common\RESPONSE_CODE;
- /**
- * 播种Handler E2E测试
- *
- * 测试播种功能的各种场景,包括成功、失败和边界情况
- */
- class SowHandlerTest extends DisasterRemovalBaseTest
- {
- /**
- * 当前测试配置
- */
- private array $currentTestConfig;
- /**
- * 测试播种成功场景
- */
- public function testSowSuccess()
- {
- $this->dumpTestStart('播种成功测试');
-
- $config = TestEnvironment::getSowConfig();
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertSuccessResponse($response, '播种成功');
-
- $this->dumpTestEnd('播种成功测试');
- }
- /**
- * 测试使用无效种子播种
- */
- public function testSowWithInvalidSeed()
- {
- $this->dumpTestStart('无效种子播种测试');
-
- $config = TestEnvironment::getSowConfig();
- $config['seed_id'] = TestEnvironment::get('TEST_INVALID_ITEM_ID', 999);
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response, '不是种子物品');
-
- $this->dumpTestEnd('无效种子播种测试');
- }
- /**
- * 测试在其他用户土地播种
- */
- public function testSowOnOtherUserLand()
- {
- $this->dumpTestStart('其他用户土地播种测试');
-
- $config = TestEnvironment::getSowConfig();
- $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 testSowOnOccupiedLand()
- {
- $this->dumpTestStart('已占用土地播种测试');
-
- $config = TestEnvironment::getSowConfig();
- $config['land_id'] = TestEnvironment::get('TEST_OCCUPIED_LAND_ID', 40);
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response, '土地已有作物');
-
- $this->dumpTestEnd('已占用土地播种测试');
- }
- /**
- * 测试播种性能
- */
- public function testSowPerformance()
- {
- $this->dumpTestStart('播种性能测试');
-
- $config = TestEnvironment::getSowConfig();
- $performanceConfig = TestEnvironment::getPerformanceConfig();
- $requestCount = $performanceConfig['request_count'];
- $maxResponseTime = $performanceConfig['max_response_time'];
-
- $this->dumpTestConfig([
- 'sow_config' => $config,
- 'request_count' => $requestCount,
- 'max_response_time' => $maxResponseTime
- ]);
-
- $startTime = microtime(true);
- $responseTimes = [];
-
- for ($i = 1; $i <= $requestCount; $i++) {
- $requestStartTime = microtime(true);
- // 使用不同的土地ID避免冲突
- $testConfig = $config;
- $testConfig['land_id'] = $config['land_id'] + $i;
- $this->currentTestConfig = $testConfig;
-
- $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 testRepeatedSow()
- {
- $this->dumpTestStart('重复播种测试');
-
- $config = TestEnvironment::getSowConfig();
- $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 testBatchSow()
- {
- $this->dumpTestStart('批量播种测试');
-
- $baseConfig = TestEnvironment::getSowConfig();
- $landIds = [
- TestEnvironment::get('TEST_SOW_LAND_ID', 10),
- TestEnvironment::get('TEST_SOW_LAND_ID_2', 50),
- TestEnvironment::get('TEST_SOW_LAND_ID_3', 51)
- ];
-
- $successCount = 0;
- $failureCount = 0;
-
- foreach ($landIds as $index => $landId) {
- $this->currentTestConfig = [
- 'land_id' => $landId,
- 'seed_id' => $baseConfig['seed_id']
- ];
-
- $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 testDifferentSeedTypes()
- {
- $this->dumpTestStart('不同种子类型播种测试');
-
- $seedIds = [
- TestEnvironment::get('TEST_SOW_SEED_ID', 201),
- TestEnvironment::get('TEST_SOW_SEED_ID_2', 202),
- TestEnvironment::get('TEST_SOW_SEED_ID_3', 203)
- ];
-
- $baseLandId = TestEnvironment::get('TEST_SOW_LAND_ID', 10);
-
- foreach ($seedIds as $index => $seedId) {
- $this->currentTestConfig = [
- 'land_id' => $baseLandId + $index + 10, // 使用不同的土地
- 'seed_id' => $seedId
- ];
-
- $response = $this->executeTestRequest();
-
- if (TestEnvironment::isDebugMode()) {
- dump("种子 {$seedId} 播种结果: " . $response->getCode() . " - " . $response->getMsg());
- }
-
- // 记录结果但不强制要求成功(因为种子可能不存在)
- $this->assertInstanceOf(\Uraus\Kku\Response::class, $response);
- }
-
- $this->dumpTestEnd('不同种子类型播种测试');
- }
- /**
- * 测试播种资源消耗验证
- */
- public function testSowResourceConsumption()
- {
- $this->dumpTestStart('播种资源消耗验证测试');
-
- $config = TestEnvironment::getSowConfig();
- $this->currentTestConfig = $config;
-
- $response = $this->executeTestRequest();
-
- if ($response->getCode() === RESPONSE_CODE::OK) {
- // 验证响应中包含消耗信息
- $responseJson = $response->serializeToJsonString();
- $responseData = json_decode($responseJson, true);
-
- // 检查是否有扣除数据(种子消耗)
- if (isset($responseData['deduct'])) {
- $this->assertNotEmpty($responseData['deduct'], '播种成功应该包含资源消耗信息');
- if (TestEnvironment::isDebugMode()) {
- dump('播种消耗:', $responseData['deduct']);
- }
- }
-
- // 检查是否有最新数据更新
- if (isset($responseData['last_data'])) {
- if (TestEnvironment::isDebugMode()) {
- dump('数据更新:', $responseData['last_data']);
- }
- }
- }
-
- $this->dumpTestEnd('播种资源消耗验证测试');
- }
- /**
- * 创建播种请求
- */
- public function create_request_protobuf(): Message
- {
- $request = new Request();
- $sowRequest = new RequestLandSow();
-
- // 使用当前测试配置或默认配置
- $config = $this->currentTestConfig ?? TestEnvironment::getSowConfig();
-
- $sowRequest->setLandId($config['land_id']);
- $sowRequest->setUserItemId($config['seed_id']);
-
- $request->setLandSow($sowRequest);
-
- return $this->createBaseRequest($request);
- }
- /**
- * 测试播种参数验证
- */
- public function testSowParameterValidation()
- {
- $this->dumpTestStart('播种参数验证测试');
-
- // 测试无效土地ID
- $this->currentTestConfig = [
- 'land_id' => -1,
- 'seed_id' => TestEnvironment::get('TEST_SOW_SEED_ID', 201)
- ];
-
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response);
-
- // 测试无效种子ID
- $this->currentTestConfig = [
- 'land_id' => TestEnvironment::get('TEST_SOW_LAND_ID', 10),
- 'seed_id' => -1
- ];
-
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response);
-
- $this->dumpTestEnd('播种参数验证测试');
- }
- /**
- * 测试所有预定义场景
- */
- public function testAllSowScenarios()
- {
- $this->dumpTestStart('所有播种场景测试');
-
- // 测试成功场景
- $successScenarios = TestConfig::getTestScenario('success_scenarios', 'sow_success');
- if (!empty($successScenarios)) {
- if (TestEnvironment::isDebugMode()) {
- dump('测试成功场景:', $successScenarios);
- }
- $this->currentTestConfig = [
- 'land_id' => $successScenarios['land_id'],
- 'seed_id' => $successScenarios['seed_id']
- ];
-
- $response = $this->executeTestRequest();
- $this->assertSuccessResponse($response, '播种成功');
- }
-
- $this->dumpTestEnd('所有播种场景测试');
- }
- }
|