1 | <?php |
||
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); |
||
|
|||
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() |
||
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 | ); |
||
550 | |||
551 | /** |
||
552 | * testFindOneByWithoutResult |
||
553 | * |
||
554 | * @access public |
||
555 | * @return void |
||
556 | */ |
||
557 | public function testFindOneByWithoutResult() |
||
593 | } |
||
594 |
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.