pay/tests/Traits/HasWechatEncryptionTest.php
yansongda 9e29c732d0
fix: 未设置微信公钥证书时,加密不生效的问题 (#549)
* fix: 未设置微信公钥证书时,加密不生效的问题

* changelog
2022-01-09 15:53:12 +08:00

86 lines
4.5 KiB
PHP

<?php
namespace Yansongda\Pay\Tests\Traits;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery;
use Yansongda\Pay\Contract\HttpClientInterface;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Tests\Stubs\Traits\HasWechatEncryptionStub;
use Yansongda\Pay\Tests\TestCase;
class HasWechatEncryptionTest extends TestCase
{
/**
* @var HasWechatEncryptionStub
*/
protected $trait;
protected function setUp(): void
{
parent::setUp();
$this->trait = new HasWechatEncryptionStub();
}
public function testLoadSerialNo()
{
// 不传证书
$params = [];
$result = $this->trait->loadSerialNo($params);
self::assertTrue(in_array($result['_serial_no'], ['45F59D4DABF31918AFCEC556D5D2C6E376675D57', 'yansongda']));
// 传证书
$params = ['_serial_no' => 'yansongda',];
$result = $this->trait->loadSerialNo($params);
self::assertEquals('yansongda', $result['_serial_no']);
}
public function testLoadSerialNoWithRequestWechat()
{
$response = new Response(
200,
[],
json_encode([
'data' => [
[
'effective_time' => '2021-07-16T17:51:10+08:00',
'encrypt_certificate' => [
'algorithm' => 'AEAD_AES_256_GCM',
'associated_data' => 'certificate',
'ciphertext' => 'kbbHAUhBwdjYZkHPW149MW/8WNpxQo1Gyp4kVNVjd+zrXnyOFhgZic2U2+tobFAgfdr93zr0JZF3FdbxgkaOAV2NAeCfU8jsUYXSfn7fM8487jXMVXKKEneGiiv1/bDLkz7KFsTfu2y5Rv+igWQ+bvCUQAwoNzjupTXnnDR5hBiofZcFLHL45govyYE2o0qD5SLiJHcFS4pg/IOx8SIqUFNepr3piKXUxKowU8/kNxXyRzL8yp7XnhrzAzclupvjveNwZyiw3TqlLZdR5TbEFLCogWaR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sJY2yHJt2zTZeIdsvM8KK5v0NkH8FiPbWqFG8LaRmUrqhJGLuLLRTcJnt6YVYESxUVTb3pmriUbXfg/ThHF/y0THyrM6bVDNOwNWZOpMYPPNaVmOTX39JdYayWl2HX0n8AsIRmevXzD4N9iDh2HGwie4gh92Qdcogwua++uhkhSsLFuWBpJiaPdxVtzz3E3jHfy+yryfh6msaXc/jmhwqBm/ii3j76lDP5YaRv4+JWZmom72+pmZuKD8qPKrPRxI2/aGiKEqgs25knpLLnbAhWAEYeIzVK1sQkjc5JFss1Std8FdDrHeM6agAB+MWncK1LloXZmiwz/6WmlwSDepnGHqLEciXThAZq6FwunJZTcHY9LamJgIY81c9t/KHlSFqlc/9mW4OZHM4BOZQ5sTj5PWE+OP2Aq9CKdJqoK3OmphBg2ewjrZt5/tSn9jpk6NlVrHD7MsJcKi5a0he4qvNPh1cHqUqWcF4rBFmfPptdHIBV77LXnizJZMUAwf16KsmJpwJg==',
'nonce' => '4196a5b75276',
],
'expire_time' => '2026-07-15T17:51:10+08:00',
'serial_no' => 'test-45F59D4DABF31918AFCEC556D5D2C6E376675D57',
]
]
])
);
$http = Mockery::mock(Client::class);
$http->shouldReceive('sendRequest')->andReturn($response);
Pay::set(HttpClientInterface::class, $http);
$params = ['_config' => 'empty_wechat_public_cert'];
$result = $this->trait->loadSerialNo($params);
self::assertEquals('test-45F59D4DABF31918AFCEC556D5D2C6E376675D57', $result['_serial_no']);
}
public function testGetPublicKey()
{
$serialNo = '45F59D4DABF31918AFCEC556D5D2C6E376675D57';
$result = $this->trait->getPublicKey([], $serialNo);
self::assertIsString($result);
$serialNo = 'non-exist';
self::expectException(InvalidParamsException::class);
self::expectExceptionCode(Exception::WECHAT_SERIAL_NO_NOT_FOUND);
$this->trait->getPublicKey([], $serialNo);
}
}