mirror of
https://devops.lemonos.cn/lawson/FendxPHP.git
synced 2026-06-15 15:02:49 +08:00
- 创建用户表(users)包含基本信息和认证字段 - 创建角色表(roles)用于权限控制 - 创建权限表(permissions)定义系统权限 - 创建用户角色关联表(user_roles)建立用户与角色关系 - 创建角色权限关联表(role_permissions)建立角色与权限关系 - 创建迁移记录表(migrations)追踪数据库变更 - 添加AdminController提供管理员面板功能 - 实现系统监控、配置管理、缓存清理等功能 - 添加AOP切面编程支持的各种通知类型 - 实现告警管理AlertManager支持多渠道告警 - 添加文档注解接口规范
7.7 KiB
7.7 KiB
FendxPHP 快速测试指南
🚀 快速开始
1. 环境准备
# 克隆项目
git clone https://github.com/your-org/fendx-php.git
cd fendx-php
# 安装依赖
composer install
# 复制环境配置
cp .env.example .env
2. 运行测试
Windows 环境
# 运行所有测试
.\scripts\run-tests.ps1
# 运行单元测试
.\scripts\run-tests.ps1 unit --coverage
# 运行集成测试
.\scripts\run-tests.ps1 integration
# 运行API测试
.\scripts\run-tests.ps1 api
# 运行性能测试
.\scripts\run-tests.ps1 performance
Linux/Mac 环境
# 设置执行权限
chmod +x scripts/run-tests.sh
# 运行所有测试
./scripts/run-tests.sh
# 运行单元测试
./scripts/run-tests.sh unit --coverage
# 运行集成测试
./scripts/run-tests.sh integration
# 运行API测试
./scripts/run-tests.sh api
# 运行性能测试
./scripts/run-tests.sh performance
3. 使用控制台命令
# 运行单元测试
php bin/console test:unit
# 运行集成测试
php bin/console test:integration
# 运行API测试
php bin/console test:api
# 运行性能测试
php bin/console test:performance
# 运行所有测试
php bin/console test:all --coverage
📊 测试类型说明
单元测试 (Unit Tests)
- 位置:
tests/Unit/ - 用途: 测试单个类和方法
- 运行时间: 快速 (< 1分钟)
- 覆盖率: 代码覆盖率分析
# 运行单元测试
php bin/console test:unit --coverage
# 过滤特定测试
php bin/console test:unit --filter=UserTest
集成测试 (Integration Tests)
- 位置:
tests/Integration/ - 用途: 测试组件间交互
- 运行时间: 中等 (2-5分钟)
- 环境: 需要数据库和缓存
# 运行集成测试
php bin/console test:integration
# 使用Docker环境
./scripts/run-tests.sh integration --no-docker=false
API测试 (API Tests)
- 位置:
tests/API/ - 用途: 测试HTTP接口
- 运行时间: 中等 (3-8分钟)
- 环境: 需要完整应用服务
# 运行API测试
php bin/console test:api
# 启动服务器进行测试
php -S localhost:8000 -t public/
vendor/bin/codecept run api
性能测试 (Performance Tests)
- 位置:
tests/Performance/ - 用途: 性能基准测试
- 运行时间: 较长 (5-15分钟)
- 指标: 响应时间、吞吐量、内存使用
# 运行性能测试
php bin/console test:performance
# 并发测试
ab -n 1000 -c 10 http://localhost:8000/api/users
安全测试 (Security Tests)
- 位置:
tests/Security/ - 用途: 安全漏洞扫描
- 运行时间: 中等 (2-5分钟)
- 检查: 依赖漏洞、代码安全
# 运行安全测试
php bin/console test:security
# 依赖审计
composer audit
🐳 Docker测试环境
启动测试环境
# 启动所有测试服务
docker-compose -f docker-compose.test.yml up -d
# 查看服务状态
docker-compose -f docker-compose.test.yml ps
# 查看日志
docker-compose -f docker-compose.test.yml logs -f app
测试服务说明
| 服务 | 端口 | 用途 |
|---|---|---|
| app | - | PHP应用 |
| mysql-test | 3307 | 测试数据库 |
| redis-test | 6380 | 测试缓存 |
| nginx-test | 8080 | Web服务器 |
| selenium-hub | 4444 | 浏览器自动化 |
| mailhog | 8025 | 邮件测试 |
| minio | 9000 | 对象存储测试 |
停止测试环境
# 停止并清理
docker-compose -f docker-compose.test.yml down --volumes
# 清理所有测试数据
docker-compose -f docker-compose.test.yml down --volumes --remove-orphans
📈 测试报告
查看覆盖率报告
# HTML报告
open reports/coverage/index.html
# XML报告(用于CI/CD)
cat reports/coverage.xml
查看测试结果
# JUnit格式报告
cat reports/junit.xml
# 完整测试报告
open reports/test-report.html
性能测试结果
# 查看并发测试结果
cat reports/performance/ab-results.txt
# 查看内存测试结果
cat reports/performance/memory-test.txt
# 查看数据库性能
cat reports/performance/database-test.txt
🔧 故障排查
常见问题
1. 依赖安装失败
# 清理并重新安装
rm -rf vendor/
composer install --no-cache
# 更新Composer
composer self-update
2. 数据库连接失败
# 检查MySQL服务
docker-compose -f docker-compose.test.yml logs mysql-test
# 重启数据库
docker-compose -f docker-compose.test.yml restart mysql-test
3. 测试超时
# 增加超时时间
php -d max_execution_time=300 bin/console test:integration
# 并行运行
./scripts/run-tests.sh all --parallel=4
4. 内存不足
# 增加内存限制
php -d memory_limit=1G bin/console test:unit
# 检查内存使用
php -d memory_limit=1G -r "echo memory_get_usage(true) / 1024 / 1024 . ' MB\n';"
调试技巧
1. 详细输出
# 详细模式
php bin/console test:unit --verbose
# 调试模式
XDEBUG_MODE=debug php bin/console test:unit
2. 单个测试
# 运行单个测试类
vendor/bin/phpunit tests/Unit/UserServiceTest.php
# 运行单个测试方法
vendor/bin/phpunit --filter testRegisterSuccess tests/Unit/UserServiceTest.php
3. 停止在失败处
# 遇到失败停止
vendor/bin/phpunit --stop-on-failure
# 遇到错误停止
vendor/bin/phpunit --stop-on-error
🎯 测试最佳实践
1. 编写测试
// 测试命名规范
class UserServiceTest extends TestCase
{
public function testRegisterSuccess(): void
{
// Given - 准备测试数据
$userData = ['username' => 'test', 'email' => 'test@example.com'];
// When - 执行操作
$result = $this->userService->register($userData);
// Then - 验证结果
$this->assertTrue($result['success']);
$this->assertArrayHasKey('user_id', $result);
}
}
2. Mock对象
// 创建Mock对象
$userDao = $this->createMock(UserDao::class);
$userDao->method('findById')->willReturn($testUser);
// 设置期望
$userDao->expects($this->once())
->method('create')
->with($this->isInstanceOf(UserDto::class));
3. 数据库测试
// 使用事务回滚
protected function setUp(): void
{
$this->app['db']->beginTransaction();
}
protected function tearDown(): void
{
$this->app['db']->rollback();
}
4. API测试
// 发送HTTP请求
$response = $this->client->post('/api/users', [
'json' => ['username' => 'test', 'email' => 'test@example.com']
]);
$this->assertEquals(201, $response->getStatusCode());
$this->assertJson($response->getContent());
📋 测试检查清单
提交前检查
- 所有单元测试通过
- 代码覆盖率 > 80%
- 集成测试通过
- API测试通过
- 无安全漏洞
发布前检查
- 性能测试达标
- E2E测试通过
- 压力测试通过
- 兼容性测试通过
持续集成
# .github/workflows/test.yml
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- name: Install dependencies
run: composer install
- name: Run tests
run: ./scripts/run-tests.sh
📞 获取帮助
文档资源
社区支持
- GitHub Issues: 报告问题和建议
- 讨论区: 技术讨论和问答
- Wiki: 详细文档和教程
最后更新:2024-01-15
版本:v1.0
维护:FendxPHP 开发团队