WeChatDeveloper/tests/ClientTest.php
Anyon 80af84bcb9 refactor(wechat): 迁移并重命名微信协议 Trait
- 将 InteractsProtocol 移动到 src/Contract/Trait,并更名为 WechatInteractsProtocol。

- 更新公众平台、小程序、第三方平台客户端的 Trait 引用和通用调用入口。

- 保留 access_token 缓存、raw/download/upload、消息加解密伪路径等协议层能力。

- 补充客户端工厂与 token 缓存相关测试夹具,确保迁移后调用路径保持兼容。
2026-05-08 11:32:46 +08:00

155 lines
5.4 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\Client;
use We\Config\AlipayPlatformConfig;
use We\Config\WechatPlatformConfig;
use We\Config\WechatServiceConfig;
use We\Exception\WechatException;
use We\Platform\Alipay\PlatformClient as AlipayPlatformClient;
use We\Platform\Wechat\PlatformClient as WechatPlatformClient;
use We\Platform\Wechat\ServiceClient as WechatServiceClient;
/**
* SDK 根入口通道工厂测试用例。
* @internal
*/
#[CoversClass(Client::class)]
final class ClientTest extends TestCase
{
/**
* 测试根客户端缓存前缀为空时抛出异常。
*/
public function testConstructorThrowsWhenCacheKeyPrefixEmpty(): void
{
$this->expectException(WechatException::class);
$this->expectExceptionMessage('cacheKeyPrefix');
new Client(cacheKeyPrefix: ' ');
}
/**
* 测试默认缓存目录位于系统临时目录下。
*/
public function testDefaultCacheStoreDirectoryUnderSysTemp(): void
{
$dir = Client::defaultCacheStoreDirectory();
$this->assertStringStartsWith(rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR . '/'), $dir);
$this->assertStringEndsWith(Client::DEFAULT_CACHE_STORE_DIR_NAME, $dir);
}
/**
* 测试不支持的通道标识会抛出异常。
*/
public function testGetThrowsWhenChannelUnsupported(): void
{
$client = new Client();
$this->expectException(WechatException::class);
$this->expectExceptionMessage('不支持的通道标识');
$client->get('unknown.channel', new WechatPlatformConfig('wx_x', 'sec'));
}
/**
* 测试通道配置类型不匹配会抛出异常。
*/
public function testGetThrowsWhenConfigMismatch(): void
{
$client = new Client();
$this->expectException(WechatException::class);
$this->expectExceptionMessage('WechatPlatformConfig');
$client->get('wechat.platform', new WechatServiceConfig('app', 'sec', 'token', TestKeys::encodingAesKey()));
}
/**
* 测试微信公众平台工厂返回正确客户端。
*/
public function testWechatPlatformFactoryReturnsTypedClient(): void
{
$client = new Client();
$wechat = $client->wechatPlatform(new WechatPlatformConfig('wx_appid', 'app_secret'));
$this->assertInstanceOf(WechatPlatformClient::class, $wechat);
}
/**
* 测试微信公众平台可生成 open.weixin.qq.com 网页授权地址。
*/
public function testWechatPlatformBuildsOpenAuthorizeUrl(): void
{
$client = new Client();
$wechat = $client->wechatPlatform(new WechatPlatformConfig('wx_appid', 'app_secret'));
$result = $wechat->get('connect/oauth2/authorize', [
'redirect_uri' => 'https://example.com/wechat/callback',
'scope' => 'snsapi_userinfo',
'state' => 'S1',
]);
$this->assertArrayHasKey('url', $result);
$this->assertStringStartsWith('https://open.weixin.qq.com/connect/oauth2/authorize?', (string)$result['url']);
$this->assertStringContainsString('appid=wx_appid', (string)$result['url']);
$this->assertStringContainsString('scope=snsapi_userinfo', (string)$result['url']);
$this->assertStringEndsWith('#wechat_redirect', (string)$result['url']);
}
/**
* 测试微信服务平台工厂返回正确客户端,并保留授权地址生成能力。
*/
public function testWechatServiceFactoryReturnsTypedClientAndBuildsAuthorizationUrl(): void
{
$client = new Client();
$service = $client->wechatService(new WechatServiceConfig(
'wx_component',
'component_secret',
'componentToken123',
TestKeys::encodingAesKey()
));
$url = $service->authorizationUrl('preauthcode', 'https://example.com/callback', 3, 'STATE_TEST');
$this->assertInstanceOf(WechatServiceClient::class, $service);
$this->assertStringContainsString('componentloginpage', $url);
$this->assertStringContainsString('pre_auth_code=preauthcode', $url);
$this->assertStringContainsString('state=STATE_TEST', $url);
}
/**
* 测试支付宝授权调用返回跳转地址。
*/
public function testAlipayPlatformCallReturnsAuthorizationUrl(): void
{
$client = new Client();
$alipay = $client->alipayPlatform(new AlipayPlatformConfig('202605010001', TestKeys::privateKey()));
$result = $alipay->get('auth', [
'redirect_uri' => 'https://example.com/alipay/callback',
'scope' => 'auth_user',
'state' => 'S2',
]);
$this->assertArrayHasKey('url', $result);
$this->assertStringContainsString('state=S2', (string)$result['url']);
}
/**
* 测试按通道字符串创建指定客户端。
*/
public function testGetCanReturnSpecificChannelClient(): void
{
$client = new Client();
$channelClient = $client->get('alipay.platform', new AlipayPlatformConfig('202605010001', TestKeys::privateKey()));
$this->assertInstanceOf(AlipayPlatformClient::class, $channelClient);
$this->assertNotInstanceOf(WechatServiceClient::class, $channelClient);
}
}