displayHeader(); try { // 1. 准备测试用户 $this->prepareTestUser(); // 2. 准备测试物品 $this->prepareTestItems(); // 3. 准备测试土地 $this->prepareTestLands(); // 4. 准备用户物品关联 $this->prepareUserItems(); // 5. 验证数据 $this->verifyTestData(); echo "✅ 测试数据准备完成!\n\n"; } catch (\Exception $e) { echo "❌ 测试数据准备失败: " . $e->getMessage() . "\n"; echo "错误详情: " . $e->getTraceAsString() . "\n"; } } /** * 显示头部信息 */ private function displayHeader(): void { echo "\n"; echo "========================================\n"; echo " 灾害去除测试数据准备工具\n"; echo "========================================\n"; echo "时间: " . date('Y-m-d H:i:s') . "\n"; echo "========================================\n\n"; } /** * 准备测试用户 */ private function prepareTestUser(): void { echo "准备测试用户...\n"; $userId = TestConfig::getTestUserId(); $userConfig = TestConfig::TEST_USER; // 检查用户是否存在 $userExists = DB::table('users')->where('id', $userId)->exists(); if (!$userExists) { echo "创建测试用户 (ID: {$userId})...\n"; DB::table('users')->insert([ 'id' => $userId, 'username' => $userConfig['username'], 'password' => bcrypt($userConfig['password']), 'email' => 'test@example.com', 'created_at' => now(), 'updated_at' => now() ]); echo "✅ 测试用户创建成功\n"; } else { echo "✅ 测试用户已存在\n"; } echo "\n"; } /** * 准备测试物品 */ private function prepareTestItems(): void { echo "准备测试物品...\n"; $items = [ TestConfig::getTestItem('pesticide'), TestConfig::getTestItem('weedicide'), TestConfig::getTestItem('watering_tool'), TestConfig::getTestItem('invalid_item') ]; foreach ($items as $item) { if (empty($item)) continue; $itemId = $item['item_id']; $itemExists = DB::table('game_items')->where('id', $itemId)->exists(); if (!$itemExists) { echo "创建物品: {$item['name']} (ID: {$itemId})...\n"; DB::table('game_items')->insert([ 'id' => $itemId, 'name' => $item['name'], 'description' => "测试用{$item['name']}", 'type' => 'tool', 'numeric_attributes' => json_encode($item['numeric_attributes'] ?? []), 'created_at' => now(), 'updated_at' => now() ]); echo "✅ 物品创建成功\n"; } else { echo "✅ 物品已存在: {$item['name']}\n"; } } echo "\n"; } /** * 准备测试土地 */ private function prepareTestLands(): void { echo "准备测试土地...\n"; $lands = [ TestConfig::getTestLand('pest_land'), TestConfig::getTestLand('weed_land'), TestConfig::getTestLand('drought_land'), TestConfig::getTestLand('normal_land'), TestConfig::getTestLand('other_user_land') ]; foreach ($lands as $land) { if (empty($land)) continue; $landId = $land['land_id']; $landExists = DB::table('kku_farm_lands')->where('id', $landId)->exists(); if (!$landExists) { echo "创建土地 (ID: {$landId}, 用户: {$land['user_id']})...\n"; DB::table('kku_farm_lands')->insert([ 'id' => $landId, 'user_id' => $land['user_id'], 'status' => 2, // 已种植 'created_at' => now(), 'updated_at' => now() ]); // 如果有作物,创建作物记录 if (isset($land['crop_id'])) { $this->createCropForLand($land); } echo "✅ 土地创建成功\n"; } else { echo "✅ 土地已存在 (ID: {$landId})\n"; } } echo "\n"; } /** * 为土地创建作物 */ private function createCropForLand(array $land): void { $cropExists = DB::table('kku_farm_crops') ->where('land_id', $land['land_id']) ->exists(); if (!$cropExists) { echo " 创建作物 (土地: {$land['land_id']}, 灾害类型: {$land['disaster_type']})...\n"; DB::table('kku_farm_crops')->insert([ 'land_id' => $land['land_id'], 'user_id' => $land['user_id'], 'seed_id' => $land['crop_id'], 'growth_stage' => $land['growth_stage'], 'disaster_type' => $land['disaster_type'], 'planted_at' => now()->subHours(2), // 2小时前种植 'created_at' => now(), 'updated_at' => now() ]); echo " ✅ 作物创建成功\n"; } } /** * 准备用户物品关联 */ private function prepareUserItems(): void { echo "准备用户物品关联...\n"; $userId = TestConfig::getTestUserId(); $items = [ TestConfig::getTestItem('pesticide'), TestConfig::getTestItem('weedicide'), TestConfig::getTestItem('watering_tool') ]; foreach ($items as $item) { if (empty($item)) continue; $itemId = $item['item_id']; $userItemExists = DB::table('kku_user_items') ->where('user_id', $userId) ->where('item_id', $itemId) ->exists(); if (!$userItemExists) { echo "给用户添加物品: {$item['name']}...\n"; DB::table('kku_user_items')->insert([ 'user_id' => $userId, 'item_id' => $itemId, 'quantity' => 100, // 给足够的数量用于测试 'created_at' => now(), 'updated_at' => now() ]); echo "✅ 用户物品添加成功\n"; } else { echo "✅ 用户已拥有物品: {$item['name']}\n"; } } echo "\n"; } /** * 验证测试数据 */ private function verifyTestData(): void { echo "验证测试数据...\n"; $errors = []; // 验证测试用户 $userId = TestConfig::getTestUserId(); if (!DB::table('users')->where('id', $userId)->exists()) { $errors[] = "测试用户不存在 (ID: {$userId})"; } // 验证测试物品 $items = [ TestConfig::getTestItem('pesticide'), TestConfig::getTestItem('weedicide'), TestConfig::getTestItem('watering_tool') ]; foreach ($items as $item) { if (empty($item)) continue; if (!DB::table('game_items')->where('id', $item['item_id'])->exists()) { $errors[] = "测试物品不存在: {$item['name']} (ID: {$item['item_id']})"; } if (!DB::table('kku_user_items') ->where('user_id', $userId) ->where('item_id', $item['item_id']) ->exists()) { $errors[] = "用户未拥有物品: {$item['name']}"; } } // 验证测试土地 $lands = [ TestConfig::getTestLand('pest_land'), TestConfig::getTestLand('weed_land'), TestConfig::getTestLand('drought_land') ]; foreach ($lands as $land) { if (empty($land)) continue; if (!DB::table('kku_farm_lands')->where('id', $land['land_id'])->exists()) { $errors[] = "测试土地不存在 (ID: {$land['land_id']})"; } if (!DB::table('kku_farm_crops') ->where('land_id', $land['land_id']) ->where('disaster_type', $land['disaster_type']) ->exists()) { $errors[] = "土地缺少对应灾害 (土地: {$land['land_id']}, 灾害: {$land['disaster_type']})"; } } if (empty($errors)) { echo "✅ 所有测试数据验证通过\n"; } else { echo "❌ 发现以下问题:\n"; foreach ($errors as $error) { echo " - {$error}\n"; } } echo "\n"; } /** * 清理测试数据 */ public function cleanup(): void { echo "清理测试数据...\n"; try { $userId = TestConfig::getTestUserId(); // 清理用户物品 DB::table('kku_user_items')->where('user_id', $userId)->delete(); // 清理作物 DB::table('kku_farm_crops')->where('user_id', $userId)->delete(); // 清理土地 DB::table('kku_farm_lands')->where('user_id', $userId)->delete(); // 清理测试物品 $itemIds = [ TestConfig::getTestItem('pesticide')['item_id'], TestConfig::getTestItem('weedicide')['item_id'], TestConfig::getTestItem('watering_tool')['item_id'], TestConfig::getTestItem('invalid_item')['item_id'] ]; DB::table('game_items')->whereIn('id', $itemIds)->delete(); // 清理测试用户 DB::table('users')->where('id', $userId)->delete(); echo "✅ 测试数据清理完成\n"; } catch (\Exception $e) { echo "❌ 测试数据清理失败: " . $e->getMessage() . "\n"; } } } // 检查命令行参数 $action = $argv[1] ?? 'prepare'; $preparer = new TestDataPreparer(); switch ($action) { case 'prepare': $preparer->prepare(); break; case 'cleanup': $preparer->cleanup(); break; default: echo "使用方法:\n"; echo " php prepare_test_data.php prepare # 准备测试数据\n"; echo " php prepare_test_data.php cleanup # 清理测试数据\n"; break; }