DisasterRemovalTestSuite.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. namespace App\Module\AppGame\Tests\Land;
  3. use App\Module\AppGame\Tests\TestConfig;
  4. use App\Module\AppGame\Tests\TestEnvironment;
  5. use Tests\TestCase;
  6. /**
  7. * 灾害去除测试套件
  8. *
  9. * 集成所有灾害去除相关的测试,提供统一的测试入口和报告
  10. */
  11. class DisasterRemovalTestSuite extends TestCase
  12. {
  13. /**
  14. * 测试结果统计
  15. */
  16. private array $testResults = [
  17. 'total' => 0,
  18. 'passed' => 0,
  19. 'failed' => 0,
  20. 'errors' => []
  21. ];
  22. /**
  23. * 运行完整的灾害去除测试套件
  24. */
  25. public function testCompleteDisasterRemovalSuite()
  26. {
  27. $this->dumpSuiteStart();
  28. try {
  29. // 1. 运行除虫测试
  30. $this->runPesticideTests();
  31. // 2. 运行除草测试
  32. $this->runWeedicideTests();
  33. // 3. 运行浇水测试
  34. $this->runWateringTests();
  35. // 4. 运行施肥测试
  36. $this->runFertilizerTests();
  37. // 5. 运行播种测试
  38. $this->runSowTests();
  39. // 6. 运行收获测试
  40. $this->runHarvestTests();
  41. // 7. 运行集成测试
  42. $this->runIntegrationTests();
  43. } catch (\Exception $e) {
  44. $this->recordError('测试套件执行异常', $e);
  45. }
  46. $this->dumpSuiteResults();
  47. $this->dumpSuiteEnd();
  48. }
  49. /**
  50. * 运行除虫测试
  51. */
  52. private function runPesticideTests(): void
  53. {
  54. dump("=== 开始除虫测试模块 ===");
  55. try {
  56. $pesticideTest = new PesticideHandlerTest();
  57. $pesticideTest->setUp();
  58. // 基础功能测试
  59. $this->runSingleTest('除虫成功测试', function() use ($pesticideTest) {
  60. $pesticideTest->testPesticideSuccess();
  61. });
  62. $this->runSingleTest('无效物品除虫测试', function() use ($pesticideTest) {
  63. $pesticideTest->testPesticideWithInvalidItem();
  64. });
  65. $this->runSingleTest('其他用户土地除虫测试', function() use ($pesticideTest) {
  66. $pesticideTest->testPesticideOnOtherUserLand();
  67. });
  68. // 概率测试
  69. $this->runSingleTest('除虫概率机制测试', function() use ($pesticideTest) {
  70. $pesticideTest->testPesticideProbability();
  71. });
  72. } catch (\Exception $e) {
  73. $this->recordError('除虫测试模块', $e);
  74. }
  75. dump("=== 除虫测试模块完成 ===");
  76. }
  77. /**
  78. * 运行除草测试
  79. */
  80. private function runWeedicideTests(): void
  81. {
  82. dump("=== 开始除草测试模块 ===");
  83. try {
  84. $weedicideTest = new WeedicideHandlerTest();
  85. $weedicideTest->setUp();
  86. // 基础功能测试
  87. $this->runSingleTest('除草成功测试', function() use ($weedicideTest) {
  88. $weedicideTest->testWeedicideSuccess();
  89. });
  90. $this->runSingleTest('无效物品除草测试', function() use ($weedicideTest) {
  91. $weedicideTest->testWeedicideWithInvalidItem();
  92. });
  93. $this->runSingleTest('错误物品类型除草测试', function() use ($weedicideTest) {
  94. $weedicideTest->testWeedicideWithWrongItemType();
  95. });
  96. // 概率测试
  97. $this->runSingleTest('除草概率机制测试', function() use ($weedicideTest) {
  98. $weedicideTest->testWeedicideProbability();
  99. });
  100. // 并发测试
  101. $this->runSingleTest('并发除草请求测试', function() use ($weedicideTest) {
  102. $weedicideTest->testConcurrentWeedicideRequests();
  103. });
  104. } catch (\Exception $e) {
  105. $this->recordError('除草测试模块', $e);
  106. }
  107. dump("=== 除草测试模块完成 ===");
  108. }
  109. /**
  110. * 运行浇水测试
  111. */
  112. private function runWateringTests(): void
  113. {
  114. dump("=== 开始浇水测试模块 ===");
  115. try {
  116. $wateringTest = new WateringHandlerTest();
  117. $wateringTest->setUp();
  118. // 基础功能测试
  119. $this->runSingleTest('浇水成功测试', function() use ($wateringTest) {
  120. $wateringTest->testWateringSuccess();
  121. });
  122. $this->runSingleTest('无效物品浇水测试', function() use ($wateringTest) {
  123. $wateringTest->testWateringWithInvalidItem();
  124. });
  125. $this->runSingleTest('错误物品类型浇水测试', function() use ($wateringTest) {
  126. $wateringTest->testWateringWithWrongItemType();
  127. });
  128. // 概率测试
  129. $this->runSingleTest('浇水概率机制测试', function() use ($wateringTest) {
  130. $wateringTest->testWateringProbability();
  131. });
  132. // 重复操作测试
  133. $this->runSingleTest('重复浇水测试', function() use ($wateringTest) {
  134. $wateringTest->testRepeatedWateringOnSameLand();
  135. });
  136. // 性能测试
  137. $this->runSingleTest('浇水性能测试', function() use ($wateringTest) {
  138. $wateringTest->testWateringPerformance();
  139. });
  140. } catch (\Exception $e) {
  141. $this->recordError('浇水测试模块', $e);
  142. }
  143. dump("=== 浇水测试模块完成 ===");
  144. }
  145. /**
  146. * 运行施肥测试
  147. */
  148. private function runFertilizerTests(): void
  149. {
  150. if (TestEnvironment::isDebugMode()) {
  151. dump("=== 开始施肥测试模块 ===");
  152. }
  153. try {
  154. $fertilizerTest = new FertilizerHandlerTest();
  155. // 基础功能测试
  156. $this->runSingleTest('施肥成功测试', function() use ($fertilizerTest) {
  157. $fertilizerTest->testFertilizerSuccess();
  158. });
  159. $this->runSingleTest('无效物品施肥测试', function() use ($fertilizerTest) {
  160. $fertilizerTest->testFertilizerWithInvalidItem();
  161. });
  162. $this->runSingleTest('其他用户土地施肥测试', function() use ($fertilizerTest) {
  163. $fertilizerTest->testFertilizerOnOtherUserLand();
  164. });
  165. // 性能测试
  166. $this->runSingleTest('施肥性能测试', function() use ($fertilizerTest) {
  167. $fertilizerTest->testFertilizerPerformance();
  168. });
  169. // 重复操作测试
  170. $this->runSingleTest('重复施肥测试', function() use ($fertilizerTest) {
  171. $fertilizerTest->testRepeatedFertilizer();
  172. });
  173. // 并发测试
  174. $this->runSingleTest('并发施肥请求测试', function() use ($fertilizerTest) {
  175. $fertilizerTest->testConcurrentFertilizerRequests();
  176. });
  177. } catch (\Exception $e) {
  178. $this->recordError('施肥测试模块', $e);
  179. }
  180. if (TestEnvironment::isDebugMode()) {
  181. dump("=== 施肥测试模块完成 ===");
  182. }
  183. }
  184. /**
  185. * 运行播种测试
  186. */
  187. private function runSowTests(): void
  188. {
  189. if (TestEnvironment::isDebugMode()) {
  190. dump("=== 开始播种测试模块 ===");
  191. }
  192. try {
  193. $sowTest = new SowHandlerTest();
  194. // 基础功能测试
  195. $this->runSingleTest('播种成功测试', function() use ($sowTest) {
  196. $sowTest->testSowSuccess();
  197. });
  198. $this->runSingleTest('无效种子播种测试', function() use ($sowTest) {
  199. $sowTest->testSowWithInvalidSeed();
  200. });
  201. $this->runSingleTest('其他用户土地播种测试', function() use ($sowTest) {
  202. $sowTest->testSowOnOtherUserLand();
  203. });
  204. $this->runSingleTest('已占用土地播种测试', function() use ($sowTest) {
  205. $sowTest->testSowOnOccupiedLand();
  206. });
  207. // 性能测试
  208. $this->runSingleTest('播种性能测试', function() use ($sowTest) {
  209. $sowTest->testSowPerformance();
  210. });
  211. // 批量测试
  212. $this->runSingleTest('批量播种测试', function() use ($sowTest) {
  213. $sowTest->testBatchSow();
  214. });
  215. } catch (\Exception $e) {
  216. $this->recordError('播种测试模块', $e);
  217. }
  218. if (TestEnvironment::isDebugMode()) {
  219. dump("=== 播种测试模块完成 ===");
  220. }
  221. }
  222. /**
  223. * 运行收获测试
  224. */
  225. private function runHarvestTests(): void
  226. {
  227. if (TestEnvironment::isDebugMode()) {
  228. dump("=== 开始收获测试模块 ===");
  229. }
  230. try {
  231. $harvestTest = new HarvestHandlerTest();
  232. // 基础功能测试
  233. $this->runSingleTest('收获成功测试', function() use ($harvestTest) {
  234. $harvestTest->testHarvestSuccess();
  235. });
  236. $this->runSingleTest('收获未成熟作物测试', function() use ($harvestTest) {
  237. $harvestTest->testHarvestImmatureCrop();
  238. });
  239. $this->runSingleTest('收获其他用户土地测试', function() use ($harvestTest) {
  240. $harvestTest->testHarvestOtherUserLand();
  241. });
  242. $this->runSingleTest('收获空土地测试', function() use ($harvestTest) {
  243. $harvestTest->testHarvestEmptyLand();
  244. });
  245. // 性能测试
  246. $this->runSingleTest('收获性能测试', function() use ($harvestTest) {
  247. $harvestTest->testHarvestPerformance();
  248. });
  249. // 批量测试
  250. $this->runSingleTest('批量收获测试', function() use ($harvestTest) {
  251. $harvestTest->testBatchHarvest();
  252. });
  253. } catch (\Exception $e) {
  254. $this->recordError('收获测试模块', $e);
  255. }
  256. if (TestEnvironment::isDebugMode()) {
  257. dump("=== 收获测试模块完成 ===");
  258. }
  259. }
  260. /**
  261. * 运行集成测试
  262. */
  263. private function runIntegrationTests(): void
  264. {
  265. dump("=== 开始集成测试模块 ===");
  266. try {
  267. // 测试混合使用不同类型的灾害去除道具
  268. $this->runSingleTest('混合灾害去除测试', function() {
  269. $this->testMixedDisasterRemoval();
  270. });
  271. // 测试配置验证
  272. $this->runSingleTest('测试配置验证', function() {
  273. $this->testConfigValidation();
  274. });
  275. } catch (\Exception $e) {
  276. $this->recordError('集成测试模块', $e);
  277. }
  278. dump("=== 集成测试模块完成 ===");
  279. }
  280. /**
  281. * 测试混合灾害去除
  282. */
  283. private function testMixedDisasterRemoval(): void
  284. {
  285. dump("开始混合灾害去除测试");
  286. // 依次测试不同类型的灾害去除
  287. $scenarios = [
  288. ['type' => 'pesticide', 'description' => '除虫'],
  289. ['type' => 'weedicide', 'description' => '除草'],
  290. ['type' => 'watering', 'description' => '浇水']
  291. ];
  292. foreach ($scenarios as $scenario) {
  293. dump("测试 {$scenario['description']} 功能");
  294. $config = TestConfig::getTestScenario('success_scenarios', $scenario['type'] . '_success');
  295. if (!empty($config)) {
  296. dump("配置: ", $config);
  297. // 这里可以添加具体的测试逻辑
  298. $this->assertTrue(true, "{$scenario['description']} 测试通过");
  299. }
  300. }
  301. dump("混合灾害去除测试完成");
  302. }
  303. /**
  304. * 测试配置验证
  305. */
  306. private function testConfigValidation(): void
  307. {
  308. dump("开始测试配置验证");
  309. // 验证测试用户配置
  310. $testUser = TestConfig::getTestUserId();
  311. $this->assertGreaterThan(0, $testUser, '测试用户ID应该大于0');
  312. // 验证灾害去除配置
  313. $pesticideConfig = TestConfig::getPesticideTestConfig();
  314. $this->assertArrayHasKey('land_id', $pesticideConfig, '除虫配置应包含land_id');
  315. $this->assertArrayHasKey('item_id', $pesticideConfig, '除虫配置应包含item_id');
  316. $weedicideConfig = TestConfig::getWeedicideTestConfig();
  317. $this->assertArrayHasKey('land_id', $weedicideConfig, '除草配置应包含land_id');
  318. $this->assertArrayHasKey('item_id', $weedicideConfig, '除草配置应包含item_id');
  319. $wateringConfig = TestConfig::getWateringTestConfig();
  320. $this->assertArrayHasKey('land_id', $wateringConfig, '浇水配置应包含land_id');
  321. $this->assertArrayHasKey('item_id', $wateringConfig, '浇水配置应包含item_id');
  322. // 验证测试物品配置
  323. $pesticideItem = TestConfig::getTestItem('pesticide');
  324. $this->assertArrayHasKey('item_id', $pesticideItem, '除虫剂配置应包含item_id');
  325. dump("测试配置验证完成");
  326. }
  327. /**
  328. * 运行单个测试
  329. */
  330. private function runSingleTest(string $testName, callable $testFunction): void
  331. {
  332. $this->testResults['total']++;
  333. try {
  334. dump("开始测试: {$testName}");
  335. $testFunction();
  336. $this->testResults['passed']++;
  337. dump("测试通过: {$testName}");
  338. } catch (\Exception $e) {
  339. $this->testResults['failed']++;
  340. $this->recordError($testName, $e);
  341. dump("测试失败: {$testName} - " . $e->getMessage());
  342. }
  343. }
  344. /**
  345. * 记录错误
  346. */
  347. private function recordError(string $testName, \Exception $e): void
  348. {
  349. $this->testResults['errors'][] = [
  350. 'test' => $testName,
  351. 'error' => $e->getMessage(),
  352. 'trace' => $e->getTraceAsString()
  353. ];
  354. \UCore\Helper\Logger::exception("测试失败: {$testName}", $e);
  355. }
  356. /**
  357. * 输出测试套件开始信息
  358. */
  359. private function dumpSuiteStart(): void
  360. {
  361. dump("========================================");
  362. dump(" 灾害去除系统 E2E 测试套件");
  363. dump("========================================");
  364. dump("测试环境: " . env('UNITTEST_URL', 'http://localhost:8000'));
  365. dump("测试用户: " . TestConfig::getTestUserId());
  366. dump("开始时间: " . date('Y-m-d H:i:s'));
  367. dump("========================================");
  368. }
  369. /**
  370. * 输出测试结果
  371. */
  372. private function dumpSuiteResults(): void
  373. {
  374. dump("========================================");
  375. dump(" 测试结果统计");
  376. dump("========================================");
  377. dump("总测试数: " . $this->testResults['total']);
  378. dump("通过数量: " . $this->testResults['passed']);
  379. dump("失败数量: " . $this->testResults['failed']);
  380. if (!empty($this->testResults['errors'])) {
  381. dump("失败详情:");
  382. foreach ($this->testResults['errors'] as $error) {
  383. dump("- {$error['test']}: {$error['error']}");
  384. }
  385. }
  386. $successRate = $this->testResults['total'] > 0
  387. ? round(($this->testResults['passed'] / $this->testResults['total']) * 100, 2)
  388. : 0;
  389. dump("成功率: {$successRate}%");
  390. dump("========================================");
  391. }
  392. /**
  393. * 输出测试套件结束信息
  394. */
  395. private function dumpSuiteEnd(): void
  396. {
  397. dump("结束时间: " . date('Y-m-d H:i:s'));
  398. dump("========================================");
  399. dump(" 灾害去除系统 E2E 测试套件完成");
  400. dump("========================================");
  401. }
  402. }