1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Tests\Unit; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use stdClass; |
9
|
|
|
use Yiisoft\Yii\Debug as D; |
10
|
|
|
use Yiisoft\Yii\Debug\Dumper; |
11
|
|
|
|
12
|
|
|
final class DumperTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @dataProvider asJsonObjectMapDataProvider |
16
|
|
|
* |
17
|
|
|
* @param string $expectedResult |
18
|
|
|
* |
19
|
|
|
* @group JOM |
20
|
|
|
*/ |
21
|
|
|
public function testAsJsonObjectsMap(mixed $var, $expectedResult): void |
22
|
|
|
{ |
23
|
|
|
$exportResult = Dumper::create($var)->asJsonObjectsMap(); |
24
|
|
|
$this->assertEquals($expectedResult, $exportResult); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function asJsonObjectMapDataProvider(): array |
28
|
|
|
{ |
29
|
|
|
$user = new stdClass(); |
30
|
|
|
$user->id = 1; |
31
|
|
|
$objectId = spl_object_id($user); |
32
|
|
|
|
33
|
|
|
$decoratedUser = clone $user; |
34
|
|
|
$decoratedUser->name = 'Name'; |
35
|
|
|
$decoratedUser->originalUser = $user; |
36
|
|
|
$decoratedObjectId = spl_object_id($decoratedUser); |
37
|
|
|
|
38
|
|
|
return [ |
39
|
|
|
[ |
40
|
|
|
$user, |
41
|
|
|
<<<S |
42
|
|
|
[{"stdClass#{$objectId}":{"public \$id":1}}] |
43
|
|
|
S, |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
$decoratedUser, |
47
|
|
|
<<<S |
48
|
|
|
[{"stdClass#{$decoratedObjectId}":{"public \$id":1,"public \$name":"Name","public \$originalUser":"object@stdClass#{$objectId}"}},{"stdClass#{$objectId}":{"public \$id":1}}] |
49
|
|
|
S, |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @dataProvider jsonDataProvider() |
56
|
|
|
*/ |
57
|
|
|
public function testAsJson($variable, string $result): void |
58
|
|
|
{ |
59
|
|
|
$output = Dumper::create($variable)->asJson(); |
60
|
|
|
$this->assertEqualsWithoutLE($result, $output); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function jsonDataProvider(): array |
64
|
|
|
{ |
65
|
|
|
$objectWithClosureInProperty = new stdClass(); |
66
|
|
|
// @formatter:off |
67
|
|
|
$objectWithClosureInProperty->a = fn () => 1; |
68
|
|
|
// @formatter:on |
69
|
|
|
$objectWithClosureInPropertyId = spl_object_id($objectWithClosureInProperty); |
70
|
|
|
$objectWithClosureInPropertyClosureId = spl_object_id($objectWithClosureInProperty->a); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$emptyObject = new stdClass(); |
73
|
|
|
$emptyObjectId = spl_object_id($emptyObject); |
74
|
|
|
|
75
|
|
|
// @formatter:off |
76
|
|
|
$shortFunctionObject = fn () => 1; |
77
|
|
|
// @formatter:on |
78
|
|
|
$shortFunctionObjectId = spl_object_id($shortFunctionObject); |
|
|
|
|
79
|
|
|
|
80
|
|
|
// @formatter:off |
81
|
|
|
$staticShortFunctionObject = static fn () => 1; |
82
|
|
|
// @formatter:on |
83
|
|
|
$staticShortFunctionObjectId = spl_object_id($staticShortFunctionObject); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// @formatter:off |
86
|
|
|
$functionObject = function () { |
87
|
|
|
return 1; |
88
|
|
|
}; |
89
|
|
|
// @formatter:on |
90
|
|
|
$functionObjectId = spl_object_id($functionObject); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// @formatter:off |
93
|
|
|
$staticFunctionObject = static function () { |
94
|
|
|
return 1; |
95
|
|
|
}; |
96
|
|
|
// @formatter:on |
97
|
|
|
$staticFunctionObjectId = spl_object_id($staticFunctionObject); |
|
|
|
|
98
|
|
|
|
99
|
|
|
// @formatter:off |
100
|
|
|
$closureWithNullCollisionOperatorObject = fn () => $_ENV['var'] ?? null; |
101
|
|
|
// @formatter:on |
102
|
|
|
$closureWithNullCollisionOperatorObjectId = spl_object_id($closureWithNullCollisionOperatorObject); |
|
|
|
|
103
|
|
|
|
104
|
|
|
// @formatter:off |
105
|
|
|
$closureWithUsualClassNameObject = fn (Dumper $date) => new \DateTimeZone(''); |
|
|
|
|
106
|
|
|
// @formatter:on |
107
|
|
|
$closureWithUsualClassNameObjectId = spl_object_id($closureWithUsualClassNameObject); |
|
|
|
|
108
|
|
|
|
109
|
|
|
// @formatter:off |
110
|
|
|
$closureWithAliasedClassNameObject = fn (Dumper $date) => new \DateTimeZone(''); |
|
|
|
|
111
|
|
|
// @formatter:on |
112
|
|
|
$closureWithAliasedClassNameObjectId = spl_object_id($closureWithAliasedClassNameObject); |
|
|
|
|
113
|
|
|
|
114
|
|
|
// @formatter:off |
115
|
|
|
$closureWithAliasedNamespaceObject = fn (D\Dumper $date) => new \DateTimeZone(''); |
|
|
|
|
116
|
|
|
// @formatter:on |
117
|
|
|
$closureWithAliasedNamespaceObjectId = spl_object_id($closureWithAliasedNamespaceObject); |
|
|
|
|
118
|
|
|
|
119
|
|
|
// @formatter:off |
120
|
|
|
$closureInArrayObject = fn () => new \DateTimeZone(''); |
121
|
|
|
// @formatter:on |
122
|
|
|
$closureInArrayObjectId = spl_object_id($closureInArrayObject); |
|
|
|
|
123
|
|
|
|
124
|
|
|
$fileResource = tmpfile(); |
125
|
|
|
$fileResourceUri = preg_quote(stream_get_meta_data($fileResource)['uri'], '/'); |
126
|
|
|
|
127
|
|
|
$closedFileResource = tmpfile(); |
128
|
|
|
fclose($closedFileResource); |
129
|
|
|
$closedFileResourceUri = preg_quote(stream_get_meta_data($fileResource)['uri'], '/'); |
|
|
|
|
130
|
|
|
|
131
|
|
|
$opendirResource = opendir('/tmp'); |
132
|
|
|
//$opendirResourceUri = preg_quote(stream_get_meta_data($fileResource)['uri'], '/'); |
133
|
|
|
|
134
|
|
|
$curlResource = curl_init('https://example.com'); |
135
|
|
|
$curlResourceObjectId = spl_object_id($curlResource); |
|
|
|
|
136
|
|
|
|
137
|
|
|
return [ |
138
|
|
|
'empty object' => [ |
139
|
|
|
$emptyObject, |
140
|
|
|
<<<S |
141
|
|
|
{"stdClass#{$emptyObjectId}":"{stateless object}"} |
142
|
|
|
S, |
143
|
|
|
], |
144
|
|
|
'short function' => [ |
145
|
|
|
$shortFunctionObject, |
146
|
|
|
<<<S |
147
|
|
|
{"Closure#{$shortFunctionObjectId}":"fn () => 1"} |
148
|
|
|
S, |
149
|
|
|
], |
150
|
|
|
'short static function' => [ |
151
|
|
|
$staticShortFunctionObject, |
152
|
|
|
<<<S |
153
|
|
|
{"Closure#{$staticShortFunctionObjectId}":"static fn () => 1"} |
154
|
|
|
S, |
155
|
|
|
], |
156
|
|
|
'function' => [ |
157
|
|
|
$functionObject, |
158
|
|
|
<<<S |
159
|
|
|
{"Closure#{$functionObjectId}":"function () {\\n return 1;\\n }"} |
160
|
|
|
S, |
161
|
|
|
], |
162
|
|
|
'static function' => [ |
163
|
|
|
$staticFunctionObject, |
164
|
|
|
<<<S |
165
|
|
|
{"Closure#{$staticFunctionObjectId}":"static function () {\\n return 1;\\n }"} |
166
|
|
|
S, |
167
|
|
|
], |
168
|
|
|
'string' => [ |
169
|
|
|
'Hello, Yii!', |
170
|
|
|
'"Hello, Yii!"', |
171
|
|
|
], |
172
|
|
|
'empty string' => [ |
173
|
|
|
'', |
174
|
|
|
'""', |
175
|
|
|
], |
176
|
|
|
'null' => [ |
177
|
|
|
null, |
178
|
|
|
'null', |
179
|
|
|
], |
180
|
|
|
'integer' => [ |
181
|
|
|
1, |
182
|
|
|
'1', |
183
|
|
|
], |
184
|
|
|
'integer with separator' => [ |
185
|
|
|
1_23_456, |
|
|
|
|
186
|
|
|
'123456', |
187
|
|
|
], |
188
|
|
|
'boolean' => [ |
189
|
|
|
true, |
190
|
|
|
'true', |
191
|
|
|
], |
192
|
|
|
'fileResource' => [ |
193
|
|
|
fopen('php://input', 'rb'), |
194
|
|
|
'{"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"PHP","stream_type":"Input","mode":"rb","unread_bytes":0,"seekable":true,"uri":"php:\/\/input"}', |
195
|
|
|
], |
196
|
|
|
'empty array' => [ |
197
|
|
|
[], |
198
|
|
|
'[]', |
199
|
|
|
], |
200
|
|
|
'array of 3 elements, automatic keys' => [ |
201
|
|
|
[ |
202
|
|
|
'one', |
203
|
|
|
'two', |
204
|
|
|
'three', |
205
|
|
|
], |
206
|
|
|
'["one","two","three"]', |
207
|
|
|
], |
208
|
|
|
'array of 3 elements, custom keys' => [ |
209
|
|
|
[ |
210
|
|
|
2 => 'one', |
211
|
|
|
'two' => 'two', |
212
|
|
|
0 => 'three', |
213
|
|
|
], |
214
|
|
|
'{"2":"one","two":"two","0":"three"}', |
215
|
|
|
], |
216
|
|
|
'closure in array' => [ |
217
|
|
|
// @formatter:off |
218
|
|
|
[$closureInArrayObject], |
219
|
|
|
// @formatter:on |
220
|
|
|
<<<S |
221
|
|
|
[{"Closure#{$closureInArrayObjectId}":"fn () => new \\\DateTimeZone('')"}] |
222
|
|
|
S, |
223
|
|
|
], |
224
|
|
|
'original class name' => [ |
225
|
|
|
$closureWithUsualClassNameObject, |
226
|
|
|
<<<S |
227
|
|
|
{"Closure#{$closureWithUsualClassNameObjectId}":"fn (\\\Yiisoft\\\Yii\\\Debug\\\Dumper \$date) => new \\\DateTimeZone('')"} |
228
|
|
|
S, |
229
|
|
|
], |
230
|
|
|
'class alias' => [ |
231
|
|
|
$closureWithAliasedClassNameObject, |
232
|
|
|
<<<S |
233
|
|
|
{"Closure#{$closureWithAliasedClassNameObjectId}":"fn (\\\Yiisoft\\\Yii\\\Debug\\\Dumper \$date) => new \\\DateTimeZone('')"} |
234
|
|
|
S, |
235
|
|
|
], |
236
|
|
|
'namespace alias' => [ |
237
|
|
|
$closureWithAliasedNamespaceObject, |
238
|
|
|
<<<S |
239
|
|
|
{"Closure#{$closureWithAliasedNamespaceObjectId}":"fn (\\\Yiisoft\\\Yii\\\Debug\\\Dumper \$date) => new \\\DateTimeZone('')"} |
240
|
|
|
S, |
241
|
|
|
], |
242
|
|
|
'closure with null-collision operator' => [ |
243
|
|
|
$closureWithNullCollisionOperatorObject, |
244
|
|
|
<<<S |
245
|
|
|
{"Closure#{$closureWithNullCollisionOperatorObjectId}":"fn () => \$_ENV['var'] ?? null"} |
246
|
|
|
S, |
247
|
|
|
], |
248
|
|
|
'utf8 supported' => [ |
249
|
|
|
'🤣', |
250
|
|
|
'"🤣"', |
251
|
|
|
], |
252
|
|
|
'closure in property supported' => [ |
253
|
|
|
$objectWithClosureInProperty, |
254
|
|
|
<<<S |
255
|
|
|
{"stdClass#{$objectWithClosureInPropertyId}":{"public \$a":{"Closure#{$objectWithClosureInPropertyClosureId}":"fn () => 1"}}} |
256
|
|
|
S, |
257
|
|
|
], |
258
|
|
|
'binary string' => [ |
259
|
|
|
pack('H*', md5('binary string')), |
260
|
|
|
'"ɍ��^��\u00191\u0017�]�-f�"', |
261
|
|
|
], |
262
|
|
|
'file resource' => [ |
263
|
|
|
$fileResource, |
264
|
|
|
<<<S |
265
|
|
|
{"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"plainfile","stream_type":"STDIO","mode":"r+b","unread_bytes":0,"seekable":true,"uri":"{$fileResourceUri}"} |
266
|
|
|
S, |
267
|
|
|
], |
268
|
|
|
'closed file resource' => [ |
269
|
|
|
$closedFileResource, |
270
|
|
|
'"{closed resource}"', |
271
|
|
|
], |
272
|
|
|
'opendir resource' => [ |
273
|
|
|
$opendirResource, |
274
|
|
|
<<<S |
275
|
|
|
{"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"plainfile","stream_type":"dir","mode":"r","unread_bytes":0,"seekable":true} |
276
|
|
|
S, |
277
|
|
|
], |
278
|
|
|
'curl resource' => [ |
279
|
|
|
$curlResource, |
280
|
|
|
<<<S |
281
|
|
|
{"CurlHandle#{$curlResourceObjectId}":"{stateless object}"} |
282
|
|
|
S, |
283
|
|
|
], |
284
|
|
|
'stdout' => [ |
285
|
|
|
STDOUT, |
286
|
|
|
<<<S |
287
|
|
|
{"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"PHP","stream_type":"STDIO","mode":"wb","unread_bytes":0,"seekable":false,"uri":"php:\/\/stdout"} |
288
|
|
|
S, |
289
|
|
|
], |
290
|
|
|
'stderr' => [ |
291
|
|
|
STDERR, |
292
|
|
|
<<<S |
293
|
|
|
{"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"PHP","stream_type":"STDIO","mode":"wb","unread_bytes":0,"seekable":false,"uri":"php:\/\/stderr"} |
294
|
|
|
S, |
295
|
|
|
], |
296
|
|
|
'stdin' => [ |
297
|
|
|
STDIN, |
298
|
|
|
<<<S |
299
|
|
|
{"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"PHP","stream_type":"STDIO","mode":"rb","unread_bytes":0,"seekable":false,"uri":"php:\/\/stdin"} |
300
|
|
|
S, |
301
|
|
|
], |
302
|
|
|
]; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Asserting two strings equality ignoring line endings. |
307
|
|
|
*/ |
308
|
|
|
protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void |
309
|
|
|
{ |
310
|
|
|
$expected = str_replace(["\r\n", '\r\n'], ["\n", '\n'], $expected); |
311
|
|
|
$actual = str_replace(["\r\n", '\r\n'], ["\n", '\n'], $actual); |
312
|
|
|
$this->assertEquals($expected, $actual, $message); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|