Skip to content

故障排查指南

本文档帮助您解决测试运行过程中遇到的常见问题。


目录


连接问题

问题: cURL error 7 - Failed to connect

错误信息:

cURL error 7: Failed to connect to localhost port 8000: Connection refused

可能原因:

  1. API 服务器未启动
  2. 端口配置错误
  3. 防火墙阻止连接

解决方案:

  1. 检查 API 服务器状态
bash
# 检查端口是否监听
lsof -i :8000
# 或
netstat -tlnp | grep 8000
  1. 确认服务器正在运行
bash
# 查看进程
ps aux | grep php
  1. 验证 API_BASE_URL 配置
bash
# 测试连接
curl http://localhost:8000/
  1. 检查防火墙
bash
# macOS
sudo pfctl -sr | grep 8000

# Linux
sudo iptables -L -n | grep 8000

问题: cURL error 6 - Could not resolve host

错误信息:

cURL error 6: Could not resolve host: your-api-server

解决方案:

  1. 检查 hosts 文件
bash
cat /etc/hosts
  1. 使用 IP 地址测试
bash
curl http://127.0.0.1:8000/
  1. 检查 DNS 配置
bash
nslookup your-api-server

超时问题

问题: cURL error 28 - Operation timed out

错误信息:

cURL error 28: Operation timed out after 60000 milliseconds

可能原因:

  1. API 处理时间过长
  2. 网络延迟高
  3. 超时设置过短

解决方案:

  1. 增加超时时间
php
// 在测试中修改
$this->httpClient = new Client([
    'base_uri' => $this->baseUrl,
    'timeout' => 300, // 增加到 300 秒
    'verify' => false,
]);
  1. 手动测试 API 响应时间
bash
time curl http://localhost:8000/api/app/copy/marketing
  1. 检查后端日志
bash
# 查看应用日志
tail -f api/runtime/log/$(date +%Y%m%d).log
  1. 检查 AIGC 服务状态

问题: PHPUnit 整体超时

错误信息:

PHP Fatal error: Maximum execution time of 30 seconds exceeded

解决方案:

  1. 修改 php.ini
ini
max_execution_time = 300
  1. 在测试中临时设置
php
public function testLongRunning(): void
{
    set_time_limit(300);
    // 测试代码
}
  1. 使用 phpunit.xml 配置
xml
<php>
    <ini name="max_execution_time" value="300"/>
</php>

认证问题

问题: 401 Unauthorized

错误信息:

HTTP 401 Unauthorized

可能原因:

  1. 缺少认证令牌
  2. 令牌过期
  3. 签名错误

解决方案:

  1. 检查认证中间件 查看 app/base/middleware/RouteAuth.phpSignAuth.php

  2. 确认测试绕过认证 检查路由是否在公开路由列表中

  3. 查看后端日志中的认证失败原因

bash
tail -f api/runtime/log/$(date +%Y%m%d).log | grep -i auth

问题: API 密钥无效

错误信息:

API key is invalid or expired

解决方案:

  1. 检查环境变量配置
bash
# 查看当前环境变量
env | grep VOLCENGINE
env | grep ALIYUN
  1. 检查 .env 文件
bash
cat api/.env | grep -E "(VOLC|ALI)"
  1. 验证密钥在控制台中是否有效

API 响应问题

问题: JSON 解析失败

错误信息:

json_decode() expects parameter 1 to be string, array given

解决方案:

  1. 查看原始响应
php
$responseBody = (string)$response->getBody();
echo "Raw response: " . $responseBody . "\n";
  1. 检查响应 Content-Type
php
$contentType = $response->getHeader('Content-Type')[0] ?? '';
echo "Content-Type: " . $contentType . "\n";
  1. 验证 API 返回的是 JSON 而非 HTML 错误页

问题: 业务错误码非 0

错误信息:

Failed asserting that 1 matches expected 0.

解决方案:

  1. 查看完整的错误响应
php
echo "Error message: " . ($body['message'] ?? 'No message') . "\n";
  1. 检查后端日志
bash
tail -f api/runtime/log/$(date +%Y%m%d).log
  1. 验证请求参数 确认所有必填参数都已提供且格式正确

测试框架问题

问题: Class not found

错误信息:

Fatal error: Uncaught Error: Class 'PHPUnit\Framework\TestCase' not found

解决方案:

  1. 确认 Composer 依赖已安装
bash
cd api
composer install
  1. 确认 autoload 已加载 检查测试文件顶部的 autoload 加载代码

  2. 运行 composer dump-autoload

bash
composer dump-autoload

问题: Test method not found

错误信息:

No tests found in class "AigcCopyPipelineTest"

解决方案:

  1. 确认测试方法以 test 开头
php
// ✅ 好
public function testMarketingCopyGeneration(): void {}

// ❌ 不好
public function marketingCopyGeneration(): void {}
  1. 确认方法是 public
php
// ✅ 好
public function testSomething(): void {}

// ❌ 不好
protected function testSomething(): void {}
  1. 使用 --list-tests 查看可用测试
bash
vendor/bin/phpunit tests/api/AigcCopyPipelineTest.php --list-tests

性能问题

问题: 测试运行太慢

解决方案:

  1. 只运行必要的测试
bash
# 只运行特定测试
vendor/bin/phpunit --filter testMarketingCopyGeneration

# 排除慢速测试
vendor/bin/phpunit --exclude-group slow
  1. 使用测试缓存
bash
vendor/bin/phpunit --cache-result
  1. 并行运行
bash
composer require --dev brianium/paratest
vendor/bin/paratest -p4

问题: 内存耗尽

错误信息:

Fatal error: Allowed memory size of 134217728 bytes exhausted

解决方案:

  1. 增加 PHP 内存限制
php
// 在测试文件开头
ini_set('memory_limit', '512M');
  1. 在 phpunit.xml 中配置
xml
<php>
    <ini name="memory_limit" value="512M"/>
</php>
  1. 优化测试,避免加载过多数据

获取帮助

如果以上方案都无法解决问题:

  1. 收集诊断信息
bash
# PHP 版本
php -v

# PHPUnit 版本
vendor/bin/phpunit --version

# Composer 依赖
composer show

# 环境变量
env | grep -E "(API|VOLC|ALI|APP)"
  1. 查看完整的错误堆栈
bash
vendor/bin/phpunit --debug
  1. 检查后端日志
bash
# 应用日志
ls -la api/runtime/log/

# Web 服务器日志
# Nginx: /var/log/nginx/
# Apache: /var/log/apache2/

相关文档