WeChatDeveloper/tests/ClientTest.php
Anyon 27c6d03cbd test: 迁移测试目录并补充协议层用例
- 将 src/Tests 迁移到根目录 tests,并同步 composer autoload-dev 与 phpunit 配置。

- 补充 raw/download/upload、微信支付签名下载、XML 嵌套重复节点和消息加解密异常用例。

- 保持测试命名空间 We\Tests,统一通过 tests/bootstrap.php 引导 Composer 自动加载。
2026-05-08 00:29:23 +08:00

152 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
/**
* SDK 根入口通道工厂测试。
*/
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 根入口通道工厂测试用例。
*/
#[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', 'encoding'));
}
/**
* 测试微信公众平台工厂返回正确客户端。
*/
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',
'component_token',
'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG'
));
$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', str_repeat('a', 64)));
$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', str_repeat('a', 64)));
$this->assertInstanceOf(AlipayPlatformClient::class, $channelClient);
$this->assertNotInstanceOf(WechatServiceClient::class, $channelClient);
}
}