Completed
Pull Request — master (#58)
by
unknown
06:38
created

UnitOfWork::getMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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