| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- <?php
- namespace App\Module\AppGame\Tests\Land;
- use App\Module\AppGame\Tests\TestConfig;
- use App\Module\AppGame\Tests\TestEnvironment;
- use Tests\TestCase;
- /**
- * 灾害去除测试套件
- *
- * 集成所有灾害去除相关的测试,提供统一的测试入口和报告
- */
- class DisasterRemovalTestSuite extends TestCase
- {
- /**
- * 测试结果统计
- */
- private array $testResults = [
- 'total' => 0,
- 'passed' => 0,
- 'failed' => 0,
- 'errors' => []
- ];
- /**
- * 运行完整的灾害去除测试套件
- */
- public function testCompleteDisasterRemovalSuite()
- {
- $this->dumpSuiteStart();
- try {
- // 1. 运行除虫测试
- $this->runPesticideTests();
- // 2. 运行除草测试
- $this->runWeedicideTests();
- // 3. 运行浇水测试
- $this->runWateringTests();
- // 4. 运行施肥测试
- $this->runFertilizerTests();
- // 5. 运行播种测试
- $this->runSowTests();
- // 6. 运行收获测试
- $this->runHarvestTests();
- // 7. 运行集成测试
- $this->runIntegrationTests();
- } catch (\Exception $e) {
- $this->recordError('测试套件执行异常', $e);
- }
- $this->dumpSuiteResults();
- $this->dumpSuiteEnd();
- }
- /**
- * 运行除虫测试
- */
- private function runPesticideTests(): void
- {
- dump("=== 开始除虫测试模块 ===");
- try {
- $pesticideTest = new PesticideHandlerTest();
- $pesticideTest->setUp();
- // 基础功能测试
- $this->runSingleTest('除虫成功测试', function() use ($pesticideTest) {
- $pesticideTest->testPesticideSuccess();
- });
- $this->runSingleTest('无效物品除虫测试', function() use ($pesticideTest) {
- $pesticideTest->testPesticideWithInvalidItem();
- });
- $this->runSingleTest('其他用户土地除虫测试', function() use ($pesticideTest) {
- $pesticideTest->testPesticideOnOtherUserLand();
- });
- // 概率测试
- $this->runSingleTest('除虫概率机制测试', function() use ($pesticideTest) {
- $pesticideTest->testPesticideProbability();
- });
- } catch (\Exception $e) {
- $this->recordError('除虫测试模块', $e);
- }
- dump("=== 除虫测试模块完成 ===");
- }
- /**
- * 运行除草测试
- */
- private function runWeedicideTests(): void
- {
- dump("=== 开始除草测试模块 ===");
- try {
- $weedicideTest = new WeedicideHandlerTest();
- $weedicideTest->setUp();
- // 基础功能测试
- $this->runSingleTest('除草成功测试', function() use ($weedicideTest) {
- $weedicideTest->testWeedicideSuccess();
- });
- $this->runSingleTest('无效物品除草测试', function() use ($weedicideTest) {
- $weedicideTest->testWeedicideWithInvalidItem();
- });
- $this->runSingleTest('错误物品类型除草测试', function() use ($weedicideTest) {
- $weedicideTest->testWeedicideWithWrongItemType();
- });
- // 概率测试
- $this->runSingleTest('除草概率机制测试', function() use ($weedicideTest) {
- $weedicideTest->testWeedicideProbability();
- });
- // 并发测试
- $this->runSingleTest('并发除草请求测试', function() use ($weedicideTest) {
- $weedicideTest->testConcurrentWeedicideRequests();
- });
- } catch (\Exception $e) {
- $this->recordError('除草测试模块', $e);
- }
- dump("=== 除草测试模块完成 ===");
- }
- /**
- * 运行浇水测试
- */
- private function runWateringTests(): void
- {
- dump("=== 开始浇水测试模块 ===");
- try {
- $wateringTest = new WateringHandlerTest();
- $wateringTest->setUp();
- // 基础功能测试
- $this->runSingleTest('浇水成功测试', function() use ($wateringTest) {
- $wateringTest->testWateringSuccess();
- });
- $this->runSingleTest('无效物品浇水测试', function() use ($wateringTest) {
- $wateringTest->testWateringWithInvalidItem();
- });
- $this->runSingleTest('错误物品类型浇水测试', function() use ($wateringTest) {
- $wateringTest->testWateringWithWrongItemType();
- });
- // 概率测试
- $this->runSingleTest('浇水概率机制测试', function() use ($wateringTest) {
- $wateringTest->testWateringProbability();
- });
- // 重复操作测试
- $this->runSingleTest('重复浇水测试', function() use ($wateringTest) {
- $wateringTest->testRepeatedWateringOnSameLand();
- });
- // 性能测试
- $this->runSingleTest('浇水性能测试', function() use ($wateringTest) {
- $wateringTest->testWateringPerformance();
- });
- } catch (\Exception $e) {
- $this->recordError('浇水测试模块', $e);
- }
- dump("=== 浇水测试模块完成 ===");
- }
- /**
- * 运行施肥测试
- */
- private function runFertilizerTests(): void
- {
- if (TestEnvironment::isDebugMode()) {
- dump("=== 开始施肥测试模块 ===");
- }
- try {
- $fertilizerTest = new FertilizerHandlerTest();
- // 基础功能测试
- $this->runSingleTest('施肥成功测试', function() use ($fertilizerTest) {
- $fertilizerTest->testFertilizerSuccess();
- });
- $this->runSingleTest('无效物品施肥测试', function() use ($fertilizerTest) {
- $fertilizerTest->testFertilizerWithInvalidItem();
- });
- $this->runSingleTest('其他用户土地施肥测试', function() use ($fertilizerTest) {
- $fertilizerTest->testFertilizerOnOtherUserLand();
- });
- // 性能测试
- $this->runSingleTest('施肥性能测试', function() use ($fertilizerTest) {
- $fertilizerTest->testFertilizerPerformance();
- });
- // 重复操作测试
- $this->runSingleTest('重复施肥测试', function() use ($fertilizerTest) {
- $fertilizerTest->testRepeatedFertilizer();
- });
- // 并发测试
- $this->runSingleTest('并发施肥请求测试', function() use ($fertilizerTest) {
- $fertilizerTest->testConcurrentFertilizerRequests();
- });
- } catch (\Exception $e) {
- $this->recordError('施肥测试模块', $e);
- }
- if (TestEnvironment::isDebugMode()) {
- dump("=== 施肥测试模块完成 ===");
- }
- }
- /**
- * 运行播种测试
- */
- private function runSowTests(): void
- {
- if (TestEnvironment::isDebugMode()) {
- dump("=== 开始播种测试模块 ===");
- }
- try {
- $sowTest = new SowHandlerTest();
- // 基础功能测试
- $this->runSingleTest('播种成功测试', function() use ($sowTest) {
- $sowTest->testSowSuccess();
- });
- $this->runSingleTest('无效种子播种测试', function() use ($sowTest) {
- $sowTest->testSowWithInvalidSeed();
- });
- $this->runSingleTest('其他用户土地播种测试', function() use ($sowTest) {
- $sowTest->testSowOnOtherUserLand();
- });
- $this->runSingleTest('已占用土地播种测试', function() use ($sowTest) {
- $sowTest->testSowOnOccupiedLand();
- });
- // 性能测试
- $this->runSingleTest('播种性能测试', function() use ($sowTest) {
- $sowTest->testSowPerformance();
- });
- // 批量测试
- $this->runSingleTest('批量播种测试', function() use ($sowTest) {
- $sowTest->testBatchSow();
- });
- } catch (\Exception $e) {
- $this->recordError('播种测试模块', $e);
- }
- if (TestEnvironment::isDebugMode()) {
- dump("=== 播种测试模块完成 ===");
- }
- }
- /**
- * 运行收获测试
- */
- private function runHarvestTests(): void
- {
- if (TestEnvironment::isDebugMode()) {
- dump("=== 开始收获测试模块 ===");
- }
- try {
- $harvestTest = new HarvestHandlerTest();
- // 基础功能测试
- $this->runSingleTest('收获成功测试', function() use ($harvestTest) {
- $harvestTest->testHarvestSuccess();
- });
- $this->runSingleTest('收获未成熟作物测试', function() use ($harvestTest) {
- $harvestTest->testHarvestImmatureCrop();
- });
- $this->runSingleTest('收获其他用户土地测试', function() use ($harvestTest) {
- $harvestTest->testHarvestOtherUserLand();
- });
- $this->runSingleTest('收获空土地测试', function() use ($harvestTest) {
- $harvestTest->testHarvestEmptyLand();
- });
- // 性能测试
- $this->runSingleTest('收获性能测试', function() use ($harvestTest) {
- $harvestTest->testHarvestPerformance();
- });
- // 批量测试
- $this->runSingleTest('批量收获测试', function() use ($harvestTest) {
- $harvestTest->testBatchHarvest();
- });
- } catch (\Exception $e) {
- $this->recordError('收获测试模块', $e);
- }
- if (TestEnvironment::isDebugMode()) {
- dump("=== 收获测试模块完成 ===");
- }
- }
- /**
- * 运行集成测试
- */
- private function runIntegrationTests(): void
- {
- dump("=== 开始集成测试模块 ===");
- try {
- // 测试混合使用不同类型的灾害去除道具
- $this->runSingleTest('混合灾害去除测试', function() {
- $this->testMixedDisasterRemoval();
- });
- // 测试配置验证
- $this->runSingleTest('测试配置验证', function() {
- $this->testConfigValidation();
- });
- } catch (\Exception $e) {
- $this->recordError('集成测试模块', $e);
- }
- dump("=== 集成测试模块完成 ===");
- }
- /**
- * 测试混合灾害去除
- */
- private function testMixedDisasterRemoval(): void
- {
- dump("开始混合灾害去除测试");
- // 依次测试不同类型的灾害去除
- $scenarios = [
- ['type' => 'pesticide', 'description' => '除虫'],
- ['type' => 'weedicide', 'description' => '除草'],
- ['type' => 'watering', 'description' => '浇水']
- ];
- foreach ($scenarios as $scenario) {
- dump("测试 {$scenario['description']} 功能");
- $config = TestConfig::getTestScenario('success_scenarios', $scenario['type'] . '_success');
- if (!empty($config)) {
- dump("配置: ", $config);
- // 这里可以添加具体的测试逻辑
- $this->assertTrue(true, "{$scenario['description']} 测试通过");
- }
- }
- dump("混合灾害去除测试完成");
- }
- /**
- * 测试配置验证
- */
- private function testConfigValidation(): void
- {
- dump("开始测试配置验证");
- // 验证测试用户配置
- $testUser = TestConfig::getTestUserId();
- $this->assertGreaterThan(0, $testUser, '测试用户ID应该大于0');
- // 验证灾害去除配置
- $pesticideConfig = TestConfig::getPesticideTestConfig();
- $this->assertArrayHasKey('land_id', $pesticideConfig, '除虫配置应包含land_id');
- $this->assertArrayHasKey('item_id', $pesticideConfig, '除虫配置应包含item_id');
- $weedicideConfig = TestConfig::getWeedicideTestConfig();
- $this->assertArrayHasKey('land_id', $weedicideConfig, '除草配置应包含land_id');
- $this->assertArrayHasKey('item_id', $weedicideConfig, '除草配置应包含item_id');
- $wateringConfig = TestConfig::getWateringTestConfig();
- $this->assertArrayHasKey('land_id', $wateringConfig, '浇水配置应包含land_id');
- $this->assertArrayHasKey('item_id', $wateringConfig, '浇水配置应包含item_id');
- // 验证测试物品配置
- $pesticideItem = TestConfig::getTestItem('pesticide');
- $this->assertArrayHasKey('item_id', $pesticideItem, '除虫剂配置应包含item_id');
- dump("测试配置验证完成");
- }
- /**
- * 运行单个测试
- */
- private function runSingleTest(string $testName, callable $testFunction): void
- {
- $this->testResults['total']++;
- try {
- dump("开始测试: {$testName}");
- $testFunction();
- $this->testResults['passed']++;
- dump("测试通过: {$testName}");
- } catch (\Exception $e) {
- $this->testResults['failed']++;
- $this->recordError($testName, $e);
- dump("测试失败: {$testName} - " . $e->getMessage());
- }
- }
- /**
- * 记录错误
- */
- private function recordError(string $testName, \Exception $e): void
- {
- $this->testResults['errors'][] = [
- 'test' => $testName,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ];
- \UCore\Helper\Logger::exception("测试失败: {$testName}", $e);
- }
- /**
- * 输出测试套件开始信息
- */
- private function dumpSuiteStart(): void
- {
- dump("========================================");
- dump(" 灾害去除系统 E2E 测试套件");
- dump("========================================");
- dump("测试环境: " . env('UNITTEST_URL', 'http://localhost:8000'));
- dump("测试用户: " . TestConfig::getTestUserId());
- dump("开始时间: " . date('Y-m-d H:i:s'));
- dump("========================================");
- }
- /**
- * 输出测试结果
- */
- private function dumpSuiteResults(): void
- {
- dump("========================================");
- dump(" 测试结果统计");
- dump("========================================");
- dump("总测试数: " . $this->testResults['total']);
- dump("通过数量: " . $this->testResults['passed']);
- dump("失败数量: " . $this->testResults['failed']);
- if (!empty($this->testResults['errors'])) {
- dump("失败详情:");
- foreach ($this->testResults['errors'] as $error) {
- dump("- {$error['test']}: {$error['error']}");
- }
- }
- $successRate = $this->testResults['total'] > 0
- ? round(($this->testResults['passed'] / $this->testResults['total']) * 100, 2)
- : 0;
- dump("成功率: {$successRate}%");
- dump("========================================");
- }
- /**
- * 输出测试套件结束信息
- */
- private function dumpSuiteEnd(): void
- {
- dump("结束时间: " . date('Y-m-d H:i:s'));
- dump("========================================");
- dump(" 灾害去除系统 E2E 测试套件完成");
- dump("========================================");
- }
- }
|