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 |
||
| 17 | class DimensionServiceTest extends UnitTestCase |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var DimensionService |
||
| 22 | */ |
||
| 23 | protected $service; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Build up |
||
| 27 | */ |
||
| 28 | protected function setUp() |
||
| 29 | { |
||
| 30 | $this->service = GeneralUtility::makeInstance(DimensionService::class); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @test |
||
| 35 | * @expectedException \Exception |
||
| 36 | */ |
||
| 37 | public function testInvalidShortRatio() |
||
| 38 | { |
||
| 39 | $this->service->getRatio(1); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @test |
||
| 44 | * @expectedException \Exception |
||
| 45 | */ |
||
| 46 | public function testInvalidLongRatio() |
||
| 47 | { |
||
| 48 | $this->service->getRatio('1:1:2'); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @test |
||
| 53 | */ |
||
| 54 | public function testValidRatio() |
||
| 55 | { |
||
| 56 | $this->assertEquals([1, 1], $this->service->getRatio('1:1')); |
||
| 57 | $this->assertEquals([16, 9], $this->service->getRatio('16:9')); |
||
| 58 | $this->assertEquals([4, 3], $this->service->getRatio('4:3')); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | public function providerFocusBoxSize() |
||
| 65 | { |
||
| 66 | return [ |
||
| 67 | [ |
||
| 68 | 100, |
||
| 69 | 0, |
||
| 70 | '1:1', |
||
| 71 | 3644, |
||
| 72 | 2397, |
||
| 73 | 2397, |
||
| 74 | 2397, |
||
| 75 | ], |
||
| 76 | ]; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @dataProvider providerFocusBoxSize |
||
| 81 | * |
||
| 82 | * @param int $focusX |
||
| 83 | * @param int $focusY |
||
| 84 | * @param string $ratio |
||
| 85 | * @param int $imageWidth |
||
| 86 | * @param int $imageHeight |
||
| 87 | * @param int $expectedWidth |
||
| 88 | * @param int $expectedHeight |
||
| 89 | * |
||
| 90 | * @test |
||
| 91 | */ |
||
| 92 | public function testFocusBoxSize( |
||
| 93 | $focusX, |
||
| 94 | $focusY, |
||
| 95 | $ratio, |
||
| 96 | $imageWidth, |
||
| 97 | $imageHeight, |
||
| 98 | $expectedWidth, |
||
| 99 | $expectedHeight |
||
| 100 | ) { |
||
| 101 | $expected = [ |
||
| 102 | $expectedWidth, |
||
| 103 | $expectedHeight, |
||
| 104 | ]; |
||
| 105 | |||
| 106 | $this->assertEquals($expected, $this->service->getFocusWidthAndHeight($imageWidth, $imageHeight, $ratio)); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function providerFocusSourcePoint() |
||
| 113 | { |
||
| 114 | return [ |
||
| 115 | [ |
||
| 116 | 100, |
||
| 117 | 0, |
||
| 118 | '1:1', |
||
| 119 | 3644, |
||
| 120 | 2397, |
||
| 121 | 1247, |
||
| 122 | 0, |
||
| 123 | ], |
||
| 124 | ]; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @depends testFocusBoxSize |
||
| 129 | * @dataProvider providerFocusSourcePoint |
||
| 130 | * |
||
| 131 | * @param int $focusX |
||
| 132 | * @param int $focusY |
||
| 133 | * @param string $ratio |
||
| 134 | * @param int $imageWidth |
||
| 135 | * @param int $imageHeight |
||
| 136 | * @param int $expectedX |
||
| 137 | * @param int $expectedY |
||
| 138 | * |
||
| 139 | * @test |
||
| 140 | */ |
||
| 141 | public function testFocusSourcePoint($focusX, $focusY, $ratio, $imageWidth, $imageHeight, $expectedX, $expectedY) |
||
| 142 | { |
||
| 143 | |||
| 144 | $expected = [ |
||
| 145 | $expectedX, |
||
| 146 | $expectedY, |
||
| 147 | ]; |
||
| 148 | |||
| 149 | list($focusWidth, $focusHeight) = $this->service->getFocusWidthAndHeight($imageWidth, $imageHeight, $ratio); |
||
| 150 | |||
| 151 | $this->assertEquals($expected, |
||
| 152 | $this->service->calculateSourcePosition(DimensionService::CROP_LANDSCAPE, $imageWidth, $imageHeight, $focusWidth, |
||
| 153 | $focusHeight, $focusX, $focusY)); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @expectedException \Exception |
||
| 158 | * @expectedExceptionMessage Ratio have to be in the format of e.g. "1:1" or "16:9" |
||
| 159 | * @test |
||
| 160 | */ |
||
| 161 | public function testRatioException() |
||
| 162 | { |
||
| 163 | $this->service->getFocusWidthAndHeight(100, 100, '11'); |
||
| 164 | } |
||
| 165 | } |
||
| 166 |