Completed
Push — master ( 483f95...829c8a )
by Julien
08:25
created

testSerializingAttributeNameDiffThanPropertyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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