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
Push — master ( 68c6a7...457e68 )
by Juan
8s
created

testSerializeBinaryStringScalar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
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
        $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()
173
    {
174
        $obj = new stdClass();
175
        $this->assertSame('{"@type":"stdClass"}', $this->serializer->serialize($obj));
176
177
        $obj = $empty = new SupportClasses\EmptyClass();
178
        $this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}', $this->serializer->serialize($obj));
179
180
        $obj = new SupportClasses\AllVisibilities();
181
        $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"this is public","prot":"protected","priv":"dont tell anyone"}';
182
        $this->assertSame($expected, $this->serializer->serialize($obj));
183
184
        $obj->pub = 'new value';
185
        $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"new value","prot":"protected","priv":"dont tell anyone"}';
186
        $this->assertSame($expected, $this->serializer->serialize($obj));
187
188
        $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...
189
        $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}';
190
        $this->assertSame($expected, $this->serializer->serialize($obj));
191
192
        $array = array('instance' => $empty);
193
        $expected = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}';
194
        $this->assertSame($expected, $this->serializer->serialize($array));
195
196
        $obj = new stdClass();
197
        $obj->total = 10.0;
198
        $obj->discount = 0.0;
199
        $expected = '{"@type":"stdClass","total":10.0,"discount":0.0}';
200
        $this->assertSame($expected, $this->serializer->serialize($obj));
201
    }
202
203
    /**
204
     * Test unserialization of objects
205
     *
206
     * @return void
207
     */
208
    public function testUnserializeObjects()
209
    {
210
        $serialized = '{"@type":"stdClass"}';
211
        $obj = $this->serializer->unserialize($serialized);
212
        $this->assertInstanceOf('stdClass', $obj);
213
214
        $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}';
215
        $obj = $this->serializer->unserialize($serialized);
216
        $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj);
217
218
        $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}';
219
        $obj = $this->serializer->unserialize($serialized);
220
        $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\AllVisibilities', $obj);
221
        $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj->pub);
222
        $this->assertAttributeSame('protected', 'prot', $obj);
223
        $this->assertAttributeSame('dont tell anyone', 'priv', $obj);
224
225
        $serialized = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}';
226
        $array = $this->serializer->unserialize($serialized);
227
        $this->assertTrue(is_array($array));
228
        $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $array['instance']);
229
    }
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));
241
        $this->assertFalse($obj->woke);
242
243
        $obj = $this->serializer->unserialize($serialized);
244
        $this->assertTrue($obj->woke);
245
    }
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()
256
    {
257
        $date = new \DateTime('2014-06-15 12:00:00', new \DateTimeZone('UTC'));
258
        $obj = $this->serializer->unserialize($this->serializer->serialize($date));
259
        $this->assertSame($date->getTimestamp(), $obj->getTimestamp());
260
    }
261
262
    /**
263
     * Test the serialization of closures providing closure serializer
264
     *
265
     * @return void
266
     */
267
    public function testSerializationOfClosure()
268
    {
269
        if (!class_exists('SuperClosure\Serializer')) {
270
            $this->markTestSkipped('SuperClosure is not installed.');
271
        }
272
273
        $closureSerializer = new ClosureSerializer();
274
        $serializer = new JsonSerializer($closureSerializer);
275
        $serialized = $serializer->serialize(array(
276
            'func' => function () {
277
                return 'it works';
278
            },
279
            'nice' => true
280
        ));
281
282
        $unserialized = $serializer->unserialize($serialized);
283
        $this->assertTrue(is_array($unserialized));
284
        $this->assertTrue($unserialized['nice']);
285
        $this->assertInstanceOf('Closure', $unserialized['func']);
286
        $this->assertSame('it works', $unserialized['func']());
287
    }
288
289
    /**
290
     * Test the unserialization of closures without providing closure serializer
291
     *
292
     * @return void
293
     */
294
    public function testUnserializeOfClosureWithoutSerializer()
295
    {
296
        if (!class_exists('SuperClosure\Serializer')) {
297
            $this->markTestSkipped('SuperClosure is not installed.');
298
        }
299
300
        $closureSerializer = new ClosureSerializer();
301
        $serializer = new JsonSerializer($closureSerializer);
302
        $serialized = $serializer->serialize(array(
303
            'func' => function () {
304
                return 'it works';
305
            },
306
            'nice' => true
307
        ));
308
309
        $this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException');
310
        $this->serializer->unserialize($serialized);
311
    }
312
313
    /**
314
     * Test unserialize of unknown class
315
     *
316
     * @return void
317
     */
318
    public function testUnserializeUnknownClass()
319
    {
320
        $this->setExpectedException('Zumba\JsonSerializer\Exception\JsonSerializerException');
321
        $serialized = '{"@type":"UnknownClass"}';
322
        $this->serializer->unserialize($serialized);
323
    }
324
325
    /**
326
     * Test serialization of undeclared properties
327
     *
328
     * @return void
329
     */
330
    public function testSerializationUndeclaredProperties()
331
    {
332
        $obj = new stdClass();
333
        $obj->param1 = true;
334
        $obj->param2 = 'store me, please';
335
        $serialized = '{"@type":"stdClass","param1":true,"param2":"store me, please"}';
336
        $this->assertSame($serialized, $this->serializer->serialize($obj));
337
338
        $obj2 = $this->serializer->unserialize($serialized);
339
        $this->assertInstanceOf('stdClass', $obj2);
340
        $this->assertTrue($obj2->param1);
341
        $this->assertSame('store me, please', $obj2->param2);
342
343
        $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
344
        $obj = $this->serializer->unserialize($serialized);
345
        $this->assertInstanceOf('stdClass', $obj->sub);
346
        $this->assertSame('value', $obj->sub->key);
347
    }
348
349
    /**
350
     * Test serialize with recursion
351
     *
352
     * @return void
353
     */
354
    public function testSerializeRecursion()
355
    {
356
        $c1 = new stdClass();
357
        $c1->c2 = new stdClass();
358
        $c1->c2->c3 = new stdClass();
359
        $c1->c2->c3->c1 = $c1;
360
        $c1->something = 'ok';
361
        $c1->c2->c3->ok = true;
362
363
        $expected = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}';
364
        $this->assertSame($expected, $this->serializer->serialize($c1));
365
366
        $c1 = new stdClass();
367
        $c1->mirror = $c1;
368
        $expected = '{"@type":"stdClass","mirror":{"@type":"@0"}}';
369
        $this->assertSame($expected, $this->serializer->serialize($c1));
370
    }
371
372
    /**
373
     * Test unserialize with recursion
374
     *
375
     * @return void
376
     */
377
    public function testUnserializeRecursion()
378
    {
379
        $serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}';
380
        $obj = $this->serializer->unserialize($serialized);
381
        $this->assertTrue($obj->c2->c3->ok);
382
        $this->assertSame($obj, $obj->c2->c3->c1);
383
        $this->assertNotSame($obj, $obj->c2);
384
385
        $serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"c2":{"@type":"@1"},"c3":{"@type":"@2"}},"c3_copy":{"@type":"@2"}}}';
386
        $obj = $this->serializer->unserialize($serialized);
387
        $this->assertSame($obj, $obj->c2->c3->c1);
388
        $this->assertSame($obj->c2, $obj->c2->c3->c2);
389
        $this->assertSame($obj->c2->c3, $obj->c2->c3->c3);
390
        $this->assertSame($obj->c2->c3_copy, $obj->c2->c3);
391
    }
392
393
    /**
394
     * Test unserialize with bad JSON
395
     *
396
     * @return void
397
     */
398
    public function testUnserializeBadJSON()
399
    {
400
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
401
        $this->serializer->unserialize('[this is not a valid json!}');
402
    }
403
404
    /**
405
     * The test attempts to serialize an array containing a NAN
406
     */
407
    public function testSerializeInvalidData()
408
    {
409
        if (PHP_VERSION_ID < 50500) {
410
            $this->markTestSkipped('PHP 5.4 raises a warning when encoding NAN, which fails the test.');
411
        }
412
413
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
414
        $this->serializer->serialize(array(NAN));
415
    }
416
417
    /**
418
     * @return void
419
     */
420
    public function testSerializeBinaryStringScalar()
421
    {
422
        $data = '';
423
        for ($i = 0; $i <= 255; $i++) {
424
            $data .= chr($i);
425
        }
426
427
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
428
        $this->assertSame($data, $unserialized);
429
    }
430
431
    /**
432
     * @return void
433
     */
434
    public function testSerializeArrayWithBinaryStringsAsValues()
435
    {
436
        $data = '';
437
        for ($i = 0; $i <= 255; $i++) {
438
            $data .= chr($i);
439
        }
440
441
        $data = [$data, "$data 1", "$data 2"];
442
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
443
        $this->assertSame($data, $unserialized);
444
    }
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()
454
    {
455
        $data = '';
456
        for ($i = 1; $i <= 255; $i++) {
457
            $data .= chr($i);
458
        }
459
460
        $data = [$data => $data, "$data 1" => 'something'];
461
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
462
        $this->assertSame($data, $unserialized);
463
    }
464
465
    /**
466
     * @return void
467
     */
468
    public function testSerializeObjectWithBinaryStrings()
469
    {
470
        $data = '';
471
        for ($i = 0; $i <= 255; $i++) {
472
            $data .= chr($i);
473
        }
474
475
        $obj = new \stdClass();
476
        $obj->string = $data;
477
        $unserialized = $this->serializer->unserialize($this->serializer->serialize($obj));
478
        $this->assertInstanceOf('stdClass', $obj);
479
        $this->assertSame($obj->string, $unserialized->string);
480
    }
481
482
    /*
483
     * Test namespace change (backward compatibility)
484
     *
485
     * @return void
486
     * @deprecated
487
     */
488
    public function testNamespaceRename()
489
    {
490
        $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...
491
492
        $f = fopen(__FILE__, 'r');
493
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
494
        $this->serializer->serialize($f);
495
    }
496
}
497