Completed
Push — master ( 6ada9f...ea3100 )
by Julien
06:56
created

Serializer::testDeserializeWithExtraFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Tests\Units\Model;
4
5
use atoum;
6
use DateTime;
7
use libphonenumber\PhoneNumberUtil;
8
use libphonenumber\PhoneNumberFormat;
9
use Mapado\RestClientSdk\Mapping;
10
use Mapado\RestClientSdk\Mapping\Attribute;
11
use Mapado\RestClientSdk\Mapping\ClassMetadata;
12
use Mapado\RestClientSdk\Mapping\Relation;
13
14
/**
15
 * Class Serializer
16
 * @author Julien Deniau <[email protected]>
17
 */
18
class Serializer extends atoum
19
{
20
    /**
21
     * testJsonEncode
22
     *
23
     * @access public
24
     * @return void
25
     */
26
    public function testJsonEncode()
27
    {
28
        $this->createNewInstance();
29
30
        $this
31
            ->given($cart = $this->createCart())
32
            ->then
33
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart'))
34
                    ->isIdenticalTo([
35
                        '@id' => '/v1/carts/8',
36
                        'status' => 'payed',
37
                        "clientPhoneNumber" => '+33 1 23 45 67 89',
38
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
39
                        'cart_items' => [],
40
                        'order' => null,
41
                    ])
42
43
            // reverse the serialization
44
            ->then
45
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart'))
46
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart')
47
                ->string($cart->getId())
48
                    ->isEqualTo('/v1/carts/8')
49
                ->string($cart->getStatus())
50
                    ->isEqualTo('payed')
51
                ->datetime($cart->getCreatedAt())
52
                    ->isEqualTo(new \DateTime('2015-09-20T12:08:00'))
53
                ->array($cart->getCartItemList())
54
                    ->isEmpty()
55
56
        ;
57
    }
58
59
    /**
60
     * testJsonEncodeRelation
61
     *
62
     * @access public
63
     * @return void
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\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\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\Cart'))
133
                ->array($cart->getCartItemList())
134
                    ->size->isEqualTo(1)
135
                ->object($cartItem = current($cart->getCartItemList()))
136
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem')
137
                ->string($cartItem->getId())
138
                    ->isEqualTo('/v1/cart_items/16')
139
            ;
140
    }
141
142
    /**
143
     * testJsonEncodeRelationWithoutLink
144
     *
145
     * @access public
146
     * @return void
147
     */
148
    public function testJsonEncodeRelationWithoutLink()
149
    {
150
        $this->createNewInstance();
151
152
        $this
153
            ->given($cart = $this->createCart())
154
                ->and($cartItem = $this->createNewCartItem())
155
                ->and($cart->addCartItemList($cartItem))
156
            ->then
157
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart'))
158
                    ->isIdenticalTo([
159
                        '@id' => '/v1/carts/8',
160
                        'status' => 'payed',
161
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
162
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
163
                        'cart_items' => [
164
                            [
165
                                'amount' => 2,
166
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
167
                                'data' => [
168
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
169
                                    'who' => 'John',
170
                                ],
171
                                'product' => '/v1/products/10',
172
                                'cartItemDetailList' => [],
173
                            ],
174
                        ],
175
                        'order' => null,
176
                    ])
177
178
            // reverse the serialization
179
            ->then
180
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart'))
181
                ->array($cart->getCartItemList()) // we can not uneserialize an unlinked entity
182
                    ->isEmpty()
183
        ;
184
    }
185
186 View Code Duplication
    public function testSerializeThreeLevel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
187
    {
188
        $this->createNewInstance();
189
190
        $this
191
            ->given($cart = $this->createNewCart())
192
                ->and($cartItem = $this->createNewCartItem())
193
                ->and($cart->addCartItemList($cartItem))
194
            ->then
195
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart'))
196
                    ->isIdenticalTo([
197
                        'status' => 'payed',
198
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
199
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
200
                        'cart_items' => [
201
                            [
202
                                'amount' => 2,
203
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
204
                                'data' => [
205
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
206
                                    'who' => 'John',
207
                                ],
208
                                'product' => '/v1/products/10',
209
                                'cartItemDetailList' => [],
210
                            ],
211
                        ],
212
                        'order' => null,
213
                    ])
214
        ;
215
    }
216
217
    /**
218
     * testJsonEncodeRelationWithoutLinkMultipleLevel
219
     *
220
     * @access public
221
     * @return void
222
     */
223
    public function testJsonEncodeRelationWithoutLinkMultipleLevel()
224
    {
225
        $this->createNewInstance();
226
        $this
227
            ->given($cart = $this->createCart())
228
                ->and($cartItem = $this->createNewCartItem(false))
229
                ->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail()))
230
                ->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail()))
231
            ->if($cart->addCartItemList($cartItem))
232
            ->then
233
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart'))
234
                    ->isIdenticalTo([
235
                        '@id' => '/v1/carts/8',
236
                        'status' => 'payed',
237
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
238
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
239
                        'cart_items' => [
240
                            [
241
                                'amount' => 2,
242
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
243
                                'data' => [
244
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
245
                                    'who' => 'John',
246
                                ],
247
                                'cartItemDetailList' => [
248
                                    [ 'name' => 'Bill' ],
249
                                    [ 'name' => 'Bill', ],
250
                                ],
251
                            ],
252
                        ],
253
                        'order' => null,
254
                    ])
255
        ;
256
    }
257
258
    /**
259
     * testJsonEncodeMixRelations
260
     *
261
     * @access public
262
     * @return void
263
     */
264
    public function testJsonEncodeMixRelations()
265
    {
266
        $this->createNewInstance();
267
268
        $this
269
            ->given($cart = $this->createCart())
270
                ->and($cartItem = $this->createNewCartItem())
271
                ->and($knownedCartItem = $this->createKnownCartItem())
272
            ->if($cart->addCartItemList($knownedCartItem))
273
                ->and($cart->addCartItemList($cartItem))
274
            ->then
275
                ->array($data = $this->testedInstance->serialize(
276
                    $cart,
277
                    'Mapado\RestClientSdk\Tests\Model\Cart',
278
                    [ 'serializeRelations' => ['cart_items'] ]
279
                ))
280
                    ->isIdenticalTo([
281
                        '@id' => '/v1/carts/8',
282
                        'status' => 'payed',
283
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
284
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
285
                        'cart_items' => [
286
                            [
287
                                '@id' => '/v1/cart_items/16',
288
                                'amount' => 1,
289
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
290
                                'data' => [
291
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
292
                                    'who' => 'Jane',
293
                                ],
294
                                'cart' => '/v1/carts/8',
295
                                'product' => '/v1/products/10',
296
                                'cartItemDetailList' => [],
297
                            ],
298
                            [
299
                                'amount' => 2,
300
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
301
                                'data' => [
302
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
303
                                    'who' => 'John',
304
                                ],
305
                                'product' => '/v1/products/10',
306
                                'cartItemDetailList' => [],
307
                            ],
308
                        ],
309
                        'order' => null,
310
                    ])
311
312
            // reverse the serialization
313
            ->then
314
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart'))
315
                ->array($cart->getCartItemList()) // we can not uneserialize an unlinked entity
316
                    ->size->isEqualTo(1)
317
                ->object($cartItem = current($cart->getCartItemList()))
318
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem')
319
                ->string($cartItem->getId())
320
                    ->isEqualTo('/v1/cart_items/16')
321
        ;
322
    }
323
324
    /**
325
     * testNotAllowedSerialization
326
     *
327
     * @access public
328
     * @return void
329
     */
330
    public function testNotAllowedSerialization()
331
    {
332
        $this->createNewInstance();
333
        $this
334
            ->given($cartItem = $this->createNewCartItem())
335
                ->and($cartItemDetail = $this->createNewCartItemDetail())
336
                ->and($cartItemDetail->setCartItem($cartItem))
337
                ->and($testedInstance = $this->testedInstance)
338
            ->then
339
                ->object($cartItemDetail->getCartItem())
340
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem')
341
                ->exception(function () use ($testedInstance, $cartItemDetail) {
342
                    $testedInstance->serialize($cartItemDetail, 'Mapado\RestClientSdk\Tests\Model\CartItemDetail');
343
                })
344
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\SdkException')
345
        ;
346
    }
347
348
    /**
349
     * testMultipleLevelSerialization
350
     *
351
     * @access public
352
     * @return void
353
     */
354 View Code Duplication
    public function testMultipleLevelSerialization()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
355
    {
356
        $this->createNewInstance();
357
        $this
358
            ->given($cart = $this->createNewCart())
359
                ->and($cartItem = $this->createNewCartItem())
360
                ->and($cartItem->setCart($cart))
361
            ->then
362
                ->array($this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart'))
363
                    ->isIdenticalTo([
364
                        'status' => 'payed',
365
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
366
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
367
                        'cart_items' => [
368
                            [
369
                                'amount' => 2,
370
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
371
                                'data' => [
372
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
373
                                    'who' => 'John',
374
                                ],
375
                                'product' => '/v1/products/10',
376
                                'cartItemDetailList' => [],
377
                            ],
378
                        ],
379
                        'order' => null,
380
                    ])
381
382
        ;
383
    }
384
385
    /**
386
     * testLinkedUnserialize
387
     *
388
     * @access public
389
     * @return void
390
     */
391
    public function testLinkedUnserialize()
392
    {
393
        $this->createNewInstance();
394
        $phoneNumberUtil = PhoneNumberUtil::getInstance();
395
396
        $this
397
            ->given($data = [
398
                    '@id' => '/v1/carts/8',
399
                    'status' => 'payed',
400
                    'clientPhoneNumber' => $phoneNumberUtil->parse('+330123456789', PhoneNumberFormat::INTERNATIONAL),
401
                    'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
402
                    'cart_items' => [
403
                        [
404
                            '@id' => '/v1/cart_items/16',
405
                            'amount' => 2,
406
                            'createdAt' => (new \DateTime('2015-09-20T12:11:00+00:00'))->format(DateTime::RFC3339),
407
                            'data' => [
408
                                'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format(DateTime::RFC3339),
409
                                'who' => 'John',
410
                            ],
411
                            'product' => '/v1/products/10',
412
                            'cartItemDetailList' => [],
413
                        ],
414
                    ],
415
                ])
416
            ->then
417
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart'))
418
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart')
419
                ->object($cart->getClientPhoneNumber())
420
                    ->isInstanceOf('libphonenumber\PhoneNumber')
421
                ->array($cart->getCartItemList())
422
                    ->size->isEqualTo(1)
423
                ->object($cartItem = current($cart->getCartItemList()))
424
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem')
425
                ->string($cartItem->getId())
426
                    ->isEqualTo('/v1/cart_items/16')
427
                ->integer($cartItem->getAmount())
428
                    ->isEqualTo(2)
429
                ->datetime($cartItem->getCreatedAt())
430
                    ->isEqualTo(new \DateTime('2015-09-20T12:11:00+00:00'))
431
                ->array($cartItem->getData())
432
                    ->isEqualTo([
433
                        'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format(DateTime::RFC3339),
434
                        'who' => 'John',
435
                    ])
436
                ->object($cartItem->getProduct())
437
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Product')
438
                ->string($cartItem->getProduct()->getId())
439
                    ->isEqualTo('/v1/products/10')
440
                ->array($cartItem->getCartItemDetailList())
441
                    ->isEmpty()
442
        ;
443
444
        $this->createNewInstance();
445
        $this
446
            ->given($data = [
447
                    '@id' => '/v1/cart_items/16',
448
                    'amount' => 2,
449
                    'cart' => [
450
                        '@id' => '/v1/carts/10',
451
                        'status' => 'waiting',
452
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
453
                    ],
454
                ])
455
456
            ->then
457
                ->object($cartItem = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\CartItem'))
458
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem')
459
                ->object($cart = $cartItem->getCart())
460
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart')
461
                ->string($cart->getClientPhoneNumber())
462
                    ->isEqualTo('+33 1 23 45 67 89')
463
                ->string($cart->getId())
464
                    ->isEqualTo('/v1/carts/10')
465
                ->string($cart->getStatus())
466
                    ->isEqualTo('waiting')
467
        ;
468
    }
469
470
    public function testSerializeNullValues()
471
    {
472
        $this->createNewInstance();
473
        $this
474
            ->given($cart = $this->createNewCart())
475
                ->and($cart->setStatus(null))
476
                ->and($cart->setOrder(null))
477
            ->then
478
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\Cart'))
479
                    ->isIdenticalTo([
480
                        'status' => null,
481
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
482
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
483
                        'cart_items' => [],
484
                        'order' => null,
485
                    ])
486
        ;
487
    }
488
489
    public function testSerializingAttributeNameDiffThanPropertyName()
490
    {
491
        $this->createNewInstance();
492
        $this
493
            ->given($product = $this->createNewProduct())
494
            ->then
495
                ->array($data = $this->testedInstance->serialize($product, 'Mapado\RestClientSdk\Tests\Model\Product'))
496
                ->isIdenticalTo([
497
                    'product_value' => 8.2,
498
                    'currency' => 'eur',
499
                ])
500
        ;
501
    }
502
503
    public function testWeirdIdentifier()
504
    {
505
        $this->createNewInstance($this->getMapping('weirdId'));
506
507
        $this
508
            ->given($cart = $this->createCart())
509
                ->and($cartItem = $this->createKnownCartItem())
510
                ->and($cart->addCartItemList($cartItem))
511
512
            ->then
513
                ->array($data = $this->testedInstance->serialize(
514
                    $cart,
515
                    'Mapado\RestClientSdk\Tests\Model\Cart',
516
                    [ 'serializeRelations' => ['cart_items'] ]
517
                ))
518
                    ->isIdenticalTo([
519
                        'weirdId' => '/v1/carts/8',
520
                        'status' => 'payed',
521
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
522
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
523
                        'cart_items' => [
524
                            [
525
                                'weirdId' => '/v1/cart_items/16',
526
                                'amount' => 1,
527
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
528
                                'data' => [
529
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
530
                                    'who' => 'Jane',
531
                                ],
532
                                'cart' => '/v1/carts/8',
533
                                'product' => '/v1/products/10',
534
                                'cartItemDetailList' => [],
535
                            ],
536
                        ],
537
                        'order' => null,
538
                    ])
539
540
            ->then
541
                ->array($data = $this->testedInstance->serialize(
542
                    $cart,
543
                    'Mapado\RestClientSdk\Tests\Model\Cart',
544
                    [ 'serializeRelations' => ['cart_items'] ]
545
                ))
546
                    ->isIdenticalTo([
547
                        'weirdId' => '/v1/carts/8',
548
                        'status' => 'payed',
549
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
550
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
551
                        'cart_items' => [
552
                            [
553
                                'weirdId' => '/v1/cart_items/16',
554
                                'amount' => 1,
555
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
556
                                'data' => [
557
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
558
                                    'who' => 'Jane',
559
                                ],
560
                                'cart' => '/v1/carts/8',
561
                                'product' => '/v1/products/10',
562
                                'cartItemDetailList' => [],
563
                            ],
564
                        ],
565
                        'order' => null,
566
                    ])
567
568
            // reverse the serialization
569
            ->then
570
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart'))
571
                ->string($cart->getId())
572
                    ->isEqualTo('/v1/carts/8')
573
                ->array($cart->getCartItemList())
574
                    ->size->isEqualTo(1)
575
                // ->object($cartItem = current($cart->getCartItemList()))
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
576
                //     ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\CartItem')
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
577
                // ->string($cartItem->getId())
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
578
                //     ->isEqualTo('/v1/cart_items/16')
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
579
            ;
580
    }
581
582
    public function testDeserializeWithExtraFields()
583
    {
584
        $this->createNewInstance();
585
586
        $this
587
            ->given($data = [
588
                '@foo' => 'bar',
589
                '@id' => '/v1/carts/8',
590
                'status' => 'payed',
591
                "clientPhoneNumber" => '+33 1 23 45 67 89',
592
                'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
593
                'cart_items' => [],
594
                'order' => null,
595
            ])
596
597
            ->then
598
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\Cart'))
599
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Cart')
600
        ;
601
    }
602
603
    /**
604
     * getMapping
605
     *
606
     * @access private
607
     * @return Mapping
608
     */
609
    private function getMapping($idKey = '@id')
610
    {
611
        $cartMetadata = new ClassMetadata(
612
            'carts',
613
            'Mapado\RestClientSdk\Tests\Model\Cart',
614
            ''
615
        );
616
        $cartMetadata->setAttributeList([
617
            new Attribute($idKey, 'id', 'string', true),
618
            new Attribute('status'),
619
            new Attribute('clientPhoneNumber', 'clientPhoneNumber', 'phone_number'),
620
            new Attribute('createdAt', 'createdAt', 'datetime'),
621
            new Attribute('cart_items', 'cartItemList'),
622
            new Attribute('order'),
623
        ]);
624
        $cartMetadata->setRelationList([
625
            new Relation('cart_items', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\CartItem'),
626
            new Relation('order', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\Order'),
627
        ]);
628
629
        $cartItemMetadata = new ClassMetadata(
630
            'cart_items',
631
            'Mapado\RestClientSdk\Tests\Model\CartItem',
632
            ''
633
        );
634
635
        $cartItemMetadata->setRelationList([
636
            new Relation('cart', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\Cart'),
637
            new Relation('product', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\Product'),
638
            new Relation('cartItemDetailList', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\CartItemDetail'),
639
        ]);
640
        $cartItemMetadata->setAttributeList([
641
            new Attribute($idKey, 'id', 'string', true),
642
            new Attribute('amount'),
643
            new Attribute('createdAt', 'createdAt', 'datetime'),
644
            new Attribute('data'),
645
            new Attribute('cart'),
646
            new Attribute('product'),
647
            new Attribute('cartItemDetailList'),
648
        ]);
649
650
        $productMetadata = new ClassMetadata(
651
            'products',
652
            'Mapado\RestClientSdk\Tests\Model\Product',
653
            ''
654
        );
655
656
        $productMetadata->setAttributeList([
657
            new Attribute($idKey, 'id', 'string', true),
658
            new Attribute('product_value', 'value'),
659
            new Attribute('currency'),
660
        ]);
661
662
663
        $cartItemDetailMetadata = new ClassMetadata(
664
            'cart_item_details',
665
            'Mapado\RestClientSdk\Tests\Model\CartItemDetail',
666
            ''
667
        );
668
669
        $cartItemDetailMetadata->setRelationList([
670
            new Relation('cartItem', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\CartItem'),
671
        ]);
672
        $cartItemDetailMetadata->setAttributeList([
673
            new Attribute($idKey, 'id', 'string', true),
674
            new Attribute('name'),
675
            new Attribute('cartItem'),
676
        ]);
677
678
679
        $mapping = new Mapping('/v1');
680
        $mapping->setMapping([
681
            $cartMetadata,
682
            $cartItemMetadata,
683
            $cartItemDetailMetadata,
684
            $productMetadata,
685
        ]);
686
687
        return $mapping;
688
    }
689
690
    /**
691
     * createNewCart
692
     *
693
     * @access private
694
     * @return AbstractModel
695
     */
696
    private function createNewCart()
697
    {
698
        $cart = new \Mapado\RestClientSdk\Tests\Model\Cart();
699
        $cart->setStatus('payed');
700
        $cart->setCreatedAt(new DateTime('2015-09-20 12:08:00'));
701
702
        $phoneNumberUtil = PhoneNumberUtil::getInstance();
703
        $clientPhoneNumber = $phoneNumberUtil->parse('+33123456789', PhoneNumberFormat::INTERNATIONAL);
704
        $cart->setClientPhoneNumber($clientPhoneNumber);
705
706
        return $cart;
707
    }
708
709
    /**
710
     * createCart
711
     *
712
     * @access private
713
     * @return void
714
     */
715
    private function createCart()
716
    {
717
        $cart = $this->createNewCart();
718
        $cart->setId('/v1/carts/8');
719
720
        return $cart;
721
    }
722
723
    /**
724
     * createKnownCartItem
725
     *
726
     * @access private
727
     * @return AbstractModel
728
     */
729
    private function createKnownCartItem()
730
    {
731
        $cartItem = $this->createNewCartItem();
732
        $cartItem->setId('/v1/cart_items/16');
733
        $cartItem->setAmount(1);
734
        $cartItem->setCreatedAt(new DateTime('2015-11-04 15:13:00'));
735
        $cartItem->setData([
736
            'when' => new DateTime('2015-11-04 15:00:00'),
737
            'who' => 'Jane',
738
        ]);
739
        $cartItem->setCart($this->createCart());
740
741
        return $cartItem;
742
    }
743
744
    /**
745
     * createNewCartItem
746
     *
747
     * @access private
748
     * @return AbstractModel
749
     */
750
    private function createNewCartItem($addKnownedProduct = true)
751
    {
752
        $cartItem = new \Mapado\RestClientSdk\Tests\Model\CartItem();
753
        $cartItem->setAmount(2);
754
        $cartItem->setCreatedAt(new DateTime('2015-09-20 12:11:00'));
755
        $cartItem->setData([
756
            'when' => new DateTime('2015-09-20 15:00:00'),
757
            'who' => 'John',
758
        ]);
759
760
        if ($addKnownedProduct) {
761
            $cartItem->setProduct($this->createKnownedProduct());
762
        }
763
764
        return $cartItem;
765
    }
766
767
    /**
768
     * createNewProduct
769
     *
770
     * @access private
771
     * @return AbstractModel
772
     */
773
    private function createNewProduct()
774
    {
775
        $product = new \Mapado\RestClientSdk\Tests\Model\Product();
776
777
        $product->setValue(8.2);
778
        $product->setCurrency('eur');
779
780
        return $product;
781
    }
782
783
784
    /**
785
     * createKnownedProduct
786
     *
787
     * @access private
788
     * @return AbstractModel
789
     */
790
    private function createKnownedProduct()
791
    {
792
        $product = $this->createNewProduct();
793
        $product->setId('/v1/products/10');
794
795
        return $product;
796
    }
797
798
    private function createNewCartItemDetail()
799
    {
800
        $item = new \Mapado\RestClientSdk\Tests\Model\CartItemDetail();
801
802
        $item->setName('Bill');
803
804
        return $item;
805
    }
806
807
    /**
808
     * createNewInstance
809
     *
810
     * @access private
811
     * @return void
812
     */
813
    private function createNewInstance($mapping = null)
814
    {
815
        $mapping = $mapping ?: $this->getMapping();
816
        $this->newTestedInstance($mapping);
817
818
        $this->mockGenerator->orphanize('__construct');
819
        $this->mockGenerator->shuntParentClassCalls();
820
        $restClient = new \mock\Mapado\RestClientSdk\RestClient();
821
        $this->mockGenerator->unshuntParentClassCalls();
822
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient($restClient, $mapping, $this->testedInstance);
823
        $sdk->setFileCachePath(__DIR__ . '/../../cache/');
824
825
        $cartRepositoryMock = $this->getCartRepositoryMock($sdk, $restClient, 'Mapado\RestClientSdk\Tests\Model\Cart');
826
827
        $this->calling($sdk)->getRepository = function ($modelName) use ($cartRepositoryMock) {
828
            switch ($modelName) {
829
                case 'Mapado\RestClientSdk\Tests\Model\Cart':
830
                    return $cartRepositoryMock;
831
                default:
832
                    return null;
833
            }
834
        };
835
836
        $this->testedInstance->setSdk($sdk);
837
    }
838
839
    private function getCartRepositoryMock($sdk, $restClient, $modelName)
840
    {
841
        $repository = new \mock\Mapado\RestClientSdk\EntityRepository(
842
            $sdk,
843
            $restClient,
844
            $modelName
845
        );
846
847
        $_this = $this;
848
849
        $this->calling($repository)->find = function ($id) use ($_this) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
850
            return $_this->createCart();
851
        };
852
853
        return $repository;
854
    }
855
}
856