Completed
Pull Request — master (#45)
by Julien
03:06
created

EntityRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 422
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 422
rs 10

8 Methods

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