Passed
Push — master ( 7067f6...c51298 )
by Julien
01:15 queued 11s
created

Serializer   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 935
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 935
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
 * @author Julien Deniau <[email protected]>
20
 */
21
class Serializer extends atoum
22
{
23
    /**
24
     * testJsonEncode
25
     *
26
     * @access public
27
     * @return void
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
     * @access public
66
     * @return void
67
     */
68
    public function testJsonEncodeRelationWithLink()
69
    {
70
        $this->createNewInstance();
71
72
        $this
73
            ->given($cart = $this->createCart())
74
                ->and($cartItem = $this->createKnownCartItem())
75
                ->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...
76
77
            ->then
78
                ->array($data = $this->testedInstance->serialize(
79
                    $cart,
80
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
81
                    [ 'serializeRelations' => ['cart_items'] ]
82
                ))
83
                    ->isIdenticalTo([
84
                        '@id' => '/v1/carts/8',
85
                        'status' => 'payed',
86
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
87
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
88
                        'cart_items' => [
89
                            [
90
                                '@id' => '/v1/cart_items/16',
91
                                'amount' => 1,
92
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
93
                                'data' => [
94
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
95
                                    'who' => 'Jane',
96
                                ],
97
                                'cart' => '/v1/carts/8',
98
                                'product' => '/v1/products/10',
99
                                'cartItemDetailList' => [],
100
                            ],
101
                        ],
102
                        'order' => null,
103
                    ])
104
105
            ->then
106
                ->array($data = $this->testedInstance->serialize(
107
                    $cart,
108
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
109
                    [ 'serializeRelations' => ['cart_items'] ]
110
                ))
111
                    ->isIdenticalTo([
112
                        '@id' => '/v1/carts/8',
113
                        'status' => 'payed',
114
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
115
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
116
                        'cart_items' => [
117
                            [
118
                                '@id' => '/v1/cart_items/16',
119
                                'amount' => 1,
120
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
121
                                'data' => [
122
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
123
                                    'who' => 'Jane',
124
                                ],
125
                                'cart' => '/v1/carts/8',
126
                                'product' => '/v1/products/10',
127
                                'cartItemDetailList' => [],
128
                            ],
129
                        ],
130
                        'order' => null,
131
                    ])
132
133
            // reverse the serialization
134
            ->then
135
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
136
                ->array($cart->getCartItemList())
137
                    ->size->isEqualTo(1)
138
                ->object($cartItem = current($cart->getCartItemList()))
139
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
140
                ->string($cartItem->getId())
141
                    ->isEqualTo('/v1/cart_items/16')
142
            ;
143
    }
144
145
    /**
146
     * testJsonEncodeRelationWithoutLink
147
     *
148
     * @access public
149
     * @return void
150
     */
151
    public function testJsonEncodeRelationWithoutLink()
152
    {
153
        $this->createNewInstance();
154
155
        $this
156
            ->given($cart = $this->createCart())
157
                ->and($cartItem = $this->createNewCartItem())
158
                ->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...
159
            ->then
160
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
161
                    ->isIdenticalTo([
162
                        '@id' => '/v1/carts/8',
163
                        'status' => 'payed',
164
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
165
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
166
                        'cart_items' => [
167
                            [
168
                                'amount' => 2,
169
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
170
                                'data' => [
171
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
172
                                    'who' => 'John',
173
                                ],
174
                                'product' => '/v1/products/10',
175
                                'cartItemDetailList' => [],
176
                            ],
177
                        ],
178
                        'order' => null,
179
                    ])
180
181
            // reverse the serialization
182
            ->then
183
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
184
                ->array($cart->getCartItemList()) // we can not uneserialize an unlinked entity
185
                    ->isEmpty()
186
        ;
187
    }
188
189
    public function testSerializeThreeLevel()
190
    {
191
        $this->createNewInstance();
192
193
        $this
194
            ->given($cart = $this->createNewCart())
195
                ->and($cartItem = $this->createNewCartItem())
196
                ->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...
197
            ->then
198
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
199
                    ->isIdenticalTo([
200
                        'status' => 'payed',
201
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
202
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
203
                        'cart_items' => [
204
                            [
205
                                'amount' => 2,
206
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
207
                                'data' => [
208
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
209
                                    'who' => 'John',
210
                                ],
211
                                'product' => '/v1/products/10',
212
                                'cartItemDetailList' => [],
213
                            ],
214
                        ],
215
                        'order' => null,
216
                    ])
217
        ;
218
    }
219
220
    /**
221
     * testJsonEncodeRelationWithoutLinkMultipleLevel
222
     *
223
     * @access public
224
     * @return void
225
     */
226
    public function testJsonEncodeRelationWithoutLinkMultipleLevel()
227
    {
228
        $this->createNewInstance();
229
        $this
230
            ->given($cart = $this->createCart())
231
                ->and($cartItem = $this->createNewCartItem(false))
232
                ->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail()))
233
                ->and($cartItem->addCartItemDetailList($this->createNewCartItemDetail()))
234
            ->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...
235
            ->then
236
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
237
                    ->isIdenticalTo([
238
                        '@id' => '/v1/carts/8',
239
                        'status' => 'payed',
240
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
241
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
242
                        'cart_items' => [
243
                            [
244
                                'amount' => 2,
245
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
246
                                'data' => [
247
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
248
                                    'who' => 'John',
249
                                ],
250
                                'cartItemDetailList' => [
251
                                    [ 'name' => 'Bill' ],
252
                                    [ 'name' => 'Bill', ],
253
                                ],
254
                            ],
255
                        ],
256
                        'order' => null,
257
                    ])
258
        ;
259
    }
260
261
    /**
262
     * testJsonEncodeMixRelations
263
     *
264
     * @access public
265
     * @return void
266
     */
267
    public function testJsonEncodeMixRelations()
268
    {
269
        $this->createNewInstance();
270
271
        $this
272
            ->given($cart = $this->createCart())
273
                ->and($cartItem = $this->createNewCartItem())
274
                ->and($knownedCartItem = $this->createKnownCartItem())
275
            ->if($cart->addCartItemList($knownedCartItem))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cart->addCartItemList($knownedCartItem) targeting Mapado\RestClientSdk\Tes...Cart::addCartItemList() seems to always return null.

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

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

}

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

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

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

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

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

}

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

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
277
            ->then
278
                ->array($data = $this->testedInstance->serialize(
279
                    $cart,
280
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
281
                    [ 'serializeRelations' => ['cart_items'] ]
282
                ))
283
                    ->isIdenticalTo([
284
                        '@id' => '/v1/carts/8',
285
                        'status' => 'payed',
286
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
287
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
288
                        'cart_items' => [
289
                            [
290
                                '@id' => '/v1/cart_items/16',
291
                                'amount' => 1,
292
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
293
                                'data' => [
294
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
295
                                    'who' => 'Jane',
296
                                ],
297
                                'cart' => '/v1/carts/8',
298
                                'product' => '/v1/products/10',
299
                                'cartItemDetailList' => [],
300
                            ],
301
                            [
302
                                'amount' => 2,
303
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
304
                                'data' => [
305
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
306
                                    'who' => 'John',
307
                                ],
308
                                'product' => '/v1/products/10',
309
                                'cartItemDetailList' => [],
310
                            ],
311
                        ],
312
                        'order' => null,
313
                    ])
314
315
            // reverse the serialization
316
            ->then
317
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
318
                ->array($cart->getCartItemList()) // we can not uneserialize an unlinked entity
319
                    ->size->isEqualTo(1)
320
                ->object($cartItem = current($cart->getCartItemList()))
321
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
322
                ->string($cartItem->getId())
323
                    ->isEqualTo('/v1/cart_items/16')
324
        ;
325
    }
326
327
    /**
328
     * testNotAllowedSerialization
329
     *
330
     * @access public
331
     * @return void
332
     */
333
    public function testNotAllowedSerialization()
334
    {
335
        $this->createNewInstance();
336
        $this
337
            ->given($cartItem = $this->createNewCartItem())
338
                ->and($cartItemDetail = $this->createNewCartItemDetail())
339
                ->and($cartItemDetail->setCartItem($cartItem))
340
                ->and($testedInstance = $this->testedInstance)
341
            ->then
342
                ->object($cartItemDetail->getCartItem())
343
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
344
                ->exception(function () use ($testedInstance, $cartItemDetail) {
345
                    $testedInstance->serialize($cartItemDetail, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail');
346
                })
347
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\SdkException')
348
        ;
349
    }
350
351
    /**
352
     * testMultipleLevelSerialization
353
     *
354
     * @access public
355
     * @return void
356
     */
357
    public function testMultipleLevelSerialization()
358
    {
359
        $this->createNewInstance();
360
        $this
361
            ->given($cart = $this->createNewCart())
362
                ->and($cartItem = $this->createNewCartItem())
363
                ->and($cartItem->setCart($cart))
364
            ->then
365
                ->array($this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
366
                    ->isIdenticalTo([
367
                        'status' => 'payed',
368
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
369
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
370
                        'cart_items' => [
371
                            [
372
                                'amount' => 2,
373
                                'createdAt' => (new \DateTime('2015-09-20T12:11:00'))->format(DateTime::RFC3339),
374
                                'data' => [
375
                                    'when' => (new \DateTime('2015-09-20T15:00:00'))->format(DateTime::RFC3339),
376
                                    'who' => 'John',
377
                                ],
378
                                'product' => '/v1/products/10',
379
                                'cartItemDetailList' => [],
380
                            ],
381
                        ],
382
                        'order' => null,
383
                    ])
384
385
        ;
386
    }
387
388
    /**
389
     * testLinkedUnserialize
390
     *
391
     * @access public
392
     * @return void
393
     */
394
    public function testLinkedUnserialize()
395
    {
396
        $this->createNewInstance();
397
        $phoneNumberUtil = PhoneNumberUtil::getInstance();
398
399
        $this
400
            ->given($data = [
401
                    '@id' => '/v1/carts/8',
402
                    'status' => 'payed',
403
                    'clientPhoneNumber' => $phoneNumberUtil->parse('+330123456789', PhoneNumberFormat::INTERNATIONAL),
404
                    'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
405
                    'cart_items' => [
406
                        [
407
                            '@id' => '/v1/cart_items/16',
408
                            'amount' => 2,
409
                            'createdAt' => (new \DateTime('2015-09-20T12:11:00+00:00'))->format(DateTime::RFC3339),
410
                            'data' => [
411
                                'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format(DateTime::RFC3339),
412
                                'who' => 'John',
413
                            ],
414
                            'product' => '/v1/products/10',
415
                            'cartItemDetailList' => [],
416
                        ],
417
                    ],
418
                ])
419
            ->then
420
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
421
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart')
422
                ->object($cart->getClientPhoneNumber())
423
                    ->isInstanceOf('libphonenumber\PhoneNumber')
424
                ->array($cart->getCartItemList())
425
                    ->size->isEqualTo(1)
426
                ->object($cartItem = current($cart->getCartItemList()))
427
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
428
                ->string($cartItem->getId())
429
                    ->isEqualTo('/v1/cart_items/16')
430
                ->integer($cartItem->getAmount())
431
                    ->isEqualTo(2)
432
                ->datetime($cartItem->getCreatedAt())
433
                    ->isEqualTo(new \DateTime('2015-09-20T12:11:00+00:00'))
434
                ->array($cartItem->getData())
435
                    ->isEqualTo([
436
                        'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format(DateTime::RFC3339),
437
                        'who' => 'John',
438
                    ])
439
                ->object($cartItem->getProduct())
440
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Product')
441
                ->string($cartItem->getProduct()->getId())
442
                    ->isEqualTo('/v1/products/10')
443
                ->array($cartItem->getCartItemDetailList())
444
                    ->isEmpty()
445
        ;
446
447
        $this->createNewInstance();
448
        $this
449
            ->given($data = [
450
                    '@id' => '/v1/cart_items/16',
451
                    'amount' => 2,
452
                    'cart' => [
453
                        '@id' => '/v1/carts/10',
454
                        'status' => 'waiting',
455
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
456
                    ],
457
                ])
458
459
            ->then
460
                ->object($cartItem = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'))
461
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem')
462
                ->object($cart = $cartItem->getCart())
463
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart')
464
                ->string($cart->getClientPhoneNumber())
465
                    ->isEqualTo('+33 1 23 45 67 89')
466
                ->string($cart->getId())
467
                    ->isEqualTo('/v1/carts/10')
468
                ->string($cart->getStatus())
469
                    ->isEqualTo('waiting')
470
        ;
471
    }
472
473
    public function testSerializeNullValues()
474
    {
475
        $this->createNewInstance();
476
        $this
477
            ->given($cart = $this->createNewCart())
478
                ->and($cart->setStatus(null))
479
                ->and($cart->setOrder(null))
480
            ->then
481
                ->array($data = $this->testedInstance->serialize($cart, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
482
                    ->isIdenticalTo([
483
                        'status' => null,
484
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
485
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
486
                        'cart_items' => [],
487
                        'order' => null,
488
                    ])
489
        ;
490
    }
491
492
    public function testSerializingAttributeNameDiffThanPropertyName()
493
    {
494
        $this->createNewInstance();
495
        $this
496
            ->given($product = $this->createNewProduct())
497
            ->then
498
                ->array($data = $this->testedInstance->serialize($product, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'))
499
                ->isIdenticalTo([
500
                    'product_value' => 8.2,
501
                    'currency' => 'eur',
502
                ])
503
        ;
504
    }
505
506
    public function testWeirdIdentifier()
507
    {
508
        $mapping = $this->getMapping('weirdId');
509
        $this->createNewInstance($mapping);
510
511
        $this
512
            ->given($cart = $this->createCart())
513
                ->and($cartItem = $this->createKnownCartItem())
514
                ->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...
515
516
            ->then
517
                ->array($data = $this->testedInstance->serialize(
518
                    $cart,
519
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
520
                    [ 'serializeRelations' => ['cart_items'] ]
521
                ))
522
                    ->isIdenticalTo([
523
                        'weirdId' => '/v1/carts/8',
524
                        'status' => 'payed',
525
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
526
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
527
                        'cart_items' => [
528
                            [
529
                                'weirdId' => '/v1/cart_items/16',
530
                                'amount' => 1,
531
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
532
                                'data' => [
533
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
534
                                    'who' => 'Jane',
535
                                ],
536
                                'cart' => '/v1/carts/8',
537
                                'product' => '/v1/products/10',
538
                                'cartItemDetailList' => [],
539
                            ],
540
                        ],
541
                        'order' => null,
542
                    ])
543
544
            ->then
545
                ->array($data = $this->testedInstance->serialize(
546
                    $cart,
547
                    'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
548
                    [ 'serializeRelations' => ['cart_items'] ]
549
                ))
550
                    ->isIdenticalTo([
551
                        'weirdId' => '/v1/carts/8',
552
                        'status' => 'payed',
553
                        'clientPhoneNumber' => '+33 1 23 45 67 89',
554
                        'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
555
                        'cart_items' => [
556
                            [
557
                                'weirdId' => '/v1/cart_items/16',
558
                                'amount' => 1,
559
                                'createdAt' => (new \DateTime('2015-11-04 15:13:00'))->format(DateTime::RFC3339),
560
                                'data' => [
561
                                    'when' => (new \DateTime('2015-11-04 15:00:00'))->format(DateTime::RFC3339),
562
                                    'who' => 'Jane',
563
                                ],
564
                                'cart' => '/v1/carts/8',
565
                                'product' => '/v1/products/10',
566
                                'cartItemDetailList' => [],
567
                            ],
568
                        ],
569
                        'order' => null,
570
                    ])
571
572
            // reverse the serialization
573
            ->then
574
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
575
                ->string($cart->getId())
576
                    ->isEqualTo('/v1/carts/8')
577
                ->array($cart->getCartItemList())
578
                    ->size->isEqualTo(1)
579
                // ->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...
580
                //     ->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...
581
                // ->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...
582
                //     ->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...
583
            ;
584
    }
585
586
    public function testDeserializeWithExtraFields()
587
    {
588
        $this->createNewInstance();
589
590
        $this
591
            ->given($data = [
592
                '@foo' => 'bar',
593
                '@id' => '/v1/carts/8',
594
                'status' => 'payed',
595
                "clientPhoneNumber" => '+33 1 23 45 67 89',
596
                'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format(DateTime::RFC3339),
597
                'cart_items' => [],
598
                'order' => null,
599
            ])
600
601
            ->then
602
                ->object($cart = $this->testedInstance->deserialize($data, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'))
603
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart')
604
        ;
605
    }
606
607
    public function testSerializingIriManyToOne()
608
    {
609
        $annotationDriver = new AnnotationDriver(__DIR__ . '/../../cache/');
610
        $mapping = new Mapping();
611
        $mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../../Model/Issue46/'));
612
613
        $section = new Issue46\Section();
614
        $section->setId(46);
615
        $section->setIri('/sections/46');
616
        $section->setTitle('section title');
617
618
        $article = new Issue46\Article();
619
        $article->setSection($section);
620
621
        $this->createNewInstance($mapping);
622
623
        $this
624
            ->then
625
                ->array($this->testedInstance->serialize($article, 'Mapado\RestClientSdk\Tests\Model\Issue46\Article'))
626
                    ->isIdenticalTo([
627
                        'id' => null,
628
                        'section' => '/sections/46',
629
                    ])
630
631
                ->if($article->setIri('/articles/44'))
632
633
                ->array($this->testedInstance
634
                    ->serialize(
635
                        $section,
636
                        'Mapado\RestClientSdk\Tests\Model\Issue46\Section'
637
                    ))
638
                ->isIdenticalTo([
639
                    '@id' => '/sections/46',
640
                    'id' => 46,
641
                    'title' => 'section title',
642
                    'articleList' => [
643
                        '/articles/44',
644
                    ],
645
                ])
646
647
                ->array($this->testedInstance
648
                    ->serialize(
649
                        $section,
650
                        'Mapado\RestClientSdk\Tests\Model\Issue46\Section',
651
                        [ 'serializeRelations' => ['articleList'] ]
652
                    ))
653
                ->isIdenticalTo([
654
                    '@id' => '/sections/46',
655
                    'id' => 46,
656
                    'title' => 'section title',
657
                    'articleList' => [
658
                        [
659
                            '@id' => '/articles/44',
660
                            'id' => null,
661
                            'section' => '/sections/46',
662
                        ]
663
                    ],
664
                ])
665
        ;
666
    }
667
668
    /**
669
     * getMapping
670
     *
671
     * @access private
672
     * @return Mapping
673
     */
674
    private function getMapping($idKey = '@id')
675
    {
676
        $mapping = new Mapping('/v1');
677
        $mapping->setMapping([
678
            $this->getCartMetadata($idKey),
679
            $this->getCartItemMetadata($idKey),
680
            $this->getCartItemDetailMetadata($idKey),
681
            $this->getProductMetadata($idKey),
682
        ]);
683
684
        return $mapping;
685
    }
686
687
    /**
688
     * @param string $idKey
689
     */
690
    private function getProductMetadata($idKey)
691
    {
692
        $productMetadata = new ClassMetadata(
693
            'products',
694
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Product',
695
            ''
696
        );
697
698
        $productMetadata->setAttributeList([
699
            new Attribute($idKey, 'id', 'string', true),
700
            new Attribute('product_value', 'value'),
701
            new Attribute('currency'),
702
        ]);
703
704
        return $productMetadata;
705
    }
706
707
    /**
708
     * @param string $idKey
709
     */
710
    private function getCartItemDetailMetadata($idKey)
711
    {
712
        $cartItemDetailMetadata = new ClassMetadata(
713
            'cart_item_details',
714
            'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail',
715
            ''
716
        );
717
718
        $cartItemDetailMetadata->setRelationList([
719
            new Relation('cartItem', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'),
720
        ]);
721
        $cartItemDetailMetadata->setAttributeList([
722
            new Attribute($idKey, 'id', 'string', true),
723
            new Attribute('name'),
724
            new Attribute('cartItem'),
725
        ]);
726
727
        return $cartItemDetailMetadata;
728
    }
729
730
    /**
731
     * @param string $idKey
732
     */
733
    private function getCartItemMetadata($idKey)
734
    {
735
        $cartItemMetadata = new ClassMetadata(
736
            'cart_items',
737
            'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem',
738
            ''
739
        );
740
741
        $cartItemMetadata->setRelationList([
742
            new Relation('cart', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'),
743
            new Relation('product', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'),
744
            new Relation('cartItemDetailList', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail'),
745
        ]);
746
        $cartItemMetadata->setAttributeList([
747
            new Attribute($idKey, 'id', 'string', true),
748
            new Attribute('amount'),
749
            new Attribute('createdAt', 'createdAt', 'datetime'),
750
            new Attribute('data'),
751
            new Attribute('cart'),
752
            new Attribute('product'),
753
            new Attribute('cartItemDetailList'),
754
        ]);
755
756
        return $cartItemMetadata;
757
    }
758
759
    /**
760
     * @param string $idKey
761
     */
762
    private function getCartMetadata($idKey)
763
    {
764
        $cartMetadata = new ClassMetadata(
765
            'carts',
766
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
767
            ''
768
        );
769
        $cartMetadata->setAttributeList([
770
            new Attribute($idKey, 'id', 'string', true),
771
            new Attribute('status'),
772
            new Attribute('clientPhoneNumber', 'clientPhoneNumber', 'phone_number'),
773
            new Attribute('createdAt', 'createdAt', 'datetime'),
774
            new Attribute('cart_items', 'cartItemList'),
775
            new Attribute('order'),
776
        ]);
777
        $cartMetadata->setRelationList([
778
            new Relation('cart_items', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'),
779
            new Relation('order', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Order'),
780
        ]);
781
782
        return $cartMetadata;
783
    }
784
785
    /**
786
     * createNewCart
787
     *
788
     * @access private
789
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart
790
     */
791
    private function createNewCart()
792
    {
793
        $cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart();
794
        $cart->setStatus('payed');
795
        $cart->setCreatedAt(new DateTime('2015-09-20 12:08:00'));
796
797
        $phoneNumberUtil = PhoneNumberUtil::getInstance();
798
        $clientPhoneNumber = $phoneNumberUtil->parse('+33123456789', PhoneNumberFormat::INTERNATIONAL);
799
        $cart->setClientPhoneNumber($clientPhoneNumber);
800
801
        return $cart;
802
    }
803
804
    /**
805
     * createCart
806
     *
807
     * @access private
808
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart
809
     */
810
    private function createCart()
811
    {
812
        $cart = $this->createNewCart();
813
        $cart->setId('/v1/carts/8');
814
815
        return $cart;
816
    }
817
818
    /**
819
     * createKnownCartItem
820
     *
821
     * @access private
822
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem
823
     */
824
    private function createKnownCartItem()
825
    {
826
        $cartItem = $this->createNewCartItem();
827
        $cartItem->setId('/v1/cart_items/16');
828
        $cartItem->setAmount(1);
829
        $cartItem->setCreatedAt(new DateTime('2015-11-04 15:13:00'));
830
        $cartItem->setData([
831
            'when' => new DateTime('2015-11-04 15:00:00'),
832
            'who' => 'Jane',
833
        ]);
834
        $cartItem->setCart($this->createCart());
835
836
        return $cartItem;
837
    }
838
839
    /**
840
     * createNewCartItem
841
     *
842
     * @access private
843
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem
844
     */
845
    private function createNewCartItem($addKnownedProduct = true)
846
    {
847
        $cartItem = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem();
848
        $cartItem->setAmount(2);
849
        $cartItem->setCreatedAt(new DateTime('2015-09-20 12:11:00'));
850
        $cartItem->setData([
851
            'when' => new DateTime('2015-09-20 15:00:00'),
852
            'who' => 'John',
853
        ]);
854
855
        if ($addKnownedProduct) {
856
            $cartItem->setProduct($this->createKnownedProduct());
857
        }
858
859
        return $cartItem;
860
    }
861
862
    /**
863
     * createNewProduct
864
     *
865
     * @access private
866
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\Product
867
     */
868
    private function createNewProduct()
869
    {
870
        $product = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product();
871
872
        $product->setValue(8.2);
873
        $product->setCurrency('eur');
874
875
        return $product;
876
    }
877
878
879
    /**
880
     * createKnownedProduct
881
     *
882
     * @access private
883
     * @return \Mapado\RestClientSdk\Tests\Model\JsonLd\Product
884
     */
885
    private function createKnownedProduct()
886
    {
887
        $product = $this->createNewProduct();
888
        $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

888
        $product->setId(/** @scrutinizer ignore-type */ '/v1/products/10');
Loading history...
889
890
        return $product;
891
    }
892
893
    private function createNewCartItemDetail()
894
    {
895
        $item = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail();
896
897
        $item->setName('Bill');
898
899
        return $item;
900
    }
901
902
    /**
903
     * createNewInstance
904
     *
905
     * @access private
906
     * @param Mapping $mapping
907
     * @return void
908
     */
909
    private function createNewInstance($mapping = null)
910
    {
911
        $mapping = $mapping ?: $this->getMapping();
912
        $unitOfWork = new UnitOfWork($mapping);
913
        $this->newTestedInstance($mapping, $unitOfWork);
914
915
        $this->mockGenerator->orphanize('__construct');
916
        $this->mockGenerator->shuntParentClassCalls();
917
        $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...
918
        $this->mockGenerator->unshuntParentClassCalls();
919
        $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...
920
        $sdk->setFileCachePath(__DIR__ . '/../../cache/');
921
922
        $cartRepositoryMock = $this->getCartRepositoryMock($sdk, $restClient, $unitOfWork, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart');
923
924
        $this->calling($sdk)->getRepository = function ($modelName) use ($cartRepositoryMock) {
925
            switch ($modelName) {
926
                case 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart':
927
                    return $cartRepositoryMock;
928
                default:
929
                    return null;
930
            }
931
        };
932
933
        $this->testedInstance->setSdk($sdk);
934
    }
935
936
    /**
937
     * @param string $modelName
938
     * @param UnitOfWork $unitOfWork
939
     */
940
    private function getCartRepositoryMock($sdk, $restClient, $unitOfWork, $modelName)
941
    {
942
        $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...
943
            $sdk,
944
            $restClient,
945
            $unitOfWork,
946
            $modelName
947
        );
948
949
        $_this = $this;
950
951
        $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

951
        $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...
952
            return $_this->createCart();
953
        };
954
955
        return $repository;
956
    }
957
}
958