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