DisasterRemovalTestSuite.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace App\Module\AppGame\Tests\Land;
  3. use App\Module\AppGame\Tests\TestConfig;
  4. use Tests\TestCase;
  5. use Illuminate\Support\Facades\Log;
  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->runIntegrationTests();
  37. } catch (\Exception $e) {
  38. $this->recordError('测试套件执行异常', $e);
  39. }
  40. $this->dumpSuiteResults();
  41. $this->dumpSuiteEnd();
  42. }
  43. /**
  44. * 运行除虫测试
  45. */
  46. private function runPesticideTests(): void
  47. {
  48. dump("=== 开始除虫测试模块 ===");
  49. try {
  50. $pesticideTest = new PesticideHandlerTest();
  51. $pesticideTest->setUp();
  52. // 基础功能测试
  53. $this->runSingleTest('除虫成功测试', function() use ($pesticideTest) {
  54. $pesticideTest->testPesticideSuccess();
  55. });
  56. $this->runSingleTest('无效物品除虫测试', function() use ($pesticideTest) {
  57. $pesticideTest->testPesticideWithInvalidItem();
  58. });
  59. $this->runSingleTest('其他用户土地除虫测试', function() use ($pesticideTest) {
  60. $pesticideTest->testPesticideOnOtherUserLand();
  61. });
  62. // 概率测试
  63. $this->runSingleTest('除虫概率机制测试', function() use ($pesticideTest) {
  64. $pesticideTest->testPesticideProbability();
  65. });
  66. } catch (\Exception $e) {
  67. $this->recordError('除虫测试模块', $e);
  68. }
  69. dump("=== 除虫测试模块完成 ===");
  70. }
  71. /**
  72. * 运行除草测试
  73. */
  74. private function runWeedicideTests(): void
  75. {
  76. dump("=== 开始除草测试模块 ===");
  77. try {
  78. $weedicideTest = new WeedicideHandlerTest();
  79. $weedicideTest->setUp();
  80. // 基础功能测试
  81. $this->runSingleTest('除草成功测试', function() use ($weedicideTest) {
  82. $weedicideTest->testWeedicideSuccess();
  83. });
  84. $this->runSingleTest('无效物品除草测试', function() use ($weedicideTest) {
  85. $weedicideTest->testWeedicideWithInvalidItem();
  86. });
  87. $this->runSingleTest('错误物品类型除草测试', function() use ($weedicideTest) {
  88. $weedicideTest->testWeedicideWithWrongItemType();
  89. });
  90. // 概率测试
  91. $this->runSingleTest('除草概率机制测试', function() use ($weedicideTest) {
  92. $weedicideTest->testWeedicideProbability();
  93. });
  94. // 并发测试
  95. $this->runSingleTest('并发除草请求测试', function() use ($weedicideTest) {
  96. $weedicideTest->testConcurrentWeedicideRequests();
  97. });
  98. } catch (\Exception $e) {
  99. $this->recordError('除草测试模块', $e);
  100. }
  101. dump("=== 除草测试模块完成 ===");
  102. }
  103. /**
  104. * 运行浇水测试
  105. */
  106. private function runWateringTests(): void
  107. {
  108. dump("=== 开始浇水测试模块 ===");
  109. try {
  110. $wateringTest = new WateringHandlerTest();
  111. $wateringTest->setUp();
  112. // 基础功能测试
  113. $this->runSingleTest('浇水成功测试', function() use ($wateringTest) {
  114. $wateringTest->testWateringSuccess();
  115. });
  116. $this->runSingleTest('无效物品浇水测试', function() use ($wateringTest) {
  117. $wateringTest->testWateringWithInvalidItem();
  118. });
  119. $this->runSingleTest('错误物品类型浇水测试', function() use ($wateringTest) {
  120. $wateringTest->testWateringWithWrongItemType();
  121. });
  122. // 概率测试
  123. $this->runSingleTest('浇水概率机制测试', function() use ($wateringTest) {
  124. $wateringTest->testWateringProbability();
  125. });
  126. // 重复操作测试
  127. $this->runSingleTest('重复浇水测试', function() use ($wateringTest) {
  128. $wateringTest->testRepeatedWateringOnSameLand();
  129. });
  130. // 性能测试
  131. $this->runSingleTest('浇水性能测试', function() use ($wateringTest) {
  132. $wateringTest->testWateringPerformance();
  133. });
  134. } catch (\Exception $e) {
  135. $this->recordError('浇水测试模块', $e);
  136. }
  137. dump("=== 浇水测试模块完成 ===");
  138. }
  139. /**
  140. * 运行集成测试
  141. */
  142. private function runIntegrationTests(): void
  143. {
  144. dump("=== 开始集成测试模块 ===");
  145. try {
  146. // 测试混合使用不同类型的灾害去除道具
  147. $this->runSingleTest('混合灾害去除测试', function() {
  148. $this->testMixedDisasterRemoval();
  149. });
  150. // 测试配置验证
  151. $this->runSingleTest('测试配置验证', function() {
  152. $this->testConfigValidation();
  153. });
  154. } catch (\Exception $e) {
  155. $this->recordError('集成测试模块', $e);
  156. }
  157. dump("=== 集成测试模块完成 ===");
  158. }
  159. /**
  160. * 测试混合灾害去除
  161. */
  162. private function testMixedDisasterRemoval(): void
  163. {
  164. dump("开始混合灾害去除测试");
  165. // 依次测试不同类型的灾害去除
  166. $scenarios = [
  167. ['type' => 'pesticide', 'description' => '除虫'],
  168. ['type' => 'weedicide', 'description' => '除草'],
  169. ['type' => 'watering', 'description' => '浇水']
  170. ];
  171. foreach ($scenarios as $scenario) {
  172. dump("测试 {$scenario['description']} 功能");
  173. $config = TestConfig::getTestScenario('success_scenarios', $scenario['type'] . '_success');
  174. if (!empty($config)) {
  175. dump("配置: ", $config);
  176. // 这里可以添加具体的测试逻辑
  177. $this->assertTrue(true, "{$scenario['description']} 测试通过");
  178. }
  179. }
  180. dump("混合灾害去除测试完成");
  181. }
  182. /**
  183. * 测试配置验证
  184. */
  185. private function testConfigValidation(): void
  186. {
  187. dump("开始测试配置验证");
  188. // 验证测试用户配置
  189. $testUser = TestConfig::getTestUserId();
  190. $this->assertGreaterThan(0, $testUser, '测试用户ID应该大于0');
  191. // 验证灾害去除配置
  192. $pesticideConfig = TestConfig::getPesticideTestConfig();
  193. $this->assertArrayHasKey('land_id', $pesticideConfig, '除虫配置应包含land_id');
  194. $this->assertArrayHasKey('item_id', $pesticideConfig, '除虫配置应包含item_id');
  195. $weedicideConfig = TestConfig::getWeedicideTestConfig();
  196. $this->assertArrayHasKey('land_id', $weedicideConfig, '除草配置应包含land_id');
  197. $this->assertArrayHasKey('item_id', $weedicideConfig, '除草配置应包含item_id');
  198. $wateringConfig = TestConfig::getWateringTestConfig();
  199. $this->assertArrayHasKey('land_id', $wateringConfig, '浇水配置应包含land_id');
  200. $this->assertArrayHasKey('item_id', $wateringConfig, '浇水配置应包含item_id');
  201. // 验证测试物品配置
  202. $pesticideItem = TestConfig::getTestItem('pesticide');
  203. $this->assertArrayHasKey('item_id', $pesticideItem, '除虫剂配置应包含item_id');
  204. dump("测试配置验证完成");
  205. }
  206. /**
  207. * 运行单个测试
  208. */
  209. private function runSingleTest(string $testName, callable $testFunction): void
  210. {
  211. $this->testResults['total']++;
  212. try {
  213. dump("开始测试: {$testName}");
  214. $testFunction();
  215. $this->testResults['passed']++;
  216. dump("测试通过: {$testName}");
  217. } catch (\Exception $e) {
  218. $this->testResults['failed']++;
  219. $this->recordError($testName, $e);
  220. dump("测试失败: {$testName} - " . $e->getMessage());
  221. }
  222. }
  223. /**
  224. * 记录错误
  225. */
  226. private function recordError(string $testName, \Exception $e): void
  227. {
  228. $this->testResults['errors'][] = [
  229. 'test' => $testName,
  230. 'error' => $e->getMessage(),
  231. 'trace' => $e->getTraceAsString()
  232. ];
  233. Log::error("测试失败: {$testName}", [
  234. 'error' => $e->getMessage(),
  235. 'trace' => $e->getTraceAsString()
  236. ]);
  237. }
  238. /**
  239. * 输出测试套件开始信息
  240. */
  241. private function dumpSuiteStart(): void
  242. {
  243. dump("========================================");
  244. dump(" 灾害去除系统 E2E 测试套件");
  245. dump("========================================");
  246. dump("测试环境: " . env('UNITTEST_URL', 'http://localhost:8000'));
  247. dump("测试用户: " . TestConfig::getTestUserId());
  248. dump("开始时间: " . date('Y-m-d H:i:s'));
  249. dump("========================================");
  250. }
  251. /**
  252. * 输出测试结果
  253. */
  254. private function dumpSuiteResults(): void
  255. {
  256. dump("========================================");
  257. dump(" 测试结果统计");
  258. dump("========================================");
  259. dump("总测试数: " . $this->testResults['total']);
  260. dump("通过数量: " . $this->testResults['passed']);
  261. dump("失败数量: " . $this->testResults['failed']);
  262. if (!empty($this->testResults['errors'])) {
  263. dump("失败详情:");
  264. foreach ($this->testResults['errors'] as $error) {
  265. dump("- {$error['test']}: {$error['error']}");
  266. }
  267. }
  268. $successRate = $this->testResults['total'] > 0
  269. ? round(($this->testResults['passed'] / $this->testResults['total']) * 100, 2)
  270. : 0;
  271. dump("成功率: {$successRate}%");
  272. dump("========================================");
  273. }
  274. /**
  275. * 输出测试套件结束信息
  276. */
  277. private function dumpSuiteEnd(): void
  278. {
  279. dump("结束时间: " . date('Y-m-d H:i:s'));
  280. dump("========================================");
  281. dump(" 灾害去除系统 E2E 测试套件完成");
  282. dump("========================================");
  283. }
  284. }