Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
16 | class CacheManagerTest extends AbstractTest |
||
17 | { |
||
18 | protected $resolver; |
||
19 | |||
20 | public function testAddCacheManagerAwareResolver() |
||
21 | { |
||
22 | $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
23 | |||
24 | $resolver = $this->getMock('Liip\ImagineBundle\Tests\Fixtures\CacheManagerAwareResolver'); |
||
25 | $resolver |
||
26 | ->expects($this->once()) |
||
27 | ->method('setCacheManager') |
||
28 | ->with($cacheManager) |
||
29 | ; |
||
30 | |||
31 | $cacheManager->addResolver('thumbnail', $resolver); |
||
32 | } |
||
33 | |||
34 | public function testGetBrowserPathWithoutResolver() |
||
35 | { |
||
36 | $config = $this->createFilterConfigurationMock(); |
||
37 | $config |
||
38 | ->expects($this->once()) |
||
39 | ->method('get') |
||
40 | ->with('thumbnail') |
||
41 | ->will($this->returnValue(array( |
||
42 | 'size' => array(180, 180), |
||
43 | 'mode' => 'outbound', |
||
44 | 'cache' => null, |
||
45 | ))) |
||
46 | ; |
||
47 | |||
48 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
49 | |||
50 | $this->setExpectedException('OutOfBoundsException', 'Could not find resolver "default" for "thumbnail" filter type'); |
||
51 | $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail'); |
||
52 | } |
||
53 | |||
54 | public function testGetRuntimePath() |
||
55 | { |
||
56 | $config = $this->createFilterConfigurationMock(); |
||
57 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
58 | |||
59 | $rcPath = $cacheManager->getRuntimePath('image.jpg', array( |
||
60 | 'thumbnail' => array( |
||
61 | 'size' => array(180, 180), |
||
62 | ), |
||
63 | )); |
||
64 | |||
65 | $this->assertEquals('rc/ILfTutxX/image.jpg', $rcPath); |
||
66 | } |
||
67 | |||
68 | public function testDefaultResolverUsedIfNoneSetOnGetBrowserPath() |
||
69 | { |
||
70 | $resolver = $this->createResolverMock(); |
||
71 | $resolver |
||
72 | ->expects($this->once()) |
||
73 | ->method('isStored') |
||
74 | ->with('cats.jpeg', 'thumbnail') |
||
75 | ->will($this->returnValue(true)) |
||
76 | ; |
||
77 | $resolver |
||
78 | ->expects($this->once()) |
||
79 | ->method('resolve') |
||
80 | ->with('cats.jpeg', 'thumbnail') |
||
81 | ->will($this->returnValue('http://a/path/to/an/image.png')) |
||
82 | ; |
||
83 | |||
84 | $config = $this->createFilterConfigurationMock(); |
||
85 | $config |
||
86 | ->expects($this->exactly(2)) |
||
87 | ->method('get') |
||
88 | ->with('thumbnail') |
||
89 | ->will($this->returnValue(array( |
||
90 | 'size' => array(180, 180), |
||
91 | 'mode' => 'outbound', |
||
92 | 'cache' => null, |
||
93 | ))) |
||
94 | ; |
||
95 | |||
96 | $router = $this->createRouterMock(); |
||
97 | $router |
||
98 | ->expects($this->never()) |
||
99 | ->method('generate') |
||
100 | ; |
||
101 | |||
102 | $cacheManager = new CacheManager($config, $router, new Signer('secret'), $this->createEventDispatcherMock()); |
||
103 | $cacheManager->addResolver('default', $resolver); |
||
104 | |||
105 | $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail'); |
||
106 | |||
107 | $this->assertEquals('http://a/path/to/an/image.png', $actualBrowserPath); |
||
108 | } |
||
109 | |||
110 | public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBrowserPath() |
||
111 | { |
||
112 | $resolver = $this->createResolverMock(); |
||
113 | $resolver |
||
114 | ->expects($this->once()) |
||
115 | ->method('isStored') |
||
116 | ->with('cats.jpeg', 'thumbnail') |
||
117 | ->will($this->returnValue(false)) |
||
118 | ; |
||
119 | $resolver |
||
120 | ->expects($this->never()) |
||
121 | ->method('resolve') |
||
122 | ; |
||
123 | |||
124 | $config = $this->createFilterConfigurationMock(); |
||
125 | $config |
||
126 | ->expects($this->atLeastOnce()) |
||
127 | ->method('get') |
||
128 | ->with('thumbnail') |
||
129 | ->will($this->returnValue(array( |
||
130 | 'size' => array(180, 180), |
||
131 | 'mode' => 'outbound', |
||
132 | 'cache' => null, |
||
133 | ))) |
||
134 | ; |
||
135 | |||
136 | $router = $this->createRouterMock(); |
||
137 | $router |
||
138 | ->expects($this->once()) |
||
139 | ->method('generate') |
||
140 | ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) |
||
141 | ; |
||
142 | |||
143 | $cacheManager = new CacheManager($config, $router, new Signer('secret'), $this->createEventDispatcherMock()); |
||
144 | $cacheManager->addResolver('default', $resolver); |
||
145 | |||
146 | $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail'); |
||
147 | |||
148 | $this->assertEquals('/media/cache/thumbnail/cats.jpeg', $actualBrowserPath); |
||
149 | } |
||
150 | |||
151 | public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBrowserPathWithRuntimeConfig() |
||
152 | { |
||
153 | $runtimeConfig = array( |
||
154 | 'thumbnail' => array( |
||
155 | 'size' => array(100, 100), |
||
156 | ), |
||
157 | ); |
||
158 | |||
159 | $resolver = $this->createResolverMock(); |
||
160 | $resolver |
||
161 | ->expects($this->once()) |
||
162 | ->method('isStored') |
||
163 | ->with('rc/VhOzTGRB/cats.jpeg', 'thumbnail') |
||
164 | ->will($this->returnValue(false)) |
||
165 | ; |
||
166 | $resolver |
||
167 | ->expects($this->never()) |
||
168 | ->method('resolve') |
||
169 | ; |
||
170 | |||
171 | $config = $this->createFilterConfigurationMock(); |
||
172 | $config |
||
173 | ->expects($this->atLeastOnce()) |
||
174 | ->method('get') |
||
175 | ->with('thumbnail') |
||
176 | ->will($this->returnValue(array( |
||
177 | 'size' => array(180, 180), |
||
178 | 'mode' => 'outbound', |
||
179 | 'cache' => null, |
||
180 | ))) |
||
181 | ; |
||
182 | |||
183 | $router = $this->createRouterMock(); |
||
184 | $router |
||
185 | ->expects($this->once()) |
||
186 | ->method('generate') |
||
187 | ->will($this->returnValue('/media/cache/thumbnail/rc/VhOzTGRB/cats.jpeg')) |
||
188 | ; |
||
189 | |||
190 | $cacheManager = new CacheManager($config, $router, new Signer('secret'), $this->createEventDispatcherMock()); |
||
191 | $cacheManager->addResolver('default', $resolver); |
||
192 | |||
193 | $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail', $runtimeConfig); |
||
194 | |||
195 | $this->assertEquals('/media/cache/thumbnail/rc/VhOzTGRB/cats.jpeg', $actualBrowserPath); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @dataProvider invalidPathProvider |
||
200 | */ |
||
201 | public function testResolveInvalidPath($path) |
||
202 | { |
||
203 | $cacheManager = new CacheManager( |
||
204 | $this->createFilterConfigurationMock(), |
||
205 | $this->createRouterMock(), |
||
206 | new Signer('secret'), |
||
207 | $this->createEventDispatcherMock() |
||
208 | ); |
||
209 | |||
210 | $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); |
||
211 | $cacheManager->resolve($path, 'thumbnail'); |
||
212 | } |
||
213 | |||
214 | public function testThrowsIfConcreteResolverNotExists() |
||
215 | { |
||
216 | $cacheManager = new CacheManager( |
||
217 | $this->createFilterConfigurationMock(), |
||
218 | $this->createRouterMock(), |
||
219 | new Signer('secret'), |
||
220 | $this->createEventDispatcherMock() |
||
221 | ); |
||
222 | |||
223 | $this->setExpectedException('OutOfBoundsException', 'Could not find resolver "default" for "thumbnail" filter type'); |
||
224 | $this->assertFalse($cacheManager->resolve('cats.jpeg', 'thumbnail')); |
||
225 | } |
||
226 | |||
227 | public function testFallbackToDefaultResolver() |
||
228 | { |
||
229 | $binary = new Binary('aContent', 'image/png', 'png'); |
||
230 | |||
231 | $resolver = $this->createResolverMock(); |
||
232 | $resolver |
||
233 | ->expects($this->once()) |
||
234 | ->method('resolve') |
||
235 | ->with('cats.jpeg', 'thumbnail') |
||
236 | ->will($this->returnValue('/thumbs/cats.jpeg')) |
||
237 | ; |
||
238 | $resolver |
||
239 | ->expects($this->once()) |
||
240 | ->method('store') |
||
241 | ->with($binary, '/thumbs/cats.jpeg', 'thumbnail') |
||
242 | ; |
||
243 | $resolver |
||
244 | ->expects($this->once()) |
||
245 | ->method('remove') |
||
246 | ->with(array('/thumbs/cats.jpeg'), array('thumbnail')) |
||
247 | ->will($this->returnValue(true)) |
||
248 | ; |
||
249 | |||
250 | $config = $this->createFilterConfigurationMock(); |
||
251 | $config |
||
252 | ->expects($this->exactly(3)) |
||
253 | ->method('get') |
||
254 | ->with('thumbnail') |
||
255 | ->will($this->returnValue(array( |
||
256 | 'size' => array(180, 180), |
||
257 | 'mode' => 'outbound', |
||
258 | 'cache' => null, |
||
259 | ))) |
||
260 | ; |
||
261 | |||
262 | $cacheManager = new CacheManager( |
||
263 | $config, |
||
264 | $this->createRouterMock(), |
||
265 | new Signer('secret'), |
||
266 | $this->createEventDispatcherMock() |
||
267 | ); |
||
268 | $cacheManager->addResolver('default', $resolver); |
||
269 | |||
270 | // Resolve fallback to default resolver |
||
271 | $this->assertEquals('/thumbs/cats.jpeg', $cacheManager->resolve('cats.jpeg', 'thumbnail')); |
||
272 | |||
273 | $cacheManager->store($binary, '/thumbs/cats.jpeg', 'thumbnail'); |
||
274 | |||
275 | // Remove fallback to default resolver |
||
276 | $cacheManager->remove('/thumbs/cats.jpeg', 'thumbnail'); |
||
277 | } |
||
278 | |||
279 | public function testGenerateUrl() |
||
280 | { |
||
281 | $path = 'thePath'; |
||
282 | $expectedUrl = 'theUrl'; |
||
283 | |||
284 | $routerMock = $this->createRouterMock(); |
||
285 | $routerMock |
||
286 | ->expects($this->once()) |
||
287 | ->method('generate') |
||
288 | ->with( |
||
289 | 'liip_imagine_filter', |
||
290 | array( |
||
291 | 'path' => $path, |
||
292 | 'filter' => 'thumbnail', |
||
293 | ), |
||
294 | UrlGeneratorInterface::ABSOLUTE_URL |
||
295 | ) |
||
296 | ->will($this->returnValue($expectedUrl)) |
||
297 | ; |
||
298 | |||
299 | $cacheManager = new CacheManager( |
||
300 | $this->createFilterConfigurationMock(), |
||
301 | $routerMock, |
||
302 | new Signer('secret'), |
||
303 | $this->createEventDispatcherMock() |
||
304 | ); |
||
305 | |||
306 | $this->assertEquals( |
||
307 | $expectedUrl, |
||
308 | $cacheManager->generateUrl($path, 'thumbnail') |
||
309 | ); |
||
310 | } |
||
311 | |||
312 | public function testRemoveCacheForPathAndFilterOnRemove() |
||
313 | { |
||
314 | $expectedPath = 'thePath'; |
||
315 | $expectedFilter = 'theFilter'; |
||
316 | |||
317 | $resolver = $this->createResolverMock(); |
||
318 | $resolver |
||
319 | ->expects($this->once()) |
||
320 | ->method('remove') |
||
321 | ->with(array($expectedPath), array($expectedFilter)) |
||
322 | ; |
||
323 | |||
324 | $config = $this->createFilterConfigurationMock(); |
||
325 | $config |
||
326 | ->expects($this->atLeastOnce()) |
||
327 | ->method('get') |
||
328 | ->will($this->returnCallback(function ($filter) { |
||
329 | return array( |
||
330 | 'cache' => $filter, |
||
331 | ); |
||
332 | })) |
||
333 | ; |
||
334 | |||
335 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
336 | $cacheManager->addResolver($expectedFilter, $resolver); |
||
337 | |||
338 | $cacheManager->remove($expectedPath, $expectedFilter); |
||
339 | } |
||
340 | |||
341 | public function testRemoveCacheForPathAndSomeFiltersOnRemove() |
||
342 | { |
||
343 | $expectedPath = 'thePath'; |
||
344 | $expectedFilterOne = 'theFilterOne'; |
||
345 | $expectedFilterTwo = 'theFilterTwo'; |
||
346 | |||
347 | $resolverOne = $this->createResolverMock(); |
||
348 | $resolverOne |
||
349 | ->expects($this->once()) |
||
350 | ->method('remove') |
||
351 | ->with(array($expectedPath), array($expectedFilterOne)) |
||
352 | ; |
||
353 | |||
354 | $resolverTwo = $this->createResolverMock(); |
||
355 | $resolverTwo |
||
356 | ->expects($this->once()) |
||
357 | ->method('remove') |
||
358 | ->with(array($expectedPath), array($expectedFilterTwo)) |
||
359 | ; |
||
360 | |||
361 | $config = $this->createFilterConfigurationMock(); |
||
362 | $config |
||
363 | ->expects($this->atLeastOnce()) |
||
364 | ->method('get') |
||
365 | ->will($this->returnCallback(function ($filter) { |
||
366 | return array( |
||
367 | 'cache' => $filter, |
||
368 | ); |
||
369 | })) |
||
370 | ; |
||
371 | |||
372 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
373 | $cacheManager->addResolver($expectedFilterOne, $resolverOne); |
||
374 | $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); |
||
375 | |||
376 | $cacheManager->remove($expectedPath, array($expectedFilterOne, $expectedFilterTwo)); |
||
377 | } |
||
378 | |||
379 | public function testRemoveCacheForSomePathsAndFilterOnRemove() |
||
380 | { |
||
381 | $expectedPathOne = 'thePathOne'; |
||
382 | $expectedPathTwo = 'thePathTwo'; |
||
383 | $expectedFilter = 'theFilter'; |
||
384 | |||
385 | $resolver = $this->createResolverMock(); |
||
386 | $resolver |
||
387 | ->expects($this->once()) |
||
388 | ->method('remove') |
||
389 | ->with( |
||
390 | array($expectedPathOne, $expectedPathTwo), |
||
391 | array($expectedFilter) |
||
392 | ) |
||
393 | ; |
||
394 | |||
395 | $config = $this->createFilterConfigurationMock(); |
||
396 | $config |
||
397 | ->expects($this->atLeastOnce()) |
||
398 | ->method('get') |
||
399 | ->will($this->returnCallback(function ($filter) { |
||
400 | return array( |
||
401 | 'cache' => $filter, |
||
402 | ); |
||
403 | })) |
||
404 | ; |
||
405 | |||
406 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
407 | $cacheManager->addResolver($expectedFilter, $resolver); |
||
408 | |||
409 | $cacheManager->remove(array($expectedPathOne, $expectedPathTwo), $expectedFilter); |
||
410 | } |
||
411 | |||
412 | public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() |
||
413 | { |
||
414 | $expectedPathOne = 'thePath'; |
||
415 | $expectedPathTwo = 'thePath'; |
||
416 | $expectedFilterOne = 'theFilterOne'; |
||
417 | $expectedFilterTwo = 'theFilterTwo'; |
||
418 | |||
419 | $resolverOne = $this->createResolverMock(); |
||
420 | $resolverOne |
||
421 | ->expects($this->once()) |
||
422 | ->method('remove') |
||
423 | ->with(array($expectedPathOne, $expectedPathTwo), array($expectedFilterOne)) |
||
424 | ; |
||
425 | |||
426 | $resolverTwo = $this->createResolverMock(); |
||
427 | $resolverTwo |
||
428 | ->expects($this->once()) |
||
429 | ->method('remove') |
||
430 | ->with(array($expectedPathOne, $expectedPathTwo), array($expectedFilterTwo)) |
||
431 | ; |
||
432 | |||
433 | $config = $this->createFilterConfigurationMock(); |
||
434 | $config |
||
435 | ->expects($this->atLeastOnce()) |
||
436 | ->method('get') |
||
437 | ->will($this->returnCallback(function ($filter) { |
||
438 | return array( |
||
439 | 'cache' => $filter, |
||
440 | ); |
||
441 | })) |
||
442 | ; |
||
443 | |||
444 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
445 | $cacheManager->addResolver($expectedFilterOne, $resolverOne); |
||
446 | $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); |
||
447 | |||
448 | $cacheManager->remove( |
||
449 | array($expectedPathOne, $expectedPathTwo), |
||
450 | array($expectedFilterOne, $expectedFilterTwo) |
||
451 | ); |
||
452 | } |
||
453 | |||
454 | public function testRemoveCacheForAllFiltersOnRemove() |
||
455 | { |
||
456 | $expectedFilterOne = 'theFilterOne'; |
||
457 | $expectedFilterTwo = 'theFilterTwo'; |
||
458 | |||
459 | $resolverOne = $this->createResolverMock(); |
||
460 | $resolverOne |
||
461 | ->expects($this->once()) |
||
462 | ->method('remove') |
||
463 | ->with(array(), array($expectedFilterOne)) |
||
464 | ; |
||
465 | |||
466 | $resolverTwo = $this->createResolverMock(); |
||
467 | $resolverTwo |
||
468 | ->expects($this->once()) |
||
469 | ->method('remove') |
||
470 | ->with(array(), array($expectedFilterTwo)) |
||
471 | ; |
||
472 | |||
473 | $config = $this->createFilterConfigurationMock(); |
||
474 | $config |
||
475 | ->expects($this->atLeastOnce()) |
||
476 | ->method('get') |
||
477 | ->will($this->returnCallback(function ($filter) { |
||
478 | return array( |
||
479 | 'cache' => $filter, |
||
480 | ); |
||
481 | })) |
||
482 | ; |
||
483 | $config |
||
484 | ->expects($this->once()) |
||
485 | ->method('all') |
||
486 | ->will($this->returnValue(array( |
||
487 | $expectedFilterOne => array(), |
||
488 | $expectedFilterTwo => array(), |
||
489 | ))) |
||
490 | ; |
||
491 | |||
492 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
493 | $cacheManager->addResolver($expectedFilterOne, $resolverOne); |
||
494 | $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); |
||
495 | |||
496 | $cacheManager->remove(); |
||
497 | } |
||
498 | |||
499 | public function testRemoveCacheForPathAndAllFiltersOnRemove() |
||
500 | { |
||
501 | $expectedFilterOne = 'theFilterOne'; |
||
502 | $expectedFilterTwo = 'theFilterTwo'; |
||
503 | $expectedPath = 'thePath'; |
||
504 | |||
505 | $resolverOne = $this->createResolverMock(); |
||
506 | $resolverOne |
||
507 | ->expects($this->once()) |
||
508 | ->method('remove') |
||
509 | ->with(array($expectedPath), array($expectedFilterOne)) |
||
510 | ; |
||
511 | |||
512 | $resolverTwo = $this->createResolverMock(); |
||
513 | $resolverTwo |
||
514 | ->expects($this->once()) |
||
515 | ->method('remove') |
||
516 | ->with(array($expectedPath), array($expectedFilterTwo)) |
||
517 | ; |
||
518 | |||
519 | $config = $this->createFilterConfigurationMock(); |
||
520 | $config |
||
521 | ->expects($this->atLeastOnce()) |
||
522 | ->method('get') |
||
523 | ->will($this->returnCallback(function ($filter) { |
||
524 | return array( |
||
525 | 'cache' => $filter, |
||
526 | ); |
||
527 | })) |
||
528 | ; |
||
529 | $config |
||
530 | ->expects($this->once()) |
||
531 | ->method('all') |
||
532 | ->will($this->returnValue(array( |
||
533 | $expectedFilterOne => array(), |
||
534 | $expectedFilterTwo => array(), |
||
535 | ))) |
||
536 | ; |
||
537 | |||
538 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
539 | $cacheManager->addResolver($expectedFilterOne, $resolverOne); |
||
540 | $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); |
||
541 | |||
542 | $cacheManager->remove($expectedPath); |
||
543 | } |
||
544 | |||
545 | public function testAggregateFiltersByResolverOnRemove() |
||
546 | { |
||
547 | $expectedFilterOne = 'theFilterOne'; |
||
548 | $expectedFilterTwo = 'theFilterTwo'; |
||
549 | |||
550 | $resolver = $this->createResolverMock(); |
||
551 | $resolver |
||
552 | ->expects($this->once()) |
||
553 | ->method('remove') |
||
554 | ->with(array(), array($expectedFilterOne, $expectedFilterTwo)) |
||
555 | ; |
||
556 | |||
557 | $config = $this->createFilterConfigurationMock(); |
||
558 | $config |
||
559 | ->expects($this->atLeastOnce()) |
||
560 | ->method('get') |
||
561 | ->will($this->returnCallback(function ($filter) { |
||
562 | return array( |
||
563 | 'cache' => $filter, |
||
564 | ); |
||
565 | })) |
||
566 | ; |
||
567 | |||
568 | $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); |
||
569 | $cacheManager->addResolver($expectedFilterOne, $resolver); |
||
570 | $cacheManager->addResolver($expectedFilterTwo, $resolver); |
||
571 | |||
572 | $cacheManager->remove(null, array($expectedFilterOne, $expectedFilterTwo)); |
||
573 | } |
||
574 | |||
575 | public function testShouldDispatchCachePreResolveEvent() |
||
576 | { |
||
577 | $dispatcher = $this->createEventDispatcherMock(); |
||
578 | $dispatcher |
||
579 | ->expects($this->at(0)) |
||
580 | ->method('dispatch') |
||
581 | ->with(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent('cats.jpg', 'thumbnail')) |
||
582 | ; |
||
583 | |||
584 | $cacheManager = new CacheManager( |
||
585 | $this->createFilterConfigurationMock(), |
||
586 | $this->createRouterMock(), |
||
587 | new Signer('secret'), |
||
588 | $dispatcher |
||
589 | ); |
||
590 | $cacheManager->addResolver('default', $this->createResolverMock()); |
||
591 | |||
592 | $cacheManager->resolve('cats.jpg', 'thumbnail'); |
||
593 | } |
||
594 | |||
595 | public function testShouldDispatchCachePostResolveEvent() |
||
596 | { |
||
597 | $dispatcher = $this->createEventDispatcherMock(); |
||
598 | $dispatcher |
||
599 | ->expects($this->at(1)) |
||
600 | ->method('dispatch') |
||
601 | ->with(ImagineEvents::POST_RESOLVE, new CacheResolveEvent('cats.jpg', 'thumbnail')) |
||
602 | ; |
||
603 | |||
604 | $cacheManager = new CacheManager( |
||
605 | $this->createFilterConfigurationMock(), |
||
606 | $this->createRouterMock(), |
||
607 | new Signer('secret'), |
||
608 | $dispatcher |
||
609 | ); |
||
610 | $cacheManager->addResolver('default', $this->createResolverMock()); |
||
611 | |||
612 | $cacheManager->resolve('cats.jpg', 'thumbnail'); |
||
613 | } |
||
614 | |||
615 | public function testShouldAllowToPassChangedDataFromPreResolveEventToResolver() |
||
616 | { |
||
617 | $dispatcher = $this->createEventDispatcherMock(); |
||
618 | $dispatcher |
||
619 | ->expects($this->at(0)) |
||
620 | ->method('dispatch') |
||
621 | ->with(ImagineEvents::PRE_RESOLVE, $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent')) |
||
622 | ->will($this->returnCallback(function ($name, $event) { |
||
623 | $event->setPath('changed_path'); |
||
624 | $event->setFilter('changed_filter'); |
||
625 | })) |
||
626 | ; |
||
627 | |||
628 | $resolver = $this->createResolverMock(); |
||
629 | $resolver |
||
630 | ->expects($this->once()) |
||
631 | ->method('resolve') |
||
632 | ->with('changed_path', 'changed_filter') |
||
633 | ; |
||
634 | |||
635 | $cacheManager = new CacheManager( |
||
636 | $this->createFilterConfigurationMock(), |
||
637 | $this->createRouterMock(), |
||
638 | new Signer('secret'), |
||
639 | $dispatcher |
||
640 | ); |
||
641 | $cacheManager->addResolver('default', $resolver); |
||
642 | |||
643 | $cacheManager->resolve('cats.jpg', 'thumbnail'); |
||
644 | } |
||
645 | |||
646 | public function testShouldAllowToGetResolverByFilterChangedInPreResolveEvent() |
||
647 | { |
||
648 | $dispatcher = $this->createEventDispatcherMock(); |
||
649 | $dispatcher |
||
650 | ->expects($this->at(0)) |
||
651 | ->method('dispatch') |
||
652 | ->will($this->returnCallback(function ($name, $event) { |
||
653 | $event->setFilter('thumbnail'); |
||
654 | })) |
||
655 | ; |
||
656 | |||
657 | $cacheManager = $this->getMock('Liip\ImagineBundle\Imagine\Cache\CacheManager', array('getResolver'), array( |
||
658 | $this->createFilterConfigurationMock(), |
||
659 | $this->createRouterMock(), |
||
660 | new Signer('secret'), |
||
661 | $dispatcher, |
||
662 | )); |
||
663 | |||
664 | $cacheManager |
||
665 | ->expects($this->once()) |
||
666 | ->method('getResolver') |
||
667 | ->with('thumbnail') |
||
668 | ->will($this->returnValue($this->createResolverMock())) |
||
669 | ; |
||
670 | |||
671 | $cacheManager->resolve('cats.jpg', 'default'); |
||
672 | } |
||
673 | |||
674 | public function testShouldAllowToPassChangedDataFromPreResolveEventToPostResolveEvent() |
||
675 | { |
||
676 | $dispatcher = $this->createEventDispatcherMock(); |
||
677 | $dispatcher |
||
678 | ->expects($this->at(0)) |
||
679 | ->method('dispatch') |
||
680 | ->with(ImagineEvents::PRE_RESOLVE, $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent')) |
||
681 | ->will($this->returnCallback(function ($name, $event) { |
||
682 | $event->setPath('changed_path'); |
||
683 | $event->setFilter('changed_filter'); |
||
684 | })) |
||
685 | ; |
||
686 | |||
687 | $dispatcher |
||
688 | ->expects($this->at(1)) |
||
689 | ->method('dispatch') |
||
690 | ->with( |
||
691 | ImagineEvents::POST_RESOLVE, |
||
692 | $this->logicalAnd( |
||
693 | $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent'), |
||
694 | $this->attributeEqualTo('path', 'changed_path'), |
||
695 | $this->attributeEqualTo('filter', 'changed_filter') |
||
696 | )) |
||
697 | ; |
||
698 | |||
699 | $cacheManager = new CacheManager( |
||
700 | $this->createFilterConfigurationMock(), |
||
701 | $this->createRouterMock(), |
||
702 | new Signer('secret'), |
||
703 | $dispatcher |
||
704 | ); |
||
705 | $cacheManager->addResolver('default', $this->createResolverMock()); |
||
706 | |||
707 | $cacheManager->resolve('cats.jpg', 'thumbnail'); |
||
708 | } |
||
709 | |||
710 | public function testShouldReturnUrlChangedInPostResolveEvent() |
||
711 | { |
||
712 | $dispatcher = $this->createEventDispatcherMock(); |
||
713 | $dispatcher |
||
714 | ->expects($this->at(1)) |
||
715 | ->method('dispatch') |
||
716 | ->with(ImagineEvents::POST_RESOLVE, $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent')) |
||
717 | ->will($this->returnCallback(function ($name, $event) { |
||
718 | $event->setUrl('changed_url'); |
||
719 | })) |
||
720 | ; |
||
721 | |||
722 | $cacheManager = new CacheManager( |
||
723 | $this->createFilterConfigurationMock(), |
||
724 | $this->createRouterMock(), |
||
725 | new Signer('secret'), |
||
726 | $dispatcher |
||
727 | ); |
||
728 | $cacheManager->addResolver('default', $this->createResolverMock()); |
||
729 | |||
730 | $url = $cacheManager->resolve('cats.jpg', 'thumbnail'); |
||
731 | |||
732 | $this->assertEquals('changed_url', $url); |
||
733 | } |
||
734 | } |
||
735 |