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 ImagineControllerTest extends WebTestCase |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var Client |
||
| 17 | */ |
||
| 18 | protected $client; |
||
| 19 | |||
| 20 | protected $webRoot; |
||
| 21 | |||
| 22 | protected $cacheRoot; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Filesystem |
||
| 26 | */ |
||
| 27 | protected $filesystem; |
||
| 28 | |||
| 29 | View Code Duplication | public function setUp() |
|
|
|
|||
| 30 | { |
||
| 31 | parent::setUp(); |
||
| 32 | |||
| 33 | $this->client = $this->createClient(); |
||
| 34 | |||
| 35 | $this->webRoot = self::$kernel->getContainer()->getParameter('kernel.root_dir').'/web'; |
||
| 36 | $this->cacheRoot = $this->webRoot.'/media/cache'; |
||
| 37 | |||
| 38 | $this->filesystem = new Filesystem(); |
||
| 39 | $this->filesystem->remove($this->cacheRoot); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function testCouldBeGetFromContainer() |
||
| 43 | { |
||
| 44 | $controller = self::$kernel->getContainer()->get('liip_imagine.controller'); |
||
| 45 | |||
| 46 | $this->assertInstanceOf('Liip\ImagineBundle\Controller\ImagineController', $controller); |
||
| 47 | } |
||
| 48 | |||
| 49 | View Code Duplication | public function testShouldResolvePopulatingCacheFirst() |
|
| 50 | { |
||
| 51 | //guard |
||
| 52 | $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); |
||
| 53 | |||
| 54 | $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/cats.jpeg'); |
||
| 55 | |||
| 56 | $response = $this->client->getResponse(); |
||
| 57 | |||
| 58 | $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 59 | $this->assertEquals(301, $response->getStatusCode()); |
||
| 60 | $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl()); |
||
| 61 | |||
| 62 | $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); |
||
| 63 | } |
||
| 64 | |||
| 65 | View Code Duplication | public function testShouldResolveFromCache() |
|
| 66 | { |
||
| 67 | $this->filesystem->dumpFile( |
||
| 68 | $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', |
||
| 69 | 'anImageContent' |
||
| 70 | ); |
||
| 71 | |||
| 72 | $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/cats.jpeg'); |
||
| 73 | |||
| 74 | $response = $this->client->getResponse(); |
||
| 75 | |||
| 76 | $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 77 | $this->assertEquals(301, $response->getStatusCode()); |
||
| 78 | $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl()); |
||
| 79 | |||
| 80 | $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
||
| 85 | * @expectedExceptionMessage Signed url does not pass the sign check for path "images/cats.jpeg" and filter "thumbnail_web_path" and runtime config {"thumbnail":{"size":["50","50"]}} |
||
| 86 | */ |
||
| 87 | public function testThrowBadRequestIfSignInvalidWhileUsingCustomFilters() |
||
| 88 | { |
||
| 89 | $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/rc/invalidHash/images/cats.jpeg?'.http_build_query(array( |
||
| 90 | 'filters' => array( |
||
| 91 | 'thumbnail' => array('size' => array(50, 50)), |
||
| 92 | ), |
||
| 93 | '_hash' => 'invalid', |
||
| 94 | ))); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 99 | * @expectedExceptionMessage Filters must be an array. Value was "some-string" |
||
| 100 | */ |
||
| 101 | public function testShouldThrowNotFoundHttpExceptionIfFiltersNotArray() |
||
| 102 | { |
||
| 103 | $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/rc/invalidHash/images/cats.jpeg?'.http_build_query(array( |
||
| 104 | 'filters' => 'some-string', |
||
| 105 | '_hash' => 'hash', |
||
| 106 | ))); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 111 | * @expectedExceptionMessage Source image could not be found |
||
| 112 | */ |
||
| 113 | public function testShouldThrowNotFoundHttpExceptionIfFileNotExists() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 120 | */ |
||
| 121 | public function testInvalidFilterShouldThrowNotFoundHttpException() |
||
| 125 | |||
| 126 | View Code Duplication | public function testShouldResolveWithCustomFiltersPopulatingCacheFirst() |
|
| 127 | { |
||
| 128 | /** @var Signer $signer */ |
||
| 129 | $signer = self::$kernel->getContainer()->get('liip_imagine.cache.signer'); |
||
| 130 | |||
| 131 | $params = array( |
||
| 158 | |||
| 159 | View Code Duplication | public function testShouldResolveWithCustomFiltersFromCache() |
|
| 193 | |||
| 194 | public function testShouldResolvePathWithSpecialCharactersAndWhiteSpaces() |
||
| 213 | } |
||
| 214 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.