-
Notifications
You must be signed in to change notification settings - Fork 101
/
TransformerTest.php
225 lines (207 loc) · 8.74 KB
/
TransformerTest.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
<?php declare(strict_types=1);
namespace WeChatPay\Tests;
use const DIRECTORY_SEPARATOR;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
use function array_map;
use function file_get_contents;
use function json_encode;
use function is_string;
use function error_clear_last;
use function error_get_last;
use function method_exists;
use WeChatPay\Transformer;
use PHPUnit\Framework\TestCase;
class TransformerTest extends TestCase
{
/**
* @return array<string,array{string,string[]}>
*/
public function xmlToArrayDataProvider(): array
{
$baseDir = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR;
return [
'sendredpack.sample.xml' => [
file_get_contents($baseDir . 'sendredpack.sample.xml') ?: '',
[
'sign', 'mch_billno', 'mch_id', 'wxappid', 'send_name', 're_openid', 'total_amount',
'total_num', 'wishing', 'client_ip', 'act_name', 'remark', 'scene_id', 'nonce_str', 'risk_info',
],
],
'paysuccess.notification.sample.xml' => [
file_get_contents($baseDir . 'paysuccess.notification.sample.xml') ?: '',
[
'appid', 'attach', 'bank_type', 'fee_type', 'is_subscribe', 'mch_id', 'nonce_str', 'openid',
'out_trade_no', 'result_code', 'return_code', 'sign', 'time_end', 'total_fee', 'coupon_fee',
'coupon_count', 'coupon_type', 'coupon_id', 'trade_type', 'transaction_id',
],
],
'unifiedorder.sample.xml' => [
file_get_contents($baseDir . 'unifiedorder.sample.xml') ?: '',
[
'appid', 'attach', 'body', 'mch_id', 'detail', 'nonce_str', 'notify_url', 'openid',
'out_trade_no', 'spbill_create_ip', 'total_fee', 'trade_type', 'sign',
],
],
'refund.notification.req_info.sample.xml' => [
file_get_contents($baseDir . 'refund.notification.req_info.sample.xml') ?: '',
[
'out_refund_no', 'out_trade_no',
'refund_account', 'refund_fee', 'refund_id', 'refund_recv_accout', 'refund_request_source', 'refund_status',
'settlement_refund_fee','settlement_total_fee', 'success_time', 'total_fee', 'transaction_id',
],
],
'getpublickey.response.sample.xml' => [
file_get_contents($baseDir . 'getpublickey.response.sample.xml') ?: '',
[
'return_code', 'return_msg', 'result_code', 'mch_id', 'pub_key',
],
],
];
}
/**
* @dataProvider xmlToArrayDataProvider
* @param string $xmlString
* @param string[] $keys
*/
public function testToArray(string $xmlString, array $keys): void
{
/** @var string[] $array */
$array = Transformer::toArray($xmlString);
self::assertIsArray($array);
self::assertNotEmpty($array);
array_map(static function($key) use ($array): void {
static::assertArrayHasKey($key, $array);
static::assertIsString($array[$key]);
static::assertStringNotContainsString('<![CDATA[', $array[$key]);
static::assertStringNotContainsString(']]>', $array[$key]);
}, $keys);
}
/**
* @return array<string,array{string,?string}>
*/
public function xmlToArrayBadPhrasesDataProvider(): array
{
$baseDir = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR;
return [
$f = 'fragment_injection.sample.xml' => [(string)file_get_contents($baseDir . $f), null],
$f = 'invalid.xxe_injection.sample.xml' => [(string)file_get_contents($baseDir . $f), null],
$f = 'invalid.bad_entity.sample.xml' => [(string)file_get_contents($baseDir . $f), '#^Parsing the \$xml failed with the last error#'],
$f = 'invalid.normal_404.sample.html' => [(string)file_get_contents($baseDir . $f), '#^Parsing the \$xml failed with the last error#'],
];
}
/**
* @dataProvider xmlToArrayBadPhrasesDataProvider
* @param string $xmlString
* @param ?string $pattern
*/
public function testToArrayBadPhrases(string $xmlString, ?string $pattern = null): void
{
error_clear_last();
$array = Transformer::toArray($xmlString);
self::assertIsArray($array);
if (is_string($pattern)) {
self::assertEmpty($array);
/** @var array{'message':string,'type':int,'file':string,'line':int} $err */
$err = error_get_last();
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression($pattern, $err['message']);
} else {
self::assertRegExp($pattern, $err['message']);
}
} else {
self::assertNotEmpty($array);
}
}
/**
* @return array<string,array{array<mixed>,bool,bool,string,string}>
*/
public function arrayToXmlDataProvider(): array
{
$jsonModifier = JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE;
return [
'normal 1-depth array with extra default options' => [
[
'appid' => 'wx2421b1c4370ec43b',
'body' => 'dummybot',
'mch_id' => '10000100',
'detail' => json_encode([['goods_detail' => '华为手机', 'url' => 'https://huawei.com']], $jsonModifier) ?: ''
],
true, false, 'xml', 'item',
],
'normal 1-depth array with headless=false and indent=true' => [
[
'appid' => 'wx2421b1c4370ec43b',
'body' => 'dummybot',
'mch_id' => '10000100',
'detail' => json_encode([['goods_detail' => '华为手机', 'url' => 'https://huawei.com']], $jsonModifier) ?: ''
],
false, true, 'xml', 'item',
],
'2-depth array with extra default options' => [
[
'appid' => 'wx2421b1c4370ec43b',
'body' => 'dummybot',
'mch_id' => '10000100',
'detail' => [['goods_detail' => '华为手机', 'url' => 'https://huawei.com']],
],
true, false, 'xml', 'item',
],
'2-depth array with with headless=false, indent=true and root=qqpay' => [
[
'appid' => 'wx2421b1c4370ec43b',
'body' => 'dummybot',
'mch_id' => '10000100',
'detail' => [['goods_detail' => '华为手机', 'url' => 'https://huawei.com']],
],
false, true, 'qqpay', 'item',
],
'transform the Stringable values' => [
[
'appid' => 'wx2421b1c4370ec43b',
'body' => 'dummybot',
'mch_id' => '10000100',
'finished' => true,
'amount' => 100,
'recevier' => new class {
public function __toString(): string {
return json_encode(['type' => 'MERCHANT_ID', 'account' => '190001001']) ?: '';
}
},
],
true, false, 'xml', 'item',
]
];
}
/**
* @dataProvider arrayToXmlDataProvider
* @param array<string,int|string|mixed> $data
* @param bool $headless
* @param bool $indent
* @param string $root
* @param string $item
*/
public function testToXml(array $data, bool $headless, bool $indent, string $root, string $item): void
{
$xml = Transformer::toXml($data, $headless, $indent, $root, $item);
self::assertIsString($xml);
self::assertNotEmpty($xml);
if ($headless) {
self::assertStringNotContainsString('<?xml version="1.0" encoding="UTF-8"?>', $xml);
} else {
self::assertStringStartsWith('<?xml version="1.0" encoding="UTF-8"?>', $xml);
}
if ($indent) {
self::assertGreaterThanOrEqual(preg_match('#\n#', $xml), 2);
} else {
self::assertLessThanOrEqual(preg_match('#\n#', $xml), 0);
}
$tag = preg_quote($root);
$pattern = '#(?:<\?xml[^>]+\?>\n?)?<' . $tag . '>.*</' . $tag . '>\n?#smu';
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression($pattern, $xml);
} else {
self::assertRegExp($pattern, $xml);
}
}
}