forked from wechatpay-apiv3/wechatpay-php
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ClientDecoratorTest.php
611 lines (545 loc) · 24.3 KB
/
ClientDecoratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<?php declare(strict_types=1);
namespace WeChatPay\Tests;
use function class_implements;
use function class_uses;
use function is_array;
use function strval;
use function abs;
use function json_encode;
use function explode;
use function array_reduce;
use function array_map;
use function trim;
use function openssl_pkey_get_private;
use function openssl_pkey_get_public;
use function sprintf;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
use ReflectionClass;
use ReflectionMethod;
use WeChatPay\Formatter;
use WeChatPay\Crypto\Rsa;
use WeChatPay\ClientDecorator;
use WeChatPay\ClientDecoratorInterface;
use WeChatPay\Exception\InvalidArgumentException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\RequestInterface;
use PHPUnit\Framework\TestCase;
class ClientDecoratorTest extends TestCase
{
private const FIXTURES = __DIR__ . '/fixtures/mock.%s.%s';
/** @var int - The maximum clock offset in second */
private const MAXIMUM_CLOCK_OFFSET = 300;
public function testImplementsClientDecoratorInterface(): void
{
$map = class_implements(ClientDecorator::class);
self::assertIsArray($map);
self::assertNotEmpty($map);
self::assertArrayHasKey(ClientDecoratorInterface::class, $map);
if (method_exists($this, 'assertContainsEquals')) {
$this->assertContainsEquals(ClientDecoratorInterface::class, $map);
}
}
public function testClassUsesTraits(): void
{
$traits = class_uses(ClientDecorator::class);
self::assertIsArray($traits);
self::assertNotEmpty($traits);
self::assertContains(\WeChatPay\ClientJsonTrait::class, $traits);
self::assertContains(\WeChatPay\ClientXmlTrait::class, $traits);
}
public function testClassConstants(): void
{
self::assertIsString(ClientDecorator::VERSION);
self::assertIsString(ClientDecorator::XML_BASED);
self::assertIsString(ClientDecorator::JSON_BASED);
}
public function testByReflectionClass(): void
{
$ref = new ReflectionClass(ClientDecorator::class);
self::assertInstanceOf(ReflectionClass::class, $ref);
$methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
self::assertIsArray($methods);
self::assertTrue($ref->isFinal());
self::assertTrue($ref->hasMethod('select'));
self::assertTrue($ref->hasMethod('request'));
self::assertTrue($ref->hasMethod('requestAsync'));
$traits = $ref->getTraitNames();
self::assertIsArray($traits);
self::assertContains(\WeChatPay\ClientJsonTrait::class, $traits);
self::assertContains(\WeChatPay\ClientXmlTrait::class, $traits);
}
/**
* @return array<string,array{array<string,mixed>,string}>
*/
public function constructorExceptionsProvider(): array
{
return [
'none args passed' => [
[],
'#`mchid` is required#',
],
'only `mchid` passed' => [
['mchid' => '1230000109',],
'#`serial` is required#',
],
'`mchid` and `serial` passed' => [
['mchid' => '1230000109', 'serial' => 'MCH123SERIAL',],
'#`privateKey` is required#',
],
'`mchid`, `serial` and `priviateKey` in' => [
['mchid' => '1230000109', 'serial' => 'MCH123SERIAL', 'privateKey' => '------ BEGIN PRIVATE ------',],
'#`certs` is required#',
],
'`mchid`, `serial`, `priviateKey` and bad `certs` in' => [
['mchid' => '1230000109', 'serial' => 'MCH123SERIAL', 'privateKey' => '------ BEGIN PRIVATE ------', 'certs' => ['MCH123SERIAL' => '']],
'#the merchant\'s certificate serial number\(MCH123SERIAL\) which is not allowed here#',
],
];
}
/**
* @dataProvider constructorExceptionsProvider
* @param array<string,mixed> $config
* @param string $pattern
*/
public function testConstructorExceptions(array $config, string $pattern): void
{
$this->expectException(InvalidArgumentException::class);
// for PHPUnit8+
if (method_exists($this, 'expectExceptionMessageMatches')) {
$this->expectExceptionMessageMatches($pattern);
}
// for PHPUnit7
elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
$this->expectExceptionMessageRegExp($pattern);
}
new ClientDecorator($config);
}
/** @var MockHandler $mock */
private $mock;
private function guzzleMockStack(): HandlerStack
{
$this->mock = new MockHandler();
return HandlerStack::create($this->mock);
}
/**
* @return array<string,array{array<string,mixed>}>
*/
public function constructorSuccessProvider(): array
{
return [
'default' => [
[
'mchid' => '1230000109', 'serial' => 'MCH123SERIAL', 'privateKey' => '------ BEGIN PRIVATE ------', 'certs' => ['PLAT123SERIAL' => ''],
],
],
'with base_uri' => [
[
'mchid' => '1230000109', 'serial' => 'MCH123SERIAL', 'privateKey' => '------ BEGIN PRIVATE ------', 'certs' => ['PLAT123SERIAL' => ''],
'base_uri' => 'https://api.mch.weixin.qq.com/hk/',
],
],
'with base_uri and handler' => [
[
'mchid' => '1230000109', 'serial' => 'MCH123SERIAL', 'privateKey' => '------ BEGIN PRIVATE ------', 'certs' => ['PLAT123SERIAL' => ''],
'base_uri' => 'https://apius.mch.weixin.qq.com/v3/sandbox/',
'handler' => $this->guzzleMockStack(),
],
],
];
}
/**
* @dataProvider constructorSuccessProvider
*
* @param array<string,mixed> $config
*/
public function testSelect(array $config): void
{
$instance = new ClientDecorator($config);
self::assertInstanceOf(ClientDecoratorInterface::class, $instance);
$client = $instance->select(ClientDecoratorInterface::JSON_BASED);
self::assertInstanceOf(\GuzzleHttp\Client::class, $client);
/** @var array<string,mixed> $settings */
$settings = $client->getConfig(); // TODO: refactor while Guzzle8 dropped this API
self::assertIsArray($settings);
self::assertArrayHasKey('handler', $settings);
/** @var HandlerStack $stack */
['handler' => $stack] = $settings;
self::assertInstanceOf(HandlerStack::class, $stack);
self::assertArrayHasKey('base_uri', $settings);
/** @var \GuzzleHttp\Psr7\Uri $baseUri */
['base_uri' => $baseUri] = $settings;
self::assertEquals($config['base_uri'] ?? 'https://api.mch.weixin.qq.com/', (string)$baseUri);
self::assertArrayHasKey('headers', $settings);
/** @var array<string,mixed> $headers */
['headers' => $headers] = $settings;
self::assertIsArray($headers);
self::assertArrayHasKey('Content-Type', $headers);
self::assertArrayHasKey('Accept', $headers);
/**
* @var string $accept
* @var string $contentType
*/
['Accept' => $accept, 'Content-Type' => $contentType] = $headers;
self::assertStringStartsWith('application/json, text/plain, application/x-gzip', $accept);
self::assertEquals('application/json; charset=utf-8', $contentType);
$stackDebugInfo = strval($stack);
self::assertStringContainsString('verifier', $stackDebugInfo);
self::assertStringContainsString('signer', $stackDebugInfo);
self::assertStringNotContainsString('transform_request', $stackDebugInfo);
self::assertStringNotContainsString('transform_response', $stackDebugInfo);
$client = $instance->select(ClientDecoratorInterface::XML_BASED);
self::assertInstanceOf(\GuzzleHttp\Client::class, $client);
/** @var array<string,mixed> $settings */
$settings = $client->getConfig(); // TODO: refactor while Guzzle8 dropped this API
self::assertIsArray($settings);
self::assertArrayHasKey('handler', $settings);
/** @var HandlerStack $stack */
['handler' => $stack] = $settings;
self::assertInstanceOf(HandlerStack::class, $stack);
/** @var \GuzzleHttp\Psr7\Uri $baseUri */
['base_uri' => $baseUri] = $settings;
self::assertEquals($config['base_uri'] ?? 'https://api.mch.weixin.qq.com/', (string)$baseUri);
self::assertArrayHasKey('headers', $settings);
/** @var array<string,mixed> $headers */
['headers' => $headers] = $settings;
self::assertIsArray($headers);
self::assertArrayHasKey('Content-Type', $headers);
self::assertArrayHasKey('Accept', $headers);
/**
* @var string $accept
* @var string $contentType
*/
['Accept' => $accept, 'Content-Type' => $contentType] = $headers;
self::assertEquals('text/xml, text/plain, application/x-gzip', $accept);
self::assertEquals('text/xml; charset=utf-8', $contentType);
$stackDebugInfo = strval($stack);
self::assertStringNotContainsString('verifier', $stackDebugInfo);
self::assertStringNotContainsString('signer', $stackDebugInfo);
self::assertStringContainsString('transform_request', $stackDebugInfo);
self::assertStringContainsString('transform_response', $stackDebugInfo);
}
/**
* @return array{string,\OpenSSLAsymmetricKey|resource|string|mixed,\OpenSSLAsymmetricKey|\OpenSSLCertificate|resource|string|mixed,string,string}
*/
private function mockConfiguration(): array
{
$privateKey = openssl_pkey_get_private('file://' . sprintf(self::FIXTURES, 'pkcs8', 'key'));
$publicKey = openssl_pkey_get_public('file://' . sprintf(self::FIXTURES, 'sha256', 'crt'));
if (false === $privateKey || false === $publicKey) {
throw new \Exception('Loading the pkey failed.');
}
return ['1230000109', $privateKey, $publicKey, Formatter::nonce(40), Formatter::nonce(40)];
}
/**
* @return array<string,array{string,resource|mixed,string|resource|mixed,string,string,object|mixed,string,string,string}>
*/
public function withMockHandlerProvider(): array
{
[$mchid, $privateKey, $publicKey, $mchSerial, $platSerial] = $this->mockConfiguration();
return [
'HTTP 400 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(400), ClientException::class, 'GET', '/',
],
'HTTP 401 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(401), ClientException::class, 'POST', '/next/aipay',
],
'HTTP 403 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(403), ClientException::class, 'PUT', 'app/done',
],
'HTTP 404 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(404), ClientException::class, 'DELETE', 'secapi/micro',
],
'HTTP 429 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(429), ClientException::class, 'PATCH', 'sandboxnew',
],
'HTTP 500 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(500), ServerException::class, 'POST', 'v3/sandbox',
],
'HTTP 502 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(502), ServerException::class, 'PATCH', 'facepay',
],
'HTTP 503 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(503), ServerException::class, 'PUT', 'frog-over-miniprogram/pay',
],
'HTTP 200 STATUS without mandatory headers' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(200), RequestException::class, 'GET', 'v3/pay/transcations',
],
'HTTP 200 STATUS with bad clock offset(in the server late)' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(200, [
'Wechatpay-Nonce' => Formatter::nonce(),
'Wechatpay-Serial' => $platSerial,
'Wechatpay-Timestamp' => strval(Formatter::timestamp() - 60 * 6),
'Wechatpay-Signature' => Formatter::nonce(200),
]), RequestException::class, 'DELETE', 'v3/pay/transcations',
],
'HTTP 200 STATUS with bad clock offset(in the server ahead)' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(200, [
'Wechatpay-Nonce' => Formatter::nonce(),
'Wechatpay-Serial' => $platSerial,
'Wechatpay-Timestamp' => strval(Formatter::timestamp() + 60 * 6),
'Wechatpay-Signature' => Formatter::nonce(200),
]), RequestException::class, 'PUT', 'v3/pay/transcations',
],
'HTTP 200 STATUS with unreachable platform certificate serial number' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(200, [
'Wechatpay-Nonce' => Formatter::nonce(),
'Wechatpay-Serial' => Formatter::nonce(40),
'Wechatpay-Timestamp' => strval(Formatter::timestamp()),
'Wechatpay-Signature' => Formatter::nonce(200),
]), RequestException::class, 'PATCH', 'v3/pay/transcations',
],
'HTTP 200 STATUS with bad digest signature' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
new Response(200, [
'Wechatpay-Nonce' => Formatter::nonce(),
'Wechatpay-Serial' => $platSerial,
'Wechatpay-Timestamp' => strval(Formatter::timestamp()),
'Wechatpay-Signature' => Formatter::nonce(200),
]), RequestException::class, 'POST', 'v3/pay/transcations',
],
];
}
/**
* @dataProvider withMockHandlerProvider
*
* @param string $mchid
* @param resource|mixed $privateKey
* @param string|resource|mixed $publicKey
* @param string $mchSerial
* @param string $platSerial
* @param ResponseInterface $response
* @param class-string<\Exception> $expectedGuzzleException
* @param string $method
* @param string $uri
*/
public function testRequestsWithMockHandler(
string $mchid, $privateKey, $publicKey, string $mchSerial, string $platSerial,
ResponseInterface $response, string $expectedGuzzleException, string $method, string $uri): void
{
$instance = new ClientDecorator([
'mchid' => $mchid,
'serial' => $mchSerial,
'privateKey' => $privateKey,
'certs' => [$platSerial => $publicKey],
'handler' => $this->guzzleMockStack(),
]);
$this->mock->reset();
$this->mock->append($response);
$this->expectException($expectedGuzzleException);
$instance->request($method, $uri);
}
/**
* @dataProvider withMockHandlerProvider
*
* @param string $mchid
* @param resource|mixed $privateKey
* @param string|resource|mixed $publicKey
* @param string $mchSerial
* @param string $platSerial
* @param ResponseInterface $response
* @param class-string<\Exception> $expectedGuzzleException
* @param string $method
* @param string $uri
*/
public function testAsyncRequestsWithMockHandler(
string $mchid, $privateKey, $publicKey, string $mchSerial, string $platSerial,
ResponseInterface $response, string $expectedGuzzleException, string $method, string $uri): void
{
$instance = new ClientDecorator([
'mchid' => $mchid,
'serial' => $mchSerial,
'privateKey' => $privateKey,
'certs' => [$platSerial => $publicKey],
'handler' => $this->guzzleMockStack(),
]);
$mock = $this->mock;
$mock->reset();
$mock->append($response);
$instance->requestAsync($method, $uri)->otherwise(static function($actual) use ($expectedGuzzleException, $mock, $method, $uri) {
/** @var \GuzzleHttp\Psr7\Request $req */
$req = $mock->getLastRequest();
static::assertInstanceOf(\GuzzleHttp\Psr7\Request::class, $req);
static::assertEquals($method, $req->getMethod());
if (static::stringStartsWith('/')->evaluate($uri, '', true)) {
static::assertEquals($uri, $req->getRequestTarget());
}
static::assertStringEndsWith($uri, $req->getRequestTarget());
static::assertInstanceOf($expectedGuzzleException, $actual);
})->wait();
}
/**
* @return array<string,string>
*/
private static function parseAuthorization(string $value): array
{
[$type, $credentials] = explode(' ', $value, 2);
return array_reduce(array_map(static function($item) {
[$key, $value] = explode('=', $item, 2);
return [$key => trim($value, '"')];
}, explode(',', $credentials)), static function(array $carry, array $item) {
return $carry + $item;
}, ['type' => $type]);
}
/**
* @param RequestInterface $request
* @param string $mchid
* @param string $mchSerial
* @param \OpenSSLAsymmetricKey|\OpenSSLCertificate|resource|string|mixed $publicKey
*/
private static function verification(RequestInterface $request, string $mchid, string $mchSerial, $publicKey): void
{
static::assertTrue($request->hasHeader('Authorization'));
[$authorization] = $request->getHeader('Authorization');
static::assertIsString($authorization);
$rules = self::parseAuthorization($authorization);
static::assertIsArray($rules);
static::assertNotEmpty($rules);
static::assertArrayHasKey('type', $rules);
static::assertArrayHasKey('mchid', $rules);
static::assertArrayHasKey('nonce_str', $rules);
static::assertArrayHasKey('timestamp', $rules);
static::assertArrayHasKey('signature', $rules);
static::assertArrayHasKey('serial_no', $rules);
['type' => $type] = $rules;
static::assertEquals('WECHATPAY2-SHA256-RSA2048', $type);
['mchid' => $mchId, 'nonce_str' => $nonceStr, 'timestamp' => $timestamp, 'serial_no' => $serialNo, 'signature' => $signature] = $rules;
static::assertEquals($mchId, $mchid);
static::assertEquals($serialNo, $mchSerial);
static::assertFalse(abs(Formatter::timestamp() - intval($timestamp)) > self::MAXIMUM_CLOCK_OFFSET);
static::assertTrue(Rsa::verify(Formatter::request(
$request->getMethod(),
$request->getRequestTarget(),
$timestamp,
$nonceStr,
(string) $request->getBody()
), $signature, $publicKey));
}
/**
* @param int $status
* @param string $serial
* @param string $body
* @param \OpenSSLAsymmetricKey|resource|string|mixed $privateKey
*/
private static function pickResponse(int $status, string $serial, string $body, $privateKey): ResponseInterface
{
return new Response($status, [
'Wechatpay-Nonce' => $nonce = Formatter::nonce(),
'Wechatpay-Serial' => $serial,
'Wechatpay-Timestamp' => $timestamp = strval(Formatter::timestamp()),
'Wechatpay-Signature' => Rsa::sign(Formatter::response($timestamp, $nonce, $body), $privateKey),
], $body);
}
/**
* @return array<string,array{string,resource|mixed,string|resource|mixed,string,string,string,string,string,callable}>
*/
public function normalRequestsDataProvider(): array
{
[$mchid, $privateKey, $publicKey, $mchSerial, $platSerial] = $this->mockConfiguration();
return [
'HTTP 204 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
'PATCH', 'v3/pay/transcations',
$body = '',
static function(RequestInterface $request) use ($privateKey, $platSerial, $body, $mchid, $mchSerial, $publicKey): ResponseInterface {
self::verification($request, $mchid, $mchSerial, $publicKey);
return self::pickResponse(204, $platSerial, $body, $privateKey);
},
],
'HTTP 202 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
'PUT', 'v3/pay/transcations',
$body = '',
static function(RequestInterface $request) use ($privateKey, $platSerial, $body, $mchid, $mchSerial, $publicKey): ResponseInterface {
self::verification($request, $mchid, $mchSerial, $publicKey);
return self::pickResponse(202, $platSerial, $body, $privateKey);
},
],
'HTTP 200 STATUS' => [
$mchid, $privateKey, $publicKey, $mchSerial, $platSerial,
'POST', 'v3/pay/transcations',
$body = (string)json_encode(['code_url' => 'weixin://wxpay/bizpayurl?pr=qnu8GBtzz'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
static function(RequestInterface $request) use ($privateKey, $platSerial, $body, $mchid, $mchSerial, $publicKey): ResponseInterface {
self::verification($request, $mchid, $mchSerial, $publicKey);
return self::pickResponse(200, $platSerial, $body, $privateKey);
},
],
];
}
/**
* @dataProvider normalRequestsDataProvider
*
* @param string $mchid
* @param resource|mixed $privateKey
* @param string|resource|mixed $publicKey
* @param string $mchSerial
* @param string $platSerial
* @param string $expected
* @param string $method
* @param string $uri
* @param callable $respondor
*/
public function testRequest(
string $mchid, $privateKey, $publicKey, string $mchSerial, string $platSerial,
string $method, string $uri, string $expected, callable $respondor): void
{
$instance = new ClientDecorator([
'mchid' => $mchid,
'serial' => $mchSerial,
'privateKey' => $privateKey,
'certs' => [$platSerial => $publicKey],
'handler' => $this->guzzleMockStack(),
]);
$this->mock->reset();
$this->mock->append($respondor);
self::assertEquals($expected, (string) $instance->request($method, $uri)->getBody());
}
/**
* @dataProvider normalRequestsDataProvider
*
* @param string $mchid
* @param resource|mixed $privateKey
* @param string|resource|mixed $publicKey
* @param string $mchSerial
* @param string $platSerial
* @param string $expected
* @param string $method
* @param string $uri
* @param callable $respondor
*/
public function testRequestAsync(
string $mchid, $privateKey, $publicKey, string $mchSerial, string $platSerial,
string $method, string $uri, string $expected, callable $respondor): void
{
$instance = new ClientDecorator([
'mchid' => $mchid,
'serial' => $mchSerial,
'privateKey' => $privateKey,
'certs' => [$platSerial => $publicKey],
'handler' => $this->guzzleMockStack(),
]);
$this->mock->reset();
$this->mock->append($respondor);
$instance->requestAsync($method, $uri)->then(static function(ResponseInterface $response) use($expected) {
static::assertEquals($expected, (string) $response->getBody());
})->wait();
}
}