Completed
Push — master ( bc4c8d...a20a70 )
by
unknown
01:45
created

UnitOfWork::testNoMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 34

Duplication

Lines 69
Ratio 100 %

Importance

Changes 0
Metric Value
dl 69
loc 69
rs 9.2083
c 0
b 0
f 0
cc 1
eloc 34
nc 1
nop 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace Mapado\RestClientSdk\Tests\Units;
4
5
use atoum;
6
use Mapado\RestClientSdk\Mapping as RestMapping;
7
use Mapado\RestClientSdk\Mapping\ClassMetadata;
8
use Mapado\RestClientSdk\Mapping\Attribute;
9
use Mapado\RestClientSdk\Mapping\Relation;
10
11
/**
12
 * UnitOfWork
13
 *
14
 * @uses atoum
15
 * @author Julien Petit <[email protected]>
16
 */
17
class UnitOfWork extends atoum
18
{
19
    private $unitOfWork;
0 ignored issues
show
Unused Code introduced by
The property $unitOfWork is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
21
    public function testRegister()
22
    {
23
        $mapping = $this->getMapping();
24
        $unitOfWork = $this->newTestedInstance($mapping);
25
26
        $ticket = (object) [
27
            'firstname' => 'foo',
28
            'lastname' => 'bar',
29
            'email' => '[email protected]',
30
        ];
31
        $unitOfWork->registerClean('@id1', $ticket);
32
        $this
33
            ->then
34
                ->variable($unitOfWork->getDirtyEntity('@id1'))
35
                    ->isEqualTo($ticket)
36
        ;
37
    }
38
39 View Code Duplication
    public function testNoChanges()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $mapping = $this->getMapping();
42
        $unitOfWork = $this->newTestedInstance($mapping);
43
44
        $this
45
            ->given($newSerializedModel = [
46
                '@id' => '/v12/carts/1',
47
                'cartItemList' => [
48
                    '/v12/cart_items/1',
49
                    '/v12/cart_items/2',
50
                    '/v12/cart_items/3',
51
                ],
52
            ])
53
            ->then
54
                ->variable($unitOfWork->getDirtyData(
55
                    $newSerializedModel,
56
                    [
57
                        '@id' => '/v12/carts/1',
58
                        'cartItemList' => [
59
                            '/v12/cart_items/1',
60
                            '/v12/cart_items/2',
61
                            '/v12/cart_items/3',
62
                        ],
63
                    ],
64
                    $this->getCartMetadata()
65
                ))
66
                ->isEqualTo(
67
                    [
68
                    ]
69
                )
70
            ->then
71
                ->variable($unitOfWork->getDirtyData(
72
                    [
73
                        '@id' => '/v12/carts/1',
74
                        'cartItemList' => [
75
                            [
76
                                '@id' => '/v12/cart_items/1',
77
                                'amount' => 1,
78
                            ],
79
                            [
80
                                '@id' => '/v12/cart_items/2',
81
                                'amount' => 1,
82
                            ],
83
                        ],
84
                    ],
85
                    [
86
                        '@id' => '/v12/carts/1',
87
                        'cartItemList' => [
88
                            [
89
                                '@id' => '/v12/cart_items/1',
90
                                'amount' => 1,
91
                            ],
92
                            [
93
                                '@id' => '/v12/cart_items/2',
94
                                'amount' => 1,
95
                            ],
96
                        ],
97
                    ],
98
                    $this->getCartMetadata()
99
                ))
100
                ->isEqualTo(
101
                    [
102
                    ]
103
                )
104
        ;
105
    }
106
107 View Code Duplication
    public function testNoMetadata()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $mapping = $this->getMapping();
110
        $unitOfWork = $this->newTestedInstance($mapping);
111
112
        $this
113
            ->given($newSerializedModel = [
114
                '@id' => '/v12/carts/1',
115
                'cartInfo' => [
116
                    [
117
                        'firstname' => 'john',
118
                        'lastname' => 'doe',
119
                    ],
120
                ],
121
            ])
122
            ->then
123
                ->variable($unitOfWork->getDirtyData(
124
                    $newSerializedModel,
125
                    [
126
                        '@id' => '/v12/carts/1',
127
                        'cartInfo' => [
128
                            [
129
                                'firstname' => 'john',
130
                                'lastname' => 'doe',
131
                            ],
132
                        ],
133
                    ],
134
                    $this->getCartMetadata()
135
                ))
136
                ->isEqualTo(
137
                    [
138
                    ]
139
                )
140
            ->then
141
                ->variable($unitOfWork->getDirtyData(
142
                    [
143
                        '@id' => '/v12/carts/1',
144
                        'cartItemList' => [
145
                            [
146
                                '@id' => '/v12/cart_items/1',
147
                                'amount' => 1,
148
                            ],
149
                            [
150
                                '@id' => '/v12/cart_items/2',
151
                                'amount' => 1,
152
                            ],
153
                        ],
154
                    ],
155
                    [
156
                        '@id' => '/v12/carts/1',
157
                        'cartItemList' => [
158
                            [
159
                                '@id' => '/v12/cart_items/1',
160
                                'amount' => 1,
161
                            ],
162
                            [
163
                                '@id' => '/v12/cart_items/2',
164
                                'amount' => 1,
165
                            ],
166
                        ],
167
                    ],
168
                    $this->getCartMetadata()
169
                ))
170
                ->isEqualTo(
171
                    [
172
                    ]
173
                )
174
        ;
175
    }
176
177
    public function testRemoveItem()
178
    {
179
        $mapping = $this->getMapping();
180
        $unitOfWork = $this->newTestedInstance($mapping);
181
182
        $this
183
            ->then
184
                ->variable($unitOfWork->getDirtyData(
185
                    [
186
                        '@id' => '/v12/carts/1',
187
                        'cartItemList' => [
188
                            '/v12/cart_items/1',
189
                            '/v12/cart_items/2',
190
                        ],
191
                    ],
192
                    [
193
                        '@id' => '/v12/carts/1',
194
                        'cartItemList' => [
195
                            '/v12/cart_items/1',
196
                            '/v12/cart_items/2',
197
                            '/v12/cart_items/3',
198
                        ],
199
                    ],
200
                    $this->getCartMetadata()
201
                ))
202
                ->isEqualTo(
203
                    [
204
                        'cartItemList' => [
205
                            '/v12/cart_items/1',
206
                            '/v12/cart_items/2',
207
                        ],
208
                    ]
209
                )
210
            ->then
211
                ->variable($unitOfWork->getDirtyData(
212
                    [
213
                        '@id' => '/v12/carts/1',
214
                        'cartItemList' => [
215
                            '/v12/cart_items/1',
216
                            '/v12/cart_items/3',
217
                        ],
218
                    ],
219
                    [
220
                        '@id' => '/v12/carts/1',
221
                        'cartItemList' => [
222
                            '/v12/cart_items/1',
223
                            '/v12/cart_items/2',
224
                            '/v12/cart_items/3',
225
                        ],
226
                    ],
227
                    $this->getCartMetadata()
228
                ))
229
                ->isEqualTo(
230
                    [
231
                        'cartItemList' => [
232
                            '/v12/cart_items/1',
233
                            '/v12/cart_items/3',
234
                        ],
235
                    ]
236
                )
237
            ->then
238
                ->variable($unitOfWork->getDirtyData(
239
                    [
240
                        '@id' => '/v12/carts/1',
241
                        'cartItemList' => [
242
                            [
243
                                '@id' => '/v12/cart_items/1',
244
                                'amount' => 2,
245
                            ],
246
                        ],
247
                    ],
248
                    [
249
                        '@id' => '/v12/carts/1',
250
                        'cartItemList' => [
251
                            [
252
                                '@id' => '/v12/cart_items/1',
253
                                'amount' => 2,
254
                            ],
255
                            [
256
                                '@id' => '/v12/cart_items/2',
257
                                'amount' => 1,
258
                            ],
259
                        ],
260
                    ],
261
                    $this->getCartMetadata()
262
                ))
263
                ->isEqualTo(
264
                    [
265
                        'cartItemList' => [
266
                            [
267
                                '@id' => '/v12/cart_items/1',
268
                            ],
269
                        ],
270
                    ]
271
                )
272
            ->then
273
                ->variable($unitOfWork->getDirtyData(
274
                    [
275
                        '@id' => '/v12/carts/1',
276
                        'cartItemList' => [
277
                            [
278
                                '@id' => '/v12/cart_items/2',
279
                                'amount' => 1,
280
                            ],
281
                        ],
282
                    ],
283
                    [
284
                        '@id' => '/v12/carts/1',
285
                        'cartItemList' => [
286
                            [
287
                                '@id' => '/v12/cart_items/1',
288
                                'amount' => 2,
289
                            ],
290
                            [
291
                                '@id' => '/v12/cart_items/2',
292
                                'amount' => 1,
293
                            ],
294
                        ],
295
                    ],
296
                    $this->getCartMetadata()
297
                ))
298
                ->isEqualTo(
299
                    [
300
                        'cartItemList' => [
301
                            [
302
                                '@id' => '/v12/cart_items/2',
303
                                'amount' => 1,
304
                            ],
305
                        ],
306
                    ]
307
                )
308
        ;
309
    }
310
311
    public function testAddItem()
312
    {
313
        $mapping = $this->getMapping();
314
        $unitOfWork = $this->newTestedInstance($mapping);
315
316
        $this
317
            ->then
318
                ->variable($unitOfWork->getDirtyData(
319
                    [
320
                        '@id' => '/v12/carts/1',
321
                        'cartItemList' => [
322
                            '/v12/cart_items/1',
323
                            '/v12/cart_items/2',
324
                            '/v12/cart_items/3',
325
                        ],
326
                    ],
327
                    [
328
                        '@id' => '/v12/carts/1',
329
                        'cartItemList' => [
330
                            '/v12/cart_items/1',
331
                            '/v12/cart_items/2',
332
                        ],
333
                    ],
334
                    $this->getCartMetadata()
335
                ))
336
                ->isEqualTo(
337
                    [
338
                        'cartItemList' => [
339
                            '/v12/cart_items/1',
340
                            '/v12/cart_items/2',
341
                            '/v12/cart_items/3',
342
                        ],
343
                    ]
344
                )
345
            ->then
346
                ->variable($unitOfWork->getDirtyData(
347
                    [
348
                        '@id' => '/v12/carts/1',
349
                        'cartItemList' => [
350
                            [
351
                                '@id' => '/v12/cart_items/1',
352
                                'amount' => 1,
353
                            ],
354
                            [
355
                                '@id' => '/v12/cart_items/2',
356
                                'amount' => 1,
357
                            ],
358
                        ],
359
                    ],
360
                    [
361
                        '@id' => '/v12/carts/1',
362
                        'cartItemList' => [
363
                            [
364
                                '@id' => '/v12/cart_items/1',
365
                                'amount' => 1,
366
                            ],
367
                        ],
368
                    ],
369
                    $this->getCartMetadata()
370
                ))
371
                ->isEqualTo(
372
                    [
373
                        'cartItemList' => [
374
                            [
375
                                '@id' => '/v12/cart_items/1',
376
                            ],
377
                            [
378
                                '@id' => '/v12/cart_items/2',
379
                                'amount' => 1,
380
                            ],
381
                        ],
382
                    ]
383
                )
384
        ;
385
    }
386
387
    public function testSendOnlyDirty()
388
    {
389
        $mapping = $this->getMapping();
390
        $unitOfWork = $this->newTestedInstance($mapping);
391
392
        $this
393
            ->then
394
                ->variable($unitOfWork->getDirtyData(
395
                    [
396
                        '@id' => '/v12/carts/1',
397
                        'cartItemList' => [
398
                            [
399
                                '@id' => '/v12/cart_items/1',
400
                                'amount' => 2,
401
                            ],
402
                            [
403
                                '@id' => '/v12/cart_items/2',
404
                                'amount' => 1,
405
                            ],
406
                        ],
407
                    ],
408
                    [
409
                        '@id' => '/v12/carts/1',
410
                        'cartItemList' => [
411
                            [
412
                                '@id' => '/v12/cart_items/1',
413
                                'amount' => 1,
414
                            ],
415
                            [
416
                                '@id' => '/v12/cart_items/2',
417
                                'amount' => 1,
418
                            ],
419
                        ],
420
                    ],
421
                    $this->getCartMetadata()
422
                ))
423
                ->isEqualTo(
424
                    [
425
                        'cartItemList' => [
426
                            [
427
                                '@id' => '/v12/cart_items/1',
428
                                'amount' => 2,
429
                            ],
430
                            [
431
                                '@id' => '/v12/cart_items/2',
432
                            ],
433
                        ],
434
                    ]
435
                )
436
             ->then
437
                ->variable($unitOfWork->getDirtyData(
438
                    [
439
                        '@id' => '/v12/carts/1',
440
                        'status' => 'ok',
441
                        'clientPhoneNumber' => '+33123456789',
442
                    ],
443
                    [
444
                        '@id' => '/v12/carts/1',
445
                        'status' => 'ko',
446
                        'clientPhoneNumber' => '+33123456789',
447
                    ],
448
                    $this->getCartMetadata()
449
                ))
450
                ->isEqualTo(
451
                    [
452
                        'status' => 'ok',
453
                    ]
454
                )
455
            ->then
456
                ->variable($unitOfWork->getDirtyData(
457
                    [
458
                        'cartItemList' => [
459
                            [
460
                                '@id' => '/v12/cart_items/1',
461
                                'amount' => 1,
462
                                'cartItemDetailList' => [
463
                                    [
464
                                        '@id' => '/v12/cart_item_detail/1',
465
                                        'name' => '',
466
                                    ],
467
                                ],
468
                            ],
469
                            [
470
                                '@id' => '/v12/cart_items/2',
471
                                'amount' => 1,
472
                            ],
473
                            [
474
                                '@id' => '/v12/cart_items/3',
475
                                'amount' => 1,
476
                                'cartItemDetailList' => [
477
                                    [
478
                                        '@id' => '/v12/cart_item_detail/2',
479
                                        'name' => '',
480
                                    ],
481
                                ],
482
                            ],
483
                        ],
484
                    ],
485
                    [
486
                        'cartItemList' => [
487
                            [
488
                                '@id' => '/v12/cart_items/1',
489
                                'amount' => 2,
490
                                'cartItemDetailList' => [
491
                                    '@id' => '/v12/cart_item_detail/1',
492
                                    'name' => 'foo',
493
                                ],
494
                            ],
495
                            [
496
                                '@id' => '/v12/cart_items/3',
497
                                'amount' => 1,
498
                            ],
499
                        ],
500
                    ],
501
                    $this->getCartMetadata()
502
                ))
503
                ->isEqualTo(
504
                    [
505
                        'cartItemList' => [
506
                            [
507
                                'amount' => 1,
508
                                'cartItemDetailList' => [
509
                                    [
510
                                        '@id' => '/v12/cart_item_detail/1',
511
                                        'name' => '',
512
                                    ],
513
                                ],
514
                                '@id' => '/v12/cart_items/1',
515
                            ],
516
                            [
517
                                '@id' => '/v12/cart_items/2',
518
                            ],
519
                            [
520
                                '@id' => '/v12/cart_items/3',
521
                                'amount' => 1,
522
                                'cartItemDetailList' => [
523
                                    [
524
                                        '@id' => '/v12/cart_item_detail/2',
525
                                        'name' => '',
526
                                    ]
527
                                ],
528
                            ],
529
                        ]
530
                    ]
531
                )
532
        ;
533
    }
534
535
    private function getMapping()
536
    {
537
        $mapping = $mapping = new RestMapping();
538
        $mapping->setMapping([
539
            $this->getCartMetadata(),
540
            $this->getCartItemMetadata(),
541
            $this->getCartItemDetailMetadata(),
542
        ]);
543
544
        return $mapping;
545
    }
546
547 View Code Duplication
    private function getCartMetadata()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
548
    {
549
        $cartMetadata = new ClassMetadata(
550
            'carts',
551
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
552
            ''
553
        );
554
555
        $cartMetadata->setAttributeList([
556
            new Attribute('@id', 'id', 'string', true),
557
            new Attribute('status'),
558
            new Attribute('clientPhoneNumber', 'clientPhoneNumber', 'phone_number'),
559
            new Attribute('createdAt', 'createdAt', 'datetime'),
560
            new Attribute('cart_items', 'cartItemList'),
561
            new Attribute('order'),
562
        ]);
563
564
        $cartMetadata->setRelationList([
565
            new Relation('cartItemList', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'),
566
        ]);
567
568
        return $cartMetadata;
569
    }
570
571 View Code Duplication
    private function getCartItemMetadata()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
572
    {
573
        $cartItemMetadata = new ClassMetadata(
574
            'cart_items',
575
            'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem',
576
            ''
577
        );
578
579
        $cartItemMetadata->setAttributeList([
580
            new Attribute('@id', 'id', 'string', true),
581
            new Attribute('amount'),
582
            new Attribute('cart_item_details', 'cartItemDetailList'),
583
        ]);
584
585
        $cartItemMetadata->setRelationList([
586
            new Relation('cartItemDetailList', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail'),
587
        ]);
588
589
        return $cartItemMetadata;
590
    }
591
592 View Code Duplication
    private function getCartItemDetailMetadata()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
593
    {
594
        $cartItemDetailMetadata = new ClassMetadata(
595
            'cart_item_details',
596
            'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail',
597
            ''
598
        );
599
600
        $cartItemDetailMetadata->setAttributeList([
601
            new Attribute('@id', 'id', 'string', true),
602
            new Attribute('name'),
603
        ]);
604
605
        return $cartItemDetailMetadata;
606
    }
607
}
608