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 |
||
13 | class FilterManagerTest extends AbstractTest |
||
14 | { |
||
15 | public function testThrowsIfNoLoadersAddedForFilterOnApplyFilter() |
||
16 | { |
||
17 | $config = $this->createFilterConfigurationMock(); |
||
18 | $config |
||
|
|||
19 | ->expects($this->atLeastOnce()) |
||
20 | ->method('get') |
||
21 | ->with('thumbnail') |
||
22 | ->will($this->returnValue(array( |
||
23 | 'filters' => array( |
||
24 | 'thumbnail' => array( |
||
25 | 'size' => array(180, 180), |
||
26 | 'mode' => 'outbound', |
||
27 | ), |
||
28 | ), |
||
29 | 'post_processors' => array(), |
||
30 | ))) |
||
31 | ; |
||
32 | |||
33 | $binary = new Binary('aContent', 'image/png', 'png'); |
||
34 | |||
35 | $filterManager = new FilterManager( |
||
36 | $config, |
||
37 | $this->createImagineMock(), |
||
38 | $this->getMockMimeTypeGuesser() |
||
39 | ); |
||
40 | |||
41 | $this->setExpectedException('InvalidArgumentException', 'Could not find filter loader for "thumbnail" filter type'); |
||
42 | $filterManager->applyFilter($binary, 'thumbnail'); |
||
43 | } |
||
44 | |||
45 | View Code Duplication | public function testReturnFilteredBinaryWithExpectedContentOnApplyFilter() |
|
46 | { |
||
47 | $originalContent = 'aOriginalContent'; |
||
48 | $expectedFilteredContent = 'theFilteredContent'; |
||
49 | |||
50 | $binary = new Binary($originalContent, 'image/png', 'png'); |
||
51 | |||
52 | $thumbConfig = array( |
||
53 | 'size' => array(180, 180), |
||
54 | 'mode' => 'outbound', |
||
55 | ); |
||
56 | |||
57 | $config = $this->createFilterConfigurationMock(); |
||
58 | $config |
||
59 | ->expects($this->atLeastOnce()) |
||
60 | ->method('get') |
||
61 | ->with('thumbnail') |
||
62 | ->will($this->returnValue(array( |
||
63 | 'filters' => array( |
||
64 | 'thumbnail' => $thumbConfig, |
||
65 | ), |
||
66 | 'post_processors' => array(), |
||
67 | ))) |
||
68 | ; |
||
69 | |||
70 | $image = $this->getMockImage(); |
||
71 | $image |
||
72 | ->expects($this->once()) |
||
73 | ->method('get') |
||
74 | ->will($this->returnValue($expectedFilteredContent)) |
||
75 | ; |
||
76 | |||
77 | $imagine = $this->createImagineMock(); |
||
78 | $imagine |
||
79 | ->expects($this->once()) |
||
80 | ->method('load') |
||
81 | ->with($originalContent) |
||
82 | ->will($this->returnValue($image)) |
||
83 | ; |
||
84 | |||
85 | $loader = $this->getMockLoader(); |
||
86 | $loader |
||
87 | ->expects($this->once()) |
||
88 | ->method('load') |
||
89 | ->with($this->identicalTo($image), $thumbConfig) |
||
90 | ->will($this->returnArgument(0)) |
||
91 | ; |
||
92 | |||
93 | $filterManager = new FilterManager( |
||
94 | $config, |
||
95 | $imagine, |
||
96 | $this->getMockMimeTypeGuesser() |
||
97 | ); |
||
98 | $filterManager->addLoader('thumbnail', $loader); |
||
99 | |||
100 | $filteredBinary = $filterManager->applyFilter($binary, 'thumbnail'); |
||
101 | |||
102 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
103 | $this->assertEquals($expectedFilteredContent, $filteredBinary->getContent()); |
||
104 | } |
||
105 | |||
106 | View Code Duplication | public function testReturnFilteredBinaryWithFormatOfOriginalBinaryOnApplyFilter() |
|
107 | { |
||
108 | $originalContent = 'aOriginalContent'; |
||
109 | $expectedFormat = 'png'; |
||
110 | |||
111 | $binary = new Binary($originalContent, 'image/png', $expectedFormat); |
||
112 | |||
113 | $thumbConfig = array( |
||
114 | 'size' => array(180, 180), |
||
115 | 'mode' => 'outbound', |
||
116 | ); |
||
117 | |||
118 | $config = $this->createFilterConfigurationMock(); |
||
119 | $config |
||
120 | ->expects($this->atLeastOnce()) |
||
121 | ->method('get') |
||
122 | ->with('thumbnail') |
||
123 | ->will($this->returnValue(array( |
||
124 | 'filters' => array( |
||
125 | 'thumbnail' => $thumbConfig, |
||
126 | ), |
||
127 | 'post_processors' => array(), |
||
128 | ))) |
||
129 | ; |
||
130 | |||
131 | $image = $this->getMockImage(); |
||
132 | $image |
||
133 | ->expects($this->once()) |
||
134 | ->method('get') |
||
135 | ->will($this->returnValue('aFilteredContent')) |
||
136 | ; |
||
137 | |||
138 | $imagine = $this->createImagineMock(); |
||
139 | $imagine |
||
140 | ->expects($this->once()) |
||
141 | ->method('load') |
||
142 | ->will($this->returnValue($image)) |
||
143 | ; |
||
144 | |||
145 | $loader = $this->getMockLoader(); |
||
146 | $loader |
||
147 | ->expects($this->once()) |
||
148 | ->method('load') |
||
149 | ->with($this->identicalTo($image), $thumbConfig) |
||
150 | ->will($this->returnArgument(0)) |
||
151 | ; |
||
152 | |||
153 | $filterManager = new FilterManager( |
||
154 | $config, |
||
155 | $imagine, |
||
156 | $this->getMockMimeTypeGuesser() |
||
157 | ); |
||
158 | $filterManager->addLoader('thumbnail', $loader); |
||
159 | |||
160 | $filteredBinary = $filterManager->applyFilter($binary, 'thumbnail'); |
||
161 | |||
162 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
163 | $this->assertEquals($expectedFormat, $filteredBinary->getFormat()); |
||
164 | } |
||
165 | |||
166 | View Code Duplication | public function testReturnFilteredBinaryWithCustomFormatIfSetOnApplyFilter() |
|
167 | { |
||
168 | $originalContent = 'aOriginalContent'; |
||
169 | $originalFormat = 'png'; |
||
170 | $expectedFormat = 'jpg'; |
||
171 | |||
172 | $binary = new Binary($originalContent, 'image/png', $originalFormat); |
||
173 | |||
174 | $thumbConfig = array( |
||
175 | 'size' => array(180, 180), |
||
176 | 'mode' => 'outbound', |
||
177 | ); |
||
178 | |||
179 | $config = $this->createFilterConfigurationMock(); |
||
180 | $config |
||
181 | ->expects($this->atLeastOnce()) |
||
182 | ->method('get') |
||
183 | ->with('thumbnail') |
||
184 | ->will($this->returnValue(array( |
||
185 | 'format' => $expectedFormat, |
||
186 | 'filters' => array( |
||
187 | 'thumbnail' => $thumbConfig, |
||
188 | ), |
||
189 | 'post_processors' => array(), |
||
190 | ))) |
||
191 | ; |
||
192 | |||
193 | $image = $this->getMockImage(); |
||
194 | $image |
||
195 | ->expects($this->once()) |
||
196 | ->method('get') |
||
197 | ->will($this->returnValue('aFilteredContent')) |
||
198 | ; |
||
199 | |||
200 | $imagine = $this->createImagineMock(); |
||
201 | $imagine |
||
202 | ->expects($this->once()) |
||
203 | ->method('load') |
||
204 | ->will($this->returnValue($image)) |
||
205 | ; |
||
206 | |||
207 | $loader = $this->getMockLoader(); |
||
208 | $loader |
||
209 | ->expects($this->once()) |
||
210 | ->method('load') |
||
211 | ->with($this->identicalTo($image), $thumbConfig) |
||
212 | ->will($this->returnArgument(0)) |
||
213 | ; |
||
214 | |||
215 | $filterManager = new FilterManager( |
||
216 | $config, |
||
217 | $imagine, |
||
218 | $this->getMockMimeTypeGuesser() |
||
219 | ); |
||
220 | $filterManager->addLoader('thumbnail', $loader); |
||
221 | |||
222 | $filteredBinary = $filterManager->applyFilter($binary, 'thumbnail'); |
||
223 | |||
224 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
225 | $this->assertEquals($expectedFormat, $filteredBinary->getFormat()); |
||
226 | } |
||
227 | |||
228 | public function testReturnFilteredBinaryWithMimeTypeOfOriginalBinaryOnApplyFilter() |
||
229 | { |
||
230 | $originalContent = 'aOriginalContent'; |
||
231 | $expectedMimeType = 'image/png'; |
||
232 | |||
233 | $binary = new Binary($originalContent, $expectedMimeType, 'png'); |
||
234 | |||
235 | $thumbConfig = array( |
||
236 | 'size' => array(180, 180), |
||
237 | 'mode' => 'outbound', |
||
238 | ); |
||
239 | |||
240 | $config = $this->createFilterConfigurationMock(); |
||
241 | $config |
||
242 | ->expects($this->atLeastOnce()) |
||
243 | ->method('get') |
||
244 | ->with('thumbnail') |
||
245 | ->will($this->returnValue(array( |
||
246 | 'filters' => array( |
||
247 | 'thumbnail' => $thumbConfig, |
||
248 | ), |
||
249 | 'post_processors' => array(), |
||
250 | ))) |
||
251 | ; |
||
252 | |||
253 | $image = $this->getMockImage(); |
||
254 | $image |
||
255 | ->expects($this->once()) |
||
256 | ->method('get') |
||
257 | ->will($this->returnValue('aFilteredContent')) |
||
258 | ; |
||
259 | |||
260 | $imagine = $this->createImagineMock(); |
||
261 | $imagine |
||
262 | ->expects($this->once()) |
||
263 | ->method('load') |
||
264 | ->will($this->returnValue($image)) |
||
265 | ; |
||
266 | |||
267 | $mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
||
268 | $mimeTypeGuesser |
||
269 | ->expects($this->never()) |
||
270 | ->method('guess') |
||
271 | ; |
||
272 | |||
273 | $loader = $this->getMockLoader(); |
||
274 | $loader |
||
275 | ->expects($this->once()) |
||
276 | ->method('load') |
||
277 | ->with($this->identicalTo($image), $thumbConfig) |
||
278 | ->will($this->returnArgument(0)) |
||
279 | ; |
||
280 | |||
281 | $filterManager = new FilterManager( |
||
282 | $config, |
||
283 | $imagine, |
||
284 | $mimeTypeGuesser |
||
285 | ); |
||
286 | $filterManager->addLoader('thumbnail', $loader); |
||
287 | |||
288 | $filteredBinary = $filterManager->applyFilter($binary, 'thumbnail'); |
||
289 | |||
290 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
291 | $this->assertEquals($expectedMimeType, $filteredBinary->getMimeType()); |
||
292 | } |
||
293 | |||
294 | public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApplyFilter() |
||
295 | { |
||
296 | $originalContent = 'aOriginalContent'; |
||
297 | $originalMimeType = 'image/png'; |
||
298 | $expectedContent = 'aFilteredContent'; |
||
299 | $expectedMimeType = 'image/jpeg'; |
||
300 | |||
301 | $binary = new Binary($originalContent, $originalMimeType, 'png'); |
||
302 | |||
303 | $thumbConfig = array( |
||
304 | 'size' => array(180, 180), |
||
305 | 'mode' => 'outbound', |
||
306 | ); |
||
307 | |||
308 | $config = $this->createFilterConfigurationMock(); |
||
309 | $config |
||
310 | ->expects($this->atLeastOnce()) |
||
311 | ->method('get') |
||
312 | ->with('thumbnail') |
||
313 | ->will($this->returnValue(array( |
||
314 | 'format' => 'jpg', |
||
315 | 'filters' => array( |
||
316 | 'thumbnail' => $thumbConfig, |
||
317 | ), |
||
318 | 'post_processors' => array(), |
||
319 | ))) |
||
320 | ; |
||
321 | |||
322 | $image = $this->getMockImage(); |
||
323 | $image |
||
324 | ->expects($this->once()) |
||
325 | ->method('get') |
||
326 | ->will($this->returnValue($expectedContent)) |
||
327 | ; |
||
328 | |||
329 | $imagine = $this->createImagineMock(); |
||
330 | $imagine |
||
331 | ->expects($this->once()) |
||
332 | ->method('load') |
||
333 | ->will($this->returnValue($image)) |
||
334 | ; |
||
335 | |||
336 | $mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
||
337 | $mimeTypeGuesser |
||
338 | ->expects($this->once()) |
||
339 | ->method('guess') |
||
340 | ->with($expectedContent) |
||
341 | ->will($this->returnValue($expectedMimeType)) |
||
342 | ; |
||
343 | |||
344 | $loader = $this->getMockLoader(); |
||
345 | $loader |
||
346 | ->expects($this->once()) |
||
347 | ->method('load') |
||
348 | ->with($this->identicalTo($image), $thumbConfig) |
||
349 | ->will($this->returnArgument(0)) |
||
350 | ; |
||
351 | |||
352 | $filterManager = new FilterManager( |
||
353 | $config, |
||
354 | $imagine, |
||
355 | $mimeTypeGuesser |
||
356 | ); |
||
357 | $filterManager->addLoader('thumbnail', $loader); |
||
358 | |||
359 | $filteredBinary = $filterManager->applyFilter($binary, 'thumbnail'); |
||
360 | |||
361 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
362 | $this->assertEquals($expectedMimeType, $filteredBinary->getMimeType()); |
||
363 | } |
||
364 | |||
365 | View Code Duplication | public function testAltersQualityOnApplyFilter() |
|
366 | { |
||
367 | $originalContent = 'aOriginalContent'; |
||
368 | $expectedQuality = 80; |
||
369 | |||
370 | $binary = new Binary($originalContent, 'image/png', 'png'); |
||
371 | |||
372 | $thumbConfig = array( |
||
373 | 'size' => array(180, 180), |
||
374 | 'mode' => 'outbound', |
||
375 | ); |
||
376 | |||
377 | $config = $this->createFilterConfigurationMock(); |
||
378 | $config |
||
379 | ->expects($this->atLeastOnce()) |
||
380 | ->method('get') |
||
381 | ->with('thumbnail') |
||
382 | ->will($this->returnValue(array( |
||
383 | 'quality' => $expectedQuality, |
||
384 | 'filters' => array( |
||
385 | 'thumbnail' => $thumbConfig, |
||
386 | ), |
||
387 | 'post_processors' => array(), |
||
388 | ))) |
||
389 | ; |
||
390 | |||
391 | $image = $this->getMockImage(); |
||
392 | $image |
||
393 | ->expects($this->once()) |
||
394 | ->method('get') |
||
395 | ->with('png', array('quality' => $expectedQuality)) |
||
396 | ->will($this->returnValue('aFilteredContent')) |
||
397 | ; |
||
398 | |||
399 | $imagine = $this->createImagineMock(); |
||
400 | $imagine |
||
401 | ->expects($this->once()) |
||
402 | ->method('load') |
||
403 | ->will($this->returnValue($image)) |
||
404 | ; |
||
405 | |||
406 | $loader = $this->getMockLoader(); |
||
407 | $loader |
||
408 | ->expects($this->once()) |
||
409 | ->method('load') |
||
410 | ->with($this->identicalTo($image), $thumbConfig) |
||
411 | ->will($this->returnArgument(0)) |
||
412 | ; |
||
413 | |||
414 | $filterManager = new FilterManager( |
||
415 | $config, |
||
416 | $imagine, |
||
417 | $this->getMockMimeTypeGuesser() |
||
418 | ); |
||
419 | $filterManager->addLoader('thumbnail', $loader); |
||
420 | |||
421 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filterManager->applyFilter($binary, 'thumbnail')); |
||
422 | } |
||
423 | |||
424 | View Code Duplication | public function testAlters100QualityIfNotSetOnApplyFilter() |
|
425 | { |
||
426 | $originalContent = 'aOriginalContent'; |
||
427 | $expectedQuality = 100; |
||
428 | |||
429 | $binary = new Binary($originalContent, 'image/png', 'png'); |
||
430 | |||
431 | $thumbConfig = array( |
||
432 | 'size' => array(180, 180), |
||
433 | 'mode' => 'outbound', |
||
434 | ); |
||
435 | |||
436 | $config = $this->createFilterConfigurationMock(); |
||
437 | $config |
||
438 | ->expects($this->atLeastOnce()) |
||
439 | ->method('get') |
||
440 | ->with('thumbnail') |
||
441 | ->will($this->returnValue(array( |
||
442 | 'filters' => array( |
||
443 | 'thumbnail' => $thumbConfig, |
||
444 | ), |
||
445 | 'post_processors' => array(), |
||
446 | ))) |
||
447 | ; |
||
448 | |||
449 | $image = $this->getMockImage(); |
||
450 | $image |
||
451 | ->expects($this->once()) |
||
452 | ->method('get') |
||
453 | ->with('png', array('quality' => $expectedQuality)) |
||
454 | ->will($this->returnValue('aFilteredContent')) |
||
455 | ; |
||
456 | |||
457 | $imagine = $this->createImagineMock(); |
||
458 | $imagine |
||
459 | ->expects($this->once()) |
||
460 | ->method('load') |
||
461 | ->will($this->returnValue($image)) |
||
462 | ; |
||
463 | |||
464 | $loader = $this->getMockLoader(); |
||
465 | $loader |
||
466 | ->expects($this->once()) |
||
467 | ->method('load') |
||
468 | ->with($this->identicalTo($image), $thumbConfig) |
||
469 | ->will($this->returnArgument(0)) |
||
470 | ; |
||
471 | |||
472 | $filterManager = new FilterManager( |
||
473 | $config, |
||
474 | $imagine, |
||
475 | $this->getMockMimeTypeGuesser() |
||
476 | ); |
||
477 | $filterManager->addLoader('thumbnail', $loader); |
||
478 | |||
479 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filterManager->applyFilter($binary, 'thumbnail')); |
||
480 | } |
||
481 | |||
482 | public function testMergeRuntimeConfigWithOneFromFilterConfigurationOnApplyFilter() |
||
483 | { |
||
484 | $binary = new Binary('aContent', 'image/png', 'png'); |
||
485 | |||
486 | $runtimeConfig = array( |
||
487 | 'filters' => array( |
||
488 | 'thumbnail' => array( |
||
489 | 'size' => array(100, 100), |
||
490 | ), |
||
491 | ), |
||
492 | 'post_processors' => array(), |
||
493 | ); |
||
494 | |||
495 | $thumbConfig = array( |
||
496 | 'size' => array(180, 180), |
||
497 | 'mode' => 'outbound', |
||
498 | ); |
||
499 | |||
500 | $thumbMergedConfig = array( |
||
501 | 'size' => array(100, 100), |
||
502 | 'mode' => 'outbound', |
||
503 | ); |
||
504 | |||
505 | $config = $this->createFilterConfigurationMock(); |
||
506 | $config |
||
507 | ->expects($this->atLeastOnce()) |
||
508 | ->method('get') |
||
509 | ->with('thumbnail') |
||
510 | ->will($this->returnValue(array( |
||
511 | 'filters' => array( |
||
512 | 'thumbnail' => $thumbConfig, |
||
513 | ), |
||
514 | 'post_processors' => array(), |
||
515 | ))) |
||
516 | ; |
||
517 | |||
518 | $image = $this->getMockImage(); |
||
519 | $image |
||
520 | ->expects($this->once()) |
||
521 | ->method('get') |
||
522 | ->will($this->returnValue('aFilteredContent')) |
||
523 | ; |
||
524 | |||
525 | $imagine = $this->createImagineMock(); |
||
526 | $imagine |
||
527 | ->expects($this->once()) |
||
528 | ->method('load') |
||
529 | ->will($this->returnValue($image)) |
||
530 | ; |
||
531 | |||
532 | $loader = $this->getMockLoader(); |
||
533 | $loader |
||
534 | ->expects($this->once()) |
||
535 | ->method('load') |
||
536 | ->with($this->identicalTo($image), $thumbMergedConfig) |
||
537 | ->will($this->returnArgument(0)) |
||
538 | ; |
||
539 | |||
540 | $filterManager = new FilterManager( |
||
541 | $config, |
||
542 | $imagine, |
||
543 | $this->getMockMimeTypeGuesser() |
||
544 | ); |
||
545 | $filterManager->addLoader('thumbnail', $loader); |
||
546 | |||
547 | $this->assertInstanceOf( |
||
548 | 'Liip\ImagineBundle\Model\Binary', |
||
549 | $filterManager->applyFilter($binary, 'thumbnail', $runtimeConfig) |
||
550 | ); |
||
551 | } |
||
552 | |||
553 | public function testThrowsIfNoLoadersAddedForFilterOnApply() |
||
554 | { |
||
555 | $binary = new Binary('aContent', 'image/png', 'png'); |
||
556 | |||
557 | $filterManager = new FilterManager( |
||
558 | $this->createFilterConfigurationMock(), |
||
559 | $this->createImagineMock(), |
||
560 | $this->getMockMimeTypeGuesser() |
||
561 | ); |
||
562 | |||
563 | $this->setExpectedException('InvalidArgumentException', 'Could not find filter loader for "thumbnail" filter type'); |
||
564 | $filterManager->apply($binary, array( |
||
565 | 'filters' => array( |
||
566 | 'thumbnail' => array( |
||
567 | 'size' => array(180, 180), |
||
568 | 'mode' => 'outbound', |
||
569 | ), |
||
570 | ), |
||
571 | )); |
||
572 | } |
||
573 | |||
574 | View Code Duplication | public function testReturnFilteredBinaryWithExpectedContentOnApply() |
|
575 | { |
||
576 | $originalContent = 'aOriginalContent'; |
||
577 | $expectedFilteredContent = 'theFilteredContent'; |
||
578 | |||
579 | $binary = new Binary($originalContent, 'image/png', 'png'); |
||
580 | |||
581 | $thumbConfig = array( |
||
582 | 'size' => array(180, 180), |
||
583 | 'mode' => 'outbound', |
||
584 | ); |
||
585 | |||
586 | $image = $this->getMockImage(); |
||
587 | $image |
||
588 | ->expects($this->once()) |
||
589 | ->method('get') |
||
590 | ->will($this->returnValue($expectedFilteredContent)) |
||
591 | ; |
||
592 | |||
593 | $imagineMock = $this->createImagineMock(); |
||
594 | $imagineMock |
||
595 | ->expects($this->once()) |
||
596 | ->method('load') |
||
597 | ->with($originalContent) |
||
598 | ->will($this->returnValue($image)) |
||
599 | ; |
||
600 | |||
601 | $loader = $this->getMockLoader(); |
||
602 | $loader |
||
603 | ->expects($this->once()) |
||
604 | ->method('load') |
||
605 | ->with($this->identicalTo($image), $thumbConfig) |
||
606 | ->will($this->returnArgument(0)) |
||
607 | ; |
||
608 | |||
609 | $filterManager = new FilterManager( |
||
610 | $this->createFilterConfigurationMock(), |
||
611 | $imagineMock, |
||
612 | $this->getMockMimeTypeGuesser() |
||
613 | ); |
||
614 | $filterManager->addLoader('thumbnail', $loader); |
||
615 | |||
616 | $filteredBinary = $filterManager->apply($binary, array( |
||
617 | 'filters' => array( |
||
618 | 'thumbnail' => $thumbConfig, |
||
619 | ), |
||
620 | 'post_processors' => array(), |
||
621 | )); |
||
622 | |||
623 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
624 | $this->assertEquals($expectedFilteredContent, $filteredBinary->getContent()); |
||
625 | } |
||
626 | |||
627 | View Code Duplication | public function testReturnFilteredBinaryWithFormatOfOriginalBinaryOnApply() |
|
628 | { |
||
629 | $originalContent = 'aOriginalContent'; |
||
630 | $expectedFormat = 'png'; |
||
631 | |||
632 | $binary = new Binary($originalContent, 'image/png', $expectedFormat); |
||
633 | |||
634 | $thumbConfig = array( |
||
635 | 'size' => array(180, 180), |
||
636 | 'mode' => 'outbound', |
||
637 | ); |
||
638 | |||
639 | $image = $this->getMockImage(); |
||
640 | $image |
||
641 | ->expects($this->once()) |
||
642 | ->method('get') |
||
643 | ->will($this->returnValue('aFilteredContent')) |
||
644 | ; |
||
645 | |||
646 | $imagineMock = $this->createImagineMock(); |
||
647 | $imagineMock |
||
648 | ->expects($this->once()) |
||
649 | ->method('load') |
||
650 | ->will($this->returnValue($image)) |
||
651 | ; |
||
652 | |||
653 | $loader = $this->getMockLoader(); |
||
654 | $loader |
||
655 | ->expects($this->once()) |
||
656 | ->method('load') |
||
657 | ->with($this->identicalTo($image), $thumbConfig) |
||
658 | ->will($this->returnArgument(0)) |
||
659 | ; |
||
660 | |||
661 | $filterManager = new FilterManager( |
||
662 | $this->createFilterConfigurationMock(), |
||
663 | $imagineMock, |
||
664 | $this->getMockMimeTypeGuesser() |
||
665 | ); |
||
666 | $filterManager->addLoader('thumbnail', $loader); |
||
667 | |||
668 | $filteredBinary = $filterManager->apply($binary, array( |
||
669 | 'filters' => array( |
||
670 | 'thumbnail' => $thumbConfig, |
||
671 | ), |
||
672 | 'post_processors' => array(), |
||
673 | )); |
||
674 | |||
675 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
676 | $this->assertEquals($expectedFormat, $filteredBinary->getFormat()); |
||
677 | } |
||
678 | |||
679 | View Code Duplication | public function testReturnFilteredBinaryWithCustomFormatIfSetOnApply() |
|
680 | { |
||
681 | $originalContent = 'aOriginalContent'; |
||
682 | $originalFormat = 'png'; |
||
683 | $expectedFormat = 'jpg'; |
||
684 | |||
685 | $binary = new Binary($originalContent, 'image/png', $originalFormat); |
||
686 | |||
687 | $thumbConfig = array( |
||
688 | 'size' => array(180, 180), |
||
689 | 'mode' => 'outbound', |
||
690 | ); |
||
691 | |||
692 | $image = $this->getMockImage(); |
||
693 | $image |
||
694 | ->expects($this->once()) |
||
695 | ->method('get') |
||
696 | ->will($this->returnValue('aFilteredContent')) |
||
697 | ; |
||
698 | |||
699 | $imagineMock = $this->createImagineMock(); |
||
700 | $imagineMock |
||
701 | ->expects($this->once()) |
||
702 | ->method('load') |
||
703 | ->will($this->returnValue($image)) |
||
704 | ; |
||
705 | |||
706 | $loader = $this->getMockLoader(); |
||
707 | $loader |
||
708 | ->expects($this->once()) |
||
709 | ->method('load') |
||
710 | ->with($this->identicalTo($image), $thumbConfig) |
||
711 | ->will($this->returnArgument(0)) |
||
712 | ; |
||
713 | |||
714 | $filterManager = new FilterManager( |
||
715 | $this->createFilterConfigurationMock(), |
||
716 | $imagineMock, |
||
717 | $this->getMockMimeTypeGuesser() |
||
718 | ); |
||
719 | $filterManager->addLoader('thumbnail', $loader); |
||
720 | |||
721 | $filteredBinary = $filterManager->apply($binary, array( |
||
722 | 'format' => $expectedFormat, |
||
723 | 'filters' => array( |
||
724 | 'thumbnail' => $thumbConfig, |
||
725 | ), |
||
726 | 'post_processors' => array(), |
||
727 | )); |
||
728 | |||
729 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
730 | $this->assertEquals($expectedFormat, $filteredBinary->getFormat()); |
||
731 | } |
||
732 | |||
733 | public function testReturnFilteredBinaryWithMimeTypeOfOriginalBinaryOnApply() |
||
734 | { |
||
735 | $originalContent = 'aOriginalContent'; |
||
736 | $expectedMimeType = 'image/png'; |
||
737 | |||
738 | $binary = new Binary($originalContent, $expectedMimeType, 'png'); |
||
739 | |||
740 | $thumbConfig = array( |
||
741 | 'size' => array(180, 180), |
||
742 | 'mode' => 'outbound', |
||
743 | ); |
||
744 | |||
745 | $image = $this->getMockImage(); |
||
746 | $image |
||
747 | ->expects($this->once()) |
||
748 | ->method('get') |
||
749 | ->will($this->returnValue('aFilteredContent')) |
||
750 | ; |
||
751 | |||
752 | $imagineMock = $this->createImagineMock(); |
||
753 | $imagineMock |
||
754 | ->expects($this->once()) |
||
755 | ->method('load') |
||
756 | ->will($this->returnValue($image)) |
||
757 | ; |
||
758 | |||
759 | $mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
||
760 | $mimeTypeGuesser |
||
761 | ->expects($this->never()) |
||
762 | ->method('guess') |
||
763 | ; |
||
764 | |||
765 | $loader = $this->getMockLoader(); |
||
766 | $loader |
||
767 | ->expects($this->once()) |
||
768 | ->method('load') |
||
769 | ->with($this->identicalTo($image), $thumbConfig) |
||
770 | ->will($this->returnArgument(0)) |
||
771 | ; |
||
772 | |||
773 | $filterManager = new FilterManager( |
||
774 | $this->createFilterConfigurationMock(), |
||
775 | $imagineMock, |
||
776 | $mimeTypeGuesser |
||
777 | ); |
||
778 | $filterManager->addLoader('thumbnail', $loader); |
||
779 | |||
780 | $filteredBinary = $filterManager->apply($binary, array( |
||
781 | 'filters' => array( |
||
782 | 'thumbnail' => $thumbConfig, |
||
783 | ), |
||
784 | 'post_processors' => array(), |
||
785 | )); |
||
786 | |||
787 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
788 | $this->assertEquals($expectedMimeType, $filteredBinary->getMimeType()); |
||
789 | } |
||
790 | |||
791 | public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApply() |
||
792 | { |
||
793 | $originalContent = 'aOriginalContent'; |
||
794 | $originalMimeType = 'image/png'; |
||
795 | $expectedContent = 'aFilteredContent'; |
||
796 | $expectedMimeType = 'image/jpeg'; |
||
797 | |||
798 | $binary = new Binary($originalContent, $originalMimeType, 'png'); |
||
799 | |||
800 | $thumbConfig = array( |
||
801 | 'size' => array(180, 180), |
||
802 | 'mode' => 'outbound', |
||
803 | ); |
||
804 | |||
805 | $image = $this->getMockImage(); |
||
806 | $image |
||
807 | ->expects($this->once()) |
||
808 | ->method('get') |
||
809 | ->will($this->returnValue($expectedContent)) |
||
810 | ; |
||
811 | |||
812 | $imagineMock = $this->createImagineMock(); |
||
813 | $imagineMock |
||
814 | ->expects($this->once()) |
||
815 | ->method('load') |
||
816 | ->will($this->returnValue($image)) |
||
817 | ; |
||
818 | |||
819 | $mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
||
820 | $mimeTypeGuesser |
||
821 | ->expects($this->once()) |
||
822 | ->method('guess') |
||
823 | ->with($expectedContent) |
||
824 | ->will($this->returnValue($expectedMimeType)) |
||
825 | ; |
||
826 | |||
827 | $loader = $this->getMockLoader(); |
||
828 | $loader |
||
829 | ->expects($this->once()) |
||
830 | ->method('load') |
||
831 | ->with($this->identicalTo($image), $thumbConfig) |
||
832 | ->will($this->returnArgument(0)) |
||
833 | ; |
||
834 | |||
835 | $filterManager = new FilterManager( |
||
836 | $this->createFilterConfigurationMock(), |
||
837 | $imagineMock, |
||
838 | $mimeTypeGuesser |
||
839 | ); |
||
840 | $filterManager->addLoader('thumbnail', $loader); |
||
841 | |||
842 | $filteredBinary = $filterManager->apply($binary, array( |
||
843 | 'format' => 'jpg', |
||
844 | 'filters' => array( |
||
845 | 'thumbnail' => $thumbConfig, |
||
846 | ), |
||
847 | 'post_processors' => array(), |
||
848 | )); |
||
849 | |||
850 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
851 | $this->assertEquals($expectedMimeType, $filteredBinary->getMimeType()); |
||
852 | } |
||
853 | |||
854 | View Code Duplication | public function testAltersQualityOnApply() |
|
855 | { |
||
856 | $originalContent = 'aOriginalContent'; |
||
857 | $expectedQuality = 80; |
||
858 | |||
859 | $binary = new Binary($originalContent, 'image/png', 'png'); |
||
860 | |||
861 | $thumbConfig = array( |
||
862 | 'size' => array(180, 180), |
||
863 | 'mode' => 'outbound', |
||
864 | ); |
||
865 | |||
866 | $image = $this->getMockImage(); |
||
867 | $image |
||
868 | ->expects($this->once()) |
||
869 | ->method('get') |
||
870 | ->with('png', array('quality' => $expectedQuality)) |
||
871 | ->will($this->returnValue('aFilteredContent')) |
||
872 | ; |
||
873 | |||
874 | $imagineMock = $this->createImagineMock(); |
||
875 | $imagineMock |
||
876 | ->expects($this->once()) |
||
877 | ->method('load') |
||
878 | ->will($this->returnValue($image)) |
||
879 | ; |
||
880 | |||
881 | $loader = $this->getMockLoader(); |
||
882 | $loader |
||
883 | ->expects($this->once()) |
||
884 | ->method('load') |
||
885 | ->with($this->identicalTo($image), $thumbConfig) |
||
886 | ->will($this->returnArgument(0)) |
||
887 | ; |
||
888 | |||
889 | $filterManager = new FilterManager( |
||
890 | $this->createFilterConfigurationMock(), |
||
891 | $imagineMock, |
||
892 | $this->getMockMimeTypeGuesser() |
||
893 | ); |
||
894 | $filterManager->addLoader('thumbnail', $loader); |
||
895 | |||
896 | $filteredBinary = $filterManager->apply($binary, array( |
||
897 | 'quality' => $expectedQuality, |
||
898 | 'filters' => array( |
||
899 | 'thumbnail' => $thumbConfig, |
||
900 | ), |
||
901 | 'post_processors' => array(), |
||
902 | )); |
||
903 | |||
904 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
905 | } |
||
906 | |||
907 | View Code Duplication | public function testAlters100QualityIfNotSetOnApply() |
|
908 | { |
||
909 | $originalContent = 'aOriginalContent'; |
||
910 | $expectedQuality = 100; |
||
911 | |||
912 | $binary = new Binary($originalContent, 'image/png', 'png'); |
||
913 | |||
914 | $thumbConfig = array( |
||
915 | 'size' => array(180, 180), |
||
916 | 'mode' => 'outbound', |
||
917 | ); |
||
918 | |||
919 | $image = $this->getMockImage(); |
||
920 | $image |
||
921 | ->expects($this->once()) |
||
922 | ->method('get') |
||
923 | ->with('png', array('quality' => $expectedQuality)) |
||
924 | ->will($this->returnValue('aFilteredContent')) |
||
925 | ; |
||
926 | |||
927 | $imagineMock = $this->createImagineMock(); |
||
928 | $imagineMock |
||
929 | ->expects($this->once()) |
||
930 | ->method('load') |
||
931 | ->will($this->returnValue($image)) |
||
932 | ; |
||
933 | |||
934 | $loader = $this->getMockLoader(); |
||
935 | $loader |
||
936 | ->expects($this->once()) |
||
937 | ->method('load') |
||
938 | ->with($this->identicalTo($image), $thumbConfig) |
||
939 | ->will($this->returnArgument(0)) |
||
940 | ; |
||
941 | |||
942 | $filterManager = new FilterManager( |
||
943 | $this->createFilterConfigurationMock(), |
||
944 | $imagineMock, |
||
945 | $this->getMockMimeTypeGuesser() |
||
946 | ); |
||
947 | $filterManager->addLoader('thumbnail', $loader); |
||
948 | |||
949 | $filteredBinary = $filterManager->apply($binary, array( |
||
950 | 'filters' => array( |
||
951 | 'thumbnail' => $thumbConfig, |
||
952 | ), |
||
953 | 'post_processors' => array(), |
||
954 | )); |
||
955 | |||
956 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
957 | } |
||
958 | |||
959 | public function testApplyPostProcessor() |
||
960 | { |
||
961 | $originalContent = 'aContent'; |
||
962 | $expectedPostProcessedContent = 'postProcessedContent'; |
||
963 | $binary = new Binary($originalContent, 'image/png', 'png'); |
||
964 | |||
965 | $thumbConfig = array( |
||
966 | 'size' => array(180, 180), |
||
967 | 'mode' => 'outbound', |
||
968 | ); |
||
969 | |||
970 | $config = $this->createFilterConfigurationMock(); |
||
971 | $config |
||
972 | ->expects($this->atLeastOnce()) |
||
973 | ->method('get') |
||
974 | ->with('thumbnail') |
||
975 | ->will($this->returnValue(array( |
||
976 | 'filters' => array( |
||
977 | 'thumbnail' => $thumbConfig, |
||
978 | ), |
||
979 | 'post_processors' => array( |
||
980 | 'foo' => array(), |
||
981 | ), |
||
982 | ))) |
||
983 | ; |
||
984 | |||
985 | $thumbConfig = array( |
||
986 | 'size' => array(180, 180), |
||
987 | 'mode' => 'outbound', |
||
988 | ); |
||
989 | |||
990 | $image = $this->getMockImage(); |
||
991 | $image |
||
992 | ->expects($this->once()) |
||
993 | ->method('get') |
||
994 | ->will($this->returnValue($originalContent)) |
||
995 | ; |
||
996 | |||
997 | $imagineMock = $this->createImagineMock(); |
||
998 | $imagineMock |
||
999 | ->expects($this->once()) |
||
1000 | ->method('load') |
||
1001 | ->with($originalContent) |
||
1002 | ->will($this->returnValue($image)) |
||
1003 | ; |
||
1004 | |||
1005 | $loader = $this->getMockLoader(); |
||
1006 | $loader |
||
1007 | ->expects($this->once()) |
||
1008 | ->method('load') |
||
1009 | ->with($this->identicalTo($image), $thumbConfig) |
||
1010 | ->will($this->returnArgument(0)) |
||
1011 | ; |
||
1012 | |||
1013 | $processedBinary = new Binary($expectedPostProcessedContent, 'image/png', 'png'); |
||
1014 | |||
1015 | $postProcessor = $this->getPostProcessorMock(); |
||
1016 | $postProcessor |
||
1017 | ->expects($this->once()) |
||
1018 | ->method('process') |
||
1019 | ->with($binary) |
||
1020 | ->will($this->returnValue($processedBinary)); |
||
1021 | |||
1022 | $filterManager = new FilterManager( |
||
1023 | $config, |
||
1024 | $imagineMock, |
||
1025 | $this->getMockMimeTypeGuesser() |
||
1026 | ); |
||
1027 | $filterManager->addLoader('thumbnail', $loader); |
||
1028 | $filterManager->addPostProcessor('foo', $postProcessor); |
||
1029 | |||
1030 | $filteredBinary = $filterManager->applyFilter($binary, 'thumbnail'); |
||
1031 | $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $filteredBinary); |
||
1032 | $this->assertEquals($expectedPostProcessedContent, $filteredBinary->getContent()); |
||
1033 | } |
||
1034 | |||
1035 | public function testThrowsIfNoPostProcessorAddedForFilterOnApplyFilter() |
||
1036 | { |
||
1037 | $originalContent = 'aContent'; |
||
1038 | $binary = new Binary($originalContent, 'image/png', 'png'); |
||
1039 | |||
1040 | $thumbConfig = array( |
||
1041 | 'size' => array(180, 180), |
||
1042 | 'mode' => 'outbound', |
||
1043 | ); |
||
1044 | |||
1045 | $config = $this->createFilterConfigurationMock(); |
||
1046 | $config |
||
1047 | ->expects($this->atLeastOnce()) |
||
1048 | ->method('get') |
||
1049 | ->with('thumbnail') |
||
1050 | ->will($this->returnValue(array( |
||
1051 | 'filters' => array( |
||
1052 | 'thumbnail' => $thumbConfig, |
||
1053 | ), |
||
1054 | 'post_processors' => array( |
||
1055 | 'foo' => array(), |
||
1056 | ), |
||
1057 | ))) |
||
1058 | ; |
||
1059 | |||
1060 | $thumbConfig = array( |
||
1061 | 'size' => array(180, 180), |
||
1062 | 'mode' => 'outbound', |
||
1063 | ); |
||
1064 | |||
1065 | $image = $this->getMockImage(); |
||
1066 | $image |
||
1067 | ->expects($this->once()) |
||
1068 | ->method('get') |
||
1069 | ->will($this->returnValue($originalContent)) |
||
1070 | ; |
||
1071 | |||
1072 | $imagineMock = $this->createImagineMock(); |
||
1073 | $imagineMock |
||
1074 | ->expects($this->once()) |
||
1075 | ->method('load') |
||
1076 | ->with($originalContent) |
||
1077 | ->will($this->returnValue($image)) |
||
1078 | ; |
||
1079 | |||
1080 | $loader = $this->getMockLoader(); |
||
1081 | $loader |
||
1082 | ->expects($this->once()) |
||
1083 | ->method('load') |
||
1084 | ->with($this->identicalTo($image), $thumbConfig) |
||
1085 | ->will($this->returnArgument(0)) |
||
1086 | ; |
||
1087 | |||
1088 | $filterManager = new FilterManager( |
||
1089 | $config, |
||
1090 | $imagineMock, |
||
1091 | $this->getMockMimeTypeGuesser() |
||
1092 | ); |
||
1093 | $filterManager->addLoader('thumbnail', $loader); |
||
1094 | |||
1095 | $this->setExpectedException('InvalidArgumentException', 'Could not find post processor "foo"'); |
||
1096 | |||
1097 | $filterManager->applyFilter($binary, 'thumbnail'); |
||
1098 | } |
||
1099 | |||
1100 | public function testApplyPostProcessorsWhenNotDefined() |
||
1101 | { |
||
1102 | $binary = $this->getMock('Liip\ImagineBundle\Binary\BinaryInterface'); |
||
1103 | $filterManager = new FilterManager( |
||
1104 | $this->createFilterConfigurationMock(), |
||
1105 | $this->createImagineMock(), |
||
1106 | $this->getMockMimeTypeGuesser() |
||
1107 | ); |
||
1108 | |||
1109 | $this->assertSame($binary, $filterManager->applyPostProcessors($binary, array())); |
||
1110 | } |
||
1111 | |||
1112 | /** |
||
1113 | * @return \PHPUnit_Framework_MockObject_MockObject|LoaderInterface |
||
1114 | */ |
||
1115 | protected function getMockLoader() |
||
1119 | |||
1120 | /** |
||
1121 | * @return \PHPUnit_Framework_MockObject_MockObject|\Liip\ImagineBundle\Binary\MimeTypeGuesserInterface |
||
1122 | */ |
||
1123 | protected function getMockMimeTypeGuesser() |
||
1127 | |||
1128 | protected function getPostProcessorMock() |
||
1132 | } |
||
1133 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: