Completed
Push — master ( f9e0c8...bdc56b )
by Julien
12s
created

EntityRepository::testCacheWithIriAsId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 71
rs 9.1369
cc 1
eloc 51
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\Driver\AnnotationDriver;
9
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10
11
/**
12
 * Class EntityRepository
13
 * @author Julien Deniau <[email protected]>
14
 */
15
class EntityRepository extends atoum
16
{
17
    private $mockedRestClient;
18
19
    private $mockedSdk;
20
21
    private $mockedHydrator;
22
23
    private $repository;
24
25
    public function beforeTestMethod($method)
26
    {
27
        $this->mockGenerator->orphanize('__construct');
28
        $this->mockedSdk = new \mock\Mapado\RestClientSdk\SdkClient();
29
        $mockedHydrator = new \mock\Mapado\RestClientSdk\Model\ModelHydrator($this->mockedSdk);
30
        $this->calling($this->mockedSdk)->getModelHydrator = $mockedHydrator;
31
32
        $this->mockGenerator->orphanize('__construct');
33
        $this->mockedRestClient = new \mock\Mapado\RestClientSdk\RestClient();
34
        // $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...
35
36
        $this->mockedHydrator = new \mock\Mapado\RestClientSdk\Model\ModelHydrator($this->mockedSdk);
37
        $this->calling($this->mockedSdk)->getModelHydrator = $this->mockedHydrator;
38
39
        $mapping = new RestMapping('v12');
40
        $mapping->setMapping([
41
            new ClassMetadata(
42
                'orders',
43
                'Mapado\RestClientSdk\Tests\Model\Model',
44
                'mock\Mapado\RestClientSdk\EntityRepository'
45
            ),
46
        ]);
47
48
        $this->calling($this->mockedSdk)->getMapping = $mapping;
49
50
        $this->repository = new \mock\Mapado\RestClientSdk\EntityRepository(
51
            $this->mockedSdk,
52
            $this->mockedRestClient,
53
            'Mapado\RestClientSdk\Tests\Model\Model'
54
        );
55
    }
56
57
    /**
58
     * testFind
59
     *
60
     * @access public
61
     * @return void
62
     */
63
    public function testFind()
64
    {
65
        $this->calling($this->mockedRestClient)->get = [];
66
67
        $this
68
            ->if($this->repository->find('1'))
69
            ->then
70
                ->mock($this->mockedRestClient)
71
                    ->call('get')
72
                        ->withArguments('v12/orders/1')->once()
73
74
            ->given($this->resetMock($this->mockedRestClient))
75
            ->if($this->repository->find('v12/orders/999'))
76
            ->then
77
                ->mock($this->mockedRestClient)
78
                    ->call('get')
79
                        ->withArguments('v12/orders/999')->once()
80
81
            ->if($this->repository->findAll())
82
            ->then
83
                ->mock($this->mockedRestClient)
84
                    ->call('get')
85
                        ->withArguments('v12/orders')->once()
86
87
            ->if($this->repository->findOneByFoo('bar'))
88
            ->then
89
                ->mock($this->mockedRestClient)
90
                    ->call('get')
91
                        ->withArguments('v12/orders?foo=bar')->once()
92
                ->mock($this->mockedHydrator)
93
                    ->call('hydrate')
94
                        ->twice()
95
96
            ->if($this->repository->findByFoo('baz'))
97
            ->then
98
                ->mock($this->mockedRestClient)
99
                    ->call('get')
100
                        ->withArguments('v12/orders?foo=baz')->once()
101
                ->mock($this->mockedHydrator)
102
                    ->call('hydrateList')
103
                        ->twice()
104
        ;
105
    }
106
107
    /**
108
     * testFindWithQueryParameters
109
     *
110
     * @access public
111
     * @return void
112
     */
113
    public function testFindWithQueryParameters()
114
    {
115
        $this->calling($this->mockedRestClient)->get = [];
116
117
        $this
118
            ->if($this->repository->find('1', [ 'foo' => 'bar', 'bar'  => 'baz' ]))
119
            ->then
120
                ->mock($this->mockedRestClient)
121
                    ->call('get')
122
                        ->withArguments('v12/orders/1?foo=bar&bar=baz')->once()
123
        ;
124
    }
125
126
    /**
127
     * testFindWithCache
128
     *
129
     * @param mixed $method
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
130
     * @access public
131
     * @return void
132
     */
133
    public function testFindWithCache()
134
    {
135
        $mockOrder1 = new \mock\entity;
136
        $mockOrder2 = new \mock\entity;
137
        $mockOrder3 = new \mock\entity;
138
        $this->calling($mockOrder1)->getId = 'v12/orders/1';
139
        $this->calling($mockOrder2)->getId = 'v12/orders/2';
140
        $this->calling($mockOrder3)->getId = 'v12/orders/3';
141
142
        $this->calling($this->mockedHydrator)->hydrate = $mockOrder1;
143
        $this->calling($this->mockedHydrator)->hydrateList = [$mockOrder1, $mockOrder2, $mockOrder3];
144
145
        $arrayAdapter = new ArrayAdapter(0, false);
146
        $this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter;
147
        $this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_';
148
149
        $this->calling($this->mockedRestClient)->get = [];
150
151
        $this->calling($this->mockedHydrator)->convertId[0] = 'v12/orders/1';
152
        $this->calling($this->mockedHydrator)->convertId[1] = 'v12/orders/1';
153
        $this->calling($this->mockedHydrator)->convertId[4] = 'v12/orders/3';
154
155
        $this
156
            ->if($this->repository->find(1))
157
            ->and($this->repository->find(1))
158
            ->and($this->repository->find(1, ['foo' => 'bar']))
159
            ->then
160
                ->mock($this->mockedRestClient)
161
                    ->call('get')
162
                        ->withArguments('v12/orders/1')->once()
163
                    ->call('get')
164
                        ->withArguments('v12/orders/1?foo=bar')->once()
165
166
            // find all
167
            ->if($this->repository->findAll())
168
            ->and($this->repository->findAll())
169
            ->if($this->repository->find(3))
170
            ->then
171
                ->mock($this->mockedRestClient)
172
                    ->call('get')
173
                        ->withArguments('v12/orders')->once()
174
                    ->call('get')
175
                        ->withArguments('v12/orders/3')->never()
176
177
            // find by
178
            ->given($this->resetMock($this->mockedRestClient))
179
                ->and($this->mockedSdk->getCacheItemPool()->clear())
180
181
            ->if($this->repository->findBy([ 'foo' => 'bar', 'bar'  => 'baz' ]))
182
            ->and($this->repository->findBy([ 'foo' => 'bar', 'bar'  => 'baz' ]))
183
            ->if($this->repository->find(1))
184
            ->then
185
                ->mock($this->mockedRestClient)
186
                    ->call('get')
187
                        ->withArguments('v12/orders?foo=bar&bar=baz')->once()
188
                    ->call('get')
189
                        ->withArguments('v12/orders/1')->never()
190
191
            // find by something
192
            ->given($this->resetMock($this->mockedRestClient))
193
194
            ->if($this->repository->findByBar('baz'))
195
                ->and($this->repository->findByBar('baz'))
196
                ->and($this->repository->find(1))
197
            ->then
198
                ->mock($this->mockedRestClient)
199
                    ->call('get')
200
                        ->withArguments('v12/orders?bar=baz')->once()
201
                    ->call('get')
202
                        ->withArguments('v12/orders/1')->never()
203
204
            // find one by
205
            ->given($this->resetMock($this->mockedRestClient))
206
207
            ->if($this->repository->findOneBy([ 'foo' => 'baz', 'bar'  => 'bar' ]))
208
            ->and($this->repository->findOneBy([ 'foo' => 'baz', 'bar'  => 'bar' ]))
209
            ->then
210
                ->mock($this->mockedRestClient)
211
                    ->call('get')
212
                        ->withArguments('v12/orders?foo=baz&bar=bar')->once()
213
214
            // find one by thing
215
            ->given($this->resetMock($this->mockedRestClient))
216
217
            ->if($this->repository->findOneByFoo('bar'))
218
            ->and($this->repository->findOneByFoo('bar'))
219
            ->then
220
                ->mock($this->mockedRestClient)
221
                    ->call('get')
222
                        ->withArguments('v12/orders?foo=bar')->once()
223
224
            // find one by with data already in cache
225
            ->given($this->resetMock($this->mockedRestClient))
226
            ->if($this->repository->findOneBy([ 'foo' => 'bar', 'bar'  => 'baz' ]))
227
            ->then
228
                ->mock($this->mockedRestClient)
229
                    ->call('get')
230
                        ->withArguments('v12/orders?foo=bar&bar=baz')->never()
231
        ;
232
    }
233
234
    /**
235
     * testClearCacheAfterUpdate
236
     *
237
     * @access public
238
     *
239
     * @return void
240
     */
241
    public function testClearCacheAfterUpdate()
242
    {
243
        $mapping = new RestMapping('/v12');
244
        $mapping->setMapping([
245
            new ClassMetadata(
246
                'products',
247
                'Mapado\RestClientSdk\Tests\Model\Product',
248
                'mock\Mapado\RestClientSdk\EntityRepository'
249
            ),
250
        ]);
251
252
        $this->calling($this->mockedSdk)->getMapping = $mapping;
253
        $this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping);
254
255
256
        $product1 = new \Mapado\RestClientSdk\Tests\Model\Product;
257
        $product2 = new \Mapado\RestClientSdk\Tests\Model\Product;
258
        $product3 = new \Mapado\RestClientSdk\Tests\Model\Product;
259
        $product1->setId('/v12/products/1');
260
        $product2->setId('/v12/products/2');
261
        $product3->setId('/v12/products/3');
262
263
        $this->calling($this->mockedHydrator)->hydrate = $product1;
264
        $this->calling($this->mockedHydrator)->hydrateList = [$product1, $product2, $product3];
265
266
        $arrayAdapter = new ArrayAdapter(0, false);
267
        $this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter;
268
        $this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_';
269
270
        $this->calling($this->mockedRestClient)->get = $product1;
271
        $this->calling($this->mockedRestClient)->put = $product1;
272
        $this->calling($this->mockedRestClient)->delete = null;
273
274
        $repository = new \mock\Mapado\RestClientSdk\EntityRepository(
275
            $this->mockedSdk,
276
            $this->mockedRestClient,
277
            'Mapado\RestClientSdk\Tests\Model\Product'
278
        );
279
280
        $this
281
            ->if($repository->find(1))
282
            ->then
283
                ->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1'))
284
                    ->isTrue()
285
286
            ->if($repository->find(1))
287
            ->then
288
                ->mock($this->mockedRestClient)
289
                    ->call('get')
290
                        ->withArguments('/v12/products/1')->once()
291
292
            // after update
293
            ->if($repository->update($product1))
294
                ->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1'))
295
                    ->isFalse()
296
297
            ->if($repository->find(1))
298
            ->then
299
                ->mock($this->mockedRestClient)
300
                    ->call('get')
301
                        ->withArguments('/v12/products/1')->twice()
302
303
            // after deletion
304
            ->if($repository->remove($product1))
305
            ->then
306
                ->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1'))
307
                    ->isFalse()
308
        ;
309
    }
310
311
    public function testCacheWithIriAsId()
312
    {
313
        $annotationDriver = new AnnotationDriver(__DIR__ . '/../cache/');
314
        $mapping = new RestMapping();
315
        $mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../Model/Issue46/'));
316
317
        $this->calling($this->mockedSdk)->getMapping = $mapping;
318
        $this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping);
319
320
        $section1 = new \Mapado\RestClientSdk\Tests\Model\Issue46\Section;
321
        $section1->setIri('/sections/1');
322
323
        $this->calling($this->mockedHydrator)->hydrate = $section1;
324
        $this->calling($this->mockedHydrator)->hydrateList = [$section1];
325
326
        $arrayAdapter = new ArrayAdapter(0, false);
327
        $this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter;
328
        $this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_';
329
330
        $this->calling($this->mockedRestClient)->get = $section1;
331
        $this->calling($this->mockedRestClient)->put = $section1;
332
        $this->calling($this->mockedRestClient)->delete = null;
333
334
        $repository = new \mock\Mapado\RestClientSdk\EntityRepository(
335
            $this->mockedSdk,
336
            $this->mockedRestClient,
337
            'Mapado\RestClientSdk\Tests\Model\Issue46\Section'
338
        );
339
340
        $this
341
            ->if($repository->findBy(['section' => $section1]))
342
            // ->then(ldd(iterator_to_array($arrayAdapter->getItems())))
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
343
           ->then
344
               ->mock($this->mockedRestClient)
345
                   ->call('get')
346
                       ->withArguments('/sections?section=%2Fsections%2F1')->once()
347
348
            ->if($repository->findAll())
349
            // ->then(ldd(iterator_to_array($arrayAdapter->getItems())))
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
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\Cart',
406
                'mock\Mapado\RestClientSdk\EntityRepository'
407
            ),
408
            new ClassMetadata(
409
                'cart_items',
410
                'Mapado\RestClientSdk\Tests\Model\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\CartItem'
423
        );
424
425
426
        $cart = new \Mapado\RestClientSdk\Tests\Model\Cart;
427
        $cart->setId(1);
428
429
        $this
430
            ->given($cart = new \Mapado\RestClientSdk\Tests\Model\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\Cart',
456
                'mock\Mapado\RestClientSdk\EntityRepository'
457
            ),
458
            new ClassMetadata(
459
                'cart_items',
460
                'Mapado\RestClientSdk\Tests\Model\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\CartItem'
475
        );
476
477
478
        $cart = new \Mapado\RestClientSdk\Tests\Model\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\CartItem)
501
            ->if($cartItemRepository->persist($cartItem))
502
            ->then
503
                ->mock($this->mockedRestClient)
504
                    ->call('post')
505
                        ->withArguments('/cart_items')->once()
506
        ;
507
    }
508
}
509