forked from wechatpay-apiv3/wechatpay-php
-
Notifications
You must be signed in to change notification settings - Fork 6
/
DownloadbillTest.php
183 lines (157 loc) · 5.88 KB
/
DownloadbillTest.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
<?php declare(strict_types=1);
namespace WeChatPay\Tests\OpenAPI\V2\Pay;
use const DIRECTORY_SEPARATOR;
use function dirname;
use function substr_count;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\LazyOpenStream;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\ResponseInterface;
use WeChatPay\Builder;
use WeChatPay\Transformer;
use WeChatPay\ClientDecoratorInterface;
use PHPUnit\Framework\TestCase;
class DownloadbillTest extends TestCase
{
private const CSV_DATA_LINE_MAXIMUM_BYTES = 1024;
private const CSV_DATA_FIRST_BYTE = '`';
private const CSV_DATA_SEPERATOR = ',`';
/** @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/pay/downloadbill');
return [$endpoint, $stack];
}
/**
* @return array<string,array{string,array<string,string>,ResponseInterface}>
*/
public function mockRequestsDataProvider(): array
{
$mchid = '1230000109';
$data = [
'return_code' => 'FAIL',
'return_msg' => 'invalid reason',
'error_code' => '20001'
];
$file = dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'bill.ALL.csv';
$stream = new LazyOpenStream($file, 'rb');
$xmlDataStructure = [
'appid' => 'wx8888888888888888',
'mch_id' => $mchid,
'bill_type' => 'ALL',
'bill_date' => '20140603',
];
return [
'return_code=FAIL' => [$mchid, $xmlDataStructure, new Response(200, [], Transformer::toXml($data))],
'CSV stream' => [$mchid, $xmlDataStructure, new Response(200, [], $stream)],
];
}
/**
* @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);
$res = $endpoint->post([
'handler' => $stack,
'xml' => $data,
]);
self::responseAssertion($res);
$this->mock->reset();
$this->mock->append($respondor);
$res = $endpoint->post(['xml' => $data]);
self::responseAssertion($res);
}
/**
* @param ResponseInterface $response
* @param boolean $testFinished
*/
private static function responseAssertion(ResponseInterface $response, bool $testFinished = false): void
{
$stream = $response->getBody();
$stream->tell() && $stream->rewind();
$firstFiveBytes = $stream->read(5);
$stream->rewind();
if ('<xml>' === $firstFiveBytes) {
$txt = (string) $stream;
$array = Transformer::toArray($txt);
static::assertArrayHasKey('return_msg', $array);
static::assertArrayHasKey('return_code', $array);
static::assertArrayHasKey('error_code', $array);
} else {
$line = Utils::readLine($stream, self::CSV_DATA_LINE_MAXIMUM_BYTES);
$headerCommaCount = substr_count($line, ',');
$isRecord = false;
do {
$line = Utils::readLine($stream, self::CSV_DATA_LINE_MAXIMUM_BYTES);
$isRecord = $line[0] === self::CSV_DATA_FIRST_BYTE;
if ($isRecord) {
static::assertEquals($headerCommaCount, substr_count($line, self::CSV_DATA_SEPERATOR));
}
} while(!$stream->eof() && $isRecord);
$summaryCommaCount = substr_count($line, ',');
$line = Utils::readLine($stream, self::CSV_DATA_LINE_MAXIMUM_BYTES);
static::assertTrue($line[0] === self::CSV_DATA_FIRST_BYTE);
static::assertEquals($summaryCommaCount, substr_count($line, self::CSV_DATA_SEPERATOR));
$stream->rewind();
if ($testFinished) {
$stream->close();
static::assertFalse($stream->isSeekable());
}
}
}
/**
* @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);
$endpoint->postAsync([
'xml' => $data,
])->then(static function(ResponseInterface $response) {
self::responseAssertion($response);
})->wait();
$this->mock->reset();
$this->mock->append($respondor);
$endpoint->postAsync([
'handler' => $stack,
'xml' => $data,
])->then(static function(ResponseInterface $response) {
self::responseAssertion($response, true);
})->wait();
}
}