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