-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathTransfersTest.php
151 lines (132 loc) · 4.91 KB
/
TransfersTest.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
<?php declare(strict_types=1);
namespace WeChatPay\Tests\OpenAPI\V2\Mmpaymkttransfers\Promotion;
use WeChatPay\Builder;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Promise\RejectionException;
use PHPUnit\Framework\TestCase;
use WeChatPay\Transformer;
use WeChatPay\ClientDecoratorInterface;
class TransfersTest extends TestCase
{
/** @var MockHandler $mock */
private $mock;
private function guzzleMockStack(): HandlerStack
{
$this->mock = new MockHandler();
return HandlerStack::create($this->mock);
}
/**
* @param string $mchid
* @return array{\WeChatPay\BuilderChainable,HandlerStack}
*/
private function prepareEnvironment(string $mchid): array
{
$instance = Builder::factory([
'mchid' => $mchid,
'serial' => 'nop',
'privateKey' => 'any',
'certs' => ['any' => null],
'secret' => '',
'handler' => $this->guzzleMockStack(),
]);
/** @var HandlerStack $stack */
$stack = $instance->getDriver()->select(ClientDecoratorInterface::XML_BASED)->getConfig('handler');
$stack = clone $stack;
$stack->remove('transform_response');
$endpoint = $instance->chain('v2/mmpaymkttransfers/promotion/transfers');
return [$endpoint, $stack];
}
/**
* @return array<string,array{string,array<string,string>,ResponseInterface}>
*/
public function mockRequestsDataProvider(): array
{
return [
'return_code=SUCCESS' => [
$mchid = '1230000109',
[
'mchid' => $mchid,
'mch_appid' => 'wx8888888888888888',
'device_info' => '013467007045764',
'partner_trade_no' => '10000098201411111234567890',
'openid' => 'oxTWIuGaIt6gTKsQRLau2M0yL16E',
'check_name' => 'FORCE_CHECK',
're_user_name' => '王小王',
'amount' => '10099',
'desc' => '理赔',
'spbill_create_ip' => '192.168.0.1',
],
new Response(200, [], Transformer::toXml([
'mchid' => $mchid,
'return_code' => 'SUCCESS',
'result_code' => 'SUCCESS',
]))
],
];
}
/**
* @dataProvider mockRequestsDataProvider
* @param string $mchid
* @param array<string,string> $data
* @param ResponseInterface $respondor
*/
public function testPost(string $mchid, array $data, ResponseInterface $respondor): void
{
[$endpoint, $stack] = $this->prepareEnvironment($mchid);
$this->mock->reset();
$this->mock->append($respondor);
// yes, start with `@` to prevent the internal `E_USER_DEPRECATED`
$res = @$endpoint->post(['xml' => $data, 'handler' => $stack]);
self::responseAssertion($res);
$this->mock->reset();
$this->mock->append($respondor);
try {
// yes, start with `@` to prevent the internal `E_USER_DEPRECATED`
@$endpoint->post(['xml' => $data]);
} catch (RejectionException $e) {
/** @var ResponseInterface $res */
$res = $e->getReason();
self::responseAssertion($res);
}
}
/**
* @param ResponseInterface $response
*/
private static function responseAssertion(ResponseInterface $response): void
{
$txt = (string) $response->getBody();
$array = Transformer::toArray($txt);
static::assertArrayHasKey('mchid', $array);
static::assertArrayHasKey('return_code', $array);
static::assertArrayHasKey('result_code', $array);
}
/**
* @dataProvider mockRequestsDataProvider
* @param string $mchid
* @param array<string,string> $data
* @param ResponseInterface $respondor
*/
public function testPostAsync(string $mchid, array $data, ResponseInterface $respondor): void
{
[$endpoint, $stack] = $this->prepareEnvironment($mchid);
$this->mock->reset();
$this->mock->append($respondor);
// yes, start with `@` to prevent the internal `E_USER_DEPRECATED`
@$endpoint->postAsync([
'xml' => $data, 'handler' => $stack
])->then(static function(ResponseInterface $res) {
self::responseAssertion($res);
})->wait();
$this->mock->reset();
$this->mock->append($respondor);
// yes, start with `@` to prevent the internal `E_USER_DEPRECATED`
@$endpoint->postAsync([
'xml' => $data
])->otherwise(static function($res) {
self::responseAssertion($res);
})->wait();
}
}