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 ( 4f62fd...0a00a9 )
by Chris
9s
created

JsonSerializerTest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 346
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 18
Bugs 4 Features 9
Metric Value
wmc 21
c 18
b 4
f 9
lcom 1
cbo 5
dl 0
loc 346
rs 10

19 Methods

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