Passed
Push — master ( a73c0f...44755a )
by Julien
03:04
created

Serializer::createNewCartItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mapado\RestClientSdk\Tests\Units\Model;
6
7
use atoum;
8
use DateTime;
9
use libphonenumber\PhoneNumberFormat;
10
use libphonenumber\PhoneNumberUtil;
11
use Mapado\RestClientSdk\Mapping;
12
use Mapado\RestClientSdk\Mapping\Attribute;
13
use Mapado\RestClientSdk\Mapping\ClassMetadata;
14
use Mapado\RestClientSdk\Mapping\Driver\AnnotationDriver;
15
use Mapado\RestClientSdk\Mapping\Relation;
16
use Mapado\RestClientSdk\Tests\Model\Issue46;
17
use Mapado\RestClientSdk\Tests\Model\Issue75;
18
use Mapado\RestClientSdk\Tests\Model\Issue90;
19
use Mapado\RestClientSdk\Tests\Model\JsonLd;
20
use Mapado\RestClientSdk\UnitOfWork;
21
22
/**
23
 * Class Serializer
24
 *
25
 * @author Julien Deniau <[email protected]>
26
 */
27
class Serializer extends atoum
28
{
29
    /**
30
     * @var UnitOfWork
31
     */
32
    private $unitOfWork;
33
34
    /**
35
     * testJsonEncode
36
     */
37
    public function testJsonEncode()
38
    {
39
        $this->createNewInstance();
40
41
        $this
42
            ->given($cart = $this->createCart())
43
            ->then
44
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
45
                    ->isIdenticalTo([
46
                        '@id' => '/v1/carts/8',
47
                        'status' => 'payed',
48
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
49
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
50
                        'cart_items' => [],
51
                        'order' => null,
52
                    ])
53
54
            // reverse the serialization
55
            ->then
56
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
57
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart')
58
                ->string($cart->getId())
59
                    ->isEqualTo('/v1/carts/8')
60
                ->string($cart->getStatus())
61
                    ->isEqualTo('payed')
62
                ->datetime($cart->getCreatedAt())
63
                    ->isEqualTo(new \DateTime('2015-09-20T12:08:00'))
64
                ->array($cart->getCartItemList())
65
                    ->isEmpty()
66
67
        ;
68
    }
69
70
    /**
71
     * testJsonEncodeRelation
72
     */
73
    public function testJsonEncodeRelationWithLink()
74
    {
75
        $this->createNewInstance();
76
77
        $this
78
            ->given($cart = $this->createCart())
79
                ->and($cartItem = $this->createKnownCartItem())
80
                ->and($cart->addCartItemList($cartItem))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cart->addCartItemList($cartItem) targeting Mapado\RestClientSdk\Tes...Cart::addCartItemList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
81
82
            ->then
83
                ->array($data = $this->testedInstance->serialize(
84
                    $cart,
85
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
86
                    ['serializeRelations' => ['cart_items']]
87
                ))
88
                    ->isIdenticalTo([
89
                        '@id' => '/v1/carts/8',
90
                        'status' => 'payed',
91
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
92
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
93
                        'cart_items' => [
94
                            [
95
                                '@id' => '/v1/cart_items/16',
96
                                'amount' => 1,
97
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
98
                                'data' => [
99
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
100
                                    'who' => 'Jane',
101
                                ],
102
                                'cart' => '/v1/carts/8',
103
                                'product' => '/v1/products/10',
104
                                'cartItemDetailList' => [],
105
                            ],
106
                        ],
107
                        'order' => null,
108
                    ])
109
110
            ->then
111
                ->array($data = $this->testedInstance->serialize(
112
                    $cart,
113
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
114
                    ['serializeRelations' => ['cart_items']]
115
                ))
116
                    ->isIdenticalTo([
117
                        '@id' => '/v1/carts/8',
118
                        'status' => 'payed',
119
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
120
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
121
                        'cart_items' => [
122
                            [
123
                                '@id' => '/v1/cart_items/16',
124
                                'amount' => 1,
125
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
126
                                'data' => [
127
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
128
                                    'who' => 'Jane',
129
                                ],
130
                                'cart' => '/v1/carts/8',
131
                                'product' => '/v1/products/10',
132
                                'cartItemDetailList' => [],
133
                            ],
134
                        ],
135
                        'order' => null,
136
                    ])
137
138
            // reverse the serialization
139
            ->then
140
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
141
                ->array($cart->getCartItemList())
142
                    ->size->isEqualTo(1)
143
                ->object($cartItem = current($cart->getCartItemList()))
144
                    ->isInstanceOf(JsonLd\CartItem::class)
145
                ->string($cartItem->getId())
146
                    ->isEqualTo('/v1/cart_items/16')
147
            ;
148
    }
149
150
    /**
151
     * testJsonEncodeRelationWithoutLink
152
     */
153
    public function testJsonEncodeRelationWithoutLink()
154
    {
155
        $this->createNewInstance();
156
157
        $this
158
            ->given($cart = $this->createCart())
159
                ->and($cartItem = $this->createNewCartItem())
160
                ->and($cart->addCartItemList($cartItem))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cart->addCartItemList($cartItem) targeting Mapado\RestClientSdk\Tes...Cart::addCartItemList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
161
            ->then
162
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
163
                    ->isIdenticalTo([
164
                        '@id' => '/v1/carts/8',
165
                        'status' => 'payed',
166
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
167
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
168
                        'cart_items' => [
169
                            [
170
                                'amount' => 2,
171
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
172
                                'data' => [
173
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
174
                                    'who' => 'John',
175
                                ],
176
                                'product' => '/v1/products/10',
177
                                'cartItemDetailList' => [],
178
                            ],
179
                        ],
180
                        'order' => null,
181
                    ])
182
183
            // reverse the serialization
184
            ->then
185
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
186
                ->array($cartItemList = $cart->getCartItemList()) // we can not uneserialize an unlinked entity
187
                    ->size->isEqualTo(1)
188
                ->object($cartItemList[0])
189
                    ->isInstanceOf(JsonLd\CartItem::class)
190
                ->variable($cartItemList[0]->getId())
191
                    ->isNull
192
        ;
193
    }
194
195
    public function testSerializeThreeLevel()
196
    {
197
        $this->createNewInstance();
198
199
        $this
200
            ->given($cart = $this->createNewCart())
201
                ->and($cartItem = $this->createNewCartItem())
202
                ->and($cart->addCartItemList($cartItem))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cart->addCartItemList($cartItem) targeting Mapado\RestClientSdk\Tes...Cart::addCartItemList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
203
            ->then
204
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
205
                    ->isIdenticalTo([
206
                        'status' => 'payed',
207
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
208
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
209
                        'cart_items' => [
210
                            [
211
                                'amount' => 2,
212
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
213
                                'data' => [
214
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
215
                                    'who' => 'John',
216
                                ],
217
                                'product' => '/v1/products/10',
218
                                'cartItemDetailList' => [],
219
                            ],
220
                        ],
221
                        'order' => null,
222
                    ])
223
        ;
224
    }
225
226
    /**
227
     * testJsonEncodeRelationWithoutLinkMultipleLevel
228
     */
229
    public function testJsonEncodeRelationWithoutLinkMultipleLevel()
230
    {
231
        $this->createNewInstance();
232
        $this
233
            ->given($cart = $this->createCart())
234
                ->and($cartItem = $this->createNewCartItem(false))
235
                ->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail()))
236
                ->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail()))
237
            ->if($cart->addCartItemList($cartItem))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cart->addCartItemList($cartItem) targeting Mapado\RestClientSdk\Tes...Cart::addCartItemList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
238
            ->then
239
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
240
                    ->isIdenticalTo([
241
                        '@id' => '/v1/carts/8',
242
                        'status' => 'payed',
243
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
244
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
245
                        'cart_items' => [
246
                            [
247
                                'amount' => 2,
248
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
249
                                'data' => [
250
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
251
                                    'who' => 'John',
252
                                ],
253
                                'cartItemDetailList' => [
254
                                    ['name' => 'Bill'],
255
                                    ['name' => 'Bill'],
256
                                ],
257
                            ],
258
                        ],
259
                        'order' => null,
260
                    ])
261
        ;
262
    }
263
264
    /**
265
     * testJsonEncodeMixRelations
266
     */
267
    public function testJsonEncodeMixRelations()
268
    {
269
        $this->createNewInstance();
270
271
        $this
272
            ->given($cart = $this->createCart())
273
                ->and($cartItem = $this->createNewCartItem())
274
                ->and($knownedCartItem = $this->createKnownCartItem())
275
            ->if($cart->addCartItemList($knownedCartItem))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cart->addCartItemList($knownedCartItem) targeting Mapado\RestClientSdk\Tes...Cart::addCartItemList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
276
                ->and($cart->addCartItemList($cartItem))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cart->addCartItemList($cartItem) targeting Mapado\RestClientSdk\Tes...Cart::addCartItemList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
277
            ->then
278
                ->array($data = $this->testedInstance->serialize(
279
                    $cart,
280
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
281
                    ['serializeRelations' => ['cart_items']]
282
                ))
283
                    ->isIdenticalTo([
284
                        '@id' => '/v1/carts/8',
285
                        'status' => 'payed',
286
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
287
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
288
                        'cart_items' => [
289
                            [
290
                                '@id' => '/v1/cart_items/16',
291
                                'amount' => 1,
292
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
293
                                'data' => [
294
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
295
                                    'who' => 'Jane',
296
                                ],
297
                                'cart' => '/v1/carts/8',
298
                                'product' => '/v1/products/10',
299
                                'cartItemDetailList' => [],
300
                            ],
301
                            [
302
                                'amount' => 2,
303
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
304
                                'data' => [
305
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
306
                                    'who' => 'John',
307
                                ],
308
                                'product' => '/v1/products/10',
309
                                'cartItemDetailList' => [],
310
                            ],
311
                        ],
312
                        'order' => null,
313
                    ])
314
315
            // reverse the serialization
316
            ->then
317
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
318
                ->array($cartItemList = $cart->getCartItemList()) // we can not uneserialize an unlinked entity
319
                    ->size->isEqualTo(2)
320
                ->object($cartItem = $cartItemList[0])
321
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
322
                ->string($cartItem->getId())
323
                    ->isEqualTo('/v1/cart_items/16')
324
                ->object($cartItem = $cartItemList[1])
325
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
326
                ->variable($cartItem->getId())
327
                    ->isNull()
328
        ;
329
    }
330
331
    /**
332
     * testNotAllowedSerialization
333
     */
334
    public function testNotAllowedSerialization()
335
    {
336
        $this->createNewInstance();
337
        $this
338
            ->given($cartItem = $this->createNewCartItem())
339
                ->and($cartItemDetail = $this->createNewCartItemDetail())
340
                ->and($cartItemDetail->setCartItem($cartItem))
341
                ->and($testedInstance = $this->testedInstance)
342
            ->then
343
                ->object($cartItemDetail->getCartItem())
344
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
345
                ->exception(function () use ($testedInstance, $cartItemDetail) {
346
                    $testedInstance->serialize($cartItemDetail, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail');
347
                })
348
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\SdkException')
349
        ;
350
    }
351
352
    /**
353
     * testMultipleLevelSerialization
354
     */
355
    public function testMultipleLevelSerialization()
356
    {
357
        $this->createNewInstance();
358
        $this
359
            ->given($cart = $this->createNewCart())
360
                ->and($cartItem = $this->createNewCartItem())
361
                ->and($cartItem->setCart($cart))
362
            ->then
363
                ->array($this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
364
                    ->isIdenticalTo([
365
                        'status' => 'payed',
366
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
367
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
368
                        'cart_items' => [
369
                            [
370
                                'amount' => 2,
371
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
372
                                'data' => [
373
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
374
                                    'who' => 'John',
375
                                ],
376
                                'product' => '/v1/products/10',
377
                                'cartItemDetailList' => [],
378
                            ],
379
                        ],
380
                        'order' => null,
381
                    ])
382
383
        ;
384
    }
385
386
    /**
387
     * testLinkedUnserialize
388
     */
389
    public function testLinkedUnserialize()
390
    {
391
        $this->createNewInstance();
392
        $phoneNumberUtil = PhoneNumberUtil::getInstance();
393
394
        $this
395
            ->given($data = [
396
                    '@id' => '/v1/carts/8',
397
                    'status' => 'payed',
398
                    'clientPhoneNumber' => $phoneNumberUtil->parse('+330123456789', PhoneNumberFormat::INTERNATIONAL),
399
                    'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
400
                    'cart_items' => [
401
                        [
402
                            '@id' => '/v1/cart_items/16',
403
                            'amount' => 2,
404
                            'createdAt' => (new \DateTime('2015-09-20T12:11:00+00:00'))->format(DateTime::RFC3339),
405
                            'data' => [
406
                                'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format(DateTime::RFC3339),
407
                                'who' => 'John',
408
                            ],
409
                            'product' => '/v1/products/10',
410
                            'cartItemDetailList' => [],
411
                        ],
412
                    ],
413
                ])
414
            ->then
415
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
416
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart')
417
                ->object($cart->getClientPhoneNumber())
418
                    ->isInstanceOf('libphonenumber\PhoneNumber')
419
                ->array($cart->getCartItemList())
420
                    ->size->isEqualTo(1)
421
                ->object($cartItem = current($cart->getCartItemList()))
422
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
423
                ->string($cartItem->getId())
424
                    ->isEqualTo('/v1/cart_items/16')
425
                ->integer($cartItem->getAmount())
426
                    ->isEqualTo(2)
427
                ->datetime($cartItem->getCreatedAt())
428
                    ->isEqualTo(new \DateTime('2015-09-20T12:11:00+00:00'))
429
                ->array($cartItem->getData())
430
                    ->isEqualTo([
431
                        'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format(DateTime::RFC3339),
432
                        'who' => 'John',
433
                    ])
434
                ->object($cartItem->getProduct())
435
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Product')
436
                ->string($cartItem->getProduct()->getId())
437
                    ->isEqualTo('/v1/products/10')
438
                ->array($cartItem->getCartItemDetailList())
439
                    ->isEmpty()
440
        ;
441
442
        $this->createNewInstance();
443
        $this
444
            ->given($data = [
445
                    '@id' => '/v1/cart_items/16',
446
                    'amount' => 2,
447
                    'cart' => [
448
                        '@id' => '/v1/carts/10',
449
                        'status' => 'waiting',
450
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
451
                    ],
452
                ])
453
454
            ->then
455
                ->object($cartItem = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'))
456
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
457
                ->object($cart = $cartItem->getCart())
458
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart')
459
                ->string($cart->getClientPhoneNumber())
460
                    ->isEqualTo('+33 1 23 45 67 89')
461
                ->string($cart->getId())
462
                    ->isEqualTo('/v1/carts/10')
463
                ->string($cart->getStatus())
464
                    ->isEqualTo('waiting')
465
        ;
466
    }
467
468
    public function testSerializeNullValues()
469
    {
470
        $this->createNewInstance();
471
        $this
472
            ->given($cart = $this->createNewCart())
473
                ->and($cart->setStatus(null))
474
                ->and($cart->setOrder(null))
475
            ->then
476
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
477
                    ->isIdenticalTo([
478
                        'status' => null,
479
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
480
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
481
                        'cart_items' => [],
482
                        'order' => null,
483
                    ])
484
        ;
485
    }
486
487
    public function testSerializingAttributeNameDiffThanPropertyName()
488
    {
489
        $this->createNewInstance();
490
        $this
491
            ->given($product = $this->createNewProduct())
492
            ->then
493
                ->array($data = $this->testedInstance->serialize($product, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'))
494
                ->isIdenticalTo([
495
                    'product_value' => 8.2,
496
                    'currency' => 'eur',
497
                ])
498
        ;
499
    }
500
501
    public function testWeirdIdentifier()
502
    {
503
        $mapping = $this->getMapping('weirdId');
504
        $this->createNewInstance($mapping);
505
506
        $this
507
            ->given($cart = $this->createCart())
508
                ->and($cartItem = $this->createKnownCartItem())
509
                ->and($cart->addCartItemList($cartItem))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cart->addCartItemList($cartItem) targeting Mapado\RestClientSdk\Tes...Cart::addCartItemList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
510
511
            ->then
512
                ->array($data = $this->testedInstance->serialize(
513
                    $cart,
514
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
515
                    ['serializeRelations' => ['cart_items']]
516
                ))
517
                    ->isIdenticalTo([
518
                        'weirdId' => '/v1/carts/8',
519
                        'status' => 'payed',
520
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
521
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
522
                        'cart_items' => [
523
                            [
524
                                'weirdId' => '/v1/cart_items/16',
525
                                'amount' => 1,
526
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
527
                                'data' => [
528
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
529
                                    'who' => 'Jane',
530
                                ],
531
                                'cart' => '/v1/carts/8',
532
                                'product' => '/v1/products/10',
533
                                'cartItemDetailList' => [],
534
                            ],
535
                        ],
536
                        'order' => null,
537
                    ])
538
539
            ->then
540
                ->array($data = $this->testedInstance->serialize(
541
                    $cart,
542
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
543
                    ['serializeRelations' => ['cart_items']]
544
                ))
545
                    ->isIdenticalTo([
546
                        'weirdId' => '/v1/carts/8',
547
                        'status' => 'payed',
548
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
549
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
550
                        'cart_items' => [
551
                            [
552
                                'weirdId' => '/v1/cart_items/16',
553
                                'amount' => 1,
554
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
555
                                'data' => [
556
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
557
                                    'who' => 'Jane',
558
                                ],
559
                                'cart' => '/v1/carts/8',
560
                                'product' => '/v1/products/10',
561
                                'cartItemDetailList' => [],
562
                            ],
563
                        ],
564
                        'order' => null,
565
                    ])
566
567
            // reverse the serialization
568
            ->then
569
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
570
                ->string($cart->getId())
571
                    ->isEqualTo('/v1/carts/8')
572
                ->array($cart->getCartItemList())
573
                    ->size->isEqualTo(1)
574
                // ->object($cartItem = current($cart->getCartItemList()))
575
                //     ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
576
                // ->string($cartItem->getId())
577
                //     ->isEqualTo('/v1/cart_items/16')
578
            ;
579
    }
580
581
    public function testDeserializeWithExtraFields()
582
    {
583
        $this->createNewInstance();
584
585
        $this
586
            ->given($data = [
587
                '@foo' => 'bar',
588
                '@id' => '/v1/carts/8',
589
                'status' => 'payed',
590
                'clientPhoneNumber' => '+33 1 23 45 67 89',
591
                'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
592
                'cart_items' => [],
593
                'order' => null,
594
            ])
595
596
            ->then
597
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
598
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart')
599
        ;
600
    }
601
602
    public function testSerializingIriManyToOne()
603
    {
604
        $annotationDriver = new AnnotationDriver(__DIR__ . '/../../cache/');
605
        $mapping = new Mapping();
606
        $mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../../Model/Issue46/'));
607
608
        $section = new Issue46\Section();
609
        $section->setId(46);
610
        $section->setIri('/sections/46');
611
        $section->setTitle('section title');
612
613
        $article = new Issue46\Article();
614
        $article->setSection($section);
615
616
        $this->createNewInstance($mapping);
617
618
        $this
619
            ->then
620
                ->array($this->testedInstance->serialize($article, 'Mapado\RestClientSdk\Tests\Model\Issue46\Article'))
621
                    ->isIdenticalTo([
622
                        'id' => null,
623
                        'section' => '/sections/46',
624
                    ])
625
626
                ->if($article->setIri('/articles/44'))
627
628
                ->array($this->testedInstance
629
                    ->serialize(
630
                        $section,
631
                        'Mapado\RestClientSdk\Tests\Model\Issue46\Section'
632
                    ))
633
                ->isIdenticalTo([
634
                    '@id' => '/sections/46',
635
                    'id' => 46,
636
                    'title' => 'section title',
637
                    'articleList' => [
638
                        '/articles/44',
639
                    ],
640
                ])
641
642
                ->array($this->testedInstance
643
                    ->serialize(
644
                        $section,
645
                        'Mapado\RestClientSdk\Tests\Model\Issue46\Section',
646
                        ['serializeRelations' => ['articleList']]
647
                    ))
648
                ->isIdenticalTo([
649
                    '@id' => '/sections/46',
650
                    'id' => 46,
651
                    'title' => 'section title',
652
                    'articleList' => [
653
                        [
654
                            '@id' => '/articles/44',
655
                            'id' => null,
656
                            'section' => '/sections/46',
657
                        ],
658
                    ],
659
                ])
660
        ;
661
    }
662
663
    public function testDeserializeEntityWithoutIriAttribute()
664
    {
665
        $annotationDriver = new AnnotationDriver(__DIR__ . '/../../cache/');
666
        $mapping = new Mapping();
667
        $mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../../Model/Issue75/'));
668
669
        $this->createNewInstance($mapping);
670
        $this
671
            ->given($data = [
672
                '@id' => '/v1/articles/8',
673
                'tag' => [
674
                    'name' => 'tag name',
675
                ],
676
            ])
677
678
            ->then
679
                ->object($article = $this->testedInstance->deserialize($data, Issue75\Article::class))
680
                    ->isInstanceOf(Issue75\Article::class)
681
                ->object($article->getTag())
682
                    ->isInstanceOf(Issue75\Tag::class)
683
684
            ->given($data = [
685
                '@id' => '/v1/articles/8',
686
                'tagList' => [
687
                    [
688
                        'name' => 'tag 1 name',
689
                    ],
690
                    [
691
                        'name' => 'tag 2 name',
692
                    ],
693
                ],
694
            ])
695
696
            ->then
697
                ->object($article = $this->testedInstance->deserialize($data, Issue75\Article::class))
698
                    ->isInstanceOf(Issue75\Article::class)
699
                ->array($tagList = $article->getTagList())
700
                    ->size->isEqualTo(2)
701
                ->object($tagList[0])
702
                    ->isInstanceOf(Issue75\Tag::class)
703
                ->string($tagList[0]->getName())
704
                    ->isIdenticalTo('tag 1 name')
705
                ->object($tagList[1])
706
                    ->isInstanceOf(Issue75\Tag::class)
707
                ->string($tagList[1]->getName())
708
                    ->isIdenticalTo('tag 2 name')
709
        ;
710
    }
711
712
    public function testSerializeEntityWithoutIriAttribute()
713
    {
714
        $annotationDriver = new AnnotationDriver(__DIR__ . '/../../cache/');
715
        $mapping = new Mapping();
716
        $mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../../Model/Issue75/'));
717
718
        $tag = new Issue75\Tag();
719
        $tag->setName('tag title');
720
721
        $article = new Issue75\Article();
722
        $article->setTitle('article title');
723
724
        $this->createNewInstance($mapping);
725
        $this
726
            ->then
727
                ->array($data = $this->testedInstance->serialize($article, Issue75\Article::class))
728
                ->isIdenticalTo([
729
                    'title' => 'article title',
730
                    'tag' => null,
731
                    'tagList' => null,
732
                ])
733
734
            ->if($article->setTag($tag))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $article->setTag($tag) targeting Mapado\RestClientSdk\Tes...sue75\Article::setTag() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
735
                ->array($data = $this->testedInstance->serialize($article, Issue75\Article::class))
736
                    ->isIdenticalTo([
737
                        'title' => 'article title',
738
                        'tag' => [
739
                            'name' => 'tag title',
740
                        ],
741
                        'tagList' => null,
742
                    ])
743
744
            ->if($article->setTagList([(new Issue75\Tag())->setName('tag 1')]))
745
                ->array($data = $this->testedInstance->serialize($article, Issue75\Article::class))
746
                    ->isIdenticalTo([
747
                        'title' => 'article title',
748
                        'tag' => [
749
                            'name' => 'tag title',
750
                        ],
751
                        'tagList' => [
752
                            ['name' => 'tag 1'],
753
                        ],
754
                    ])
755
756
            ->then
757
                // as tags does not have an Attribute identifier, we ignore the serializeRelations context
758
                ->array($data = $this->testedInstance->serialize($article, Issue75\Article::class, ['serializeRelations' => ['tag', 'tagList']]))
759
                    ->isIdenticalTo([
760
                        'title' => 'article title',
761
                        'tag' => [
762
                            'name' => 'tag title',
763
                        ],
764
                        'tagList' => [
765
                            ['name' => 'tag 1'],
766
                        ],
767
                    ])
768
        ;
769
    }
770
771
    public function testDeserializeEntityWithIntAsId()
772
    {
773
        $annotationDriver = new AnnotationDriver(__DIR__ . '/../../cache/');
774
        $mapping = new Mapping();
775
        $mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../../Model/Issue90/'));
776
777
        $this->createNewInstance($mapping);
778
        $this
779
            ->given($data = [
780
                'id' => 8,
781
            ])
782
783
            ->then
784
                ->object($article = $this->testedInstance->deserialize($data, Issue90\WithIdInt::class))
785
                    ->isInstanceOf(Issue90\WithIdInt::class)
786
787
                ->object($this->unitOfWork->getDirtyEntity('8'))
788
        ;
789
    }
790
791
    /**
792
     * getMapping
793
     *
794
     * @return Mapping
795
     */
796
    private function getMapping($idKey = '@id')
797
    {
798
        $mapping = new Mapping('/v1');
799
        $mapping->setMapping([
800
            $this->getCartMetadata($idKey),
801
            $this->getCartItemMetadata($idKey),
802
            $this->getCartItemDetailMetadata($idKey),
803
            $this->getProductMetadata($idKey),
804
        ]);
805
806
        return $mapping;
807
    }
808
809
    /**
810
     * @param string $idKey
811
     */
812
    private function getProductMetadata($idKey)
813
    {
814
        $productMetadata = new ClassMetadata(
815
            'products',
816
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Product',
817
            ''
818
        );
819
820
        $productMetadata->setAttributeList([
821
            new Attribute($idKey, 'id', 'string', true),
822
            new Attribute('product_value', 'value'),
823
            new Attribute('currency'),
824
        ]);
825
826
        return $productMetadata;
827
    }
828
829
    /**
830
     * @param string $idKey
831
     */
832
    private function getCartItemDetailMetadata($idKey)
833
    {
834
        $cartItemDetailMetadata = new ClassMetadata(
835
            'cart_item_details',
836
            'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail',
837
            ''
838
        );
839
840
        $cartItemDetailMetadata->setRelationList([
841
            new Relation('cartItem', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'),
842
        ]);
843
        $cartItemDetailMetadata->setAttributeList([
844
            new Attribute($idKey, 'id', 'string', true),
845
            new Attribute('name'),
846
            new Attribute('cartItem'),
847
        ]);
848
849
        return $cartItemDetailMetadata;
850
    }
851
852
    /**
853
     * @param string $idKey
854
     */
855
    private function getCartItemMetadata($idKey)
856
    {
857
        $cartItemMetadata = new ClassMetadata(
858
            'cart_items',
859
            'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem',
860
            ''
861
        );
862
863
        $cartItemMetadata->setRelationList([
864
            new Relation('cart', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'),
865
            new Relation('product', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'),
866
            new Relation('cartItemDetailList', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail'),
867
        ]);
868
        $cartItemMetadata->setAttributeList([
869
            new Attribute($idKey, 'id', 'string', true),
870
            new Attribute('amount'),
871
            new Attribute('createdAt', 'createdAt', 'datetime'),
872
            new Attribute('data'),
873
            new Attribute('cart'),
874
            new Attribute('product'),
875
            new Attribute('cartItemDetailList'),
876
        ]);
877
878
        return $cartItemMetadata;
879
    }
880
881
    /**
882
     * @param string $idKey
883
     */
884
    private function getCartMetadata($idKey)
885
    {
886
        $cartMetadata = new ClassMetadata(
887
            'carts',
888
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
889
            ''
890
        );
891
        $cartMetadata->setAttributeList([
892
            new Attribute($idKey, 'id', 'string', true),
893
            new Attribute('status'),
894
            new Attribute('clientPhoneNumber', 'clientPhoneNumber', 'phone_number'),
895
            new Attribute('createdAt', 'createdAt', 'datetime'),
896
            new Attribute('cart_items', 'cartItemList'),
897
            new Attribute('order'),
898
        ]);
899
        $cartMetadata->setRelationList([
900
            new Relation('cart_items', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'),
901
            new Relation('order', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Order'),
902
        ]);
903
904
        return $cartMetadata;
905
    }
906
907
    /**
908
     * createNewCart
909
     *
910
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart
911
     */
912
    private function createNewCart()
913
    {
914
        $cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart();
915
        $cart->setStatus('payed');
916
        $cart->setCreatedAt(new DateTime('2015-09-20 12:08:00'));
917
918
        $phoneNumberUtil = PhoneNumberUtil::getInstance();
919
        $clientPhoneNumber = $phoneNumberUtil->parse('+33123456789', PhoneNumberFormat::INTERNATIONAL);
920
        $cart->setClientPhoneNumber($clientPhoneNumber);
921
922
        return $cart;
923
    }
924
925
    /**
926
     * createCart
927
     *
928
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart
929
     */
930
    private function createCart()
931
    {
932
        $cart = $this->createNewCart();
933
        $cart->setId('/v1/carts/8');
934
935
        return $cart;
936
    }
937
938
    /**
939
     * createKnownCartItem
940
     *
941
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem
942
     */
943
    private function createKnownCartItem()
944
    {
945
        $cartItem = $this->createNewCartItem();
946
        $cartItem->setId('/v1/cart_items/16');
947
        $cartItem->setAmount(1);
948
        $cartItem->setCreatedAt(new DateTime('2015-11-04 15:13:00'));
949
        $cartItem->setData([
950
            'when' => new DateTime('2015-11-04 15:00:00'),
951
            'who' => 'Jane',
952
        ]);
953
        $cartItem->setCart($this->createCart());
954
955
        return $cartItem;
956
    }
957
958
    /**
959
     * createNewCartItem
960
     *
961
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem
962
     */
963
    private function createNewCartItem($addKnownedProduct = true)
964
    {
965
        $cartItem = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem();
966
        $cartItem->setAmount(2);
967
        $cartItem->setCreatedAt(new DateTime('2015-09-20 12:11:00'));
968
        $cartItem->setData([
969
            'when' => new DateTime('2015-09-20 15:00:00'),
970
            'who' => 'John',
971
        ]);
972
973
        if ($addKnownedProduct) {
974
            $cartItem->setProduct($this->createKnownedProduct());
975
        }
976
977
        return $cartItem;
978
    }
979
980
    /**
981
     * createNewProduct
982
     *
983
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\Product
984
     */
985
    private function createNewProduct()
986
    {
987
        $product = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product();
988
989
        $product->setValue(8.2);
990
        $product->setCurrency('eur');
991
992
        return $product;
993
    }
994
995
    /**
996
     * createKnownedProduct
997
     *
998
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\Product
999
     */
1000
    private function createKnownedProduct()
1001
    {
1002
        $product = $this->createNewProduct();
1003
        $product->setId('/v1/products/10');
0 ignored issues
show
Bug introduced by
'/v1/products/10' of type string is incompatible with the type integer expected by parameter $id of Mapado\RestClientSdk\Tes...JsonLd\Product::setId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1003
        $product->setId(/** @scrutinizer ignore-type */ '/v1/products/10');
Loading history...
1004
1005
        return $product;
1006
    }
1007
1008
    private function createNewCartItemDetail()
1009
    {
1010
        $item = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail();
1011
1012
        $item->setName('Bill');
1013
1014
        return $item;
1015
    }
1016
1017
    /**
1018
     * createNewInstance
1019
     *
1020
     * @param Mapping $mapping
1021
     */
1022
    private function createNewInstance($mapping = null)
1023
    {
1024
        $mapping = $mapping ?: $this->getMapping();
1025
        $this->unitOfWork = new UnitOfWork($mapping);
1026
        $this->newTestedInstance($mapping, $this->unitOfWork);
1027
1028
        $this->mockGenerator->orphanize('__construct');
1029
        $this->mockGenerator->shuntParentClassCalls();
1030
        $restClient = new \mock\Mapado\RestClientSdk\RestClient();
0 ignored issues
show
Bug introduced by
The type mock\Mapado\RestClientSdk\RestClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
1031
        $this->mockGenerator->unshuntParentClassCalls();
1032
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient($restClient, $mapping, $this->unitOfWork, $this->testedInstance);
0 ignored issues
show
Bug introduced by
The type mock\Mapado\RestClientSdk\SdkClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
1033
        $sdk->setFileCachePath(__DIR__ . '/../../cache/');
1034
1035
        $cartRepositoryMock = $this->getCartRepositoryMock($sdk, $restClient, $this->unitOfWork, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart');
1036
1037
        $this->calling($sdk)->getRepository = function ($modelName) use ($cartRepositoryMock) {
1038
            switch ($modelName) {
1039
                case 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart':
1040
                    return $cartRepositoryMock;
1041
                default:
1042
                    return;
1043
            }
1044
        };
1045
1046
        $this->testedInstance->setSdk($sdk);
1047
    }
1048
1049
    /**
1050
     * @param string $modelName
1051
     * @param UnitOfWork $unitOfWork
1052
     */
1053
    private function getCartRepositoryMock($sdk, $restClient, $unitOfWork, $modelName)
1054
    {
1055
        $repository = new \mock\Mapado\RestClientSdk\EntityRepository(
0 ignored issues
show
Bug introduced by
The type mock\Mapado\RestClientSdk\EntityRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
1056
            $sdk,
1057
            $restClient,
1058
            $unitOfWork,
1059
            $modelName
1060
        );
1061
1062
        $_this = $this;
1063
1064
        $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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1064
        $this->calling($repository)->find = function (/** @scrutinizer ignore-unused */ $id) use ($_this) {

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

Loading history...
1065
            return $_this->createCart();
1066
        };
1067
1068
        return $repository;
1069
    }
1070
}
1071