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); } }