Completed
Push — master ( b27094...afbc6d )
by Julien
05:50 queued 01:38
created

Serializer   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 901
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 901
rs 9.7142
c 0
b 0
f 0
wmc 31

28 Methods

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

857
        $product->setId(/** @scrutinizer ignore-type */ '/v1/products/10');
Loading history...
858
859
        return $product;
860
    }
861
862
    private function createNewCartItemDetail()
863
    {
864
        $item = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail();
865
866
        $item->setName('Bill');
867
868
        return $item;
869
    }
870
871
    /**
872
     * createNewInstance
873
     *
874
     * @param Mapping $mapping
875
     */
876
    private function createNewInstance($mapping = null)
877
    {
878
        $mapping = $mapping ?: $this->getMapping();
879
        $unitOfWork = new UnitOfWork($mapping);
880
        $this->newTestedInstance($mapping, $unitOfWork);
881
882
        $this->mockGenerator->orphanize('__construct');
883
        $this->mockGenerator->shuntParentClassCalls();
884
        $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...
885
        $this->mockGenerator->unshuntParentClassCalls();
886
        $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...
887
        $sdk->setFileCachePath(__DIR__ . '/../../cache/');
888
889
        $cartRepositoryMock = $this->getCartRepositoryMock($sdk, $restClient, $unitOfWork, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart');
890
891
        $this->calling($sdk)->getRepository = function ($modelName) use ($cartRepositoryMock) {
892
            switch ($modelName) {
893
                case 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart':
894
                    return $cartRepositoryMock;
895
                default:
896
                    return;
897
            }
898
        };
899
900
        $this->testedInstance->setSdk($sdk);
901
    }
902
903
    /**
904
     * @param string $modelName
905
     * @param UnitOfWork $unitOfWork
906
     */
907
    private function getCartRepositoryMock($sdk, $restClient, $unitOfWork, $modelName)
908
    {
909
        $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...
910
            $sdk,
911
            $restClient,
912
            $unitOfWork,
913
            $modelName
914
        );
915
916
        $_this = $this;
917
918
        $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

918
        $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...
919
            return $_this->createCart();
920
        };
921
922
        return $repository;
923
    }
924
}
925