Completed
Push — master ( e99f14...bc9af2 )
by Julien
02:56
created

EntityRepository::testFindOneByWithoutResult()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.8571
cc 1
eloc 23
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\Attribute;
8
use Mapado\RestClientSdk\Mapping\ClassMetadata;
9
use Mapado\RestClientSdk\Mapping\Driver\AnnotationDriver;
10
use Symfony\Component\Cache\Adapter\ArrayAdapter;
11
12
/**
13
 * Class EntityRepository
14
 * @author Julien Deniau <[email protected]>
15
 */
16
class EntityRepository extends atoum
17
{
18
    private $mockedRestClient;
19
20
    private $mockedSdk;
21
22
    private $mockedHydrator;
23
24
    private $repository;
25
26
    private $mapping;
27
28
    public function beforeTestMethod($method)
29
    {
30
        $this->mockGenerator->orphanize('__construct');
31
        $this->mockedSdk = new \mock\Mapado\RestClientSdk\SdkClient();
32
        $mockedHydrator = new \mock\Mapado\RestClientSdk\Model\ModelHydrator($this->mockedSdk);
33
        $this->calling($this->mockedSdk)->getModelHydrator = $mockedHydrator;
34
35
        $this->mockGenerator->orphanize('__construct');
36
        $this->mockedRestClient = new \mock\Mapado\RestClientSdk\RestClient();
37
        // $this->resetMock($this->mockedRestClient);
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...
38
39
        $this->mockedHydrator = new \mock\Mapado\RestClientSdk\Model\ModelHydrator($this->mockedSdk);
40
        $this->calling($this->mockedSdk)->getModelHydrator = $this->mockedHydrator;
41
42
        $this->mapping = new RestMapping('v12');
43
        $this->mapping->setMapping([
44
            new ClassMetadata(
45
                'orders',
46
                'Mapado\RestClientSdk\Tests\Model\JsonLd\Model',
47
                'mock\Mapado\RestClientSdk\EntityRepository'
48
            ),
49
        ]);
50
51
        $this->calling($this->mockedSdk)->getMapping = $this->mapping;
52
53
        $this->repository = new \mock\Mapado\RestClientSdk\EntityRepository(
54
            $this->mockedSdk,
55
            $this->mockedRestClient,
56
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Model'
57
        );
58
    }
59
60
    /**
61
     * testFind
62
     *
63
     * @access public
64
     * @return void
65
     */
66
    public function testFind()
67
    {
68
        $this->calling($this->mockedRestClient)->get = [];
69
70
        $this
71
            ->if($this->repository->find('1'))
72
            ->then
73
                ->mock($this->mockedRestClient)
74
                    ->call('get')
75
                        ->withArguments('v12/orders/1')->once()
76
77
            ->given($this->resetMock($this->mockedRestClient))
78
            ->if($this->repository->find('v12/orders/999'))
79
            ->then
80
                ->mock($this->mockedRestClient)
81
                    ->call('get')
82
                        ->withArguments('v12/orders/999')->once()
83
84
            ->if($this->repository->findAll())
85
            ->then
86
                ->mock($this->mockedRestClient)
87
                    ->call('get')
88
                        ->withArguments('v12/orders')->once()
89
90
            ->if($this->repository->findOneByFoo('bar'))
91
            ->then
92
                ->mock($this->mockedRestClient)
93
                    ->call('get')
94
                        ->withArguments('v12/orders?foo=bar')->once()
95
                ->mock($this->mockedHydrator)
96
                    ->call('hydrate')
97
                        ->twice()
98
99
            ->if($this->repository->findByFoo('baz'))
100
            ->then
101
                ->mock($this->mockedRestClient)
102
                    ->call('get')
103
                        ->withArguments('v12/orders?foo=baz')->once()
104
                ->mock($this->mockedHydrator)
105
                    ->call('hydrateList')
106
                        ->twice()
107
        ;
108
    }
109
110
    /**
111
     * testFindWithQueryParameters
112
     *
113
     * @access public
114
     * @return void
115
     */
116
    public function testFindWithQueryParameters()
117
    {
118
        $this->calling($this->mockedRestClient)->get = [];
119
120
        $this
121
            ->if($this->repository->find('1', [ 'foo' => 'bar', 'bar'  => 'baz' ]))
122
            ->then
123
                ->mock($this->mockedRestClient)
124
                    ->call('get')
125
                        ->withArguments('v12/orders/1?foo=bar&bar=baz')->once()
126
        ;
127
    }
128
129
    /**
130
     * testFindWithCache
131
     *
132
     * @access public
133
     * @return void
134
     */
135
    public function testFindWithCache()
136
    {
137
        $mockOrder1 = new \mock\entity;
138
        $mockOrder2 = new \mock\entity;
139
        $mockOrder3 = new \mock\entity;
140
        $this->calling($mockOrder1)->getId = 'v12/orders/1';
141
        $this->calling($mockOrder2)->getId = 'v12/orders/2';
142
        $this->calling($mockOrder3)->getId = 'v12/orders/3';
143
144
        $this->calling($this->mockedHydrator)->hydrate = $mockOrder1;
145
        $this->calling($this->mockedHydrator)->hydrateList = [$mockOrder1, $mockOrder2, $mockOrder3];
146
147
        $arrayAdapter = new ArrayAdapter(0, false);
148
        $this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter;
149
        $this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_';
150
151
        $this->calling($this->mockedRestClient)->get = [];
152
153
        $this->calling($this->mockedHydrator)->convertId[0] = 'v12/orders/1';
154
        $this->calling($this->mockedHydrator)->convertId[1] = 'v12/orders/1';
155
        $this->calling($this->mockedHydrator)->convertId[4] = 'v12/orders/3';
156
157
        $this
158
            ->if($this->repository->find(1))
159
            ->and($this->repository->find(1))
160
            ->and($this->repository->find(1, ['foo' => 'bar']))
161
            ->then
162
                ->mock($this->mockedRestClient)
163
                    ->call('get')
164
                        ->withArguments('v12/orders/1')->once()
165
                    ->call('get')
166
                        ->withArguments('v12/orders/1?foo=bar')->once()
167
168
            // find all
169
            ->if($this->repository->findAll())
170
            ->and($this->repository->findAll())
171
            ->if($this->repository->find(3))
172
            ->then
173
                ->mock($this->mockedRestClient)
174
                    ->call('get')
175
                        ->withArguments('v12/orders')->once()
176
                    ->call('get')
177
                        ->withArguments('v12/orders/3')->never()
178
179
            // find by
180
            ->given($this->resetMock($this->mockedRestClient))
181
                ->and($this->mockedSdk->getCacheItemPool()->clear())
182
183
            ->if($this->repository->findBy([ 'foo' => 'bar', 'bar'  => 'baz' ]))
184
            ->and($this->repository->findBy([ 'foo' => 'bar', 'bar'  => 'baz' ]))
185
            ->if($this->repository->find(1))
186
            ->then
187
                ->mock($this->mockedRestClient)
188
                    ->call('get')
189
                        ->withArguments('v12/orders?foo=bar&bar=baz')->once()
190
                    ->call('get')
191
                        ->withArguments('v12/orders/1')->never()
192
193
            // find by something
194
            ->given($this->resetMock($this->mockedRestClient))
195
196
            ->if($this->repository->findByBar('baz'))
197
                ->and($this->repository->findByBar('baz'))
198
                ->and($this->repository->find(1))
199
            ->then
200
                ->mock($this->mockedRestClient)
201
                    ->call('get')
202
                        ->withArguments('v12/orders?bar=baz')->once()
203
                    ->call('get')
204
                        ->withArguments('v12/orders/1')->never()
205
206
            // find one by
207
            ->given($this->resetMock($this->mockedRestClient))
208
209
            ->if($this->repository->findOneBy([ 'foo' => 'baz', 'bar'  => 'bar' ]))
210
            ->and($this->repository->findOneBy([ 'foo' => 'baz', 'bar'  => 'bar' ]))
211
            ->then
212
                ->mock($this->mockedRestClient)
213
                    ->call('get')
214
                        ->withArguments('v12/orders?foo=baz&bar=bar')->once()
215
216
            // find one by thing
217
            ->given($this->resetMock($this->mockedRestClient))
218
219
            ->if($this->repository->findOneByFoo('bar'))
220
            ->and($this->repository->findOneByFoo('bar'))
221
            ->then
222
                ->mock($this->mockedRestClient)
223
                    ->call('get')
224
                        ->withArguments('v12/orders?foo=bar')->once()
225
226
            // find one by with data already in cache
227
            ->given($this->resetMock($this->mockedRestClient))
228
            ->if($this->repository->findOneBy([ 'foo' => 'bar', 'bar'  => 'baz' ]))
229
            ->then
230
                ->mock($this->mockedRestClient)
231
                    ->call('get')
232
                        ->withArguments('v12/orders?foo=bar&bar=baz')->never()
233
        ;
234
    }
235
236
    /**
237
     * testClearCacheAfterUpdate
238
     *
239
     * @access public
240
     *
241
     * @return void
242
     */
243
    public function testClearCacheAfterUpdate()
244
    {
245
        $mapping = new RestMapping('/v12');
246
        $mapping->setMapping([
247
            new ClassMetadata(
248
                'products',
249
                'Mapado\RestClientSdk\Tests\Model\JsonLd\Product',
250
                'mock\Mapado\RestClientSdk\EntityRepository'
251
            ),
252
        ]);
253
254
        $this->calling($this->mockedSdk)->getMapping = $mapping;
255
        $this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping);
256
257
258
        $product1 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product;
259
        $product2 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product;
260
        $product3 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product;
261
        $product1->setId('/v12/products/1');
262
        $product2->setId('/v12/products/2');
263
        $product3->setId('/v12/products/3');
264
265
        $this->calling($this->mockedHydrator)->hydrate = $product1;
266
        $this->calling($this->mockedHydrator)->hydrateList = [$product1, $product2, $product3];
267
268
        $arrayAdapter = new ArrayAdapter(0, false);
269
        $this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter;
270
        $this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_';
271
272
        $this->calling($this->mockedRestClient)->get = $product1;
273
        $this->calling($this->mockedRestClient)->put = $product1;
274
        $this->calling($this->mockedRestClient)->delete = null;
275
276
        $repository = new \mock\Mapado\RestClientSdk\EntityRepository(
277
            $this->mockedSdk,
278
            $this->mockedRestClient,
279
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'
280
        );
281
282
        $this
283
            ->if($repository->find(1))
284
            ->then
285
                ->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1'))
286
                    ->isTrue()
287
288
            ->if($repository->find(1))
289
            ->then
290
                ->mock($this->mockedRestClient)
291
                    ->call('get')
292
                        ->withArguments('/v12/products/1')->once()
293
294
            // after update
295
            ->if($repository->update($product1))
296
                ->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1'))
297
                    ->isFalse()
298
299
            ->if($repository->find(1))
300
            ->then
301
                ->mock($this->mockedRestClient)
302
                    ->call('get')
303
                        ->withArguments('/v12/products/1')->twice()
304
305
            // after deletion
306
            ->if($repository->remove($product1))
307
            ->then
308
                ->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1'))
309
                    ->isFalse()
310
        ;
311
    }
312
313
    public function testCacheWithIriAsId()
314
    {
315
        $annotationDriver = new AnnotationDriver(__DIR__ . '/../cache/');
316
        $mapping = new RestMapping();
317
        $mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../Model/Issue46/'));
318
319
        $this->calling($this->mockedSdk)->getMapping = $mapping;
320
        $this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping);
321
322
        $section1 = new \Mapado\RestClientSdk\Tests\Model\Issue46\Section;
323
        $section1->setIri('/sections/1');
324
325
        $this->calling($this->mockedHydrator)->hydrate = $section1;
326
        $this->calling($this->mockedHydrator)->hydrateList = [$section1];
327
328
        $arrayAdapter = new ArrayAdapter(0, false);
329
        $this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter;
330
        $this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_';
331
332
        $this->calling($this->mockedRestClient)->get = $section1;
333
        $this->calling($this->mockedRestClient)->put = $section1;
334
        $this->calling($this->mockedRestClient)->delete = null;
335
336
        $repository = new \mock\Mapado\RestClientSdk\EntityRepository(
337
            $this->mockedSdk,
338
            $this->mockedRestClient,
339
            'Mapado\RestClientSdk\Tests\Model\Issue46\Section'
340
        );
341
342
        $this
343
            ->if($repository->findBy(['section' => $section1]))
344
           ->then
345
               ->mock($this->mockedRestClient)
346
                   ->call('get')
347
                       ->withArguments('/sections?section=%2Fsections%2F1')->once()
348
349
            ->if($repository->findAll())
350
            ->then
351
                ->boolean($arrayAdapter->hasItem('test_prefix__sections_1'))
352
                    ->isTrue()
353
354
           ->if($repository->find(1))
355
           ->then
356
               ->mock($this->mockedRestClient)
357
                   ->call('get')
358
                       ->withArguments('/sections/1')->never()
359
360
            // after update
361
            ->if($repository->update($section1))
362
                ->boolean($arrayAdapter->hasItem('test_prefix__sections_1'))
363
                    ->isFalse()
364
            ->then
365
                ->mock($this->mockedRestClient)
366
                    ->call('put')
367
                        ->withArguments('/sections/1')->once()
368
369
            ->if($repository->find(1))
370
            ->then
371
                ->mock($this->mockedRestClient)
372
                    ->call('get')
373
                        ->withArguments('/sections/1')->once()
374
375
            // after deletion
376
            ->if($repository->remove($section1))
377
            ->then
378
                ->boolean($arrayAdapter->hasItem('test_prefix__sections_1'))
379
                    ->isFalse()
380
        ;
381
    }
382
383
    /**
384
     * testFindNotFound
385
     *
386
     * @access public
387
     * @return void
388
     */
389
    public function testFindNotFound()
390
    {
391
        $this->calling($this->mockedRestClient)->get = null;
392
393
        $this
394
            ->variable($this->repository->find('1'))
395
            ->isNull()
396
        ;
397
    }
398
399
    public function testFindOneByObject()
400
    {
401
        $mapping = new RestMapping('v12');
402
        $mapping->setMapping([
403
            new ClassMetadata(
404
                'carts',
405
                'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
406
                'mock\Mapado\RestClientSdk\EntityRepository'
407
            ),
408
            new ClassMetadata(
409
                'cart_items',
410
                'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem',
411
                'mock\Mapado\RestClientSdk\EntityRepository'
412
            ),
413
        ]);
414
415
        $this->calling($this->mockedSdk)->getMapping = $mapping;
416
417
        $this->calling($this->mockedRestClient)->get = [];
418
419
        $cartItemRepository = new \mock\Mapado\RestClientSdk\EntityRepository(
420
            $this->mockedSdk,
421
            $this->mockedRestClient,
422
            'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'
423
        );
424
425
426
        $cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart;
427
        $cart->setId(1);
428
429
        $this
430
            ->given($cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart)
431
                ->and($cart->setId(1))
432
            ->if($cartItemRepository->findOneByCart($cart))
433
            ->then
434
                ->mock($this->mockedRestClient)
435
                    ->call('get')
436
                        ->withArguments('v12/cart_items?cart=1')->once()
437
438
            // test with unmapped class
439
            ->given($cart = new \mock\stdClass)
440
            ->if($cartItemRepository->findOneByCart($cart))
441
            ->then
442
                ->mock($this->mockedRestClient)
443
                    ->call('get')
444
                        ->withArguments('v12/cart_items?')->once()
445
        ;
446
    }
447
448
    public function testWithoutMappingPrefix()
449
    {
450
        $mapping = new RestMapping('/v12');
0 ignored issues
show
Unused Code introduced by
$mapping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
451
        $mapping = new RestMapping();
452
        $mapping->setMapping([
453
            new ClassMetadata(
454
                'carts',
455
                'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart',
456
                'mock\Mapado\RestClientSdk\EntityRepository'
457
            ),
458
            new ClassMetadata(
459
                'cart_items',
460
                'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem',
461
                'mock\Mapado\RestClientSdk\EntityRepository'
462
            ),
463
        ]);
464
465
        $this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping);
466
        $this->calling($this->mockedSdk)->getMapping = $mapping;
467
468
        $this->calling($this->mockedRestClient)->get = [];
469
        $this->calling($this->mockedRestClient)->post = [];
470
471
        $cartItemRepository = new \mock\Mapado\RestClientSdk\EntityRepository(
472
            $this->mockedSdk,
473
            $this->mockedRestClient,
474
            'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'
475
        );
476
477
478
        $cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart;
479
        $cart->setId(1);
480
481
        $this
482
            ->if($cartItemRepository->find(1))
483
            ->then
484
                ->mock($this->mockedRestClient)
485
                    ->call('get')
486
                        ->withArguments('/cart_items/1')->once()
487
488
            ->if($cartItemRepository->findAll())
489
            ->then
490
                ->mock($this->mockedRestClient)
491
                    ->call('get')
492
                        ->withArguments('/cart_items')->once()
493
494
            ->if($cartItemRepository->findBy(['foo' => 'bar']))
495
            ->then
496
                ->mock($this->mockedRestClient)
497
                    ->call('get')
498
                        ->withArguments('/cart_items?foo=bar')->once()
499
500
            ->given($cartItem = new \mock\Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem)
501
            ->if($cartItemRepository->persist($cartItem))
502
            ->then
503
                ->mock($this->mockedRestClient)
504
                    ->call('post')
505
                        ->withArguments('/cart_items')->once()
506
        ;
507
    }
508
509
    public function testFindOneByWithHal()
510
    {
511
        $mapping = new RestMapping('v12');
512
        $classMetadata = new ClassMetadata(
513
            'orders',
514
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Order',
515
            'mock\Mapado\RestClientSdk\EntityRepository'
516
        );
517
        $classMetadata->setAttributeList([
518
            new Attribute('@id', 'id', 'string', true),
519
        ]);
520
        $mapping->setMapping([$classMetadata]);
521
522
        $this->calling($this->mockedSdk)->getMapping = $mapping;
523
524
        $this->repository = new \mock\Mapado\RestClientSdk\EntityRepository(
525
            $this->mockedSdk,
526
            $this->mockedRestClient,
527
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Order'
528
        );
529
530
        $mapping->setConfig([
531
            'collectionKey' => 'fooList',
532
        ]);
533
        $this->calling($this->mockedRestClient)->get = [
534
            'fooList' => [
535
                [
536
                    '@id' => '/orders/2',
537
                ]
538
            ],
539
        ];
540
        $this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping);
541
542
        $this
543
            ->then
544
                ->object($order = $this->repository->findOneBy(['a' => 'a']))
545
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Order')
546
                ->string($order->getId())
547
                    ->isEqualTo('/orders/2')
548
        ;
549
    }
550
551
    /**
552
     * testFindOneByWithoutResult
553
     *
554
     * @access public
555
     * @return void
556
     */
557
    public function testFindOneByWithoutResult()
558
    {
559
        $mapping = new RestMapping('v12');
560
        $classMetadata = new ClassMetadata(
561
            'orders',
562
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Order',
563
            'mock\Mapado\RestClientSdk\EntityRepository'
564
        );
565
        $classMetadata->setAttributeList([
566
            new Attribute('@id', 'id', 'string', true),
567
        ]);
568
        $mapping->setMapping([$classMetadata]);
569
570
        $this->calling($this->mockedSdk)->getMapping = $mapping;
571
572
        $this->repository = new \mock\Mapado\RestClientSdk\EntityRepository(
573
            $this->mockedSdk,
574
            $this->mockedRestClient,
575
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Order'
576
        );
577
578
        $mapping->setConfig([
579
            'collectionKey' => 'fooList',
580
        ]);
581
        $this->calling($this->mockedRestClient)->get = [
582
            'fooList' => [
583
            ],
584
        ];
585
        $this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping);
586
587
        $this
588
            ->then
589
                ->variable($order = $this->repository->findOneBy(['a' => 'a']))
590
                    ->isNull()
591
        ;
592
    }
593
}
594