WeChatDeveloper/tests/ConfigInterfaceTest.php
Anyon e8cd998852 feat(security): 强化 token 密钥证书格式校验
- 新增 CredentialValidator,集中校验微信 Token、EncodingAESKey、APIv3 Key、RSA 密钥和证书。

- 配置对象构造时提前验证微信、微信支付、支付宝关键凭证格式。

- 消息加解密和支付通知解密复用统一校验,减少运行期隐性错误。

- 补充无效 Token、密钥、证书和解密场景测试,提升凭证错误可诊断性。
2026-05-08 11:32:58 +08:00

116 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
/**
* This file is part of HyperfAdmin.
*
* @Link https://thinkadmin.top
* @Author Anyon<zoujingli@qq.com>
*/
namespace We\Tests;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use We\Config\AlipayPaymentConfig;
use We\Config\AlipayPlatformConfig;
use We\Config\WechatPaymentConfig;
use We\Config\WechatPlatformConfig;
use We\Config\WechatServiceConfig;
use We\Config\WechatWxappConfig;
use We\Contract\ConfigInterface;
use We\Exception\WechatException;
/**
* 平台配置契约与配置对象测试用例。
* @internal
*/
#[CoversClass(ConfigInterface::class)]
final class ConfigInterfaceTest extends TestCase
{
/**
* 测试所有配置对象都实现配置契约。
*/
public function testAllConfigClassesImplementContract(): void
{
foreach ([
new WechatPlatformConfig('wx_app', 'secret'),
new WechatWxappConfig('wx_wxapp', 'secret'),
new WechatServiceConfig('wx_component', 'secret', 'token', TestKeys::encodingAesKey()),
new WechatPaymentConfig('wx_app', 'mch', str_repeat('k', 32), 'serial', TestKeys::privateKey()),
new AlipayPlatformConfig('ali_app', TestKeys::privateKey()),
new AlipayPaymentConfig('ali_pay', TestKeys::privateKey()),
] as $config) {
$this->assertInstanceOf(ConfigInterface::class, $config);
}
}
/**
* 测试构造配置对象时会校验必填字段。
*/
public function testConfigValidateRequiredFieldsOnConstruct(): void
{
$this->expectException(WechatException::class);
$this->expectExceptionMessage('不能为空');
new WechatPlatformConfig('', 'secret');
}
/**
* 测试通过数组构造配置对象时会校验必填字段。
*/
public function testConfigValidateRequiredFieldsFromArray(): void
{
$this->expectException(WechatException::class);
$this->expectExceptionMessage('不能为空');
WechatPaymentConfig::fromArray(['appid' => 'wx_app']);
}
/**
* 测试微信服务平台配置会校验 EncodingAESKey 格式。
*/
public function testWechatServiceConfigRejectsInvalidEncodingAesKey(): void
{
$this->expectException(WechatException::class);
$this->expectExceptionMessage('componentEncodingAesKey');
new WechatServiceConfig('wx_component', 'secret', 'token', 'invalid');
}
/**
* 测试微信消息 Token 只能使用官方允许的英文或数字格式。
*/
public function testWechatConfigRejectsInvalidMessageToken(): void
{
$this->expectException(WechatException::class);
$this->expectExceptionMessage('token');
new WechatPlatformConfig('wx_app', 'secret', 'message_token', TestKeys::encodingAesKey());
}
/**
* 测试微信支付配置会校验 APIv3 Key 与商户私钥格式。
*/
public function testWechatPaymentConfigRejectsInvalidCryptoMaterial(): void
{
$this->expectException(WechatException::class);
$this->expectExceptionMessage('merchantPrivateKey');
new WechatPaymentConfig('wx_app', 'mch', str_repeat('k', 32), 'serial', 'invalid-private-key');
}
/**
* 测试支付宝支付配置 fromArray 返回子类实例。
*/
public function testAlipayPaymentFromArrayReturnsChildClass(): void
{
$config = AlipayPaymentConfig::fromArray([
'appid' => 'ali_pay',
'private_key' => TestKeys::privateKey(),
]);
$this->assertInstanceOf(AlipayPaymentConfig::class, $config);
}
}