prepare_test_data.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * 测试数据准备脚本
  4. *
  5. * 为灾害去除测试准备必要的测试数据,包括用户、土地、物品等
  6. *
  7. * 使用方法:
  8. * php app/Module/AppGame/Tests/prepare_test_data.php
  9. */
  10. require_once __DIR__ . '/../../../vendor/autoload.php';
  11. use App\Module\AppGame\Tests\TestConfig;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. class TestDataPreparer
  15. {
  16. /**
  17. * 准备所有测试数据
  18. */
  19. public function prepare(): void
  20. {
  21. $this->displayHeader();
  22. try {
  23. // 1. 准备测试用户
  24. $this->prepareTestUser();
  25. // 2. 准备测试物品
  26. $this->prepareTestItems();
  27. // 3. 准备测试土地
  28. $this->prepareTestLands();
  29. // 4. 准备用户物品关联
  30. $this->prepareUserItems();
  31. // 5. 验证数据
  32. $this->verifyTestData();
  33. echo "✅ 测试数据准备完成!\n\n";
  34. } catch (\Exception $e) {
  35. echo "❌ 测试数据准备失败: " . $e->getMessage() . "\n";
  36. echo "错误详情: " . $e->getTraceAsString() . "\n";
  37. }
  38. }
  39. /**
  40. * 显示头部信息
  41. */
  42. private function displayHeader(): void
  43. {
  44. echo "\n";
  45. echo "========================================\n";
  46. echo " 灾害去除测试数据准备工具\n";
  47. echo "========================================\n";
  48. echo "时间: " . date('Y-m-d H:i:s') . "\n";
  49. echo "========================================\n\n";
  50. }
  51. /**
  52. * 准备测试用户
  53. */
  54. private function prepareTestUser(): void
  55. {
  56. echo "准备测试用户...\n";
  57. $userId = TestConfig::getTestUserId();
  58. $userConfig = TestConfig::TEST_USER;
  59. // 检查用户是否存在
  60. $userExists = DB::table('users')->where('id', $userId)->exists();
  61. if (!$userExists) {
  62. echo "创建测试用户 (ID: {$userId})...\n";
  63. DB::table('users')->insert([
  64. 'id' => $userId,
  65. 'username' => $userConfig['username'],
  66. 'password' => bcrypt($userConfig['password']),
  67. 'email' => 'test@example.com',
  68. 'created_at' => now(),
  69. 'updated_at' => now()
  70. ]);
  71. echo "✅ 测试用户创建成功\n";
  72. } else {
  73. echo "✅ 测试用户已存在\n";
  74. }
  75. echo "\n";
  76. }
  77. /**
  78. * 准备测试物品
  79. */
  80. private function prepareTestItems(): void
  81. {
  82. echo "准备测试物品...\n";
  83. $items = [
  84. TestConfig::getTestItem('pesticide'),
  85. TestConfig::getTestItem('weedicide'),
  86. TestConfig::getTestItem('watering_tool'),
  87. TestConfig::getTestItem('invalid_item')
  88. ];
  89. foreach ($items as $item) {
  90. if (empty($item)) continue;
  91. $itemId = $item['item_id'];
  92. $itemExists = DB::table('game_items')->where('id', $itemId)->exists();
  93. if (!$itemExists) {
  94. echo "创建物品: {$item['name']} (ID: {$itemId})...\n";
  95. DB::table('game_items')->insert([
  96. 'id' => $itemId,
  97. 'name' => $item['name'],
  98. 'description' => "测试用{$item['name']}",
  99. 'type' => 'tool',
  100. 'numeric_attributes' => json_encode($item['numeric_attributes'] ?? []),
  101. 'created_at' => now(),
  102. 'updated_at' => now()
  103. ]);
  104. echo "✅ 物品创建成功\n";
  105. } else {
  106. echo "✅ 物品已存在: {$item['name']}\n";
  107. }
  108. }
  109. echo "\n";
  110. }
  111. /**
  112. * 准备测试土地
  113. */
  114. private function prepareTestLands(): void
  115. {
  116. echo "准备测试土地...\n";
  117. $lands = [
  118. TestConfig::getTestLand('pest_land'),
  119. TestConfig::getTestLand('weed_land'),
  120. TestConfig::getTestLand('drought_land'),
  121. TestConfig::getTestLand('normal_land'),
  122. TestConfig::getTestLand('other_user_land')
  123. ];
  124. foreach ($lands as $land) {
  125. if (empty($land)) continue;
  126. $landId = $land['land_id'];
  127. $landExists = DB::table('kku_farm_lands')->where('id', $landId)->exists();
  128. if (!$landExists) {
  129. echo "创建土地 (ID: {$landId}, 用户: {$land['user_id']})...\n";
  130. DB::table('kku_farm_lands')->insert([
  131. 'id' => $landId,
  132. 'user_id' => $land['user_id'],
  133. 'status' => 2, // 已种植
  134. 'created_at' => now(),
  135. 'updated_at' => now()
  136. ]);
  137. // 如果有作物,创建作物记录
  138. if (isset($land['crop_id'])) {
  139. $this->createCropForLand($land);
  140. }
  141. echo "✅ 土地创建成功\n";
  142. } else {
  143. echo "✅ 土地已存在 (ID: {$landId})\n";
  144. }
  145. }
  146. echo "\n";
  147. }
  148. /**
  149. * 为土地创建作物
  150. */
  151. private function createCropForLand(array $land): void
  152. {
  153. $cropExists = DB::table('kku_farm_crops')
  154. ->where('land_id', $land['land_id'])
  155. ->exists();
  156. if (!$cropExists) {
  157. echo " 创建作物 (土地: {$land['land_id']}, 灾害类型: {$land['disaster_type']})...\n";
  158. DB::table('kku_farm_crops')->insert([
  159. 'land_id' => $land['land_id'],
  160. 'user_id' => $land['user_id'],
  161. 'seed_id' => $land['crop_id'],
  162. 'growth_stage' => $land['growth_stage'],
  163. 'disaster_type' => $land['disaster_type'],
  164. 'planted_at' => now()->subHours(2), // 2小时前种植
  165. 'created_at' => now(),
  166. 'updated_at' => now()
  167. ]);
  168. echo " ✅ 作物创建成功\n";
  169. }
  170. }
  171. /**
  172. * 准备用户物品关联
  173. */
  174. private function prepareUserItems(): void
  175. {
  176. echo "准备用户物品关联...\n";
  177. $userId = TestConfig::getTestUserId();
  178. $items = [
  179. TestConfig::getTestItem('pesticide'),
  180. TestConfig::getTestItem('weedicide'),
  181. TestConfig::getTestItem('watering_tool')
  182. ];
  183. foreach ($items as $item) {
  184. if (empty($item)) continue;
  185. $itemId = $item['item_id'];
  186. $userItemExists = DB::table('kku_user_items')
  187. ->where('user_id', $userId)
  188. ->where('item_id', $itemId)
  189. ->exists();
  190. if (!$userItemExists) {
  191. echo "给用户添加物品: {$item['name']}...\n";
  192. DB::table('kku_user_items')->insert([
  193. 'user_id' => $userId,
  194. 'item_id' => $itemId,
  195. 'quantity' => 100, // 给足够的数量用于测试
  196. 'created_at' => now(),
  197. 'updated_at' => now()
  198. ]);
  199. echo "✅ 用户物品添加成功\n";
  200. } else {
  201. echo "✅ 用户已拥有物品: {$item['name']}\n";
  202. }
  203. }
  204. echo "\n";
  205. }
  206. /**
  207. * 验证测试数据
  208. */
  209. private function verifyTestData(): void
  210. {
  211. echo "验证测试数据...\n";
  212. $errors = [];
  213. // 验证测试用户
  214. $userId = TestConfig::getTestUserId();
  215. if (!DB::table('users')->where('id', $userId)->exists()) {
  216. $errors[] = "测试用户不存在 (ID: {$userId})";
  217. }
  218. // 验证测试物品
  219. $items = [
  220. TestConfig::getTestItem('pesticide'),
  221. TestConfig::getTestItem('weedicide'),
  222. TestConfig::getTestItem('watering_tool')
  223. ];
  224. foreach ($items as $item) {
  225. if (empty($item)) continue;
  226. if (!DB::table('game_items')->where('id', $item['item_id'])->exists()) {
  227. $errors[] = "测试物品不存在: {$item['name']} (ID: {$item['item_id']})";
  228. }
  229. if (!DB::table('kku_user_items')
  230. ->where('user_id', $userId)
  231. ->where('item_id', $item['item_id'])
  232. ->exists()) {
  233. $errors[] = "用户未拥有物品: {$item['name']}";
  234. }
  235. }
  236. // 验证测试土地
  237. $lands = [
  238. TestConfig::getTestLand('pest_land'),
  239. TestConfig::getTestLand('weed_land'),
  240. TestConfig::getTestLand('drought_land')
  241. ];
  242. foreach ($lands as $land) {
  243. if (empty($land)) continue;
  244. if (!DB::table('kku_farm_lands')->where('id', $land['land_id'])->exists()) {
  245. $errors[] = "测试土地不存在 (ID: {$land['land_id']})";
  246. }
  247. if (!DB::table('kku_farm_crops')
  248. ->where('land_id', $land['land_id'])
  249. ->where('disaster_type', $land['disaster_type'])
  250. ->exists()) {
  251. $errors[] = "土地缺少对应灾害 (土地: {$land['land_id']}, 灾害: {$land['disaster_type']})";
  252. }
  253. }
  254. if (empty($errors)) {
  255. echo "✅ 所有测试数据验证通过\n";
  256. } else {
  257. echo "❌ 发现以下问题:\n";
  258. foreach ($errors as $error) {
  259. echo " - {$error}\n";
  260. }
  261. }
  262. echo "\n";
  263. }
  264. /**
  265. * 清理测试数据
  266. */
  267. public function cleanup(): void
  268. {
  269. echo "清理测试数据...\n";
  270. try {
  271. $userId = TestConfig::getTestUserId();
  272. // 清理用户物品
  273. DB::table('kku_user_items')->where('user_id', $userId)->delete();
  274. // 清理作物
  275. DB::table('kku_farm_crops')->where('user_id', $userId)->delete();
  276. // 清理土地
  277. DB::table('kku_farm_lands')->where('user_id', $userId)->delete();
  278. // 清理测试物品
  279. $itemIds = [
  280. TestConfig::getTestItem('pesticide')['item_id'],
  281. TestConfig::getTestItem('weedicide')['item_id'],
  282. TestConfig::getTestItem('watering_tool')['item_id'],
  283. TestConfig::getTestItem('invalid_item')['item_id']
  284. ];
  285. DB::table('game_items')->whereIn('id', $itemIds)->delete();
  286. // 清理测试用户
  287. DB::table('users')->where('id', $userId)->delete();
  288. echo "✅ 测试数据清理完成\n";
  289. } catch (\Exception $e) {
  290. echo "❌ 测试数据清理失败: " . $e->getMessage() . "\n";
  291. }
  292. }
  293. }
  294. // 检查命令行参数
  295. $action = $argv[1] ?? 'prepare';
  296. $preparer = new TestDataPreparer();
  297. switch ($action) {
  298. case 'prepare':
  299. $preparer->prepare();
  300. break;
  301. case 'cleanup':
  302. $preparer->cleanup();
  303. break;
  304. default:
  305. echo "使用方法:\n";
  306. echo " php prepare_test_data.php prepare # 准备测试数据\n";
  307. echo " php prepare_test_data.php cleanup # 清理测试数据\n";
  308. break;
  309. }