$fullUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => array_merge([ 'Content-Type: application/json', 'Accept: application/json' ], $headers), CURLOPT_TIMEOUT => 10 ]); if (!empty($data) && in_array($method, ['POST', 'PUT', 'PATCH'])) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) { return [ 'success' => false, 'error' => $error, 'http_code' => 0 ]; } $responseData = json_decode($response, true); return [ 'success' => $httpCode >= 200 && $httpCode < 300, 'http_code' => $httpCode, 'data' => $responseData, 'response' => $response ]; } // 输出测试结果 function outputResult(string $testName, array $result): void { global $testResults; $status = $result['success'] ? '✅ PASS' : '❌ FAIL'; echo "$status $testName\n"; if (!$result['success']) { if (isset($result['error'])) { echo " 错误: {$result['error']}\n"; } else { echo " HTTP状态码: {$result['http_code']}\n"; echo " 响应: {$result['response']}\n"; } } else { echo " 状态码: {$result['http_code']}\n"; if (isset($result['data']['traceId'])) { echo " TraceId: {$result['data']['traceId']}\n"; } } echo "\n"; $testResults[$testName] = $result['success']; } // 检查服务是否启动 echo "🔍 检查服务状态...\n"; $healthCheck = testApi('GET', '/health'); if (!$healthCheck['success']) { echo "❌ 服务未启动或无法访问\n"; echo "请先启动Web服务: php -S localhost:8000 -t public\n"; exit(1); } echo "✅ 服务运行正常\n\n"; // 开始API测试 echo "🚀 开始API功能测试...\n\n"; // 1. 测试健康检查 outputResult('健康检查接口', testApi('GET', '/health')); // 2. 测试用户统计 outputResult('用户统计接口', testApi('GET', '/api/users/stats')); // 3. 测试获取用户列表 outputResult('获取用户列表', testApi('GET', '/api/users')); // 4. 测试创建用户 $userData = [ 'username' => 'testuser_' . time(), 'email' => 'test' . time() . '@example.com', 'password' => 'password123' ]; $createResult = testApi('POST', '/api/users', $userData); outputResult('创建用户', $createResult); $userId = null; if ($createResult['success'] && isset($createResult['data']['data']['id'])) { $userId = $createResult['data']['data']['id']; echo "📝 创建的用户ID: $userId\n\n"; } // 5. 测试获取用户详情 if ($userId) { outputResult('获取用户详情', testApi('GET', "/api/users/$userId")); } // 6. 测试更新用户 if ($userId) { $updateData = ['username' => 'updated_user_' . time()]; outputResult('更新用户', testApi('PUT', "/api/users/$userId", $updateData)); } // 7. 测试用户搜索 outputResult('用户搜索', testApi('GET', '/api/users/search?keyword=test')); // 8. 测试获取活跃用户 outputResult('获取活跃用户', testApi('GET', '/api/users/active')); // 9. 测试用户存在性检查 if ($userId) { outputResult('用户存在性检查', testApi('GET', "/api/users/$userId/exists")); } // 10. 测试邮箱检查 outputResult('邮箱检查', testApi('POST', '/api/users/check-email', ['email' => 'test@example.com'])); // 11. 测试用户名检查 outputResult('用户名检查', testApi('POST', '/api/users/check-username', ['username' => 'testuser'])); // 12. 测试错误处理 - 404 outputResult('404错误处理', testApi('GET', '/api/nonexistent')); // 13. 测试错误处理 - 参数验证 outputResult('参数验证错误', testApi('POST', '/api/users', [ 'username' => '', 'email' => 'invalid-email' ])); // 14. 测试错误处理 - 方法不允许 outputResult('方法不允许', testApi('POST', '/api/users/stats')); // 15. 测试缓存功能(如果用户存在) if ($userId) { echo "🔄 测试缓存功能...\n"; // 第一次请求 $start = microtime(true); $firstRequest = testApi('GET', "/api/users/$userId"); $firstTime = microtime(true) - $start; // 第二次请求(应该从缓存返回) $start = microtime(true); $secondRequest = testApi('GET', "/api/users/$userId"); $secondTime = microtime(true) - $start; echo "第一次请求时间: " . round($firstTime * 1000, 2) . "ms\n"; echo "第二次请求时间: " . round($secondTime * 1000, 2) . "ms\n"; if ($secondTime < $firstTime) { echo "✅ 缓存可能生效(第二次请求更快)\n"; } else { echo "⚠️ 缓存可能未生效或差异不明显\n"; } echo "\n"; } // 16. 清理测试数据 if ($userId) { outputResult('删除测试用户', testApi('DELETE', "/api/users/$userId")); } // 生成测试报告 echo "📊 生成测试报告...\n"; $totalTests = count($testResults); $passedTests = array_sum($testResults); $failedTests = $totalTests - $passedTests; $report = [ 'test_time' => date('Y-m-d H:i:s'), 'total_tests' => $totalTests, 'passed_tests' => $passedTests, 'failed_tests' => $failedTests, 'success_rate' => round(($passedTests / $totalTests) * 100, 2) . '%', 'test_results' => $testResults ]; file_put_contents('runtime/api_test_report.json', json_encode($report, JSON_PRETTY_PRINT)); echo "✅ API测试报告已保存到 runtime/api_test_report.json\n\n"; // 输出测试总结 echo "📋 API测试总结\n"; echo "================\n"; echo "总测试数: $totalTests\n"; echo "通过测试: $passedTests\n"; echo "失败测试: $failedTests\n"; echo "成功率: {$report['success_rate']}\n\n"; if ($failedTests === 0) { echo "🎉 所有API测试通过!\n"; echo "✅ 框架API功能正常\n"; } else { echo "⚠️ 有 $failedTests 个测试失败\n"; echo "❌ 请检查相关功能\n"; } echo "\n📝 详细信息:\n"; echo "- 查看完整报告: runtime/api_test_report.json\n"; echo "- 查看应用日志: runtime/logs/\n"; echo "- 查看测试指南: docs/冒烟测试指南.md\n\n"; echo "🚀 API测试完成!\n";