1 | <?php |
||
9 | class JsonSerializerTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Serializer instance |
||
14 | * |
||
15 | * @var JsonSerializer |
||
16 | */ |
||
17 | protected $serializer; |
||
18 | |||
19 | /** |
||
20 | * Test case setup |
||
21 | * |
||
22 | * @return void |
||
23 | */ |
||
24 | public function setUp() |
||
25 | { |
||
26 | parent::setUp(); |
||
27 | $this->serializer = new JsonSerializer(); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Test serialization of scalar values |
||
32 | * |
||
33 | * @dataProvider scalarData |
||
34 | * @param mixed $scalar |
||
35 | * @param string $jsoned |
||
36 | * @return void |
||
37 | */ |
||
38 | public function testSerializeScalar($scalar, $jsoned) |
||
39 | { |
||
40 | $this->assertSame($jsoned, $this->serializer->serialize($scalar)); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Test serialization of float values with locale |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public function testSerializeFloatLocalized() |
||
49 | { |
||
50 | $possibleLocales = ['fr_FR', 'fr_FR.utf8', 'fr', 'fra', 'French']; |
||
51 | $originalLocale = setlocale(LC_NUMERIC, 0); |
||
52 | if (!setlocale(LC_NUMERIC, $possibleLocales)) { |
||
53 | $this->markTestSkipped('Unable to set an i18n locale.'); |
||
54 | } |
||
55 | |||
56 | $data = [1.0, 1.1, 0.00000000001, 1.999999999999, 223423.123456789, 1e5, 1e11]; |
||
57 | $expected = '[1.0,1.1,1.0e-11,1.999999999999,223423.12345679,100000.0,100000000000.0]'; |
||
58 | $this->assertSame($expected, $this->serializer->serialize($data)); |
||
59 | |||
60 | setlocale(LC_NUMERIC, $originalLocale); |
||
61 | } |
||
62 | /** |
||
63 | * Test unserialization of scalar values |
||
64 | * |
||
65 | * @dataProvider scalarData |
||
66 | * @param mixed $scalar |
||
67 | * @param string $jsoned |
||
68 | * @return void |
||
69 | */ |
||
70 | public function testUnserializeScalar($scalar, $jsoned) |
||
71 | { |
||
72 | $this->assertSame($scalar, $this->serializer->unserialize($jsoned)); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * List of scalar data |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public function scalarData() |
||
81 | { |
||
82 | return array( |
||
83 | array('testing', '"testing"'), |
||
84 | array(123, '123'), |
||
85 | array(0, '0'), |
||
86 | array(0.0, '0.0'), |
||
87 | array(17.0, '17.0'), |
||
88 | array(17e1, '170.0'), |
||
89 | array(17.2, '17.2'), |
||
90 | array(true, 'true'), |
||
91 | array(false, 'false'), |
||
92 | array(null, 'null'), |
||
93 | // Non UTF8 |
||
94 | array('ßåö', '"ßåö"') |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Test the serialization of resources |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | public function testSerializeResource() |
||
104 | { |
||
105 | $this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException'); |
||
106 | $this->serializer->serialize(fopen(__FILE__, 'r')); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Test the serialization of closures when not providing closure serializer |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | public function testSerializeClosureWithoutSerializer() |
||
115 | { |
||
116 | $this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException'); |
||
117 | $this->serializer->serialize(array('func' => function () { |
||
118 | echo 'whoops'; |
||
119 | })); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Test serialization of array without objects |
||
124 | * |
||
125 | * @dataProvider arrayNoObjectData |
||
126 | * @param array $array |
||
127 | * @param string $jsoned |
||
128 | * @return void |
||
129 | */ |
||
130 | public function testSerializeArrayNoObject($array, $jsoned) |
||
131 | { |
||
132 | $this->assertSame($jsoned, $this->serializer->serialize($array)); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Test unserialization of array without objects |
||
137 | * |
||
138 | * @dataProvider arrayNoObjectData |
||
139 | * @param array $array |
||
140 | * @param string $jsoned |
||
141 | * @return void |
||
142 | */ |
||
143 | public function testUnserializeArrayNoObject($array, $jsoned) |
||
144 | { |
||
145 | $this->assertSame($array, $this->serializer->unserialize($jsoned)); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * List of array data |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | public function arrayNoObjectData() |
||
154 | { |
||
155 | return array( |
||
156 | array(array(1, 2, 3), '[1,2,3]'), |
||
157 | array(array(1, 'abc', false), '[1,"abc",false]'), |
||
158 | array(array('a' => 1, 'b' => 2, 'c' => 3), '{"a":1,"b":2,"c":3}'), |
||
159 | array(array('integer' => 1, 'string' => 'abc', 'bool' => false), '{"integer":1,"string":"abc","bool":false}'), |
||
160 | array(array(1, array('nested')), '[1,["nested"]]'), |
||
161 | array(array('integer' => 1, 'array' => array('nested')), '{"integer":1,"array":["nested"]}'), |
||
162 | array(array('integer' => 1, 'array' => array('nested' => 'object')), '{"integer":1,"array":{"nested":"object"}}'), |
||
163 | array(array(1.0, 2, 3e1), '[1.0,2,30.0]'), |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Test serialization of objects |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | public function testSerializeObject() |
||
202 | |||
203 | /** |
||
204 | * Test unserialization of objects |
||
205 | * |
||
206 | * @return void |
||
207 | */ |
||
208 | public function testUnserializeObjects() |
||
230 | |||
231 | /** |
||
232 | * Test magic serialization methods |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | public function testSerializationMagicMethods() |
||
237 | { |
||
238 | $obj = new SupportClasses\MagicClass(); |
||
239 | $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MagicClass","show":true}'; |
||
240 | $this->assertSame($serialized, $this->serializer->serialize($obj)); |
||
246 | |||
247 | /** |
||
248 | * Test serialization of DateTime classes |
||
249 | * |
||
250 | * Some interal classes, such as DateTime, cannot be initialized with |
||
251 | * ReflectionClass::newInstanceWithoutConstructor() |
||
252 | * |
||
253 | * @return void |
||
254 | */ |
||
255 | public function testSerializationOfDateTime() |
||
261 | |||
262 | /** |
||
263 | * Test the serialization of closures providing closure serializer |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | public function testSerializationOfClosure() |
||
288 | |||
289 | /** |
||
290 | * Test the unserialization of closures without providing closure serializer |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | public function testUnserializeOfClosureWithoutSerializer() |
||
312 | |||
313 | /** |
||
314 | * Test unserialize of unknown class |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | public function testUnserializeUnknownClass() |
||
324 | |||
325 | /** |
||
326 | * Test serialization of undeclared properties |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | public function testSerializationUndeclaredProperties() |
||
348 | |||
349 | /** |
||
350 | * Test serialize with recursion |
||
351 | * |
||
352 | * @return void |
||
353 | */ |
||
354 | public function testSerializeRecursion() |
||
371 | |||
372 | /** |
||
373 | * Test unserialize with recursion |
||
374 | * |
||
375 | * @return void |
||
376 | */ |
||
377 | public function testUnserializeRecursion() |
||
392 | |||
393 | /** |
||
394 | * Test unserialize with bad JSON |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | public function testUnserializeBadJSON() |
||
403 | |||
404 | /** |
||
405 | * The test attempts to serialize an array containing a NAN |
||
406 | */ |
||
407 | public function testSerializeInvalidData() |
||
416 | |||
417 | /** |
||
418 | * @return void |
||
419 | */ |
||
420 | public function testSerializeBinaryStringScalar() |
||
430 | |||
431 | /** |
||
432 | * @return void |
||
433 | */ |
||
434 | public function testSerializeArrayWithBinaryStringsAsValues() |
||
445 | |||
446 | /** |
||
447 | * Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per: |
||
448 | * https://github.com/remicollet/pecl-json-c/issues/7 |
||
449 | * https://github.com/json-c/json-c/issues/108 |
||
450 | * |
||
451 | * @return void |
||
452 | */ |
||
453 | public function testSerializeArrayWithBinaryStringsAsKeys() |
||
464 | |||
465 | /** |
||
466 | * @return void |
||
467 | */ |
||
468 | public function testSerializeObjectWithBinaryStrings() |
||
481 | |||
482 | /* |
||
483 | * Test namespace change (backward compatibility) |
||
484 | * |
||
485 | * @return void |
||
486 | * @deprecated |
||
487 | */ |
||
488 | public function testNamespaceRename() |
||
496 | } |
||
497 |
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..