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 ( 0a00a9...96765b )
by Juan
01:43
created

testSerializeClosureWithoutSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Zumba\Util\Test;
4
5
use Zumba\Util\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 Zumba\Util\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 strign $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 unserialization of scalar values
45
     *
46
     * @dataProvider scalarData
47
     * @param mixed $scalar
48
     * @param strign $jsoned
49
     * @return void
50
     */
51
    public function testUnserializeScalar($scalar, $jsoned)
52
    {
53
        $this->assertSame($scalar, $this->serializer->unserialize($jsoned));
54
    }
55
56
    /**
57
     * List of scalar data
58
     *
59
     * @return array
60
     */
61
    public function scalarData()
62
    {
63
        return array(
64
            array('testing', '"testing"'),
65
            array(123, '123'),
66
            array(0, '0'),
67
            array(0.0, '0.0'),
68
            array(17.0, '17.0'),
69
            array(17e1, '170.0'),
70
            array(17.2, '17.2'),
71
            array(true, 'true'),
72
            array(false, 'false'),
73
            array(null, 'null'),
74
            // Non UTF8
75
            array('ßåö', '"ßåö"')
76
        );
77
    }
78
79
    /**
80
     * Test the serialization of resources
81
     *
82
     * @return void
83
     */
84
    public function testSerializeResource()
85
    {
86
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
87
        $this->serializer->serialize(fopen(__FILE__, 'r'));
88
    }
89
90
    /**
91
     * Test the serialization of closures when not providing closure serializer
92
     *
93
     * @return void
94
     */
95
    public function testSerializeClosureWithoutSerializer()
96
    {
97
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
98
        $this->serializer->serialize(array('func' => function () {
99
            echo 'whoops';
100
        }));
101
    }
102
103
    /**
104
     * Test serialization of array without objects
105
     *
106
     * @dataProvider arrayNoObjectData
107
     * @param array $array
108
     * @param strign $jsoned
109
     * @return void
110
     */
111
    public function testSerializeArrayNoObject($array, $jsoned)
112
    {
113
        $this->assertSame($jsoned, $this->serializer->serialize($array));
114
    }
115
116
    /**
117
     * Test unserialization of array without objects
118
     *
119
     * @dataProvider arrayNoObjectData
120
     * @param array $array
121
     * @param strign $jsoned
122
     * @return void
123
     */
124
    public function testUnserializeArrayNoObject($array, $jsoned)
125
    {
126
        $this->assertSame($array, $this->serializer->unserialize($jsoned));
127
    }
128
129
    /**
130
     * List of array data
131
     *
132
     * @return array
133
     */
134
    public function arrayNoObjectData()
135
    {
136
        return array(
137
            array(array(1, 2, 3), '[1,2,3]'),
138
            array(array(1, 'abc', false), '[1,"abc",false]'),
139
            array(array('a' => 1, 'b' => 2, 'c' => 3), '{"a":1,"b":2,"c":3}'),
140
            array(array('integer' => 1, 'string' => 'abc', 'bool' => false), '{"integer":1,"string":"abc","bool":false}'),
141
            array(array(1, array('nested')), '[1,["nested"]]'),
142
            array(array('integer' => 1, 'array' => array('nested')), '{"integer":1,"array":["nested"]}'),
143
            array(array('integer' => 1, 'array' => array('nested' => 'object')), '{"integer":1,"array":{"nested":"object"}}'),
144
            array(array(1.0, 2, 3e1), '[1.0,2,30.0]'),
145
        );
146
    }
147
148
    /**
149
     * Test serialization of objects
150
     *
151
     * @return void
152
     */
153
    public function testSerializeObject()
154
    {
155
        $obj = new stdClass();
156
        $this->assertSame('{"@type":"stdClass"}', $this->serializer->serialize($obj));
157
158
        $obj = $empty = new SupportClasses\EmptyClass();
159
        $this->assertSame('{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\EmptyClass"}', $this->serializer->serialize($obj));
160
161
        $obj = new SupportClasses\AllVisibilities();
162
        $expected = '{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"this is public","prot":"protected","priv":"dont tell anyone"}';
163
        $this->assertSame($expected, $this->serializer->serialize($obj));
164
165
        $obj->pub = 'new value';
166
        $expected = '{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"new value","prot":"protected","priv":"dont tell anyone"}';
167
        $this->assertSame($expected, $this->serializer->serialize($obj));
168
169
        $obj->pub = $empty;
170
        $expected = '{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}';
171
        $this->assertSame($expected, $this->serializer->serialize($obj));
172
173
        $array = array('instance' => $empty);
174
        $expected = '{"instance":{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\EmptyClass"}}';
175
        $this->assertSame($expected, $this->serializer->serialize($array));
176
177
        $obj = new stdClass();
178
        $obj->total = 10.0;
179
        $obj->discount = 0.0;
180
        $expected = '{"@type":"stdClass","total":10.0,"discount":0.0}';
181
        $this->assertSame($expected, $this->serializer->serialize($obj));
182
    }
183
184
    /**
185
     * Test unserialization of objects
186
     *
187
     * @return void
188
     */
189
    public function testUnserializeObjects()
190
    {
191
        $serialized = '{"@type":"stdClass"}';
192
        $obj = $this->serializer->unserialize($serialized);
193
        $this->assertInstanceOf('stdClass', $obj);
194
195
        $serialized = '{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\EmptyClass"}';
196
        $obj = $this->serializer->unserialize($serialized);
197
        $this->assertInstanceOf('Zumba\Util\Test\SupportClasses\EmptyClass', $obj);
198
199
        $serialized = '{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}';
200
        $obj = $this->serializer->unserialize($serialized);
201
        $this->assertInstanceOf('Zumba\Util\Test\SupportClasses\AllVisibilities', $obj);
202
        $this->assertInstanceOf('Zumba\Util\Test\SupportClasses\EmptyClass', $obj->pub);
203
        $this->assertAttributeSame('protected', 'prot', $obj);
204
        $this->assertAttributeSame('dont tell anyone', 'priv', $obj);
205
206
        $serialized = '{"instance":{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\EmptyClass"}}';
207
        $array = $this->serializer->unserialize($serialized);
208
        $this->assertTrue(is_array($array));
209
        $this->assertInstanceOf('Zumba\Util\Test\SupportClasses\EmptyClass', $array['instance']);
210
    }
211
212
    /**
213
     * Test magic serialization methods
214
     *
215
     * @return void
216
     */
217
    public function testSerializationMagicMethods()
218
    {
219
        $obj = new SupportClasses\MagicClass();
220
        $serialized = '{"@type":"Zumba\\\\Util\\\\Test\\\\SupportClasses\\\\MagicClass","show":true}';
221
        $this->assertSame($serialized, $this->serializer->serialize($obj));
222
        $this->assertFalse($obj->woke);
223
224
        $obj = $this->serializer->unserialize($serialized);
225
        $this->assertTrue($obj->woke);
226
    }
227
228
    /**
229
     * Test serialization of DateTime classes
230
     *
231
     * Some interal classes, such as DateTime, cannot be initialized with
232
     * ReflectionClass::newInstanceWithoutConstructor()
233
     *
234
     * @return void
235
     */
236
    public function testSerializationOfDateTime()
237
    {
238
        $date = new \DateTime('2014-06-15 12:00:00', new \DateTimeZone('UTC'));
239
        $obj = $this->serializer->unserialize($this->serializer->serialize($date));
240
        $this->assertSame($date->getTimestamp(), $obj->getTimestamp());
241
    }
242
243
    /**
244
     * Test the serialization of closures providing closure serializer
245
     *
246
     * @return void
247
     */
248
    public function testSerializationOfClosure()
249
    {
250
        if (!class_exists('SuperClosure\Serializer')) {
251
            $this->markTestSkipped('SuperClosure is not installed.');
252
        }
253
254
        $closureSerializer = new ClosureSerializer();
255
        $serializer = new JsonSerializer($closureSerializer);
256
        $serialized = $serializer->serialize(array(
257
            'func' => function () {
258
                return 'it works';
259
            },
260
            'nice' => true
261
        ));
262
263
        $unserialized = $serializer->unserialize($serialized);
264
        $this->assertTrue(is_array($unserialized));
265
        $this->assertTrue($unserialized['nice']);
266
        $this->assertInstanceOf('Closure', $unserialized['func']);
267
        $this->assertSame('it works', $unserialized['func']());
268
    }
269
270
    /**
271
     * Test the unserialization of closures without providing closure serializer
272
     *
273
     * @return void
274
     */
275
    public function testUnserializeOfClosureWithoutSerializer()
276
    {
277
        if (!class_exists('SuperClosure\Serializer')) {
278
            $this->markTestSkipped('SuperClosure is not installed.');
279
        }
280
281
        $closureSerializer = new ClosureSerializer();
282
        $serializer = new JsonSerializer($closureSerializer);
283
        $serialized = $serializer->serialize(array(
284
            'func' => function () {
285
                return 'it works';
286
            },
287
            'nice' => true
288
        ));
289
290
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
291
        $this->serializer->unserialize($serialized);
292
    }
293
294
    /**
295
     * Test unserialize of unknown class
296
     *
297
     * @return void
298
     */
299
    public function testUnserializeUnknownClass()
300
    {
301
        $this->setExpectedException('Zumba\Exception\JsonSerializerException');
302
        $serialized = '{"@type":"UnknownClass"}';
303
        $this->serializer->unserialize($serialized);
304
    }
305
306
    /**
307
     * Test serialization of undeclared properties
308
     *
309
     * @return void
310
     */
311
    public function testSerializationUndeclaredProperties()
312
    {
313
        $obj = new stdClass();
314
        $obj->param1 = true;
315
        $obj->param2 = 'store me, please';
316
        $serialized = '{"@type":"stdClass","param1":true,"param2":"store me, please"}';
317
        $this->assertSame($serialized, $this->serializer->serialize($obj));
318
319
        $obj2 = $this->serializer->unserialize($serialized);
320
        $this->assertInstanceOf('stdClass', $obj2);
321
        $this->assertTrue($obj2->param1);
322
        $this->assertSame('store me, please', $obj2->param2);
323
324
        $serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
325
        $obj = $this->serializer->unserialize($serialized);
326
        $this->assertInstanceOf('stdClass', $obj->sub);
327
        $this->assertSame('value', $obj->sub->key);
328
    }
329
330
    /**
331
     * Test serialize with recursion
332
     *
333
     * @return void
334
     */
335
    public function testSerializeRecursion()
336
    {
337
        $c1 = new stdClass();
338
        $c1->c2 = new stdClass();
339
        $c1->c2->c3 = new stdClass();
340
        $c1->c2->c3->c1 = $c1;
341
        $c1->something = 'ok';
342
        $c1->c2->c3->ok = true;
343
344
        $expected = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}';
345
        $this->assertSame($expected, $this->serializer->serialize($c1));
346
347
        $c1 = new stdClass();
348
        $c1->mirror = $c1;
349
        $expected = '{"@type":"stdClass","mirror":{"@type":"@0"}}';
350
        $this->assertSame($expected, $this->serializer->serialize($c1));
351
    }
352
353
    /**
354
     * Test unserialize with recursion
355
     *
356
     * @return void
357
     */
358
    public function testUnserializeRecursion()
359
    {
360
        $serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}';
361
        $obj = $this->serializer->unserialize($serialized);
362
        $this->assertTrue($obj->c2->c3->ok);
363
        $this->assertSame($obj, $obj->c2->c3->c1);
364
        $this->assertNotSame($obj, $obj->c2);
365
366
        $serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"c2":{"@type":"@1"},"c3":{"@type":"@2"}},"c3_copy":{"@type":"@2"}}}';
367
        $obj = $this->serializer->unserialize($serialized);
368
        $this->assertSame($obj, $obj->c2->c3->c1);
369
        $this->assertSame($obj->c2, $obj->c2->c3->c2);
370
        $this->assertSame($obj->c2->c3, $obj->c2->c3->c3);
371
        $this->assertSame($obj->c2->c3_copy, $obj->c2->c3);
372
    }
373
}
374