1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk\Tests\Units\Model; |
4
|
|
|
|
5
|
|
|
use atoum; |
6
|
|
|
use DateTime; |
7
|
|
|
use libphonenumber\PhoneNumberFormat; |
8
|
|
|
use libphonenumber\PhoneNumberUtil; |
9
|
|
|
use Mapado\RestClientSdk\Mapping; |
10
|
|
|
use Mapado\RestClientSdk\Mapping\Attribute; |
11
|
|
|
use Mapado\RestClientSdk\Mapping\ClassMetadata; |
12
|
|
|
use Mapado\RestClientSdk\Mapping\Driver\AnnotationDriver; |
13
|
|
|
use Mapado\RestClientSdk\Mapping\Relation; |
14
|
|
|
use Mapado\RestClientSdk\Tests\Model\Issue46; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Serializer |
18
|
|
|
* @author Julien Deniau <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Serializer extends atoum |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* testJsonEncode |
24
|
|
|
* |
25
|
|
|
* @access public |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function testJsonEncode() |
29
|
|
|
{ |
30
|
|
|
$this->createNewInstance(); |
31
|
|
|
|
32
|
|
|
$this |
33
|
|
|
->given($cart = $this->createCart()) |
34
|
|
|
->then |
35
|
|
|
->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
36
|
|
|
->isIdenticalTo([ |
37
|
|
|
'@id' => '/v1/carts/8', |
38
|
|
|
'status' => 'payed', |
39
|
|
|
"clientPhoneNumber" => '+33 1 23 45 67 89', |
40
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
41
|
|
|
'cart_items' => [], |
42
|
|
|
'order' => null, |
43
|
|
|
]) |
44
|
|
|
|
45
|
|
|
// reverse the serialization |
46
|
|
|
->then |
47
|
|
|
->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
48
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart') |
49
|
|
|
->string($cart->getId()) |
50
|
|
|
->isEqualTo('/v1/carts/8') |
51
|
|
|
->string($cart->getStatus()) |
52
|
|
|
->isEqualTo('payed') |
53
|
|
|
->datetime($cart->getCreatedAt()) |
54
|
|
|
->isEqualTo(new \DateTime('2015-09-20T12:08:00')) |
55
|
|
|
->array($cart->getCartItemList()) |
56
|
|
|
->isEmpty() |
57
|
|
|
|
58
|
|
|
; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* testJsonEncodeRelation |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function testJsonEncodeRelationWithLink() |
68
|
|
|
{ |
69
|
|
|
$this->createNewInstance(); |
70
|
|
|
|
71
|
|
|
$this |
72
|
|
|
->given($cart = $this->createCart()) |
73
|
|
|
->and($cartItem = $this->createKnownCartItem()) |
74
|
|
|
->and($cart->addCartItemList($cartItem)) |
75
|
|
|
|
76
|
|
|
->then |
77
|
|
|
->array($data = $this->testedInstance->serialize( |
78
|
|
|
$cart, |
79
|
|
|
'Mapado\RestClientSdk\Tests\Model\Cart', |
80
|
|
|
[ 'serializeRelations' => ['cart_items'] ] |
81
|
|
|
)) |
82
|
|
|
->isIdenticalTo([ |
83
|
|
|
'@id' => '/v1/carts/8', |
84
|
|
|
'status' => 'payed', |
85
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
86
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
87
|
|
|
'cart_items' => [ |
88
|
|
|
[ |
89
|
|
|
'@id' => '/v1/cart_items/16', |
90
|
|
|
'amount' => 1, |
91
|
|
|
'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339), |
92
|
|
|
'data' => [ |
93
|
|
|
'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339), |
94
|
|
|
'who' => 'Jane', |
95
|
|
|
], |
96
|
|
|
'cart' => '/v1/carts/8', |
97
|
|
|
'product' => '/v1/products/10', |
98
|
|
|
'cartItemDetailList' => [], |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
'order' => null, |
102
|
|
|
]) |
103
|
|
|
|
104
|
|
|
->then |
105
|
|
|
->array($data = $this->testedInstance->serialize( |
106
|
|
|
$cart, |
107
|
|
|
'Mapado\RestClientSdk\Tests\Model\Cart', |
108
|
|
|
[ 'serializeRelations' => ['cart_items'] ] |
109
|
|
|
)) |
110
|
|
|
->isIdenticalTo([ |
111
|
|
|
'@id' => '/v1/carts/8', |
112
|
|
|
'status' => 'payed', |
113
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
114
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
115
|
|
|
'cart_items' => [ |
116
|
|
|
[ |
117
|
|
|
'@id' => '/v1/cart_items/16', |
118
|
|
|
'amount' => 1, |
119
|
|
|
'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339), |
120
|
|
|
'data' => [ |
121
|
|
|
'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339), |
122
|
|
|
'who' => 'Jane', |
123
|
|
|
], |
124
|
|
|
'cart' => '/v1/carts/8', |
125
|
|
|
'product' => '/v1/products/10', |
126
|
|
|
'cartItemDetailList' => [], |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
'order' => null, |
130
|
|
|
]) |
131
|
|
|
|
132
|
|
|
// reverse the serialization |
133
|
|
|
->then |
134
|
|
|
->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
135
|
|
|
->array($cart->getCartItemList()) |
136
|
|
|
->size->isEqualTo(1) |
137
|
|
|
->object($cartItem = current($cart->getCartItemList())) |
138
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem') |
139
|
|
|
->string($cartItem->getId()) |
140
|
|
|
->isEqualTo('/v1/cart_items/16') |
141
|
|
|
; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* testJsonEncodeRelationWithoutLink |
146
|
|
|
* |
147
|
|
|
* @access public |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
public function testJsonEncodeRelationWithoutLink() |
151
|
|
|
{ |
152
|
|
|
$this->createNewInstance(); |
153
|
|
|
|
154
|
|
|
$this |
155
|
|
|
->given($cart = $this->createCart()) |
156
|
|
|
->and($cartItem = $this->createNewCartItem()) |
157
|
|
|
->and($cart->addCartItemList($cartItem)) |
158
|
|
|
->then |
159
|
|
|
->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
160
|
|
|
->isIdenticalTo([ |
161
|
|
|
'@id' => '/v1/carts/8', |
162
|
|
|
'status' => 'payed', |
163
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
164
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
165
|
|
|
'cart_items' => [ |
166
|
|
|
[ |
167
|
|
|
'amount' => 2, |
168
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
169
|
|
|
'data' => [ |
170
|
|
|
'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
171
|
|
|
'who' => 'John', |
172
|
|
|
], |
173
|
|
|
'product' => '/v1/products/10', |
174
|
|
|
'cartItemDetailList' => [], |
175
|
|
|
], |
176
|
|
|
], |
177
|
|
|
'order' => null, |
178
|
|
|
]) |
179
|
|
|
|
180
|
|
|
// reverse the serialization |
181
|
|
|
->then |
182
|
|
|
->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
183
|
|
|
->array($cart->getCartItemList()) // we can not uneserialize an unlinked entity |
184
|
|
|
->isEmpty() |
185
|
|
|
; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
View Code Duplication |
public function testSerializeThreeLevel() |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$this->createNewInstance(); |
191
|
|
|
|
192
|
|
|
$this |
193
|
|
|
->given($cart = $this->createNewCart()) |
194
|
|
|
->and($cartItem = $this->createNewCartItem()) |
195
|
|
|
->and($cart->addCartItemList($cartItem)) |
196
|
|
|
->then |
197
|
|
|
->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
198
|
|
|
->isIdenticalTo([ |
199
|
|
|
'status' => 'payed', |
200
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
201
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
202
|
|
|
'cart_items' => [ |
203
|
|
|
[ |
204
|
|
|
'amount' => 2, |
205
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
206
|
|
|
'data' => [ |
207
|
|
|
'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
208
|
|
|
'who' => 'John', |
209
|
|
|
], |
210
|
|
|
'product' => '/v1/products/10', |
211
|
|
|
'cartItemDetailList' => [], |
212
|
|
|
], |
213
|
|
|
], |
214
|
|
|
'order' => null, |
215
|
|
|
]) |
216
|
|
|
; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* testJsonEncodeRelationWithoutLinkMultipleLevel |
221
|
|
|
* |
222
|
|
|
* @access public |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
public function testJsonEncodeRelationWithoutLinkMultipleLevel() |
226
|
|
|
{ |
227
|
|
|
$this->createNewInstance(); |
228
|
|
|
$this |
229
|
|
|
->given($cart = $this->createCart()) |
230
|
|
|
->and($cartItem = $this->createNewCartItem(false)) |
231
|
|
|
->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail())) |
232
|
|
|
->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail())) |
233
|
|
|
->if($cart->addCartItemList($cartItem)) |
234
|
|
|
->then |
235
|
|
|
->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
236
|
|
|
->isIdenticalTo([ |
237
|
|
|
'@id' => '/v1/carts/8', |
238
|
|
|
'status' => 'payed', |
239
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
240
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
241
|
|
|
'cart_items' => [ |
242
|
|
|
[ |
243
|
|
|
'amount' => 2, |
244
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
245
|
|
|
'data' => [ |
246
|
|
|
'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
247
|
|
|
'who' => 'John', |
248
|
|
|
], |
249
|
|
|
'cartItemDetailList' => [ |
250
|
|
|
[ 'name' => 'Bill' ], |
251
|
|
|
[ 'name' => 'Bill', ], |
252
|
|
|
], |
253
|
|
|
], |
254
|
|
|
], |
255
|
|
|
'order' => null, |
256
|
|
|
]) |
257
|
|
|
; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* testJsonEncodeMixRelations |
262
|
|
|
* |
263
|
|
|
* @access public |
264
|
|
|
* @return void |
265
|
|
|
*/ |
266
|
|
|
public function testJsonEncodeMixRelations() |
267
|
|
|
{ |
268
|
|
|
$this->createNewInstance(); |
269
|
|
|
|
270
|
|
|
$this |
271
|
|
|
->given($cart = $this->createCart()) |
272
|
|
|
->and($cartItem = $this->createNewCartItem()) |
273
|
|
|
->and($knownedCartItem = $this->createKnownCartItem()) |
274
|
|
|
->if($cart->addCartItemList($knownedCartItem)) |
275
|
|
|
->and($cart->addCartItemList($cartItem)) |
276
|
|
|
->then |
277
|
|
|
->array($data = $this->testedInstance->serialize( |
278
|
|
|
$cart, |
279
|
|
|
'Mapado\RestClientSdk\Tests\Model\Cart', |
280
|
|
|
[ 'serializeRelations' => ['cart_items'] ] |
281
|
|
|
)) |
282
|
|
|
->isIdenticalTo([ |
283
|
|
|
'@id' => '/v1/carts/8', |
284
|
|
|
'status' => 'payed', |
285
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
286
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
287
|
|
|
'cart_items' => [ |
288
|
|
|
[ |
289
|
|
|
'@id' => '/v1/cart_items/16', |
290
|
|
|
'amount' => 1, |
291
|
|
|
'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339), |
292
|
|
|
'data' => [ |
293
|
|
|
'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339), |
294
|
|
|
'who' => 'Jane', |
295
|
|
|
], |
296
|
|
|
'cart' => '/v1/carts/8', |
297
|
|
|
'product' => '/v1/products/10', |
298
|
|
|
'cartItemDetailList' => [], |
299
|
|
|
], |
300
|
|
|
[ |
301
|
|
|
'amount' => 2, |
302
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
303
|
|
|
'data' => [ |
304
|
|
|
'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
305
|
|
|
'who' => 'John', |
306
|
|
|
], |
307
|
|
|
'product' => '/v1/products/10', |
308
|
|
|
'cartItemDetailList' => [], |
309
|
|
|
], |
310
|
|
|
], |
311
|
|
|
'order' => null, |
312
|
|
|
]) |
313
|
|
|
|
314
|
|
|
// reverse the serialization |
315
|
|
|
->then |
316
|
|
|
->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
317
|
|
|
->array($cart->getCartItemList()) // we can not uneserialize an unlinked entity |
318
|
|
|
->size->isEqualTo(1) |
319
|
|
|
->object($cartItem = current($cart->getCartItemList())) |
320
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem') |
321
|
|
|
->string($cartItem->getId()) |
322
|
|
|
->isEqualTo('/v1/cart_items/16') |
323
|
|
|
; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* testNotAllowedSerialization |
328
|
|
|
* |
329
|
|
|
* @access public |
330
|
|
|
* @return void |
331
|
|
|
*/ |
332
|
|
|
public function testNotAllowedSerialization() |
333
|
|
|
{ |
334
|
|
|
$this->createNewInstance(); |
335
|
|
|
$this |
336
|
|
|
->given($cartItem = $this->createNewCartItem()) |
337
|
|
|
->and($cartItemDetail = $this->createNewCartItemDetail()) |
338
|
|
|
->and($cartItemDetail->setCartItem($cartItem)) |
339
|
|
|
->and($testedInstance = $this->testedInstance) |
340
|
|
|
->then |
341
|
|
|
->object($cartItemDetail->getCartItem()) |
342
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem') |
343
|
|
|
->exception(function () use ($testedInstance, $cartItemDetail) { |
344
|
|
|
$testedInstance->serialize($cartItemDetail, 'Mapado\RestClientSdk\Tests\Model\CartItemDetail'); |
345
|
|
|
}) |
346
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Exception\SdkException') |
347
|
|
|
; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* testMultipleLevelSerialization |
352
|
|
|
* |
353
|
|
|
* @access public |
354
|
|
|
* @return void |
355
|
|
|
*/ |
356
|
|
View Code Duplication |
public function testMultipleLevelSerialization() |
|
|
|
|
357
|
|
|
{ |
358
|
|
|
$this->createNewInstance(); |
359
|
|
|
$this |
360
|
|
|
->given($cart = $this->createNewCart()) |
361
|
|
|
->and($cartItem = $this->createNewCartItem()) |
362
|
|
|
->and($cartItem->setCart($cart)) |
363
|
|
|
->then |
364
|
|
|
->array($this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
365
|
|
|
->isIdenticalTo([ |
366
|
|
|
'status' => 'payed', |
367
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
368
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
369
|
|
|
'cart_items' => [ |
370
|
|
|
[ |
371
|
|
|
'amount' => 2, |
372
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339), |
373
|
|
|
'data' => [ |
374
|
|
|
'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339), |
375
|
|
|
'who' => 'John', |
376
|
|
|
], |
377
|
|
|
'product' => '/v1/products/10', |
378
|
|
|
'cartItemDetailList' => [], |
379
|
|
|
], |
380
|
|
|
], |
381
|
|
|
'order' => null, |
382
|
|
|
]) |
383
|
|
|
|
384
|
|
|
; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* testLinkedUnserialize |
389
|
|
|
* |
390
|
|
|
* @access public |
391
|
|
|
* @return void |
392
|
|
|
*/ |
393
|
|
|
public function testLinkedUnserialize() |
394
|
|
|
{ |
395
|
|
|
$this->createNewInstance(); |
396
|
|
|
$phoneNumberUtil = PhoneNumberUtil::getInstance(); |
397
|
|
|
|
398
|
|
|
$this |
399
|
|
|
->given($data = [ |
400
|
|
|
'@id' => '/v1/carts/8', |
401
|
|
|
'status' => 'payed', |
402
|
|
|
'clientPhoneNumber' => $phoneNumberUtil->parse('+330123456789', PhoneNumberFormat::INTERNATIONAL), |
403
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
404
|
|
|
'cart_items' => [ |
405
|
|
|
[ |
406
|
|
|
'@id' => '/v1/cart_items/16', |
407
|
|
|
'amount' => 2, |
408
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:11:00+00:00'))->format(DateTime::RFC3339), |
409
|
|
|
'data' => [ |
410
|
|
|
'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format(DateTime::RFC3339), |
411
|
|
|
'who' => 'John', |
412
|
|
|
], |
413
|
|
|
'product' => '/v1/products/10', |
414
|
|
|
'cartItemDetailList' => [], |
415
|
|
|
], |
416
|
|
|
], |
417
|
|
|
]) |
418
|
|
|
->then |
419
|
|
|
->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
420
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart') |
421
|
|
|
->object($cart->getClientPhoneNumber()) |
422
|
|
|
->isInstanceOf('libphonenumber\PhoneNumber') |
423
|
|
|
->array($cart->getCartItemList()) |
424
|
|
|
->size->isEqualTo(1) |
425
|
|
|
->object($cartItem = current($cart->getCartItemList())) |
426
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem') |
427
|
|
|
->string($cartItem->getId()) |
428
|
|
|
->isEqualTo('/v1/cart_items/16') |
429
|
|
|
->integer($cartItem->getAmount()) |
430
|
|
|
->isEqualTo(2) |
431
|
|
|
->datetime($cartItem->getCreatedAt()) |
432
|
|
|
->isEqualTo(new \DateTime('2015-09-20T12:11:00+00:00')) |
433
|
|
|
->array($cartItem->getData()) |
434
|
|
|
->isEqualTo([ |
435
|
|
|
'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format(DateTime::RFC3339), |
436
|
|
|
'who' => 'John', |
437
|
|
|
]) |
438
|
|
|
->object($cartItem->getProduct()) |
439
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Product') |
440
|
|
|
->string($cartItem->getProduct()->getId()) |
441
|
|
|
->isEqualTo('/v1/products/10') |
442
|
|
|
->array($cartItem->getCartItemDetailList()) |
443
|
|
|
->isEmpty() |
444
|
|
|
; |
445
|
|
|
|
446
|
|
|
$this->createNewInstance(); |
447
|
|
|
$this |
448
|
|
|
->given($data = [ |
449
|
|
|
'@id' => '/v1/cart_items/16', |
450
|
|
|
'amount' => 2, |
451
|
|
|
'cart' => [ |
452
|
|
|
'@id' => '/v1/carts/10', |
453
|
|
|
'status' => 'waiting', |
454
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
455
|
|
|
], |
456
|
|
|
]) |
457
|
|
|
|
458
|
|
|
->then |
459
|
|
|
->object($cartItem = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\CartItem')) |
460
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem') |
461
|
|
|
->object($cart = $cartItem->getCart()) |
462
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart') |
463
|
|
|
->string($cart->getClientPhoneNumber()) |
464
|
|
|
->isEqualTo('+33 1 23 45 67 89') |
465
|
|
|
->string($cart->getId()) |
466
|
|
|
->isEqualTo('/v1/carts/10') |
467
|
|
|
->string($cart->getStatus()) |
468
|
|
|
->isEqualTo('waiting') |
469
|
|
|
; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
public function testSerializeNullValues() |
473
|
|
|
{ |
474
|
|
|
$this->createNewInstance(); |
475
|
|
|
$this |
476
|
|
|
->given($cart = $this->createNewCart()) |
477
|
|
|
->and($cart->setStatus(null)) |
478
|
|
|
->and($cart->setOrder(null)) |
479
|
|
|
->then |
480
|
|
|
->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
481
|
|
|
->isIdenticalTo([ |
482
|
|
|
'status' => null, |
483
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
484
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
485
|
|
|
'cart_items' => [], |
486
|
|
|
'order' => null, |
487
|
|
|
]) |
488
|
|
|
; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
public function testSerializingAttributeNameDiffThanPropertyName() |
492
|
|
|
{ |
493
|
|
|
$this->createNewInstance(); |
494
|
|
|
$this |
495
|
|
|
->given($product = $this->createNewProduct()) |
496
|
|
|
->then |
497
|
|
|
->array($data = $this->testedInstance->serialize($product, 'Mapado\RestClientSdk\Tests\Model\Product')) |
498
|
|
|
->isIdenticalTo([ |
499
|
|
|
'product_value' => 8.2, |
500
|
|
|
'currency' => 'eur', |
501
|
|
|
]) |
502
|
|
|
; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
public function testWeirdIdentifier() |
506
|
|
|
{ |
507
|
|
|
$this->createNewInstance($this->getMapping('weirdId')); |
508
|
|
|
|
509
|
|
|
$this |
510
|
|
|
->given($cart = $this->createCart()) |
511
|
|
|
->and($cartItem = $this->createKnownCartItem()) |
512
|
|
|
->and($cart->addCartItemList($cartItem)) |
513
|
|
|
|
514
|
|
|
->then |
515
|
|
|
->array($data = $this->testedInstance->serialize( |
516
|
|
|
$cart, |
517
|
|
|
'Mapado\RestClientSdk\Tests\Model\Cart', |
518
|
|
|
[ 'serializeRelations' => ['cart_items'] ] |
519
|
|
|
)) |
520
|
|
|
->isIdenticalTo([ |
521
|
|
|
'weirdId' => '/v1/carts/8', |
522
|
|
|
'status' => 'payed', |
523
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
524
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
525
|
|
|
'cart_items' => [ |
526
|
|
|
[ |
527
|
|
|
'weirdId' => '/v1/cart_items/16', |
528
|
|
|
'amount' => 1, |
529
|
|
|
'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339), |
530
|
|
|
'data' => [ |
531
|
|
|
'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339), |
532
|
|
|
'who' => 'Jane', |
533
|
|
|
], |
534
|
|
|
'cart' => '/v1/carts/8', |
535
|
|
|
'product' => '/v1/products/10', |
536
|
|
|
'cartItemDetailList' => [], |
537
|
|
|
], |
538
|
|
|
], |
539
|
|
|
'order' => null, |
540
|
|
|
]) |
541
|
|
|
|
542
|
|
|
->then |
543
|
|
|
->array($data = $this->testedInstance->serialize( |
544
|
|
|
$cart, |
545
|
|
|
'Mapado\RestClientSdk\Tests\Model\Cart', |
546
|
|
|
[ 'serializeRelations' => ['cart_items'] ] |
547
|
|
|
)) |
548
|
|
|
->isIdenticalTo([ |
549
|
|
|
'weirdId' => '/v1/carts/8', |
550
|
|
|
'status' => 'payed', |
551
|
|
|
'clientPhoneNumber' => '+33 1 23 45 67 89', |
552
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
553
|
|
|
'cart_items' => [ |
554
|
|
|
[ |
555
|
|
|
'weirdId' => '/v1/cart_items/16', |
556
|
|
|
'amount' => 1, |
557
|
|
|
'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339), |
558
|
|
|
'data' => [ |
559
|
|
|
'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339), |
560
|
|
|
'who' => 'Jane', |
561
|
|
|
], |
562
|
|
|
'cart' => '/v1/carts/8', |
563
|
|
|
'product' => '/v1/products/10', |
564
|
|
|
'cartItemDetailList' => [], |
565
|
|
|
], |
566
|
|
|
], |
567
|
|
|
'order' => null, |
568
|
|
|
]) |
569
|
|
|
|
570
|
|
|
// reverse the serialization |
571
|
|
|
->then |
572
|
|
|
->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
573
|
|
|
->string($cart->getId()) |
574
|
|
|
->isEqualTo('/v1/carts/8') |
575
|
|
|
->array($cart->getCartItemList()) |
576
|
|
|
->size->isEqualTo(1) |
577
|
|
|
// ->object($cartItem = current($cart->getCartItemList())) |
|
|
|
|
578
|
|
|
// ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem') |
|
|
|
|
579
|
|
|
// ->string($cartItem->getId()) |
|
|
|
|
580
|
|
|
// ->isEqualTo('/v1/cart_items/16') |
|
|
|
|
581
|
|
|
; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
public function testDeserializeWithExtraFields() |
585
|
|
|
{ |
586
|
|
|
$this->createNewInstance(); |
587
|
|
|
|
588
|
|
|
$this |
589
|
|
|
->given($data = [ |
590
|
|
|
'@foo' => 'bar', |
591
|
|
|
'@id' => '/v1/carts/8', |
592
|
|
|
'status' => 'payed', |
593
|
|
|
"clientPhoneNumber" => '+33 1 23 45 67 89', |
594
|
|
|
'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339), |
595
|
|
|
'cart_items' => [], |
596
|
|
|
'order' => null, |
597
|
|
|
]) |
598
|
|
|
|
599
|
|
|
->then |
600
|
|
|
->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart')) |
601
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart') |
602
|
|
|
; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
public function testSerializingIriManyToOne() |
606
|
|
|
{ |
607
|
|
|
$annotationDriver = new AnnotationDriver(__DIR__ . '/../../cache/'); |
608
|
|
|
$mapping = new Mapping(); |
609
|
|
|
$mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../../Model/Issue46/')); |
610
|
|
|
|
611
|
|
|
$section = new Issue46\Section(); |
612
|
|
|
$section->setId(46); |
613
|
|
|
$section->setIri('/sections/46'); |
614
|
|
|
$section->setTitle('section title'); |
615
|
|
|
|
616
|
|
|
$article = new Issue46\Article(); |
617
|
|
|
$article->setSection($section); |
618
|
|
|
|
619
|
|
|
$this->createNewInstance($mapping); |
620
|
|
|
|
621
|
|
|
$this |
622
|
|
|
->then |
623
|
|
|
->array($this->testedInstance->serialize($article, 'Mapado\RestClientSdk\Tests\Model\Issue46\Article')) |
624
|
|
|
->isIdenticalTo([ |
625
|
|
|
'id' => null, |
626
|
|
|
'section' => '/sections/46', |
627
|
|
|
]) |
628
|
|
|
|
629
|
|
|
->if($article->setIri('/articles/44')) |
630
|
|
|
|
631
|
|
|
->array($this->testedInstance |
632
|
|
|
->serialize( |
633
|
|
|
$section, |
634
|
|
|
'Mapado\RestClientSdk\Tests\Model\Issue46\Section' |
635
|
|
|
)) |
636
|
|
|
->isIdenticalTo([ |
637
|
|
|
'@id' => '/sections/46', |
638
|
|
|
'id' => 46, |
639
|
|
|
'title' => 'section title', |
640
|
|
|
'articleList' => [ |
641
|
|
|
'/articles/44', |
642
|
|
|
], |
643
|
|
|
]) |
644
|
|
|
|
645
|
|
|
->array($this->testedInstance |
646
|
|
|
->serialize( |
647
|
|
|
$section, |
648
|
|
|
'Mapado\RestClientSdk\Tests\Model\Issue46\Section', |
649
|
|
|
[ 'serializeRelations' => ['articleList'] ] |
650
|
|
|
)) |
651
|
|
|
->isIdenticalTo([ |
652
|
|
|
'@id' => '/sections/46', |
653
|
|
|
'id' => 46, |
654
|
|
|
'title' => 'section title', |
655
|
|
|
'articleList' => [ |
656
|
|
|
[ |
657
|
|
|
'@id' => '/articles/44', |
658
|
|
|
'id' => null, |
659
|
|
|
'section' => '/sections/46', |
660
|
|
|
] |
661
|
|
|
], |
662
|
|
|
]) |
663
|
|
|
; |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
/** |
667
|
|
|
* getMapping |
668
|
|
|
* |
669
|
|
|
* @access private |
670
|
|
|
* @return Mapping |
671
|
|
|
*/ |
672
|
|
|
private function getMapping($idKey = '@id') |
673
|
|
|
{ |
674
|
|
|
$mapping = new Mapping('/v1'); |
675
|
|
|
$mapping->setMapping([ |
676
|
|
|
$this->getCartMetadata($idKey), |
677
|
|
|
$this->getCartItemMetadata($idKey), |
678
|
|
|
$this->getCartItemDetailMetadata($idKey), |
679
|
|
|
$this->getProductMetadata($idKey), |
680
|
|
|
]); |
681
|
|
|
|
682
|
|
|
return $mapping; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
private function getProductMetadata($idKey) |
686
|
|
|
{ |
687
|
|
|
$productMetadata = new ClassMetadata( |
688
|
|
|
'products', |
689
|
|
|
'Mapado\RestClientSdk\Tests\Model\Product', |
690
|
|
|
'' |
691
|
|
|
); |
692
|
|
|
|
693
|
|
|
$productMetadata->setAttributeList([ |
694
|
|
|
new Attribute($idKey, 'id', 'string', true), |
695
|
|
|
new Attribute('product_value', 'value'), |
696
|
|
|
new Attribute('currency'), |
697
|
|
|
]); |
698
|
|
|
|
699
|
|
|
return $productMetadata; |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
private function getCartItemDetailMetadata($idKey) |
703
|
|
|
{ |
704
|
|
|
$cartItemDetailMetadata = new ClassMetadata( |
705
|
|
|
'cart_item_details', |
706
|
|
|
'Mapado\RestClientSdk\Tests\Model\CartItemDetail', |
707
|
|
|
'' |
708
|
|
|
); |
709
|
|
|
|
710
|
|
|
$cartItemDetailMetadata->setRelationList([ |
711
|
|
|
new Relation('cartItem', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\CartItem'), |
712
|
|
|
]); |
713
|
|
|
$cartItemDetailMetadata->setAttributeList([ |
714
|
|
|
new Attribute($idKey, 'id', 'string', true), |
715
|
|
|
new Attribute('name'), |
716
|
|
|
new Attribute('cartItem'), |
717
|
|
|
]); |
718
|
|
|
|
719
|
|
|
return $cartItemDetailMetadata; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
private function getCartItemMetadata($idKey) |
723
|
|
|
{ |
724
|
|
|
$cartItemMetadata = new ClassMetadata( |
725
|
|
|
'cart_items', |
726
|
|
|
'Mapado\RestClientSdk\Tests\Model\CartItem', |
727
|
|
|
'' |
728
|
|
|
); |
729
|
|
|
|
730
|
|
|
$cartItemMetadata->setRelationList([ |
731
|
|
|
new Relation('cart', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\Cart'), |
732
|
|
|
new Relation('product', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\Product'), |
733
|
|
|
new Relation('cartItemDetailList', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\CartItemDetail'), |
734
|
|
|
]); |
735
|
|
|
$cartItemMetadata->setAttributeList([ |
736
|
|
|
new Attribute($idKey, 'id', 'string', true), |
737
|
|
|
new Attribute('amount'), |
738
|
|
|
new Attribute('createdAt', 'createdAt', 'datetime'), |
739
|
|
|
new Attribute('data'), |
740
|
|
|
new Attribute('cart'), |
741
|
|
|
new Attribute('product'), |
742
|
|
|
new Attribute('cartItemDetailList'), |
743
|
|
|
]); |
744
|
|
|
|
745
|
|
|
return $cartItemMetadata; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
private function getCartMetadata($idKey) |
749
|
|
|
{ |
750
|
|
|
$cartMetadata = new ClassMetadata( |
751
|
|
|
'carts', |
752
|
|
|
'Mapado\RestClientSdk\Tests\Model\Cart', |
753
|
|
|
'' |
754
|
|
|
); |
755
|
|
|
$cartMetadata->setAttributeList([ |
756
|
|
|
new Attribute($idKey, 'id', 'string', true), |
757
|
|
|
new Attribute('status'), |
758
|
|
|
new Attribute('clientPhoneNumber', 'clientPhoneNumber', 'phone_number'), |
759
|
|
|
new Attribute('createdAt', 'createdAt', 'datetime'), |
760
|
|
|
new Attribute('cart_items', 'cartItemList'), |
761
|
|
|
new Attribute('order'), |
762
|
|
|
]); |
763
|
|
|
$cartMetadata->setRelationList([ |
764
|
|
|
new Relation('cart_items', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\CartItem'), |
765
|
|
|
new Relation('order', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\Order'), |
766
|
|
|
]); |
767
|
|
|
|
768
|
|
|
return $cartMetadata; |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* createNewCart |
773
|
|
|
* |
774
|
|
|
* @access private |
775
|
|
|
* @return AbstractModel |
776
|
|
|
*/ |
777
|
|
|
private function createNewCart() |
778
|
|
|
{ |
779
|
|
|
$cart = new \Mapado\RestClientSdk\Tests\Model\Cart(); |
780
|
|
|
$cart->setStatus('payed'); |
781
|
|
|
$cart->setCreatedAt(new DateTime('2015-09-20 12:08:00')); |
782
|
|
|
|
783
|
|
|
$phoneNumberUtil = PhoneNumberUtil::getInstance(); |
784
|
|
|
$clientPhoneNumber = $phoneNumberUtil->parse('+33123456789', PhoneNumberFormat::INTERNATIONAL); |
785
|
|
|
$cart->setClientPhoneNumber($clientPhoneNumber); |
786
|
|
|
|
787
|
|
|
return $cart; |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
/** |
791
|
|
|
* createCart |
792
|
|
|
* |
793
|
|
|
* @access private |
794
|
|
|
* @return void |
795
|
|
|
*/ |
796
|
|
|
private function createCart() |
797
|
|
|
{ |
798
|
|
|
$cart = $this->createNewCart(); |
799
|
|
|
$cart->setId('/v1/carts/8'); |
800
|
|
|
|
801
|
|
|
return $cart; |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* createKnownCartItem |
806
|
|
|
* |
807
|
|
|
* @access private |
808
|
|
|
* @return AbstractModel |
809
|
|
|
*/ |
810
|
|
|
private function createKnownCartItem() |
811
|
|
|
{ |
812
|
|
|
$cartItem = $this->createNewCartItem(); |
813
|
|
|
$cartItem->setId('/v1/cart_items/16'); |
814
|
|
|
$cartItem->setAmount(1); |
815
|
|
|
$cartItem->setCreatedAt(new DateTime('2015-11-04 15:13:00')); |
816
|
|
|
$cartItem->setData([ |
817
|
|
|
'when' => new DateTime('2015-11-04 15:00:00'), |
818
|
|
|
'who' => 'Jane', |
819
|
|
|
]); |
820
|
|
|
$cartItem->setCart($this->createCart()); |
821
|
|
|
|
822
|
|
|
return $cartItem; |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
/** |
826
|
|
|
* createNewCartItem |
827
|
|
|
* |
828
|
|
|
* @access private |
829
|
|
|
* @return AbstractModel |
830
|
|
|
*/ |
831
|
|
|
private function createNewCartItem($addKnownedProduct = true) |
832
|
|
|
{ |
833
|
|
|
$cartItem = new \Mapado\RestClientSdk\Tests\Model\CartItem(); |
834
|
|
|
$cartItem->setAmount(2); |
835
|
|
|
$cartItem->setCreatedAt(new DateTime('2015-09-20 12:11:00')); |
836
|
|
|
$cartItem->setData([ |
837
|
|
|
'when' => new DateTime('2015-09-20 15:00:00'), |
838
|
|
|
'who' => 'John', |
839
|
|
|
]); |
840
|
|
|
|
841
|
|
|
if ($addKnownedProduct) { |
842
|
|
|
$cartItem->setProduct($this->createKnownedProduct()); |
843
|
|
|
} |
844
|
|
|
|
845
|
|
|
return $cartItem; |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* createNewProduct |
850
|
|
|
* |
851
|
|
|
* @access private |
852
|
|
|
* @return AbstractModel |
853
|
|
|
*/ |
854
|
|
|
private function createNewProduct() |
855
|
|
|
{ |
856
|
|
|
$product = new \Mapado\RestClientSdk\Tests\Model\Product(); |
857
|
|
|
|
858
|
|
|
$product->setValue(8.2); |
859
|
|
|
$product->setCurrency('eur'); |
860
|
|
|
|
861
|
|
|
return $product; |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
|
865
|
|
|
/** |
866
|
|
|
* createKnownedProduct |
867
|
|
|
* |
868
|
|
|
* @access private |
869
|
|
|
* @return AbstractModel |
870
|
|
|
*/ |
871
|
|
|
private function createKnownedProduct() |
872
|
|
|
{ |
873
|
|
|
$product = $this->createNewProduct(); |
874
|
|
|
$product->setId('/v1/products/10'); |
875
|
|
|
|
876
|
|
|
return $product; |
877
|
|
|
} |
878
|
|
|
|
879
|
|
|
private function createNewCartItemDetail() |
880
|
|
|
{ |
881
|
|
|
$item = new \Mapado\RestClientSdk\Tests\Model\CartItemDetail(); |
882
|
|
|
|
883
|
|
|
$item->setName('Bill'); |
884
|
|
|
|
885
|
|
|
return $item; |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
/** |
889
|
|
|
* createNewInstance |
890
|
|
|
* |
891
|
|
|
* @access private |
892
|
|
|
* @return void |
893
|
|
|
*/ |
894
|
|
|
private function createNewInstance($mapping = null) |
895
|
|
|
{ |
896
|
|
|
$mapping = $mapping ?: $this->getMapping(); |
897
|
|
|
$this->newTestedInstance($mapping); |
898
|
|
|
|
899
|
|
|
$this->mockGenerator->orphanize('__construct'); |
900
|
|
|
$this->mockGenerator->shuntParentClassCalls(); |
901
|
|
|
$restClient = new \mock\Mapado\RestClientSdk\RestClient(); |
902
|
|
|
$this->mockGenerator->unshuntParentClassCalls(); |
903
|
|
|
$sdk = new \mock\Mapado\RestClientSdk\SdkClient($restClient, $mapping, $this->testedInstance); |
904
|
|
|
$sdk->setFileCachePath(__DIR__ . '/../../cache/'); |
905
|
|
|
|
906
|
|
|
$cartRepositoryMock = $this->getCartRepositoryMock($sdk, $restClient, 'Mapado\RestClientSdk\Tests\Model\Cart'); |
907
|
|
|
|
908
|
|
|
$this->calling($sdk)->getRepository = function ($modelName) use ($cartRepositoryMock) { |
909
|
|
|
switch ($modelName) { |
910
|
|
|
case 'Mapado\RestClientSdk\Tests\Model\Cart': |
911
|
|
|
return $cartRepositoryMock; |
912
|
|
|
default: |
913
|
|
|
return null; |
914
|
|
|
} |
915
|
|
|
}; |
916
|
|
|
|
917
|
|
|
$this->testedInstance->setSdk($sdk); |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
private function getCartRepositoryMock($sdk, $restClient, $modelName) |
921
|
|
|
{ |
922
|
|
|
$repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
923
|
|
|
$sdk, |
924
|
|
|
$restClient, |
925
|
|
|
$modelName |
926
|
|
|
); |
927
|
|
|
|
928
|
|
|
$_this = $this; |
929
|
|
|
|
930
|
|
|
$this->calling($repository)->find = function ($id) use ($_this) { |
|
|
|
|
931
|
|
|
return $_this->createCart(); |
932
|
|
|
}; |
933
|
|
|
|
934
|
|
|
return $repository; |
935
|
|
|
} |
936
|
|
|
} |
937
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.