1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zumba\JsonSerializer\Test; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use SuperClosure\Serializer as ClosureSerializer; |
7
|
|
|
use Zumba\JsonSerializer\JsonSerializer; |
8
|
|
|
use Zumba\JsonSerializer\Test\SupportClasses\MyTypeSerializer; |
9
|
|
|
|
10
|
|
|
class JsonSerializerTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Serializer instance |
15
|
|
|
* |
16
|
|
|
* @var JsonSerializer |
17
|
|
|
*/ |
18
|
|
|
protected $serializer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Test case setup |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
$this->serializer = new JsonSerializer(); |
29
|
|
|
$this->serializer->registerEntitySerializer(new MyTypeSerializer()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Test serialization of scalar values |
34
|
|
|
* |
35
|
|
|
* @dataProvider scalarData |
36
|
|
|
* @param mixed $scalar |
37
|
|
|
* @param string $jsoned |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function testSerializeScalar($scalar, $jsoned) |
41
|
|
|
{ |
42
|
|
|
$this->assertSame($jsoned, $this->serializer->serialize($scalar)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Test serialization of float values with locale |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function testSerializeFloatLocalized() |
51
|
|
|
{ |
52
|
|
|
$possibleLocales = ['fr_FR', 'fr_FR.utf8', 'fr', 'fra', 'French']; |
53
|
|
|
$originalLocale = setlocale(LC_NUMERIC, 0); |
54
|
|
|
if (!setlocale(LC_NUMERIC, $possibleLocales)) { |
55
|
|
|
$this->markTestSkipped('Unable to set an i18n locale.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$data = [1.0, 1.1, 0.00000000001, 1.999999999999, 223423.123456789, 1e5, 1e11]; |
59
|
|
|
$expected = '[1.0,1.1,1.0e-11,1.999999999999,223423.12345679,100000.0,100000000000.0]'; |
60
|
|
|
$this->assertSame($expected, $this->serializer->serialize($data)); |
61
|
|
|
|
62
|
|
|
setlocale(LC_NUMERIC, $originalLocale); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test unserialization of scalar values |
67
|
|
|
* |
68
|
|
|
* @dataProvider scalarData |
69
|
|
|
* @param mixed $scalar |
70
|
|
|
* @param string $jsoned |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function testUnserializeScalar($scalar, $jsoned) |
74
|
|
|
{ |
75
|
|
|
$this->assertSame($scalar, $this->serializer->unserialize($jsoned)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* List of scalar data |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function scalarData() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
['testing', '"testing"'], |
87
|
|
|
[123, '123'], |
88
|
|
|
[0, '0'], |
89
|
|
|
[0.0, '0.0'], |
90
|
|
|
[17.0, '17.0'], |
91
|
|
|
[17e1, '170.0'], |
92
|
|
|
[17.2, '17.2'], |
93
|
|
|
[true, 'true'], |
94
|
|
|
[false, 'false'], |
95
|
|
|
[null, 'null'], |
96
|
|
|
// Non UTF8 |
97
|
|
|
['ßåö', '"ßåö"'] |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Test the serialization of resources |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function testSerializeResource() |
107
|
|
|
{ |
108
|
|
|
$this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException'); |
109
|
|
|
$this->serializer->serialize(fopen(__FILE__, 'r')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Test the serialization of closures when not providing closure serializer |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function testSerializeClosureWithoutSerializer() |
118
|
|
|
{ |
119
|
|
|
$this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException'); |
120
|
|
|
$this->serializer->serialize([ |
121
|
|
|
'func' => function () { |
122
|
|
|
echo 'whoops'; |
123
|
|
|
} |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Test serialization of array without objects |
129
|
|
|
* |
130
|
|
|
* @dataProvider arrayNoObjectData |
131
|
|
|
* @param array $array |
132
|
|
|
* @param string $jsoned |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function testSerializeArrayNoObject($array, $jsoned) |
136
|
|
|
{ |
137
|
|
|
$this->assertSame($jsoned, $this->serializer->serialize($array)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Test unserialization of array without objects |
142
|
|
|
* |
143
|
|
|
* @dataProvider arrayNoObjectData |
144
|
|
|
* @param array $array |
145
|
|
|
* @param string $jsoned |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
public function testUnserializeArrayNoObject($array, $jsoned) |
149
|
|
|
{ |
150
|
|
|
$this->assertSame($array, $this->serializer->unserialize($jsoned)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* List of array data |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public function arrayNoObjectData() |
159
|
|
|
{ |
160
|
|
|
return [ |
161
|
|
|
[[1, 2, 3], '[1,2,3]'], |
162
|
|
|
[[1, 'abc', false], '[1,"abc",false]'], |
163
|
|
|
[['a' => 1, 'b' => 2, 'c' => 3], '{"a":1,"b":2,"c":3}'], |
164
|
|
|
[['integer' => 1, 'string' => 'abc', 'bool' => false], '{"integer":1,"string":"abc","bool":false}'], |
165
|
|
|
[[1, ['nested']], '[1,["nested"]]'], |
166
|
|
|
[['integer' => 1, 'array' => ['nested']], '{"integer":1,"array":["nested"]}'], |
167
|
|
|
[['integer' => 1, 'array' => ['nested' => 'object']], '{"integer":1,"array":{"nested":"object"}}'], |
168
|
|
|
[[1.0, 2, 3e1], '[1.0,2,30.0]'], |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Test serialization of objects |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function testSerializeObject() |
178
|
|
|
{ |
179
|
|
|
$obj = new stdClass(); |
180
|
|
|
$this->assertSame('{"@type":"stdClass"}', $this->serializer->serialize($obj)); |
181
|
|
|
|
182
|
|
|
$obj = $empty = new SupportClasses\EmptyClass(); |
183
|
|
|
$this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}', |
184
|
|
|
$this->serializer->serialize($obj)); |
185
|
|
|
|
186
|
|
|
$obj = new SupportClasses\AllVisibilities(); |
187
|
|
|
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"this is public","prot":"protected","priv":"dont tell anyone"}'; |
188
|
|
|
$this->assertSame($expected, $this->serializer->serialize($obj)); |
189
|
|
|
|
190
|
|
|
$obj->pub = 'new value'; |
191
|
|
|
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"new value","prot":"protected","priv":"dont tell anyone"}'; |
192
|
|
|
$this->assertSame($expected, $this->serializer->serialize($obj)); |
193
|
|
|
|
194
|
|
|
$obj->pub = $empty; |
|
|
|
|
195
|
|
|
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}'; |
196
|
|
|
$this->assertSame($expected, $this->serializer->serialize($obj)); |
197
|
|
|
|
198
|
|
|
$array = ['instance' => $empty]; |
199
|
|
|
$expected = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}'; |
200
|
|
|
$this->assertSame($expected, $this->serializer->serialize($array)); |
201
|
|
|
|
202
|
|
|
$obj = new stdClass(); |
203
|
|
|
$obj->total = 10.0; |
204
|
|
|
$obj->discount = 0.0; |
205
|
|
|
$expected = '{"@type":"stdClass","total":10.0,"discount":0.0}'; |
206
|
|
|
$this->assertSame($expected, $this->serializer->serialize($obj)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Test unserialization of objects |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
public function testUnserializeObjects() |
215
|
|
|
{ |
216
|
|
|
$serialized = '{"@type":"stdClass"}'; |
217
|
|
|
$obj = $this->serializer->unserialize($serialized); |
218
|
|
|
$this->assertInstanceOf('stdClass', $obj); |
219
|
|
|
|
220
|
|
|
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}'; |
221
|
|
|
$obj = $this->serializer->unserialize($serialized); |
222
|
|
|
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj); |
223
|
|
|
|
224
|
|
|
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}'; |
225
|
|
|
$obj = $this->serializer->unserialize($serialized); |
226
|
|
|
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\AllVisibilities', $obj); |
227
|
|
|
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj->pub); |
228
|
|
|
$this->assertAttributeSame('protected', 'prot', $obj); |
229
|
|
|
$this->assertAttributeSame('dont tell anyone', 'priv', $obj); |
230
|
|
|
|
231
|
|
|
$serialized = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}'; |
232
|
|
|
$array = $this->serializer->unserialize($serialized); |
233
|
|
|
$this->assertTrue(is_array($array)); |
234
|
|
|
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $array['instance']); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Test serialization of objects using the custom serializers |
240
|
|
|
* |
241
|
|
|
* @return void |
242
|
|
|
*/ |
243
|
|
|
public function testCustomObjectSerializer() |
244
|
|
|
{ |
245
|
|
|
$obj = new SupportClasses\MyType(); |
246
|
|
|
$obj->field1 = 'x'; |
247
|
|
|
$obj->field2 = 'y'; |
248
|
|
|
$this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","field1":"x","field2":"y"}', |
249
|
|
|
$this->serializer->serialize($obj)); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Test serialization of objects using the custom serializers |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
public function testCustomObjectInheritanceSerializer() |
258
|
|
|
{ |
259
|
|
|
$obj = new SupportClasses\MySubType(); |
260
|
|
|
$obj->field1 = 'x'; |
|
|
|
|
261
|
|
|
$obj->field2 = 'y'; |
|
|
|
|
262
|
|
|
$this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MySubType","field1":"x","field2":"y"}', |
263
|
|
|
$this->serializer->serialize($obj)); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Test unserialization of objects using the custom serializers |
268
|
|
|
* |
269
|
|
|
* @return void |
270
|
|
|
*/ |
271
|
|
|
public function testCustomObjectsUnserializer() |
272
|
|
|
{ |
273
|
|
|
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","field1":"x","field2":"y"}'; |
274
|
|
|
$obj = $this->serializer->unserialize($serialized); |
275
|
|
|
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\MyType', $obj); |
276
|
|
|
$this->assertAttributeSame('x', 'field1', $obj); |
277
|
|
|
$this->assertAttributeSame('y', 'field2', $obj); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Test unserialization of objects using the custom serializers |
282
|
|
|
* |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
|
|
public function testCustomObjectsInheritanceUnserializer() |
286
|
|
|
{ |
287
|
|
|
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MySubType","field1":"x","field2":"y"}'; |
288
|
|
|
$obj = $this->serializer->unserialize($serialized); |
289
|
|
|
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\MySubType', $obj); |
290
|
|
|
$this->assertAttributeSame('x', 'field1', $obj); |
291
|
|
|
$this->assertAttributeSame('y', 'field2', $obj); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Test magic serialization methods |
296
|
|
|
* |
297
|
|
|
* @return void |
298
|
|
|
*/ |
299
|
|
|
public function testSerializationMagicMethods() |
300
|
|
|
{ |
301
|
|
|
$obj = new SupportClasses\MagicClass(); |
302
|
|
|
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MagicClass","show":true}'; |
303
|
|
|
$this->assertSame($serialized, $this->serializer->serialize($obj)); |
304
|
|
|
$this->assertFalse($obj->woke); |
305
|
|
|
|
306
|
|
|
$obj = $this->serializer->unserialize($serialized); |
307
|
|
|
$this->assertTrue($obj->woke); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Test serialization of DateTime classes |
312
|
|
|
* |
313
|
|
|
* Some interal classes, such as DateTime, cannot be initialized with |
314
|
|
|
* ReflectionClass::newInstanceWithoutConstructor() |
315
|
|
|
* |
316
|
|
|
* @return void |
317
|
|
|
*/ |
318
|
|
|
public function testSerializationOfDateTime() |
319
|
|
|
{ |
320
|
|
|
$date = new \DateTime('2014-06-15 12:00:00', new \DateTimeZone('UTC')); |
321
|
|
|
$obj = $this->serializer->unserialize($this->serializer->serialize($date)); |
322
|
|
|
$this->assertSame($date->getTimestamp(), $obj->getTimestamp()); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Test the serialization of closures providing closure serializer |
327
|
|
|
* |
328
|
|
|
* @return void |
329
|
|
|
*/ |
330
|
|
|
public function testSerializationOfClosure() |
331
|
|
|
{ |
332
|
|
|
if (!class_exists('SuperClosure\Serializer')) { |
333
|
|
|
$this->markTestSkipped('SuperClosure is not installed.'); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$closureSerializer = new ClosureSerializer(); |
337
|
|
|
$serializer = new JsonSerializer($closureSerializer); |
338
|
|
|
$serialized = $serializer->serialize([ |
339
|
|
|
'func' => function () { |
340
|
|
|
return 'it works'; |
341
|
|
|
}, |
342
|
|
|
'nice' => true |
343
|
|
|
]); |
344
|
|
|
|
345
|
|
|
$unserialized = $serializer->unserialize($serialized); |
346
|
|
|
$this->assertTrue(is_array($unserialized)); |
347
|
|
|
$this->assertTrue($unserialized['nice']); |
348
|
|
|
$this->assertInstanceOf('Closure', $unserialized['func']); |
349
|
|
|
$this->assertSame('it works', $unserialized['func']()); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Test the unserialization of closures without providing closure serializer |
354
|
|
|
* |
355
|
|
|
* @return void |
356
|
|
|
*/ |
357
|
|
|
public function testUnserializeOfClosureWithoutSerializer() |
358
|
|
|
{ |
359
|
|
|
if (!class_exists('SuperClosure\Serializer')) { |
360
|
|
|
$this->markTestSkipped('SuperClosure is not installed.'); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
$closureSerializer = new ClosureSerializer(); |
364
|
|
|
$serializer = new JsonSerializer($closureSerializer); |
365
|
|
|
$serialized = $serializer->serialize([ |
366
|
|
|
'func' => function () { |
367
|
|
|
return 'it works'; |
368
|
|
|
}, |
369
|
|
|
'nice' => true |
370
|
|
|
]); |
371
|
|
|
|
372
|
|
|
$this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException'); |
373
|
|
|
$this->serializer->unserialize($serialized); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Test unserialize of unknown class |
378
|
|
|
* |
379
|
|
|
* @return void |
380
|
|
|
*/ |
381
|
|
|
public function testUnserializeUnknownClass() |
382
|
|
|
{ |
383
|
|
|
$this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException'); |
384
|
|
|
$serialized = '{"@type":"UnknownClass"}'; |
385
|
|
|
$this->serializer->unserialize($serialized); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Test serialization of undeclared properties |
390
|
|
|
* |
391
|
|
|
* @return void |
392
|
|
|
*/ |
393
|
|
|
public function testSerializationUndeclaredProperties() |
394
|
|
|
{ |
395
|
|
|
$obj = new stdClass(); |
396
|
|
|
$obj->param1 = true; |
397
|
|
|
$obj->param2 = 'store me, please'; |
398
|
|
|
$serialized = '{"@type":"stdClass","param1":true,"param2":"store me, please"}'; |
399
|
|
|
$this->assertSame($serialized, $this->serializer->serialize($obj)); |
400
|
|
|
|
401
|
|
|
$obj2 = $this->serializer->unserialize($serialized); |
402
|
|
|
$this->assertInstanceOf('stdClass', $obj2); |
403
|
|
|
$this->assertTrue($obj2->param1); |
404
|
|
|
$this->assertSame('store me, please', $obj2->param2); |
405
|
|
|
|
406
|
|
|
$serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}'; |
407
|
|
|
$obj = $this->serializer->unserialize($serialized); |
408
|
|
|
$this->assertInstanceOf('stdClass', $obj->sub); |
409
|
|
|
$this->assertSame('value', $obj->sub->key); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Test serialize with recursion |
414
|
|
|
* |
415
|
|
|
* @return void |
416
|
|
|
*/ |
417
|
|
|
public function testSerializeRecursion() |
418
|
|
|
{ |
419
|
|
|
$c1 = new stdClass(); |
420
|
|
|
$c1->c2 = new stdClass(); |
421
|
|
|
$c1->c2->c3 = new stdClass(); |
422
|
|
|
$c1->c2->c3->c1 = $c1; |
423
|
|
|
$c1->something = 'ok'; |
424
|
|
|
$c1->c2->c3->ok = true; |
425
|
|
|
|
426
|
|
|
$expected = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}'; |
427
|
|
|
$this->assertSame($expected, $this->serializer->serialize($c1)); |
428
|
|
|
|
429
|
|
|
$c1 = new stdClass(); |
430
|
|
|
$c1->mirror = $c1; |
431
|
|
|
$expected = '{"@type":"stdClass","mirror":{"@type":"@0"}}'; |
432
|
|
|
$this->assertSame($expected, $this->serializer->serialize($c1)); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Test unserialize with recursion |
437
|
|
|
* |
438
|
|
|
* @return void |
439
|
|
|
*/ |
440
|
|
|
public function testUnserializeRecursion() |
441
|
|
|
{ |
442
|
|
|
$serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}'; |
443
|
|
|
$obj = $this->serializer->unserialize($serialized); |
444
|
|
|
$this->assertTrue($obj->c2->c3->ok); |
445
|
|
|
$this->assertSame($obj, $obj->c2->c3->c1); |
446
|
|
|
$this->assertNotSame($obj, $obj->c2); |
447
|
|
|
|
448
|
|
|
$serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"c2":{"@type":"@1"},"c3":{"@type":"@2"}},"c3_copy":{"@type":"@2"}}}'; |
449
|
|
|
$obj = $this->serializer->unserialize($serialized); |
450
|
|
|
$this->assertSame($obj, $obj->c2->c3->c1); |
451
|
|
|
$this->assertSame($obj->c2, $obj->c2->c3->c2); |
452
|
|
|
$this->assertSame($obj->c2->c3, $obj->c2->c3->c3); |
453
|
|
|
$this->assertSame($obj->c2->c3_copy, $obj->c2->c3); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Test unserialize with bad JSON |
458
|
|
|
* |
459
|
|
|
* @return void |
460
|
|
|
*/ |
461
|
|
|
public function testUnserializeBadJSON() |
462
|
|
|
{ |
463
|
|
|
$this->setExpectedException('Zumba\Exception\JsonSerializerException'); |
464
|
|
|
$this->serializer->unserialize('[this is not a valid json!}'); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* The test attempts to serialize an array containing a NAN |
469
|
|
|
*/ |
470
|
|
|
public function testSerializeInvalidData() |
471
|
|
|
{ |
472
|
|
|
if (PHP_VERSION_ID < 50500) { |
473
|
|
|
$this->markTestSkipped('PHP 5.4 raises a warning when encoding NAN, which fails the test.'); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
$this->setExpectedException('Zumba\Exception\JsonSerializerException'); |
477
|
|
|
$this->serializer->serialize([NAN]); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* @return void |
482
|
|
|
*/ |
483
|
|
|
public function testSerializeBinaryStringScalar() |
484
|
|
|
{ |
485
|
|
|
$data = ''; |
486
|
|
|
for ($i = 0; $i <= 255; $i++) { |
487
|
|
|
$data .= chr($i); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
$unserialized = $this->serializer->unserialize($this->serializer->serialize($data)); |
491
|
|
|
$this->assertSame($data, $unserialized); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @return void |
496
|
|
|
*/ |
497
|
|
|
public function testSerializeArrayWithBinaryStringsAsValues() |
498
|
|
|
{ |
499
|
|
|
$data = ''; |
500
|
|
|
for ($i = 0; $i <= 255; $i++) { |
501
|
|
|
$data .= chr($i); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
$data = [$data, "$data 1", "$data 2"]; |
505
|
|
|
$unserialized = $this->serializer->unserialize($this->serializer->serialize($data)); |
506
|
|
|
$this->assertSame($data, $unserialized); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per: |
511
|
|
|
* https://github.com/remicollet/pecl-json-c/issues/7 |
512
|
|
|
* https://github.com/json-c/json-c/issues/108 |
513
|
|
|
* |
514
|
|
|
* @return void |
515
|
|
|
*/ |
516
|
|
|
public function testSerializeArrayWithBinaryStringsAsKeys() |
517
|
|
|
{ |
518
|
|
|
$data = ''; |
519
|
|
|
for ($i = 1; $i <= 255; $i++) { |
520
|
|
|
$data .= chr($i); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
$data = [$data => $data, "$data 1" => 'something']; |
524
|
|
|
$unserialized = $this->serializer->unserialize($this->serializer->serialize($data)); |
525
|
|
|
$this->assertSame($data, $unserialized); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* @return void |
530
|
|
|
*/ |
531
|
|
|
public function testSerializeObjectWithBinaryStrings() |
532
|
|
|
{ |
533
|
|
|
$data = ''; |
534
|
|
|
for ($i = 0; $i <= 255; $i++) { |
535
|
|
|
$data .= chr($i); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
$obj = new \stdClass(); |
539
|
|
|
$obj->string = $data; |
540
|
|
|
$unserialized = $this->serializer->unserialize($this->serializer->serialize($obj)); |
541
|
|
|
$this->assertInstanceOf('stdClass', $obj); |
542
|
|
|
$this->assertSame($obj->string, $unserialized->string); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/* |
546
|
|
|
* Test namespace change (backward compatibility) |
547
|
|
|
* |
548
|
|
|
* @return void |
549
|
|
|
* @deprecated |
550
|
|
|
*/ |
551
|
|
|
public function testNamespaceRename() |
552
|
|
|
{ |
553
|
|
|
$serializer = new \Zumba\Util\JsonSerializer(); |
|
|
|
|
554
|
|
|
|
555
|
|
|
$f = fopen(__FILE__, 'r'); |
556
|
|
|
$this->setExpectedException('Zumba\Exception\JsonSerializerException'); |
557
|
|
|
$this->serializer->serialize($f); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* Test serialization of SplDoubleLinkedList |
562
|
|
|
* |
563
|
|
|
* @return void |
564
|
|
|
*/ |
565
|
|
|
public function testSerializationOfSplDoublyLinkedList() |
566
|
|
|
{ |
567
|
|
|
$list = new \SplDoublyLinkedList(); |
568
|
|
|
$list->push('fizz'); |
569
|
|
|
$list->push(42); |
570
|
|
|
$unserialized = $this->serializer->unserialize($this->serializer->serialize($list)); |
571
|
|
|
$this->assertTrue($list->serialize() === $unserialized->serialize()); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Test serialization of SplDoubleLinkedList |
576
|
|
|
* |
577
|
|
|
* @return void |
578
|
|
|
*/ |
579
|
|
|
public function testEntitySerializerRegistration() |
580
|
|
|
{ |
581
|
|
|
$this->assertTrue($this->serializer->hasEntitySerializer('Zumba\JsonSerializer\Test\SupportClasses\MyType')); |
582
|
|
|
$this->assertFalse($this->serializer->hasEntitySerializer('Zumba\JsonSerializer\Test\SupportClasses\MySubType')); |
583
|
|
|
} |
584
|
|
|
} |
585
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..