| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?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\RequestLandFertilizer;
- use Uraus\Kku\Common\RESPONSE_CODE;
- /**
- * 施肥Handler E2E测试
- *
- * 测试施肥功能的各种场景,包括成功、失败和边界情况
- */
- class FertilizerHandlerTest extends DisasterRemovalBaseTest
- {
- /**
- * 当前测试配置
- */
- private array $currentTestConfig;
- /**
- * 测试施肥成功场景
- */
- public function testFertilizerSuccess()
- {
- $this->dumpTestStart('施肥成功测试');
-
- $config = TestEnvironment::getFertilizerConfig();
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertSuccessResponse($response, '施肥成功');
-
- $this->dumpTestEnd('施肥成功测试');
- }
- /**
- * 测试使用无效物品施肥
- */
- public function testFertilizerWithInvalidItem()
- {
- $this->dumpTestStart('无效物品施肥测试');
-
- $config = TestEnvironment::getFertilizerConfig();
- $config['item_id'] = TestEnvironment::get('TEST_INVALID_ITEM_ID', 999);
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response, '不是肥料物品');
-
- $this->dumpTestEnd('无效物品施肥测试');
- }
- /**
- * 测试对其他用户土地施肥
- */
- public function testFertilizerOnOtherUserLand()
- {
- $this->dumpTestStart('其他用户土地施肥测试');
-
- $config = TestEnvironment::getFertilizerConfig();
- $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 testFertilizerPerformance()
- {
- $this->dumpTestStart('施肥性能测试');
-
- $config = TestEnvironment::getFertilizerConfig();
- $performanceConfig = TestEnvironment::getPerformanceConfig();
- $requestCount = $performanceConfig['request_count'];
- $maxResponseTime = $performanceConfig['max_response_time'];
-
- $this->dumpTestConfig([
- 'fertilizer_config' => $config,
- 'request_count' => $requestCount,
- 'max_response_time' => $maxResponseTime
- ]);
-
- $startTime = microtime(true);
-
- for ($i = 1; $i <= $requestCount; $i++) {
- $requestStartTime = microtime(true);
- $this->currentTestConfig = $config;
-
- $response = $this->executeTestRequest();
-
- $requestEndTime = microtime(true);
- $requestDuration = ($requestEndTime - $requestStartTime) * 1000;
-
- $this->assertLessThan($maxResponseTime, $requestDuration,
- "第 {$i} 个请求响应时间 {$requestDuration}ms 超过限制 {$maxResponseTime}ms");
-
- if (TestEnvironment::isDebugMode()) {
- dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode());
- }
- }
-
- $totalTime = (microtime(true) - $startTime) * 1000;
- $avgTime = $totalTime / $requestCount;
-
- if (TestEnvironment::isDebugMode()) {
- dump("性能测试结果:", [
- 'total_requests' => $requestCount,
- 'total_time' => $totalTime . 'ms',
- 'average_time' => $avgTime . 'ms',
- 'max_allowed_time' => $maxResponseTime . 'ms'
- ]);
- }
-
- $this->dumpTestEnd('施肥性能测试');
- }
- /**
- * 测试重复施肥
- */
- public function testRepeatedFertilizer()
- {
- $this->dumpTestStart('重复施肥测试');
-
- $config = TestEnvironment::getFertilizerConfig();
- $this->currentTestConfig = $config;
-
- // 第一次施肥
- $response1 = $this->executeTestRequest();
- $this->dumpResponse($response1, '第一次施肥响应');
-
- // 第二次施肥(可能失败,因为已经施过肥)
- $response2 = $this->executeTestRequest();
- $this->dumpResponse($response2, '第二次施肥响应');
-
- // 验证至少有一次成功
- $hasSuccess = ($response1->getCode() === RESPONSE_CODE::OK) ||
- ($response2->getCode() === RESPONSE_CODE::OK);
- $this->assertTrue($hasSuccess, '至少应有一次施肥成功');
-
- $this->dumpTestEnd('重复施肥测试');
- }
- /**
- * 测试所有预定义场景
- */
- public function testAllFertilizerScenarios()
- {
- $this->dumpTestStart('所有施肥场景测试');
-
- // 测试成功场景
- $successScenarios = TestConfig::getTestScenario('success_scenarios', 'fertilizer_success');
- if (!empty($successScenarios)) {
- if (TestEnvironment::isDebugMode()) {
- dump('测试成功场景:', $successScenarios);
- }
- $this->currentTestConfig = [
- 'land_id' => $successScenarios['land_id'],
- 'item_id' => $successScenarios['item_id']
- ];
-
- $response = $this->executeTestRequest();
- $this->assertSuccessResponse($response, '施肥成功');
- }
-
- $this->dumpTestEnd('所有施肥场景测试');
- }
- /**
- * 创建施肥请求
- */
- public function create_request_protobuf(): Message
- {
- $request = new Request();
- $fertilizerRequest = new RequestLandFertilizer();
-
- // 使用当前测试配置或默认配置
- $config = $this->currentTestConfig ?? TestEnvironment::getFertilizerConfig();
-
- $fertilizerRequest->setLandId($config['land_id']);
- $fertilizerRequest->setUserItemId($config['item_id']);
-
- $request->setLandFertilizer($fertilizerRequest);
-
- return $this->createBaseRequest($request);
- }
- /**
- * 测试并发施肥请求
- */
- public function testConcurrentFertilizerRequests()
- {
- $this->dumpTestStart('并发施肥请求测试');
-
- $config = TestEnvironment::getFertilizerConfig();
- $concurrentConfig = TestEnvironment::getConcurrentConfig();
- $requestCount = $concurrentConfig['request_count'];
-
- if (TestEnvironment::isDebugMode()) {
- dump("开始并发测试,发送 {$requestCount} 个同时请求");
- }
-
- $responses = [];
- $this->currentTestConfig = $config;
-
- // 模拟并发请求(在测试环境中顺序执行)
- for ($i = 0; $i < $requestCount; $i++) {
- $responses[] = $this->executeTestRequest();
- }
-
- // 验证响应
- $successCount = 0;
- foreach ($responses as $index => $response) {
- if (TestEnvironment::isDebugMode()) {
- dump("第 " . ($index + 1) . " 个响应: " . $response->serializeToJsonString());
- }
- if ($response->getCode() === RESPONSE_CODE::OK) {
- $successCount++;
- }
- }
-
- // 至少应该有一个成功(第一个请求)
- $this->assertGreaterThan(0, $successCount, '并发请求中至少应有一个成功');
-
- if (TestEnvironment::isDebugMode()) {
- dump("并发测试结果: {$successCount}/{$requestCount} 个请求成功");
- }
-
- $this->dumpTestEnd('并发施肥请求测试');
- }
- /**
- * 测试施肥参数验证
- */
- public function testFertilizerParameterValidation()
- {
- $this->dumpTestStart('施肥参数验证测试');
-
- // 测试无效土地ID
- $this->currentTestConfig = [
- 'land_id' => -1,
- 'item_id' => TestEnvironment::get('TEST_FERTILIZER_ITEM_ID', 401)
- ];
-
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response);
-
- // 测试无效物品ID
- $this->currentTestConfig = [
- 'land_id' => TestEnvironment::get('TEST_FERTILIZER_LAND_ID', 12),
- 'item_id' => -1
- ];
-
- $response = $this->executeTestRequest();
- $this->assertFailureResponse($response);
-
- $this->dumpTestEnd('施肥参数验证测试');
- }
- }
|