-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPMessageTest.php
440 lines (345 loc) · 9.41 KB
/
PPMessageTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
class SimpleTestClass extends PPMessage {
/**
*
* @access public
* @var string
*/
public $field1;
/**
*
* @access public
* @var string
*/
public $field2;
}
class SimpleContainerTestClass extends PPMessage {
/**
* @access public
* @var string
*/
public $field1;
/**
* @array
* @access public
* @var string
*/
public $list1;
/**
* @array
* @access public
* @var SimpleTestClass
*/
public $list2;
/**
* @array
* @access public
* @var AttributeTestClass
*/
public $list3;
/**
* @access public
* @var SimpleTestClass
*/
public $nestedField;
}
class AttributeTestClass extends PPMessage {
/**
*
* @access public
* @attribute
* @var string
*/
public $attrib1;
/**
*
* @access public
* @attribute
* @var string
*/
public $attrib2;
/**
*
* @access public
* @value
* @var string
*/
public $value;
}
/**
* @hasAttribute
*
*/
class AttributeContainerTestClass extends PPMessage {
/**
*
* @access public
* @var AttributeTestClass
*/
public $member;
/**
*
* @array
* @access public
* @var AttributeTestClass
*/
public $arrayMember;
}
/**
* Test class for PPMessage.
*
*/
class PPMessageTest extends PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
/**
* @test
*/
public function attributeSerialization() {
$o = new AttributeTestClass();
$o->attrib1 = "abc";
$o->attrib2 = "random value";
$c = new AttributeContainerTestClass();
$c->member = $o;
$this->assertEquals("attrib1=abc&attrib2=random+value", $o->toNVPString());
$this->assertEquals("member.attrib1=abc&member.attrib2=random+value", $c->toNVPString());
$o->value = "value";
$this->assertEquals("attrib1=abc&attrib2=random+value&=value", $o->toNVPString());
$this->assertEquals("member.attrib1=abc&member.attrib2=random+value&member=value", $c->toNVPString());
}
/**
* @test
*/
public function attributeSerializationInArrays() {
$o = new AttributeTestClass();
$o->attrib1 = "abc";
$o->attrib2 = "random value";
$o->value = "value";
$c = new AttributeContainerTestClass();
$c->member = $o;
$o = new AttributeTestClass();
$o->attrib1 = "abc";
$o->attrib2 = "random value";
$c->arrayMember = array($o);
$this->assertEquals("member.attrib1=abc&member.attrib2=random+value&member=value&arrayMember(0).attrib1=abc&arrayMember(0).attrib2=random+value",
$c->toNVPString());
$c->arrayMember[0]->value = "value";
$this->assertEquals("member.attrib1=abc&member.attrib2=random+value&member=value&arrayMember(0).attrib1=abc&arrayMember(0).attrib2=random+value&arrayMember(0)=value",
$c->toNVPString());
}
/**
* @test
*/
public function attributeDeserialization() {
// Attributes and value present
$responseMap = array(
"member.attrib1" => "abc",
"member.attrib2" => "random+value",
"member" => "value"
);
$c = new AttributeContainerTestClass();
$c->init($responseMap);
$this->assertNotNull($c->member);
$this->assertEquals("abc", $c->member->attrib1);
$this->assertEquals("random value", $c->member->attrib2);
$this->assertEquals("value", $c->member->value);
// Only value present
$responseMap = array(
"member" => "value"
);
$c = new AttributeContainerTestClass();
$c->init($responseMap);
$this->assertNotNull($c->member);
$this->assertEquals("value", $c->member->value);
// Only attributes present
$responseMap = array(
"member.attrib1" => "abc",
"member.attrib2" => "random+value"
);
$c = new AttributeContainerTestClass();
$c->init($responseMap);
$this->assertNotNull($c->member);
$this->assertEquals("abc", $c->member->attrib1);
$this->assertEquals("random value", $c->member->attrib2);
}
/**
* @test
*/
public function attributeDeserializationInArrays() {
// Only value present. Single item in list
$responseMap = array(
"arrayMember(0)" => "value+1"
);
$c = new AttributeContainerTestClass();
$c->init($responseMap);
$this->assertNotNull($c->arrayMember[0]);
$this->assertEquals("value 1", $c->arrayMember[0]->value);
// Only attributes present. Single item in list
$responseMap = array(
"arrayMember(0).attrib1" => "abc",
"arrayMember(0).attrib2" => "random+value",
);
$c = new AttributeContainerTestClass();
$c->init($responseMap);
$this->assertNotNull($c->arrayMember[0]);
$this->assertEquals("abc", $c->arrayMember[0]->attrib1);
$this->assertEquals("random value", $c->arrayMember[0]->attrib2);
// Attributes and value present. Mulitple items in list
$responseMap = array(
"arrayMember(0).attrib1" => "abc",
"arrayMember(0).attrib2" => "random+value",
"arrayMember(0)" => "value",
"arrayMember(0).attrib1" => "xyz",
"arrayMember(1).attrib1" => "attribute1"
);
$c->init($responseMap);
$this->assertEquals("value", $c->arrayMember[0]->value);
$this->assertEquals("xyz", $c->arrayMember[0]->attrib1);
$this->assertEquals("random value", $c->arrayMember[0]->attrib2);
$this->assertEquals("attribute1", $c->arrayMember[1]->attrib1);
$this->assertNull($c->arrayMember[1]->value);
}
/**
* @test
*/
public function simpleSerialization() {
$o = new SimpleTestClass();
$o->field1 = "fieldvalue1";
$o->field2 = "fieldvalue2";
$this->assertEquals("field1=fieldvalue1&field2=fieldvalue2", $o->toNVPString(''));
}
/**
* @test
*/
public function simpleDeserialization() {
$map = array(
"field1" => "fieldvalue1",
"field2" => "field+value2"
);
$o = new SimpleTestClass();
$o->init($map);
$this->assertEquals("fieldvalue1", $o->field1);
$this->assertEquals("field value2", $o->field2);
}
/**
* @test
*/
public function nestedSerialization() {
$o = new SimpleTestClass();
$o->field1 = "fieldvalue1";
$o->field2 = "fieldvalue2";
$c = new SimpleContainerTestClass();
$c->nestedField = $o;
$c->field1 = "abc";
$this->assertEquals("field1=abc&nestedField.field1=fieldvalue1&nestedField.field2=fieldvalue2", $c->toNVPString(''));
}
/**
* @test
*/
public function nestedDeserialization() {
$map = array(
"field1" => "abc",
"nestedField.field1" => "fieldvalue1",
"nestedField.field2" => "field+value2"
);
$c = new SimpleContainerTestClass();
$c->init($map);
$this->assertEquals("abc", $c->field1);
$this->assertEquals("fieldvalue1", $c->nestedField->field1);
$this->assertEquals("field value2", $c->nestedField->field2);
}
/**
* @test
*/
public function simpleListSerialization() {
$c = new SimpleContainerTestClass();
$c->list1 = array('Array', "of", "some strings");
$c->field1 = "abc";
$this->assertEquals("field1=abc&list1(0)=Array&list1(1)=of&list1(2)=some+strings", $c->toNVPString(''));
}
/**
* @test
*/
public function simpleListDeserialization() {
$map = array(
"field1" => "abc",
"list1(0)" => "Array",
"list1(1)" => "of",
"list1(2)" => "some+strings"
);
$c = new SimpleContainerTestClass();
$c->init($map);
$this->assertEquals("abc", $c->field1);
$this->assertEquals(3, count($c->list1));
$this->assertEquals("some strings", $c->list1[2]);
}
/**
* @test
*/
public function complexListSerialization() {
$o1 = new SimpleTestClass();
$o1->field1 = "somevalue1";
$o1->field2 = "somevalue2";
$o2 = new SimpleTestClass();
$o2->field1 = "another value1";
$o2->field2 = "anothervalue2";
$c = new SimpleContainerTestClass();
$c->list2 = array($o1, $o2);
$this->assertEquals("list2(0).field1=somevalue1&list2(0).field2=somevalue2&list2(1).field1=another+value1&list2(1).field2=anothervalue2",
$c->toNVPString(''));
}
/**
* @test
*/
public function complexListDeserialization() {
$map = array(
"list2(0).field1" => "somevalue1",
"list2(0).field2" => "somevalue2",
"list2(1).field1" => "another+value1",
"list2(1).field2" => "anothervalue2"
);
$c = new SimpleContainerTestClass();
$c->init($map);
$this->assertEquals(2, count($c->list2));
$this->assertEquals("somevalue1", $c->list2[0]->field1);
$this->assertEquals("another value1", $c->list2[1]->field1);
}
/**
* @test
*/
public function serializeAndDeserialize() {
$o1 = new AttributeTestClass();
$o1->value = "some value";
$o1->attrib1 = "someattrib";
$o2 = new AttributeTestClass();
$o2->value = "some value2";
$o3 = new AttributeTestClass();
$o3->attrib1 = "attribute";
$o3->value = "some value3";
$c = new SimpleContainerTestClass();
$c->list3 = array($o1, $o2, $o3);
$newC = new SimpleContainerTestClass();
$newC->init(PPUtils::nvpToMap($c->toNVPString(''))); //TODO: Mock nvpToMap
$this->assertEquals($c, $newC);
}
/**
* @test
*/
public function deserializeAndSerialize() {
$nvpString = "list2(0).field1=somevalue1&list2(0).field2=somevalue2&list2(1).field1=another+value1&list2(1).field2=anothervalue2&list3(0).attrib1=somevalue1&list3(0).attrib2=somevalue2&list3(0)=value+field&list3(1).attrib1=another+value1&list3(2)=anothervalue2";
$newC = new SimpleContainerTestClass();
$newC->init(PPUtils::nvpToMap($nvpString)); //TODO: Mock nvpToMap
$this->assertEquals($nvpString, $newC->toNVPString());
}
}