GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#36)
by
unknown
02:36
created

testSerializationUndeclaredProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
Coding Style Comprehensibility introduced by
$customObjectSerializerMap was never initialized. Although not strictly required by PHP, it is generally a good practice to add $customObjectSerializerMap = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
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
Deprecated Code introduced by
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
Deprecated Code introduced by
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
Documentation Bug introduced by
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
        // var_dump($obj);
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","fields":"x y"}', $this->serializer->serialize($obj));
249
    }
250
251
    /**
252
     * Test unserialization of objects using the custom serializers
253
     *
254
     * @return void
255
     */
256
    public function testCustomObjectsUnserializer()
257
    {
258
        $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","fields":"x y"}';
259
        $obj = $this->serializer->unserialize($serialized);
260
        $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\MyType', $obj);
261
        $this->assertAttributeSame('x', 'field1', $obj);
262
        $this->assertAttributeSame('y', 'field2', $obj);
263
    }
264
265
    /**
266
     * Test magic serialization methods
267
     *
268
     * @return void
269
     */
270
    public function testSerializationMagicMethods()
271
    {
272
        $obj = new SupportClasses\MagicClass();
273
        $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MagicClass","show":true}';
274
        $this->assertSame($serialized, $this->serializer->serialize($obj));
275
        $this->assertFalse($obj->woke);
276
277
        $obj = $this->serializer->unserialize($serialized);
278
        $this->assertTrue($obj->woke);
279
    }
280
281
    /**
282
     * Test serialization of DateTime classes
283
     *
284
     * Some interal classes, such as DateTime, cannot be initialized with
285
     * ReflectionClass::newInstanceWithoutConstructor()
286
     *
287
     * @return void
288
     */
289
    public function testSerializationOfDateTime()
290
    {
291
        $date = new \DateTime('2014-06-15 12:00:00', new \DateTimeZone('UTC'));
292
        $obj = $this->serializer->unserialize($this->serializer->serialize($date));
293
        $this->assertSame($date->getTimestamp(), $obj->getTimestamp());
294
    }
295
296
    /**
297
     * Test the serialization of closures providing closure serializer
298
     *
299
     * @return void
300
     */
301
    public function testSerializationOfClosure()
302
    {
303
        if (!class_exists('SuperClosure\Serializer')) {
304
            $this->markTestSkipped('SuperClosure is not installed.');
305
        }
306
307
        $closureSerializer = new ClosureSerializer();
308
        $serializer = new JsonSerializer($closureSerializer);
309
        $serialized = $serializer->serialize(
310
            array(
311
            'func' => function () {
312
                return 'it works';
313
            },
314
            'nice' => true
315
            )
316
        );
317
318
        $unserialized = $serializer->unserialize($serialized);
319
        $this->assertTrue(is_array($unserialized));
320
        $this->assertTrue($unserialized['nice']);
321
        $this->assertInstanceOf('Closure', $unserialized['func']);
322
        $this->assertSame('it works', $unserialized['func']());
323
    }
324
325
    /**
326
     * Test the unserialization of closures without providing closure serializer
327
     *
328
     * @return void
329
     */
330
    public function testUnserializeOfClosureWithoutSerializer()
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
            array(
340
            'func' => function () {
341
                return 'it works';
342
            },
343
            'nice' => true
344
            )
345
        );
346
347
        $this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException');
0 ignored issues
show
Deprecated Code introduced by
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...
348
        $this->serializer->unserialize($serialized);
349
    }
350
351
    /**
352
     * Test unserialize of unknown class
353
     *
354
     * @return void
355
     */
356
    public function testUnserializeUnknownClass()
357
    {
358
        $this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException');
0 ignored issues
show
Deprecated Code introduced by
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...
359
        $serialized = '{"@type":"UnknownClass"}';
360
        $this->serializer->unserialize($serialized);
361
    }
362
363
    /**
364
     * Test serialization of undeclared properties
365
     *
366
     * @return void
367
     */
368
    public function testSerializationUndeclaredProperties()
369
    {
370
        $obj = new stdClass();
371
        $obj->param1 = true;
372
        $obj->param2 = 'store me, please';
373
        $serialized = '{"@type":"stdClass","param1":true,"param2":"store me, please"}';
374
        $this->assertSame($serialized, $this->serializer->serialize($obj));
375
376
        $obj2 = $this->serializer->unserialize($serialized);
377
        $this->assertInstanceOf('stdClass', $obj2);
378
        $this->assertTrue($obj2->param1);
379
        $this->assertSame('store me, please', $obj2->param2);
380
381
        $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
382
        $obj = $this->serializer->unserialize($serialized);
383
        $this->assertInstanceOf('stdClass', $obj->sub);
384
        $this->assertSame('value', $obj->sub->key);
385
    }
386
387
    /**
388
     * Test undeclared properties setter (valid)
389
     *
390
     * @return void
391
     */
392
    public function testSetUnserializeUndeclaredPropertyModeValid()
393
    {
394
        $value = $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_SET);
395
        $this->assertSame($value, $this->serializer);
396
    }
397
398
    /**
399
     * Test undeclared properties setter (invalid)
400
     *
401
     * @return void
402
     */
403
    public function testSetUnserializeUndeclaredPropertyModeInvalid()
404
    {
405
        $this->setExpectedException('InvalidArgumentException');
0 ignored issues
show
Deprecated Code introduced by
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...
406
        $value = $this->serializer->setUnserializeUndeclaredPropertyMode('bad value');
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
407
    }
408
409
    /**
410
     * Test unserialization of undeclared properties in SET mode
411
     *
412
     * @return void
413
     */
414
    public function testUnserializeUndeclaredPropertySet()
415
    {
416
        $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_SET);
417
418
        $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
419
        $obj = $this->serializer->unserialize($serialized);
420
        $this->assertInstanceOf('stdClass', $obj->sub);
421
        $this->assertSame('value', $obj->sub->key);
422
    }
423
424
    /**
425
     * Test unserialization of undeclared properties in IGNORE mode
426
     *
427
     * @return void
428
     */
429
    public function testUnserializeUndeclaredPropertyIgnore()
430
    {
431
        $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_IGNORE);
432
433
        $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
434
        $obj = $this->serializer->unserialize($serialized);
435
        $this->assertFalse(isset($obj->sub));
436
    }
437
438
    /**
439
     * Test unserialization of undeclared properties in EXCEPTION mode
440
     *
441
     * @return void
442
     */
443
    public function testUnserializeUndeclaredPropertyException()
444
    {
445
        $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_EXCEPTION);
446
447
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
0 ignored issues
show
Deprecated Code introduced by
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...
448
        $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
449
        $obj = $this->serializer->unserialize($serialized);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
450
    }
451
452
    /**
453
     * Test serialize with recursion
454
     *
455
     * @return void
456
     */
457
    public function testSerializeRecursion()
458
    {
459
        $c1 = new stdClass();
460
        $c1->c2 = new stdClass();
461
        $c1->c2->c3 = new stdClass();
462
        $c1->c2->c3->c1 = $c1;
463
        $c1->something = 'ok';
464
        $c1->c2->c3->ok = true;
465
466
        $expected = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}';
467
        $this->assertSame($expected, $this->serializer->serialize($c1));
468
469
        $c1 = new stdClass();
470
        $c1->mirror = $c1;
471
        $expected = '{"@type":"stdClass","mirror":{"@type":"@0"}}';
472
        $this->assertSame($expected, $this->serializer->serialize($c1));
473
    }
474
475
    /**
476
     * Test unserialize with recursion
477
     *
478
     * @return void
479
     */
480
    public function testUnserializeRecursion()
481
    {
482
        $serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}';
483
        $obj = $this->serializer->unserialize($serialized);
484
        $this->assertTrue($obj->c2->c3->ok);
485
        $this->assertSame($obj, $obj->c2->c3->c1);
486
        $this->assertNotSame($obj, $obj->c2);
487
488
        $serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"c2":{"@type":"@1"},"c3":{"@type":"@2"}},"c3_copy":{"@type":"@2"}}}';
489
        $obj = $this->serializer->unserialize($serialized);
490
        $this->assertSame($obj, $obj->c2->c3->c1);
491
        $this->assertSame($obj->c2, $obj->c2->c3->c2);
492
        $this->assertSame($obj->c2->c3, $obj->c2->c3->c3);
493
        $this->assertSame($obj->c2->c3_copy, $obj->c2->c3);
494
    }
495
496
    /**
497
     * Test unserialize with bad JSON
498
     *
499
     * @return void
500
     */
501
    public function testUnserializeBadJSON()
502
    {
503
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
0 ignored issues
show
Deprecated Code introduced by
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...
504
        $this->serializer->unserialize('[this is not a valid json!}');
505
    }
506
507
    /**
508
     * The test attempts to serialize an array containing a NAN
509
     */
510
    public function testSerializeInvalidData()
511
    {
512
        if (PHP_VERSION_ID < 50500) {
513
            $this->markTestSkipped('PHP 5.4 raises a warning when encoding NAN, which fails the test.');
514
        }
515
516
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
0 ignored issues
show
Deprecated Code introduced by
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...
517
        $this->serializer->serialize(array(NAN));
518
    }
519
520
    /**
521
     *
522
     * @return void
523
     */
524
    public function testSerializeBinaryStringScalar()
525
    {
526
        $data = '';
527
        for ($i = 0; $i <= 255; $i++) {
528
            $data .= chr($i);
529
        }
530
531
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
532
        $this->assertSame($data, $unserialized);
533
    }
534
535
    /**
536
     *
537
     * @return void
538
     */
539
    public function testSerializeArrayWithBinaryStringsAsValues()
540
    {
541
        $data = '';
542
        for ($i = 0; $i <= 255; $i++) {
543
            $data .= chr($i);
544
        }
545
546
        $data = [$data, "$data 1", "$data 2"];
547
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
548
        $this->assertSame($data, $unserialized);
549
    }
550
551
    /**
552
     * Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per:
553
     * https://github.com/remicollet/pecl-json-c/issues/7
554
     * https://github.com/json-c/json-c/issues/108
555
     *
556
     * @return void
557
     */
558
    public function testSerializeArrayWithBinaryStringsAsKeys()
559
    {
560
        $data = '';
561
        for ($i = 1; $i <= 255; $i++) {
562
            $data .= chr($i);
563
        }
564
565
        $data = [$data => $data, "$data 1" => 'something'];
566
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
567
        $this->assertSame($data, $unserialized);
568
    }
569
570
    /**
571
     *
572
     * @return void
573
     */
574
    public function testSerializeObjectWithBinaryStrings()
575
    {
576
        $data = '';
577
        for ($i = 0; $i <= 255; $i++) {
578
            $data .= chr($i);
579
        }
580
581
        $obj = new \stdClass();
582
        $obj->string = $data;
583
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($obj));
584
        $this->assertInstanceOf('stdClass', $obj);
585
        $this->assertSame($obj->string, $unserialized->string);
586
    }
587
588
    /*
589
     * Test namespace change (backward compatibility)
590
     *
591
     * @return void
592
     * @deprecated
593
     */
594
    public function testNamespaceRename()
595
    {
596
        $serializer = new \Zumba\Util\JsonSerializer();
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Deprecated Code introduced by
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...
597
598
        $f = fopen(__FILE__, 'r');
599
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
0 ignored issues
show
Deprecated Code introduced by
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...
600
        $this->serializer->serialize($f);
601
    }
602
603
    /**
604
     * Test serialization of SplDoubleLinkedList
605
     *
606
     * @return void
607
     */
608
    public function testSerializationOfSplDoublyLinkedList()
609
    {
610
        $list = new \SplDoublyLinkedList();
611
        $list->push('fizz');
612
        $list->push(42);
613
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($list));
614
        $this->assertTrue($list->serialize() === $unserialized->serialize());
615
    }
616
617
    /**
618
     * Test serialization of Array references
619
     *
620
     * @return void
621
     */
622
    public function testSerializationOfArrayReferences()
623
    {
624
        $obj = new SupportClasses\EmptyClass();
625
        $obj->arr = array(1,2);
0 ignored issues
show
Bug introduced by
The property arr does not seem to exist in Zumba\JsonSerializer\Tes...pportClasses\EmptyClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
626
        $obj->arr2 = array(3,4);
0 ignored issues
show
Bug introduced by
The property arr2 does not seem to exist in Zumba\JsonSerializer\Tes...pportClasses\EmptyClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
627
        $obj->arr_copy = &$obj->arr;
0 ignored issues
show
Bug introduced by
The property arr_copy does not seem to exist in Zumba\JsonSerializer\Tes...pportClasses\EmptyClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
628
        $obj->arr2_copy = &$obj->arr2;
0 ignored issues
show
Bug introduced by
The property arr2_copy does not seem to exist in Zumba\JsonSerializer\Tes...pportClasses\EmptyClass.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
629
630
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($obj));
631
632
        $unserialized->arr[] = "This is arr";
633
        $unserialized->arr2[] = "This is arr2";
634
        $this->assertTrue($unserialized->arr_copy[2] === "This is arr");
635
        $this->assertTrue($unserialized->arr2_copy[2] === "This is arr2");
636
    }
637
}
638