Completed
Pull Request — master (#76)
by Julien
08:32 queued 55s
created

testDeserializeEntityWithoutIriAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

922
        $product->setId(/** @scrutinizer ignore-type */ '/v1/products/10');
Loading history...
923
924
        return $product;
925
    }
926
927
    private function createNewCartItemDetail()
928
    {
929
        $item = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail();
930
931
        $item->setName('Bill');
932
933
        return $item;
934
    }
935
936
    /**
937
     * createNewInstance
938
     *
939
     * @param Mapping $mapping
940
     */
941
    private function createNewInstance($mapping = null)
942
    {
943
        $mapping = $mapping ?: $this->getMapping();
944
        $unitOfWork = new UnitOfWork($mapping);
945
        $this->newTestedInstance($mapping, $unitOfWork);
946
947
        $this->mockGenerator->orphanize('__construct');
948
        $this->mockGenerator->shuntParentClassCalls();
949
        $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...
950
        $this->mockGenerator->unshuntParentClassCalls();
951
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient($restClient, $mapping, $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...
952
        $sdk->setFileCachePath(__DIR__ . '/../../cache/');
953
954
        $cartRepositoryMock = $this->getCartRepositoryMock($sdk, $restClient, $unitOfWork, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart');
955
956
        $this->calling($sdk)->getRepository = function ($modelName) use ($cartRepositoryMock) {
957
            switch ($modelName) {
958
                case 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart':
959
                    return $cartRepositoryMock;
960
                default:
961
                    return;
962
            }
963
        };
964
965
        $this->testedInstance->setSdk($sdk);
966
    }
967
968
    /**
969
     * @param string $modelName
970
     * @param UnitOfWork $unitOfWork
971
     */
972
    private function getCartRepositoryMock($sdk, $restClient, $unitOfWork, $modelName)
973
    {
974
        $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...
975
            $sdk,
976
            $restClient,
977
            $unitOfWork,
978
            $modelName
979
        );
980
981
        $_this = $this;
982
983
        $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

983
        $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...
984
            return $_this->createCart();
985
        };
986
987
        return $repository;
988
    }
989
}
990