notfff 2e4797cebe 1 hai 7 meses
..
Urs 2e4797cebe 1 hai 7 meses
BaseRequestTest.php a66019453f 1 hai 7 meses
README.md a66019453f 1 hai 7 meses
ThirdPartyTestSuite.php a66019453f 1 hai 7 meses
UrsCryptoServiceTest.php a66019453f 1 hai 7 meses
UrsRequestTest.php a66019453f 1 hai 7 meses

README.md

ThirdParty模块测试

本目录包含ThirdParty模块的PHPUnit测试用例,用于验证模块的功能正确性和代码质量。

测试结构

tests/Unit/ThirdParty/
├── README.md                    # 本说明文件
├── ThirdPartyTestSuite.php      # 测试套件组织器
├── BaseRequestTest.php          # BaseRequest基类测试
├── UrsRequestTest.php           # URS Request机制测试
└── UrsCryptoServiceTest.php     # URS加密服务测试

测试内容

1. BaseRequestTest.php

测试ThirdParty模块的BaseRequest基类:

  • ✅ 类存在性和抽象性验证
  • ✅ 必需方法和属性检查
  • ✅ 构造函数参数验证
  • ✅ 单一职责原则设计验证
  • ✅ 基本功能和错误处理测试

2. UrsRequestTest.php

测试URS Request机制重构后的功能:

  • ✅ 3个真实Request类存在性验证
  • ✅ UrsService服务类功能测试
  • ✅ 类实例化和继承关系验证
  • ✅ 静态方法和参数验证测试
  • ✅ 单一职责原则遵循验证
  • ✅ 确保不存在虚构的Request类

3. UrsCryptoServiceTest.php

测试URS加密服务功能:

  • ✅ 加密解密基本功能测试
  • ✅ 数据完整性和格式验证
  • ✅ 时间戳过期机制测试
  • ✅ 签名验证和防篡改测试
  • ✅ 不同密钥隔离性测试
  • ✅ 异常情况处理测试

运行测试

运行所有ThirdParty模块测试

vendor/bin/phpunit tests/Unit/ThirdParty/

运行特定测试类

# 测试BaseRequest基类
vendor/bin/phpunit tests/Unit/ThirdParty/BaseRequestTest.php

# 测试URS Request机制
vendor/bin/phpunit tests/Unit/ThirdParty/UrsRequestTest.php

# 测试URS加密服务
vendor/bin/phpunit tests/Unit/ThirdParty/UrsCryptoServiceTest.php

运行特定测试方法

# 测试URS Request类存在性
vendor/bin/phpunit tests/Unit/ThirdParty/UrsRequestTest.php --filter testUrsRequestClassesExist

# 测试加密解密功能
vendor/bin/phpunit tests/Unit/ThirdParty/UrsCryptoServiceTest.php --filter testEncryptDecryptRoundTrip

详细输出模式

# 显示详细测试信息
vendor/bin/phpunit tests/Unit/ThirdParty/ --verbose

# 显示测试覆盖率(需要安装xdebug)
vendor/bin/phpunit tests/Unit/ThirdParty/ --coverage-text

测试统计

当前测试统计信息:

  • 测试类数量: 3个
  • 测试方法总数: 约30个
  • 覆盖功能: BaseRequest基类、URS Request机制、URS加密服务

各类测试分布:

  • BaseRequestTest: 8个测试方法
  • UrsRequestTest: 12个测试方法
  • UrsCryptoServiceTest: 12个测试方法

测试原则

1. 单元测试原则

  • 每个测试方法只测试一个功能点
  • 测试方法名清晰描述测试内容
  • 使用断言验证预期结果

2. 独立性原则

  • 测试之间相互独立,不依赖执行顺序
  • 避免测试间的数据共享
  • 每个测试都有明确的前置条件

3. 可重复性原则

  • 测试结果稳定,多次运行结果一致
  • 不依赖外部环境(数据库、网络等)
  • 使用模拟对象避免外部依赖

4. 快速执行原则

  • 测试执行速度快,适合频繁运行
  • 避免耗时操作(如网络请求、文件IO)
  • 使用内存数据而非持久化存储

测试覆盖范围

已覆盖功能

  • ✅ BaseRequest基类架构
  • ✅ URS Request机制重构验证
  • ✅ URS加密解密功能
  • ✅ 单一职责原则遵循
  • ✅ 错误处理和异常情况
  • ✅ 参数验证和类型检查

待扩展测试

  • 🔄 Webhook处理机制测试
  • 🔄 配额管理功能测试
  • 🔄 日志记录功能测试
  • 🔄 服务配置管理测试
  • 🔄 集成测试用例

测试最佳实践

1. 测试命名规范

// 好的测试方法名
public function testUrsRequestClassesExist()
public function testEncryptDecryptRoundTrip()
public function testSignatureVerification()

// 避免的命名
public function test1()
public function testSomething()

2. 断言使用

// 使用具体的断言方法
$this->assertTrue($condition, '描述性错误信息');
$this->assertEquals($expected, $actual, '描述性错误信息');
$this->assertInstanceOf(ClassName::class, $object);

// 避免通用断言
$this->assert($condition);

3. 异常测试

// 正确的异常测试
$this->expectException(ExceptionClass::class);
$this->expectExceptionMessage('预期的错误信息');
$object->methodThatShouldThrow();

4. 数据提供者

/**
 * @dataProvider validDataProvider
 */
public function testWithValidData($input, $expected)
{
    $result = $this->service->process($input);
    $this->assertEquals($expected, $result);
}

public function validDataProvider(): array
{
    return [
        'case1' => ['input1', 'expected1'],
        'case2' => ['input2', 'expected2'],
    ];
}

持续集成

这些测试应该集成到CI/CD流程中:

  1. 代码提交前: 运行相关测试确保功能正常
  2. Pull Request: 自动运行全部测试
  3. 部署前: 运行完整测试套件
  4. 定期检查: 定时运行测试确保代码质量

贡献指南

添加新测试时请遵循:

  1. 测试文件命名: {功能名}Test.php
  2. 测试类命名: {功能名}Test
  3. 测试方法命名: test{具体功能描述}
  4. 添加文档注释: 说明测试目的和验证内容
  5. 更新README: 在本文件中记录新增的测试内容