| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?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\RequestLandWeedicide;
- /**
- * 除草Handler E2E测试
- *
- * 测试除草功能的各种场景,包括成功、失败和边界情况
- */
- class WeedicideHandlerTest extends DisasterRemovalBaseTest
- {
- /**
- * 当前测试配置
- */
- private array $currentTestConfig;
- /**
- * 测试除草成功场景
- */
- public function testWeedicideSuccess()
- {
- $this->dumpTestStart('除草成功测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertSuccessResponse($response, '除草成功');
-
- $this->dumpTestEnd('除草成功测试');
- }
- /**
- * 测试使用无效物品除草
- */
- public function testWeedicideWithInvalidItem()
- {
- $this->dumpTestStart('无效物品除草测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- $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 testWeedicideWithWrongItemType()
- {
- $this->dumpTestStart('错误物品类型除草测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- $config['item_id'] = TestConfig::getTestItem('pesticide')['item_id']; // 使用除虫剂
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertValidationFailureResponse($response, '不是除草物品');
-
- $this->dumpTestEnd('错误物品类型除草测试');
- }
- /**
- * 测试对其他用户土地除草
- */
- public function testWeedicideOnOtherUserLand()
- {
- $this->dumpTestStart('其他用户土地除草测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- $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 testWeedicideOnNormalLand()
- {
- $this->dumpTestStart('无杂草土地除草测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- $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 testWeedicideWithInvalidLandId()
- {
- $this->dumpTestStart('无效土地ID测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- $config['land_id'] = -1; // 无效的土地ID
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- $this->assertValidationFailureResponse($response);
-
- $this->dumpTestEnd('无效土地ID测试');
- }
- /**
- * 测试概率机制 - 多次测试验证概率
- */
- public function testWeedicideProbability()
- {
- $this->dumpTestStart('除草概率机制测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- $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}%");
-
- // 允许一定的误差范围(±30%)
- $this->assertGreaterThanOrEqual($expectedRate - 30, $successRate, '实际成功率不应低于预期太多');
- $this->assertLessThanOrEqual($expectedRate + 30, $successRate, '实际成功率不应高于预期太多');
-
- $this->dumpTestEnd('除草概率机制测试');
- }
- /**
- * 测试边界情况 - 0%成功率物品
- */
- public function testWeedicideWithZeroSuccessRate()
- {
- $this->dumpTestStart('0%成功率除草测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- // 假设有一个0%成功率的除草剂用于测试
- $config['item_id'] = 1020; // 假设的0%成功率除草剂ID
- $this->dumpTestConfig($config);
- $this->currentTestConfig = $config;
- $response = $this->protobufRequest();
- // 0%成功率应该总是失败,但仍会消耗物品
- $this->assertFailureResponse($response, '除草失败,请再次尝试');
-
- $this->dumpTestEnd('0%成功率除草测试');
- }
- /**
- * 创建除草请求的protobuf消息
- */
- public function create_request_protobuf(): Message
- {
- $request = $this->createBaseRequest();
- $weedicideRequest = new RequestLandWeedicide();
-
- // 使用当前测试配置
- $config = $this->currentTestConfig ?? TestConfig::getWeedicideTestConfig();
-
- $weedicideRequest->setLandId($config['land_id']);
- $weedicideRequest->setUserItemId($config['item_id']);
-
- $request->setLandWeedicide($weedicideRequest);
-
- dump('创建除草请求:', [
- 'land_id' => $config['land_id'],
- 'user_item_id' => $config['item_id']
- ]);
-
- return $request;
- }
- /**
- * 测试所有预定义场景
- */
- public function testAllWeedicideScenarios()
- {
- $this->dumpTestStart('所有除草场景测试');
-
- // 测试成功场景
- $successScenarios = TestConfig::getTestScenario('success_scenarios', 'weedicide_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 testConcurrentWeedicideRequests()
- {
- $this->dumpTestStart('并发除草请求测试');
-
- $config = TestConfig::getWeedicideTestConfig();
- $this->dumpTestConfig($config);
-
- // 模拟快速连续的请求
- $responses = [];
- for ($i = 1; $i <= 3; $i++) {
- dump("发送第 {$i} 个并发请求");
- $this->currentTestConfig = $config;
- $responses[] = $this->protobufRequest();
- }
-
- // 验证响应
- $successCount = 0;
- foreach ($responses as $index => $response) {
- dump("第 " . ($index + 1) . " 个响应: " . $response->serializeToJsonString());
- if ($response->getCode() === 0) {
- $successCount++;
- }
- }
-
- dump("并发请求结果: {$successCount}/3 成功");
-
- // 至少应该有一个成功(假设有足够的物品)
- $this->assertGreaterThanOrEqual(1, $successCount, '并发请求中至少应该有一个成功');
-
- $this->dumpTestEnd('并发除草请求测试');
- }
- }
|