| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- namespace App\Module\AppGame\Tests\Land;
- use App\Module\AppGame\Tests\TestConfig;
- use Google\Protobuf\Internal\Message;
- use Uraus\Kku\Request;
- use Uraus\Kku\Request\RequestLandWatering;
- /**
- * 浇水Handler E2E测试
- *
- * 测试浇水功能的各种场景,包括成功、失败和边界情况
- */
- class WateringHandlerTest extends DisasterRemovalBaseTest
- {
- /**
- * 当前测试配置
- */
- private array $currentTestConfig;
- /**
- * 测试浇水成功场景
- */
- public function testWateringSuccess()
- {
- $this->dumpTestStart('浇水成功测试');
-
- $config = TestConfig::getWateringTestConfig();
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertSuccessResponse($response, '浇水成功');
-
- $this->dumpTestEnd('浇水成功测试');
- }
- /**
- * 测试使用无效物品浇水
- */
- public function testWateringWithInvalidItem()
- {
- $this->dumpTestStart('无效物品浇水测试');
-
- $config = TestConfig::getWateringTestConfig();
- $config['item_id'] = TestConfig::getTestItem('invalid_item')['item_id'];
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertValidationFailureResponse($response, '不是浇水物品');
-
- $this->dumpTestEnd('无效物品浇水测试');
- }
- /**
- * 测试使用除虫剂进行浇水(物品类型不匹配)
- */
- public function testWateringWithWrongItemType()
- {
- $this->dumpTestStart('错误物品类型浇水测试');
-
- $config = TestConfig::getWateringTestConfig();
- $config['item_id'] = TestConfig::getTestItem('pesticide')['item_id']; // 使用除虫剂
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertValidationFailureResponse($response, '不是浇水物品');
-
- $this->dumpTestEnd('错误物品类型浇水测试');
- }
- /**
- * 测试对其他用户土地浇水
- */
- public function testWateringOnOtherUserLand()
- {
- $this->dumpTestStart('其他用户土地浇水测试');
-
- $config = TestConfig::getWateringTestConfig();
- $config['land_id'] = TestConfig::getTestLand('other_user_land')['land_id'];
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertValidationFailureResponse($response, '土地不存在或不属于当前用户');
-
- $this->dumpTestEnd('其他用户土地浇水测试');
- }
- /**
- * 测试对无干旱土地浇水
- */
- public function testWateringOnNormalLand()
- {
- $this->dumpTestStart('无干旱土地浇水测试');
-
- $config = TestConfig::getWateringTestConfig();
- $config['land_id'] = TestConfig::getTestLand('normal_land')['land_id'];
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertFailureResponse($response, '灾害清理失败');
-
- $this->dumpTestEnd('无干旱土地浇水测试');
- }
- /**
- * 测试参数验证 - 无效的物品ID
- */
- public function testWateringWithInvalidItemId()
- {
- $this->dumpTestStart('无效物品ID测试');
-
- $config = TestConfig::getWateringTestConfig();
- $config['item_id'] = 0; // 无效的物品ID
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertValidationFailureResponse($response);
-
- $this->dumpTestEnd('无效物品ID测试');
- }
- /**
- * 测试概率机制 - 多次测试验证概率
- */
- public function testWateringProbability()
- {
- $this->dumpTestStart('浇水概率机制测试');
-
- $config = TestConfig::getWateringTestConfig();
- $this->dumpTestConfig($config);
-
- $successCount = 0;
- $totalAttempts = 10; // 测试10次
-
- dump("开始进行 {$totalAttempts} 次浇水测试,验证概率机制");
-
- for ($i = 1; $i <= $totalAttempts; $i++) {
- dump("第 {$i} 次测试");
- $this->currentTestConfig = $config;
-
- $response = $this->protobufRequest();
-
- if ($response->getCode() === 0) {
- $successCount++;
- dump("第 {$i} 次测试成功");
- } else {
- dump("第 {$i} 次测试失败: " . $response->getMsg());
- }
- }
-
- $successRate = ($successCount / $totalAttempts) * 100;
- $expectedRate = $config['expected_success_rate'];
-
- dump("测试结果: {$successCount}/{$totalAttempts} 成功,实际成功率: {$successRate}%,预期成功率: {$expectedRate}%");
-
- // 浇水通常有较高的成功率,允许一定的误差范围(±20%)
- $this->assertGreaterThanOrEqual($expectedRate - 20, $successRate, '实际成功率不应低于预期太多');
- $this->assertLessThanOrEqual($expectedRate + 20, $successRate, '实际成功率不应高于预期太多');
-
- $this->dumpTestEnd('浇水概率机制测试');
- }
- /**
- * 测试边界情况 - 100%成功率物品
- */
- public function testWateringWithHundredPercentSuccessRate()
- {
- $this->dumpTestStart('100%成功率浇水测试');
-
- $config = TestConfig::getWateringTestConfig();
- // 假设有一个100%成功率的浇水道具用于测试
- $config['item_id'] = 1030; // 假设的100%成功率浇水道具ID
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- // 100%成功率应该总是成功
- $this->assertSuccessResponse($response, '浇水成功');
-
- $this->dumpTestEnd('100%成功率浇水测试');
- }
- /**
- * 测试重复浇水同一块土地
- */
- public function testRepeatedWateringOnSameLand()
- {
- $this->dumpTestStart('重复浇水测试');
-
- $config = TestConfig::getWateringTestConfig();
- $this->dumpTestConfig($config);
-
- // 第一次浇水
- dump("第一次浇水");
- $this->currentTestConfig = $config;
- $firstResponse = $this->protobufRequest();
- dump("第一次浇水响应: " . $firstResponse->serializeToJsonString());
-
- // 第二次浇水(如果第一次成功,第二次应该失败)
- dump("第二次浇水");
- $this->currentTestConfig = $config;
- $secondResponse = $this->protobufRequest();
- dump("第二次浇水响应: " . $secondResponse->serializeToJsonString());
-
- // 如果第一次成功,第二次应该失败(因为已经没有干旱了)
- if ($firstResponse->getCode() === 0) {
- $this->assertFailureResponse($secondResponse, '灾害清理失败');
- dump("符合预期:第一次成功后,第二次失败");
- } else {
- dump("第一次失败,第二次结果不确定");
- }
-
- $this->dumpTestEnd('重复浇水测试');
- }
- /**
- * 创建浇水请求的protobuf消息
- */
- public function create_request_protobuf(): Message
- {
- $request = $this->createBaseRequest();
- $wateringRequest = new RequestLandWatering();
-
- // 使用当前测试配置
- $config = $this->currentTestConfig ?? TestConfig::getWateringTestConfig();
-
- $wateringRequest->setLandId($config['land_id']);
- $wateringRequest->setUserItemId($config['item_id']);
-
- $request->setLandWatering($wateringRequest);
-
- dump('创建浇水请求:', [
- 'land_id' => $config['land_id'],
- 'user_item_id' => $config['item_id']
- ]);
-
- return $request;
- }
- /**
- * 测试所有预定义场景
- */
- public function testAllWateringScenarios()
- {
- $this->dumpTestStart('所有浇水场景测试');
-
- // 测试成功场景
- $successScenarios = TestConfig::getTestScenario('success_scenarios', 'watering_success');
- if (!empty($successScenarios)) {
- dump('测试成功场景:', $successScenarios);
- $this->currentTestConfig = [
- 'land_id' => $successScenarios['land_id'],
- 'item_id' => $successScenarios['item_id']
- ];
-
- $response = $this->protobufRequest();
- $this->assertSuccessResponse($response, '浇水成功');
- }
-
- $this->dumpTestEnd('所有浇水场景测试');
- }
- /**
- * 测试性能 - 大量浇水请求
- */
- public function testWateringPerformance()
- {
- $this->dumpTestStart('浇水性能测试');
-
- $config = TestConfig::getWateringTestConfig();
- $this->dumpTestConfig($config);
-
- $startTime = microtime(true);
- $requestCount = 5; // 测试5个请求
-
- dump("开始性能测试,发送 {$requestCount} 个浇水请求");
-
- for ($i = 1; $i <= $requestCount; $i++) {
- $requestStartTime = microtime(true);
- $this->currentTestConfig = $config;
-
- $response = $this->protobufRequest();
-
- $requestEndTime = microtime(true);
- $requestDuration = ($requestEndTime - $requestStartTime) * 1000; // 转换为毫秒
-
- dump("第 {$i} 个请求耗时: {$requestDuration}ms,响应码: " . $response->getCode());
- }
-
- $endTime = microtime(true);
- $totalDuration = ($endTime - $startTime) * 1000; // 转换为毫秒
- $averageDuration = $totalDuration / $requestCount;
-
- dump("性能测试结果: 总耗时 {$totalDuration}ms,平均耗时 {$averageDuration}ms");
-
- // 验证平均响应时间不超过5秒
- $this->assertLessThan(5000, $averageDuration, '平均响应时间不应超过5秒');
-
- $this->dumpTestEnd('浇水性能测试');
- }
- }
|