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 WebPathResolverTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | /** @var Filesystem */ |
||
16 | private $filesystem; |
||
17 | |||
18 | /** @var string */ |
||
19 | private $basePath; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $existingFile; |
||
23 | |||
24 | public function setUp() |
||
25 | { |
||
26 | $this->filesystem = new Filesystem(); |
||
27 | $this->basePath = sys_get_temp_dir().'/aWebRoot'; |
||
28 | $this->existingFile = $this->basePath.'/aCachePrefix/aFilter/existingPath'; |
||
29 | $this->filesystem->mkdir(dirname($this->existingFile)); |
||
30 | $this->filesystem->touch($this->existingFile); |
||
31 | } |
||
32 | |||
33 | public function tearDown() |
||
37 | |||
38 | public function testImplementsResolverInterface() |
||
39 | { |
||
40 | $rc = new \ReflectionClass('Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver'); |
||
41 | |||
42 | $this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface')); |
||
43 | } |
||
44 | |||
45 | public function testCouldBeConstructedWithRequiredArguments() |
||
46 | { |
||
47 | $filesystemMock = $this->createFilesystemMock(); |
||
48 | $requestContext = new RequestContext(); |
||
49 | $webRoot = 'theWebRoot'; |
||
50 | |||
51 | $resolver = new WebPathResolver($filesystemMock, $requestContext, $webRoot); |
||
|
|||
52 | |||
53 | $this->assertAttributeSame($filesystemMock, 'filesystem', $resolver); |
||
54 | $this->assertAttributeSame($requestContext, 'requestContext', $resolver); |
||
55 | $this->assertAttributeSame($webRoot, 'webRoot', $resolver); |
||
56 | } |
||
57 | |||
58 | public function testCouldBeConstructedWithOptionalArguments() |
||
59 | { |
||
60 | $resolver = new WebPathResolver( |
||
61 | $this->createFilesystemMock(), |
||
62 | new RequestContext(), |
||
63 | 'aWebRoot', |
||
64 | 'theCachePrefix' |
||
65 | ); |
||
66 | |||
67 | $this->assertAttributeSame('theCachePrefix', 'cachePrefix', $resolver); |
||
68 | } |
||
69 | |||
70 | public function testTrimRightSlashFromWebPathOnConstruct() |
||
71 | { |
||
72 | $resolver = new WebPathResolver( |
||
73 | $this->createFilesystemMock(), |
||
74 | new RequestContext(), |
||
75 | 'aWebRoot/', |
||
76 | 'theCachePrefix' |
||
77 | ); |
||
78 | |||
79 | $this->assertAttributeSame('aWebRoot', 'webRoot', $resolver); |
||
80 | } |
||
81 | |||
82 | public function testRemoveDoubleSlashFromWebRootOnConstruct() |
||
83 | { |
||
84 | $resolver = new WebPathResolver( |
||
85 | $this->createFilesystemMock(), |
||
86 | new RequestContext(), |
||
87 | 'aWeb//Root', |
||
88 | '/aCachePrefix' |
||
89 | ); |
||
90 | |||
91 | $this->assertAttributeSame('aWeb/Root', 'webRoot', $resolver); |
||
92 | } |
||
93 | |||
94 | public function testTrimRightSlashFromCachePrefixOnConstruct() |
||
95 | { |
||
96 | $resolver = new WebPathResolver( |
||
97 | $this->createFilesystemMock(), |
||
98 | new RequestContext(), |
||
99 | 'aWebRoot', |
||
100 | '/aCachePrefix' |
||
101 | ); |
||
102 | |||
103 | $this->assertAttributeSame('aCachePrefix', 'cachePrefix', $resolver); |
||
104 | } |
||
105 | |||
106 | public function testRemoveDoubleSlashFromCachePrefixOnConstruct() |
||
107 | { |
||
108 | $resolver = new WebPathResolver( |
||
109 | $this->createFilesystemMock(), |
||
110 | new RequestContext(), |
||
111 | 'aWebRoot', |
||
112 | 'aCache//Prefix' |
||
113 | ); |
||
114 | |||
115 | $this->assertAttributeSame('aCache/Prefix', 'cachePrefix', $resolver); |
||
116 | } |
||
117 | |||
118 | View Code Duplication | public function testReturnTrueIfFileExistsOnIsStored() |
|
119 | { |
||
120 | $resolver = new WebPathResolver( |
||
121 | $this->createFilesystemMock(), |
||
122 | new RequestContext(), |
||
123 | $this->basePath, |
||
124 | 'aCachePrefix' |
||
125 | ); |
||
126 | |||
127 | $this->assertTrue($resolver->isStored('existingPath', 'aFilter')); |
||
128 | } |
||
129 | |||
130 | View Code Duplication | public function testReturnFalseIfFileNotExistsOnIsStored() |
|
131 | { |
||
132 | $resolver = new WebPathResolver( |
||
133 | $this->createFilesystemMock(), |
||
134 | new RequestContext(), |
||
135 | $this->basePath, |
||
136 | 'aCachePrefix' |
||
137 | ); |
||
138 | |||
139 | $this->assertFalse($resolver->isStored('nonExistingPath', 'aFilter')); |
||
140 | } |
||
141 | |||
142 | View Code Duplication | public function testReturnFalseIfIsNotFile() |
|
143 | { |
||
144 | $resolver = new WebPathResolver( |
||
145 | $this->createFilesystemMock(), |
||
146 | new RequestContext(), |
||
147 | $this->basePath, |
||
148 | 'aCachePrefix' |
||
149 | ); |
||
150 | |||
151 | $this->assertFalse($resolver->isStored('', 'aFilter')); |
||
152 | } |
||
153 | |||
154 | View Code Duplication | public function testComposeSchemaHostAndFileUrlOnResolve() |
|
155 | { |
||
156 | $requestContext = new RequestContext(); |
||
157 | $requestContext->setScheme('theSchema'); |
||
158 | $requestContext->setHost('thehost'); |
||
159 | |||
160 | $resolver = new WebPathResolver( |
||
161 | $this->createFilesystemMock(), |
||
162 | $requestContext, |
||
163 | '/aWebRoot', |
||
164 | 'aCachePrefix' |
||
165 | ); |
||
166 | |||
167 | $this->assertEquals( |
||
168 | 'theschema://thehost/aCachePrefix/aFilter/aPath', |
||
169 | $resolver->resolve('aPath', 'aFilter') |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | View Code Duplication | public function testComposeSchemaHostAndBasePathWithPhpFileAndFileUrlOnResolve() |
|
174 | { |
||
175 | $requestContext = new RequestContext(); |
||
176 | $requestContext->setScheme('theSchema'); |
||
177 | $requestContext->setHost('thehost'); |
||
178 | $requestContext->setBaseUrl('/theBasePath/app.php'); |
||
179 | |||
180 | $resolver = new WebPathResolver( |
||
181 | $this->createFilesystemMock(), |
||
182 | $requestContext, |
||
183 | '/aWebRoot', |
||
184 | 'aCachePrefix' |
||
185 | ); |
||
186 | |||
187 | $this->assertEquals( |
||
188 | 'theschema://thehost/theBasePath/aCachePrefix/aFilter/aPath', |
||
189 | $resolver->resolve('aPath', 'aFilter') |
||
190 | ); |
||
191 | } |
||
192 | |||
193 | View Code Duplication | public function testComposeSchemaHostAndBasePathWithDirsOnlyAndFileUrlOnResolve() |
|
194 | { |
||
195 | $requestContext = new RequestContext(); |
||
196 | $requestContext->setScheme('theSchema'); |
||
197 | $requestContext->setHost('thehost'); |
||
198 | $requestContext->setBaseUrl('/theBasePath/theSubBasePath'); |
||
199 | |||
200 | $resolver = new WebPathResolver( |
||
201 | $this->createFilesystemMock(), |
||
202 | $requestContext, |
||
203 | '/aWebRoot', |
||
204 | 'aCachePrefix' |
||
205 | ); |
||
206 | |||
207 | $this->assertEquals( |
||
208 | 'theschema://thehost/theBasePath/theSubBasePath/aCachePrefix/aFilter/aPath', |
||
209 | $resolver->resolve('aPath', 'aFilter') |
||
210 | ); |
||
211 | } |
||
212 | |||
213 | View Code Duplication | public function testComposeSchemaHostAndBasePathWithBackSplashOnResolve() |
|
214 | { |
||
215 | $requestContext = new RequestContext(); |
||
216 | $requestContext->setScheme('theSchema'); |
||
217 | $requestContext->setHost('thehost'); |
||
218 | $requestContext->setBaseUrl('\\'); |
||
219 | |||
220 | $resolver = new WebPathResolver( |
||
221 | $this->createFilesystemMock(), |
||
222 | $requestContext, |
||
223 | '/aWebRoot', |
||
224 | 'aCachePrefix' |
||
225 | ); |
||
226 | |||
227 | $this->assertEquals( |
||
228 | 'theschema://thehost/aCachePrefix/aFilter/aPath', |
||
229 | $resolver->resolve('aPath', 'aFilter') |
||
230 | ); |
||
231 | } |
||
232 | |||
233 | View Code Duplication | public function testComposeSchemaHttpAndCustomPortAndFileUrlOnResolve() |
|
234 | { |
||
235 | $requestContext = new RequestContext(); |
||
236 | $requestContext->setScheme('http'); |
||
237 | $requestContext->setHost('thehost'); |
||
238 | $requestContext->setHttpPort(88); |
||
239 | |||
240 | $resolver = new WebPathResolver( |
||
241 | $this->createFilesystemMock(), |
||
242 | $requestContext, |
||
243 | '/aWebRoot', |
||
244 | 'aCachePrefix' |
||
245 | ); |
||
246 | |||
247 | $this->assertEquals( |
||
248 | 'http://thehost:88/aCachePrefix/aFilter/aPath', |
||
249 | $resolver->resolve('aPath', 'aFilter') |
||
250 | ); |
||
251 | } |
||
252 | |||
253 | View Code Duplication | public function testComposeSchemaHttpsAndCustomPortAndFileUrlOnResolve() |
|
254 | { |
||
255 | $requestContext = new RequestContext(); |
||
256 | $requestContext->setScheme('https'); |
||
257 | $requestContext->setHost('thehost'); |
||
258 | $requestContext->setHttpsPort(444); |
||
259 | |||
260 | $resolver = new WebPathResolver( |
||
261 | $this->createFilesystemMock(), |
||
262 | $requestContext, |
||
263 | '/aWebRoot', |
||
264 | 'aCachePrefix' |
||
265 | ); |
||
266 | |||
267 | $this->assertEquals( |
||
268 | 'https://thehost:444/aCachePrefix/aFilter/aPath', |
||
269 | $resolver->resolve('aPath', 'aFilter') |
||
270 | ); |
||
271 | } |
||
272 | |||
273 | public function testDumpBinaryContentOnStore() |
||
274 | { |
||
275 | $binary = new Binary('theContent', 'aMimeType', 'aFormat'); |
||
276 | |||
277 | $filesystemMock = $this->createFilesystemMock(); |
||
278 | $filesystemMock |
||
279 | ->expects($this->once()) |
||
280 | ->method('dumpFile') |
||
281 | ->with('/aWebRoot/aCachePrefix/aFilter/aPath', 'theContent') |
||
282 | ; |
||
283 | |||
284 | $resolver = new WebPathResolver( |
||
285 | $filesystemMock, |
||
286 | new RequestContext(), |
||
287 | '/aWebRoot', |
||
288 | 'aCachePrefix' |
||
289 | ); |
||
290 | |||
291 | $this->assertNull($resolver->store($binary, 'aPath', 'aFilter')); |
||
292 | } |
||
293 | |||
294 | public function testDoNothingIfFiltersAndPathsEmptyOnRemove() |
||
295 | { |
||
296 | $filesystemMock = $this->createFilesystemMock(); |
||
297 | $filesystemMock |
||
298 | ->expects($this->never()) |
||
299 | ->method('remove') |
||
300 | ; |
||
301 | |||
302 | $resolver = new WebPathResolver( |
||
303 | $filesystemMock, |
||
304 | new RequestContext(), |
||
305 | '/aWebRoot', |
||
306 | 'aCachePrefix' |
||
307 | ); |
||
308 | |||
309 | $resolver->remove(array(), array()); |
||
310 | } |
||
311 | |||
312 | View Code Duplication | public function testRemoveCacheForPathAndFilterOnRemove() |
|
313 | { |
||
314 | $filesystemMock = $this->createFilesystemMock(); |
||
315 | $filesystemMock |
||
316 | ->expects($this->once()) |
||
317 | ->method('remove') |
||
318 | ->with('/aWebRoot/aCachePrefix/aFilter/aPath') |
||
319 | ; |
||
320 | |||
321 | $resolver = new WebPathResolver( |
||
322 | $filesystemMock, |
||
323 | new RequestContext(), |
||
324 | '/aWebRoot', |
||
325 | 'aCachePrefix' |
||
326 | ); |
||
327 | |||
328 | $resolver->remove(array('aPath'), array('aFilter')); |
||
329 | } |
||
330 | |||
331 | public function testRemoveCacheForSomePathsAndFilterOnRemove() |
||
332 | { |
||
333 | $filesystemMock = $this->createFilesystemMock(); |
||
334 | $filesystemMock |
||
335 | ->expects($this->at(0)) |
||
336 | ->method('remove') |
||
337 | ->with('/aWebRoot/aCachePrefix/aFilter/aPathOne') |
||
338 | ; |
||
339 | $filesystemMock |
||
340 | ->expects($this->at(1)) |
||
341 | ->method('remove') |
||
342 | ->with('/aWebRoot/aCachePrefix/aFilter/aPathTwo') |
||
343 | ; |
||
344 | |||
345 | $resolver = new WebPathResolver( |
||
346 | $filesystemMock, |
||
347 | new RequestContext(), |
||
348 | '/aWebRoot', |
||
349 | 'aCachePrefix' |
||
350 | ); |
||
351 | |||
352 | $resolver->remove(array('aPathOne', 'aPathTwo'), array('aFilter')); |
||
353 | } |
||
354 | |||
355 | public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() |
||
356 | { |
||
357 | $filesystemMock = $this->createFilesystemMock(); |
||
358 | $filesystemMock |
||
359 | ->expects($this->at(0)) |
||
360 | ->method('remove') |
||
361 | ->with('/aWebRoot/aCachePrefix/aFilterOne/aPathOne') |
||
362 | ; |
||
363 | $filesystemMock |
||
364 | ->expects($this->at(1)) |
||
365 | ->method('remove') |
||
366 | ->with('/aWebRoot/aCachePrefix/aFilterTwo/aPathOne') |
||
367 | ; |
||
368 | $filesystemMock |
||
369 | ->expects($this->at(2)) |
||
370 | ->method('remove') |
||
371 | ->with('/aWebRoot/aCachePrefix/aFilterOne/aPathTwo') |
||
372 | ; |
||
373 | $filesystemMock |
||
374 | ->expects($this->at(3)) |
||
375 | ->method('remove') |
||
376 | ->with('/aWebRoot/aCachePrefix/aFilterTwo/aPathTwo') |
||
377 | ; |
||
378 | |||
379 | $resolver = new WebPathResolver( |
||
380 | $filesystemMock, |
||
381 | new RequestContext(), |
||
382 | '/aWebRoot', |
||
383 | 'aCachePrefix' |
||
384 | ); |
||
385 | |||
386 | $resolver->remove( |
||
387 | array('aPathOne', 'aPathTwo'), |
||
388 | array('aFilterOne', 'aFilterTwo') |
||
389 | ); |
||
390 | } |
||
391 | |||
392 | View Code Duplication | public function testRemoveCacheForFilterOnRemove() |
|
393 | { |
||
394 | $filesystemMock = $this->createFilesystemMock(); |
||
395 | $filesystemMock |
||
396 | ->expects($this->once()) |
||
397 | ->method('remove') |
||
398 | ->with(array( |
||
399 | '/aWebRoot/aCachePrefix/aFilter', |
||
400 | )) |
||
401 | ; |
||
402 | |||
403 | $resolver = new WebPathResolver( |
||
404 | $filesystemMock, |
||
405 | new RequestContext(), |
||
406 | '/aWebRoot', |
||
407 | 'aCachePrefix' |
||
408 | ); |
||
409 | |||
410 | $resolver->remove(array(), array('aFilter')); |
||
411 | } |
||
412 | |||
413 | View Code Duplication | public function testRemoveCacheForSomeFiltersOnRemove() |
|
414 | { |
||
415 | $filesystemMock = $this->createFilesystemMock(); |
||
416 | $filesystemMock |
||
417 | ->expects($this->once()) |
||
418 | ->method('remove') |
||
419 | ->with(array( |
||
420 | '/aWebRoot/aCachePrefix/aFilterOne', |
||
421 | '/aWebRoot/aCachePrefix/aFilterTwo', |
||
422 | )) |
||
423 | ; |
||
424 | |||
425 | $resolver = new WebPathResolver( |
||
426 | $filesystemMock, |
||
427 | new RequestContext(), |
||
428 | '/aWebRoot', |
||
429 | 'aCachePrefix' |
||
430 | ); |
||
431 | |||
432 | $resolver->remove(array(), array('aFilterOne', 'aFilterTwo')); |
||
433 | } |
||
434 | |||
435 | View Code Duplication | public function testShouldRemoveDoubleSlashInUrl() |
|
436 | { |
||
437 | $resolver = new WebPathResolver( |
||
438 | $this->createFilesystemMock(), |
||
439 | new RequestContext(), |
||
440 | '/aWebRoot', |
||
441 | 'aCachePrefix' |
||
442 | ); |
||
443 | |||
444 | $rc = new \ReflectionClass($resolver); |
||
445 | $method = $rc->getMethod('getFileUrl'); |
||
446 | $method->setAccessible(true); |
||
447 | |||
448 | $result = $method->invokeArgs($resolver, array('/cats.jpg', 'some_filter')); |
||
449 | |||
450 | $this->assertEquals('aCachePrefix/some_filter/cats.jpg', $result); |
||
451 | } |
||
452 | |||
453 | View Code Duplication | public function testShouldSanitizeSeparatorBetweenSchemeAndAuthorityInUrl() |
|
454 | { |
||
455 | $resolver = new WebPathResolver( |
||
456 | $this->createFilesystemMock(), |
||
457 | new RequestContext(), |
||
458 | '/aWebRoot', |
||
459 | 'aCachePrefix' |
||
460 | ); |
||
461 | |||
462 | $rc = new \ReflectionClass($resolver); |
||
463 | $method = $rc->getMethod('getFileUrl'); |
||
464 | $method->setAccessible(true); |
||
465 | |||
466 | $result = $method->invokeArgs($resolver, array('https://some.meme.com/cute/cats.jpg', 'some_filter')); |
||
467 | |||
468 | $this->assertEquals('aCachePrefix/some_filter/https---some.meme.com/cute/cats.jpg', $result); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * @return \PHPUnit_Framework_MockObject_MockObject|Filesystem |
||
473 | */ |
||
474 | protected function createFilesystemMock() |
||
478 | } |
||
479 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.