Files
FendxPHP/docs/冒烟测试指南.md

401 lines
7.6 KiB
Markdown
Raw Normal View History

# FendxPHP 冒烟测试指南
## 🎯 测试目标
验证FendxPHP框架核心功能是否正常工作确保框架可以正常运行。
## 📋 测试环境要求
- PHP >= 8.1
- MySQL >= 5.7
- Redis >= 5.0
- Composer
## 🚀 快速启动测试
### 1. 环境准备
```bash
# 克隆项目
git clone <repository-url>
cd FendxPHP
# 安装依赖(如果有)
composer install
# 创建数据库
mysql -u root -p -e "CREATE DATABASE fendx CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
# 配置数据库连接
# 编辑 config/database.php 文件
```
### 2. 启动Web服务
```bash
# 启动PHP内置服务器
php -S localhost:8000 -t public
# 或使用其他Web服务器指向 public 目录
```
### 3. 基础冒烟测试
#### 3.1 健康检查测试
```bash
# 测试基础路由
curl -X GET http://localhost:8000/health
# 预期响应
{
"code": 200,
"message": "OK",
"data": {
"status": "healthy",
"timestamp": "2024-01-01 12:00:00"
},
"traceId": "trace_xxx"
}
```
#### 3.2 用户API测试
```bash
# 1. 创建用户
curl -X POST http://localhost:8000/api/users \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"password": "password123"
}'
# 预期响应
{
"code": 200,
"message": "User created successfully",
"data": {
"id": 1,
"username": "testuser",
"email": "test@example.com",
"status": 1,
"created_at": "2024-01-01 12:00:00",
"updated_at": "2024-01-01 12:00:00"
},
"traceId": "trace_xxx"
}
# 2. 获取用户列表
curl -X GET http://localhost:8000/api/users
# 3. 获取用户详情
curl -X GET http://localhost:8000/api/users/1
# 4. 更新用户
curl -X PUT http://localhost:8000/api/users/1 \
-H "Content-Type: application/json" \
-d '{
"username": "updateduser"
}'
# 5. 删除用户
curl -X DELETE http://localhost:8000/api/users/1
```
#### 3.3 缓存功能测试
```bash
# 测试缓存接口(需要先创建用户)
curl -X GET http://localhost:8000/api/users/1
# 第二次请求应该从缓存返回
curl -X GET http://localhost:8000/api/users/1
```
#### 3.4 错误处理测试
```bash
# 测试404错误
curl -X GET http://localhost:8000/api/nonexistent
# 预期响应
{
"code": 404,
"message": "Route not found",
"data": null,
"traceId": "trace_xxx"
}
# 测试参数验证错误
curl -X POST http://localhost:8000/api/users \
-H "Content-Type: application/json" \
-d '{
"username": "",
"email": "invalid-email"
}'
# 预期响应
{
"code": 422,
"message": "Validation failed",
"data": {
"username": ["The username field is required."],
"email": ["The email must be a valid email address."]
},
"traceId": "trace_xxx"
}
```
## 🔧 详细功能测试
### 1. 核心组件测试
#### 1.1 IOC容器测试
```php
// 创建测试文件 test_container.php
<?php
require_once 'fendx-framework/fendx-starter/src/Bootstrap.php';
use Fendx\Core\Container\Container;
use App\Service\UserService;
$container = new Container();
$container->singleton(UserService::class);
$userService = $container->make(UserService::class);
echo "IOC Container Test: " . ($userService instanceof UserService ? "PASS" : "FAIL") . "\n";
```
#### 1.2 路由系统测试
```php
// 创建测试文件 test_router.php
<?php
require_once 'fendx-framework/fendx-starter/src/Bootstrap.php';
use Fendx\Web\Route\Router;
use Fendx\Web\Request\Request;
$router = new Router();
$router->get('/test', function() { return 'test'; });
$request = new Request();
// 模拟请求测试
echo "Router Test: PASS\n";
```
#### 1.3 数据库连接测试
```php
// 创建测试文件 test_database.php
<?php
require_once 'fendx-framework/fendx-starter/src/Bootstrap.php';
use Fendx\Db\DB;
try {
$pdo = DB::pdo();
$result = $pdo->query('SELECT 1')->fetch();
echo "Database Test: " . ($result ? "PASS" : "FAIL") . "\n";
} catch (Exception $e) {
echo "Database Test: FAIL - " . $e->getMessage() . "\n";
}
```
#### 1.4 缓存连接测试
```php
// 创建测试文件 test_cache.php
<?php
require_once 'fendx-framework/fendx-starter/src/Bootstrap.php';
use Fendx\Cache\Cache;
try {
Cache::set('test_key', 'test_value', 60);
$value = Cache::get('test_key');
echo "Cache Test: " . ($value === 'test_value' ? "PASS" : "FAIL") . "\n";
} catch (Exception $e) {
echo "Cache Test: FAIL - " . $e->getMessage() . "\n";
}
```
### 2. 注解功能测试
#### 2.1 控制器注解测试
```bash
# 测试注解路由是否正常工作
curl -X GET http://localhost:8000/api/users/stats
# 预期响应包含用户统计信息
{
"code": 200,
"message": "Success",
"data": {
"total_users": 0,
"active_users": 0,
"inactive_users": 0
},
"traceId": "trace_xxx"
}
```
#### 2.2 缓存注解测试
```bash
# 第一次请求
time curl -X GET http://localhost:8000/api/users/active
# 第二次请求(应该更快,从缓存返回)
time curl -X GET http://localhost:8000/api/users/active
```
### 3. 拦截器测试
#### 3.1 认证拦截器测试
```bash
# 测试需要认证的接口不携带token
curl -X GET http://localhost:8000/api/users/protected
# 预期响应
{
"code": 401,
"message": "Unauthorized",
"data": null,
"traceId": "trace_xxx"
}
# 测试携带token的请求
curl -X GET http://localhost:8000/api/users/protected \
-H "Authorization: Bearer your_token_here"
```
## 📊 性能基准测试
### 1. 简单性能测试
```bash
# 使用ab工具进行压力测试
ab -n 1000 -c 10 http://localhost:8000/health
# 预期结果
# Requests per second: > 1000
# Time per request: < 10ms
```
### 2. 内存使用测试
```bash
# 监控内存使用
php -d memory_limit=128M -S localhost:8000 -t public
# 在另一个终端执行
curl -X GET http://localhost:8000/api/users
```
## 🐛 常见问题排查
### 1. 启动失败
```bash
# 检查PHP版本
php --version
# 检查必需扩展
php -m | grep -E "(pdo|redis|json|mbstring)"
# 检查文件权限
ls -la runtime/
chmod -R 755 runtime/
```
### 2. 数据库连接失败
```bash
# 测试数据库连接
mysql -h 127.0.0.1 -u root -p fendx -e "SELECT 1;"
# 检查配置文件
cat config/database.php
```
### 3. 缓存连接失败
```bash
# 测试Redis连接
redis-cli ping
# 检查Redis配置
cat config/cache.php
```
### 4. 路由不工作
```bash
# 检查路由配置
cat config/routes.php
# 检查控制器文件
ls -la app/Controller/
# 查看错误日志
tail -f runtime/logs/fendx.log
```
## ✅ 测试检查清单
- [ ] Web服务正常启动
- [ ] 健康检查接口返回200
- [ ] 用户CRUD接口正常工作
- [ ] 数据库连接正常
- [ ] 缓存功能正常
- [ ] 错误处理正确
- [ ] 日志记录正常
- [ ] 性能指标达标
- [ ] 注解功能正常
- [ ] 拦截器工作正常
## 📝 测试报告模板
```
FendxPHP 冒烟测试报告
=====================
测试时间: 2024-01-01 12:00:00
测试环境: PHP 8.1, MySQL 8.0, Redis 6.0
测试结果:
✅ 基础路由 - PASS
✅ 用户API - PASS
✅ 数据库连接 - PASS
✅ 缓存功能 - PASS
✅ 错误处理 - PASS
✅ 性能测试 - PASS
发现问题:
-
性能指标:
- QPS: 1500
- 响应时间: 5ms
- 内存使用: 8MB
结论: 框架运行正常,可以投入使用。
```
## 🚨 测试失败处理
如果测试失败,按以下步骤排查:
1. **检查错误日志**: `runtime/logs/fendx.log`
2. **验证配置文件**: 确保所有配置正确
3. **检查环境依赖**: 确保PHP版本和扩展满足要求
4. **逐步测试**: 从最简单的功能开始测试
5. **查看详细错误**: 使用 `php -f` 运行文件查看具体错误
完成所有测试后,框架即可投入生产使用!