This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Zumba\JsonSerializer\Test; |
||
4 | |||
5 | use Zumba\JsonSerializer\JsonSerializer; |
||
6 | use stdClass; |
||
7 | use SuperClosure\Serializer as ClosureSerializer; |
||
8 | |||
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 | $customObjectSerializerMap['Zumba\\JsonSerializer\\Test\\SupportClasses\\MyType'] = new \Zumba\JsonSerializer\Test\SupportClasses\MyTypeSerializer(); |
||
0 ignored issues
–
show
|
|||
28 | $this->serializer = new JsonSerializer(null, $customObjectSerializerMap); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Test serialization of scalar values |
||
33 | * |
||
34 | * @dataProvider scalarData |
||
35 | * @param mixed $scalar |
||
36 | * @param string $jsoned |
||
37 | * @return void |
||
38 | */ |
||
39 | public function testSerializeScalar($scalar, $jsoned) |
||
40 | { |
||
41 | $this->assertSame($jsoned, $this->serializer->serialize($scalar)); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Test serialization of float values with locale |
||
46 | * |
||
47 | * @return void |
||
48 | */ |
||
49 | public function testSerializeFloatLocalized() |
||
50 | { |
||
51 | $possibleLocales = ['fr_FR', 'fr_FR.utf8', 'fr', 'fra', 'French']; |
||
52 | $originalLocale = setlocale(LC_NUMERIC, 0); |
||
53 | if (!setlocale(LC_NUMERIC, $possibleLocales)) { |
||
54 | $this->markTestSkipped('Unable to set an i18n locale.'); |
||
55 | } |
||
56 | |||
57 | $data = [1.0, 1.1, 0.00000000001, 1.999999999999, 223423.123456789, 1e5, 1e11]; |
||
58 | $expected = version_compare(PHP_VERSION, '7.1', '>=') ? |
||
59 | '[1.0,1.1,1.0e-11,1.999999999999,223423.123456789,100000.0,100000000000.0]' : |
||
60 | '[1.0,1.1,1.0e-11,1.999999999999,223423.12345679,100000.0,100000000000.0]'; |
||
61 | $this->assertSame($expected, $this->serializer->serialize($data)); |
||
62 | |||
63 | setlocale(LC_NUMERIC, $originalLocale); |
||
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 array( |
||
86 | array('testing', '"testing"'), |
||
87 | array(123, '123'), |
||
88 | array(0, '0'), |
||
89 | array(0.0, '0.0'), |
||
90 | array(17.0, '17.0'), |
||
91 | array(17e1, '170.0'), |
||
92 | array(17.2, '17.2'), |
||
93 | array(true, 'true'), |
||
94 | array(false, 'false'), |
||
95 | array(null, 'null'), |
||
96 | // Non UTF8 |
||
97 | array('ßåö', '"ßåö"') |
||
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'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
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'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
120 | $this->serializer->serialize( |
||
121 | array('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 array( |
||
161 | array(array(1, 2, 3), '[1,2,3]'), |
||
162 | array(array(1, 'abc', false), '[1,"abc",false]'), |
||
163 | array(array('a' => 1, 'b' => 2, 'c' => 3), '{"a":1,"b":2,"c":3}'), |
||
164 | array(array('integer' => 1, 'string' => 'abc', 'bool' => false), '{"integer":1,"string":"abc","bool":false}'), |
||
165 | array(array(1, array('nested')), '[1,["nested"]]'), |
||
166 | array(array('integer' => 1, 'array' => array('nested')), '{"integer":1,"array":["nested"]}'), |
||
167 | array(array('integer' => 1, 'array' => array('nested' => 'object')), '{"integer":1,"array":{"nested":"object"}}'), |
||
168 | array(array(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"}', $this->serializer->serialize($obj)); |
||
184 | |||
185 | $obj = new SupportClasses\AllVisibilities(); |
||
186 | $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"this is public","prot":"protected","priv":"dont tell anyone"}'; |
||
187 | $this->assertSame($expected, $this->serializer->serialize($obj)); |
||
188 | |||
189 | $obj->pub = 'new value'; |
||
190 | $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"new value","prot":"protected","priv":"dont tell anyone"}'; |
||
191 | $this->assertSame($expected, $this->serializer->serialize($obj)); |
||
192 | |||
193 | $obj->pub = $empty; |
||
0 ignored issues
–
show
It seems like
$empty of type object<Zumba\JsonSeriali...portClasses\EmptyClass> is incompatible with the declared type string of property $pub .
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..
Loading history...
|
|||
194 | $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}'; |
||
195 | $this->assertSame($expected, $this->serializer->serialize($obj)); |
||
196 | |||
197 | $array = array('instance' => $empty); |
||
198 | $expected = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}'; |
||
199 | $this->assertSame($expected, $this->serializer->serialize($array)); |
||
200 | |||
201 | $obj = new stdClass(); |
||
202 | $obj->total = 10.0; |
||
203 | $obj->discount = 0.0; |
||
204 | $expected = '{"@type":"stdClass","total":10.0,"discount":0.0}'; |
||
205 | $this->assertSame($expected, $this->serializer->serialize($obj)); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Test unserialization of objects |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | public function testUnserializeObjects() |
||
214 | { |
||
215 | $serialized = '{"@type":"stdClass"}'; |
||
216 | $obj = $this->serializer->unserialize($serialized); |
||
217 | $this->assertInstanceOf('stdClass', $obj); |
||
218 | |||
219 | $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}'; |
||
220 | $obj = $this->serializer->unserialize($serialized); |
||
221 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj); |
||
222 | |||
223 | $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}'; |
||
224 | $obj = $this->serializer->unserialize($serialized); |
||
225 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\AllVisibilities', $obj); |
||
226 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj->pub); |
||
227 | $this->assertAttributeSame('protected', 'prot', $obj); |
||
228 | $this->assertAttributeSame('dont tell anyone', 'priv', $obj); |
||
229 | |||
230 | $serialized = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}'; |
||
231 | $array = $this->serializer->unserialize($serialized); |
||
232 | $this->assertTrue(is_array($array)); |
||
233 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $array['instance']); |
||
234 | } |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Test serialization of objects using the custom serializers |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | public function testCustomObjectSerializer() |
||
243 | { |
||
244 | $obj = new SupportClasses\MyType(); |
||
245 | $obj->field1 = 'x'; |
||
246 | $obj->field2 = 'y'; |
||
247 | $this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","fields":"x y"}', $this->serializer->serialize($obj)); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Test unserialization of objects using the custom serializers |
||
252 | * |
||
253 | * @return void |
||
254 | */ |
||
255 | public function testCustomObjectsUnserializer() |
||
256 | { |
||
257 | $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","fields":"x y"}'; |
||
258 | $obj = $this->serializer->unserialize($serialized); |
||
259 | $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\MyType', $obj); |
||
260 | $this->assertAttributeSame('x', 'field1', $obj); |
||
261 | $this->assertAttributeSame('y', 'field2', $obj); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Test magic serialization methods |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function testSerializationMagicMethods() |
||
270 | { |
||
271 | $obj = new SupportClasses\MagicClass(); |
||
272 | $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MagicClass","show":true}'; |
||
273 | $this->assertSame($serialized, $this->serializer->serialize($obj)); |
||
274 | $this->assertFalse($obj->woke); |
||
275 | |||
276 | $obj = $this->serializer->unserialize($serialized); |
||
277 | $this->assertTrue($obj->woke); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Test serialization of DateTime classes |
||
282 | * |
||
283 | * Some interal classes, such as DateTime, cannot be initialized with |
||
284 | * ReflectionClass::newInstanceWithoutConstructor() |
||
285 | * |
||
286 | * @return void |
||
287 | */ |
||
288 | public function testSerializationOfDateTime() |
||
289 | { |
||
290 | $date = new \DateTime('2014-06-15 12:00:00', new \DateTimeZone('UTC')); |
||
291 | $obj = $this->serializer->unserialize($this->serializer->serialize($date)); |
||
292 | $this->assertSame($date->getTimestamp(), $obj->getTimestamp()); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Test the serialization of closures providing closure serializer |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | public function testSerializationOfClosure() |
||
301 | { |
||
302 | if (!class_exists('SuperClosure\Serializer')) { |
||
303 | $this->markTestSkipped('SuperClosure is not installed.'); |
||
304 | } |
||
305 | |||
306 | $closureSerializer = new ClosureSerializer(); |
||
307 | $serializer = new JsonSerializer($closureSerializer); |
||
308 | $serialized = $serializer->serialize( |
||
309 | array( |
||
310 | 'func' => function () { |
||
311 | return 'it works'; |
||
312 | }, |
||
313 | 'nice' => true |
||
314 | ) |
||
315 | ); |
||
316 | |||
317 | $unserialized = $serializer->unserialize($serialized); |
||
318 | $this->assertTrue(is_array($unserialized)); |
||
319 | $this->assertTrue($unserialized['nice']); |
||
320 | $this->assertInstanceOf('Closure', $unserialized['func']); |
||
321 | $this->assertSame('it works', $unserialized['func']()); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Test the unserialization of closures without providing closure serializer |
||
326 | * |
||
327 | * @return void |
||
328 | */ |
||
329 | public function testUnserializeOfClosureWithoutSerializer() |
||
330 | { |
||
331 | if (!class_exists('SuperClosure\Serializer')) { |
||
332 | $this->markTestSkipped('SuperClosure is not installed.'); |
||
333 | } |
||
334 | |||
335 | $closureSerializer = new ClosureSerializer(); |
||
336 | $serializer = new JsonSerializer($closureSerializer); |
||
337 | $serialized = $serializer->serialize( |
||
338 | array( |
||
339 | 'func' => function () { |
||
340 | return 'it works'; |
||
341 | }, |
||
342 | 'nice' => true |
||
343 | ) |
||
344 | ); |
||
345 | |||
346 | $this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
347 | $this->serializer->unserialize($serialized); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Test unserialize of unknown class |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | public function testUnserializeUnknownClass() |
||
356 | { |
||
357 | $this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
358 | $serialized = '{"@type":"UnknownClass"}'; |
||
359 | $this->serializer->unserialize($serialized); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Test serialization of undeclared properties |
||
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | public function testSerializationUndeclaredProperties() |
||
368 | { |
||
369 | $obj = new stdClass(); |
||
370 | $obj->param1 = true; |
||
371 | $obj->param2 = 'store me, please'; |
||
372 | $serialized = '{"@type":"stdClass","param1":true,"param2":"store me, please"}'; |
||
373 | $this->assertSame($serialized, $this->serializer->serialize($obj)); |
||
374 | |||
375 | $obj2 = $this->serializer->unserialize($serialized); |
||
376 | $this->assertInstanceOf('stdClass', $obj2); |
||
377 | $this->assertTrue($obj2->param1); |
||
378 | $this->assertSame('store me, please', $obj2->param2); |
||
379 | |||
380 | $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}'; |
||
381 | $obj = $this->serializer->unserialize($serialized); |
||
382 | $this->assertInstanceOf('stdClass', $obj->sub); |
||
383 | $this->assertSame('value', $obj->sub->key); |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Test undeclared properties setter (valid) |
||
388 | * |
||
389 | * @return void |
||
390 | */ |
||
391 | public function testSetUnserializeUndeclaredPropertyModeValid() |
||
392 | { |
||
393 | $value = $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_SET); |
||
394 | $this->assertSame($value, $this->serializer); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Test undeclared properties setter (invalid) |
||
399 | * |
||
400 | * @return void |
||
401 | */ |
||
402 | public function testSetUnserializeUndeclaredPropertyModeInvalid() |
||
403 | { |
||
404 | $this->setExpectedException('InvalidArgumentException'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
405 | $value = $this->serializer->setUnserializeUndeclaredPropertyMode('bad value'); |
||
0 ignored issues
–
show
$value is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Test unserialization of undeclared properties in SET mode |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | public function testUnserializeUndeclaredPropertySet() |
||
414 | { |
||
415 | $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_SET); |
||
416 | |||
417 | $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}'; |
||
418 | $obj = $this->serializer->unserialize($serialized); |
||
419 | $this->assertInstanceOf('stdClass', $obj->sub); |
||
420 | $this->assertSame('value', $obj->sub->key); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Test unserialization of undeclared properties in IGNORE mode |
||
425 | * |
||
426 | * @return void |
||
427 | */ |
||
428 | public function testUnserializeUndeclaredPropertyIgnore() |
||
429 | { |
||
430 | $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_IGNORE); |
||
431 | |||
432 | $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}'; |
||
433 | $obj = $this->serializer->unserialize($serialized); |
||
434 | $this->assertFalse(isset($obj->sub)); |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * Test unserialization of undeclared properties in EXCEPTION mode |
||
439 | * |
||
440 | * @return void |
||
441 | */ |
||
442 | public function testUnserializeUndeclaredPropertyException() |
||
443 | { |
||
444 | $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_EXCEPTION); |
||
445 | |||
446 | $this->setExpectedException('Zumba\Exception\JsonSerializerException'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
447 | $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}'; |
||
448 | $obj = $this->serializer->unserialize($serialized); |
||
0 ignored issues
–
show
$obj is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Test serialize with recursion |
||
453 | * |
||
454 | * @return void |
||
455 | */ |
||
456 | public function testSerializeRecursion() |
||
457 | { |
||
458 | $c1 = new stdClass(); |
||
459 | $c1->c2 = new stdClass(); |
||
460 | $c1->c2->c3 = new stdClass(); |
||
461 | $c1->c2->c3->c1 = $c1; |
||
462 | $c1->something = 'ok'; |
||
463 | $c1->c2->c3->ok = true; |
||
464 | |||
465 | $expected = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}'; |
||
466 | $this->assertSame($expected, $this->serializer->serialize($c1)); |
||
467 | |||
468 | $c1 = new stdClass(); |
||
469 | $c1->mirror = $c1; |
||
470 | $expected = '{"@type":"stdClass","mirror":{"@type":"@0"}}'; |
||
471 | $this->assertSame($expected, $this->serializer->serialize($c1)); |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Test unserialize with recursion |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | public function testUnserializeRecursion() |
||
480 | { |
||
481 | $serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}'; |
||
482 | $obj = $this->serializer->unserialize($serialized); |
||
483 | $this->assertTrue($obj->c2->c3->ok); |
||
484 | $this->assertSame($obj, $obj->c2->c3->c1); |
||
485 | $this->assertNotSame($obj, $obj->c2); |
||
486 | |||
487 | $serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"c2":{"@type":"@1"},"c3":{"@type":"@2"}},"c3_copy":{"@type":"@2"}}}'; |
||
488 | $obj = $this->serializer->unserialize($serialized); |
||
489 | $this->assertSame($obj, $obj->c2->c3->c1); |
||
490 | $this->assertSame($obj->c2, $obj->c2->c3->c2); |
||
491 | $this->assertSame($obj->c2->c3, $obj->c2->c3->c3); |
||
492 | $this->assertSame($obj->c2->c3_copy, $obj->c2->c3); |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Test unserialize with bad JSON |
||
497 | * |
||
498 | * @return void |
||
499 | */ |
||
500 | public function testUnserializeBadJSON() |
||
501 | { |
||
502 | $this->setExpectedException('Zumba\Exception\JsonSerializerException'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
503 | $this->serializer->unserialize('[this is not a valid json!}'); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * The test attempts to serialize an array containing a NAN |
||
508 | */ |
||
509 | public function testSerializeInvalidData() |
||
510 | { |
||
511 | if (PHP_VERSION_ID < 50500) { |
||
512 | $this->markTestSkipped('PHP 5.4 raises a warning when encoding NAN, which fails the test.'); |
||
513 | } |
||
514 | |||
515 | $this->setExpectedException('Zumba\Exception\JsonSerializerException'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
516 | $this->serializer->serialize(array(NAN)); |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * |
||
521 | * @return void |
||
522 | */ |
||
523 | public function testSerializeBinaryStringScalar() |
||
524 | { |
||
525 | $data = ''; |
||
526 | for ($i = 0; $i <= 255; $i++) { |
||
527 | $data .= chr($i); |
||
528 | } |
||
529 | |||
530 | $unserialized = $this->serializer->unserialize($this->serializer->serialize($data)); |
||
531 | $this->assertSame($data, $unserialized); |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * |
||
536 | * @return void |
||
537 | */ |
||
538 | public function testSerializeArrayWithBinaryStringsAsValues() |
||
539 | { |
||
540 | $data = ''; |
||
541 | for ($i = 0; $i <= 255; $i++) { |
||
542 | $data .= chr($i); |
||
543 | } |
||
544 | |||
545 | $data = [$data, "$data 1", "$data 2"]; |
||
546 | $unserialized = $this->serializer->unserialize($this->serializer->serialize($data)); |
||
547 | $this->assertSame($data, $unserialized); |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per: |
||
552 | * https://github.com/remicollet/pecl-json-c/issues/7 |
||
553 | * https://github.com/json-c/json-c/issues/108 |
||
554 | * |
||
555 | * @return void |
||
556 | */ |
||
557 | public function testSerializeArrayWithBinaryStringsAsKeys() |
||
558 | { |
||
559 | $data = ''; |
||
560 | for ($i = 1; $i <= 255; $i++) { |
||
561 | $data .= chr($i); |
||
562 | } |
||
563 | |||
564 | $data = [$data => $data, "$data 1" => 'something']; |
||
565 | $unserialized = $this->serializer->unserialize($this->serializer->serialize($data)); |
||
566 | $this->assertSame($data, $unserialized); |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * |
||
571 | * @return void |
||
572 | */ |
||
573 | public function testSerializeObjectWithBinaryStrings() |
||
574 | { |
||
575 | $data = ''; |
||
576 | for ($i = 0; $i <= 255; $i++) { |
||
577 | $data .= chr($i); |
||
578 | } |
||
579 | |||
580 | $obj = new \stdClass(); |
||
581 | $obj->string = $data; |
||
582 | $unserialized = $this->serializer->unserialize($this->serializer->serialize($obj)); |
||
583 | $this->assertInstanceOf('stdClass', $obj); |
||
584 | $this->assertSame($obj->string, $unserialized->string); |
||
585 | } |
||
586 | |||
587 | /* |
||
588 | * Test namespace change (backward compatibility) |
||
589 | * |
||
590 | * @return void |
||
591 | * @deprecated |
||
592 | */ |
||
593 | public function testNamespaceRename() |
||
594 | { |
||
595 | $serializer = new \Zumba\Util\JsonSerializer(); |
||
0 ignored issues
–
show
$serializer is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
The class
Zumba\Util\JsonSerializer has been deprecated with message: Will be removed on the 3.0.0. Use Zumba\JsonSerializer\JsonSerializer instead
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.
Loading history...
|
|||
596 | |||
597 | $f = fopen(__FILE__, 'r'); |
||
598 | $this->setExpectedException('Zumba\Exception\JsonSerializerException'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
599 | $this->serializer->serialize($f); |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * Test serialization of SplDoubleLinkedList |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | public function testSerializationOfSplDoublyLinkedList() |
||
608 | { |
||
609 | $list = new \SplDoublyLinkedList(); |
||
610 | $list->push('fizz'); |
||
611 | $list->push(42); |
||
612 | $unserialized = $this->serializer->unserialize($this->serializer->serialize($list)); |
||
613 | $this->assertTrue($list->serialize() === $unserialized->serialize()); |
||
614 | } |
||
615 | } |
||
616 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.