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 |
||
| 8 | class FieldTest extends TestCase |
||
| 9 | {
|
||
| 10 | protected $defaultValues = [ |
||
| 11 | 'fid' => 1, |
||
| 12 | 'fvalueString' => '', |
||
| 13 | 'fvalueInt' => 0, |
||
| 14 | 'fvalueFloat' => 0, |
||
| 15 | 'fvalueImage' => '', |
||
| 16 | 'fvalueDatetime' => 0, |
||
| 17 | 'fvalueDate' => '', |
||
| 18 | 'fvalueRangeInt' => [ |
||
| 19 | 'fvalueRangeIntMin' => 0, |
||
| 20 | 'fvalueRangeIntMax' => 0, |
||
| 21 | ], |
||
| 22 | 'fvalueRangeFloat' => [ |
||
| 23 | 'fvalueRangeFloatMin' => 0, |
||
| 24 | 'fvalueRangeFloatMax' => 0, |
||
| 25 | ], |
||
| 26 | 'fvalueRangeDate' => [ |
||
| 27 | 'fvalueRangeDateMin' => '', |
||
| 28 | 'fvalueRangeDateMax' => '', |
||
| 29 | ], |
||
| 30 | ]; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * @expectedException InvalidArgumentException |
||
| 35 | * @expectedExceptionMessage fid must be an integer |
||
| 36 | */ |
||
| 37 | public function testNonIntegerFid() |
||
| 38 | {
|
||
| 39 | new Field('some string');
|
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Tests that output of Field->toArray() has the exact keys expected by WebAPI |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | public function testReturnedArray() |
||
| 48 | {
|
||
| 49 | $field = new Field(1, 'some string'); |
||
| 50 | |||
| 51 | $this->assertEquals([ |
||
| 52 | 'fid' => 1, |
||
| 53 | 'fvalueString' => 'some string', |
||
| 54 | 'fvalueInt' => 0, |
||
| 55 | 'fvalueFloat' => 0, |
||
| 56 | 'fvalueImage' => '', |
||
| 57 | 'fvalueDatetime' => 0, |
||
| 58 | 'fvalueDate' => '', |
||
| 59 | 'fvalueRangeInt' => [ |
||
| 60 | 'fvalueRangeIntMin' => 0, |
||
| 61 | 'fvalueRangeIntMax' => 0, |
||
| 62 | ], |
||
| 63 | 'fvalueRangeFloat' => [ |
||
| 64 | 'fvalueRangeFloatMin' => 0, |
||
| 65 | 'fvalueRangeFloatMax' => 0, |
||
| 66 | ], |
||
| 67 | 'fvalueRangeDate' => [ |
||
| 68 | 'fvalueRangeDateMin' => '', |
||
| 69 | 'fvalueRangeDateMax' => '', |
||
| 70 | ], |
||
| 71 | |||
| 72 | ], $field->toArray()); |
||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | public function valueTypesProvider() |
||
| 77 | {
|
||
| 78 | return [ |
||
| 79 | 'string' => [ |
||
| 80 | 'some string', |
||
| 81 | 'fvalueString' |
||
| 82 | ], |
||
| 83 | 'integer' => [ |
||
| 84 | 42, |
||
| 85 | 'fvalueInt' |
||
| 86 | ], |
||
| 87 | 'float' => [ |
||
| 88 | 13.5, |
||
| 89 | 'fvalueFloat' |
||
| 90 | ], |
||
| 91 | 'date' => [ |
||
| 92 | '01-03-2017', |
||
| 93 | 'fvalueDate' |
||
| 94 | ], |
||
| 95 | |||
| 96 | ]; |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Test if various value types are properly handled |
||
| 102 | * @dataProvider valueTypesProvider |
||
| 103 | */ |
||
| 104 | public function testValueTypes($testValue, $arrayKey) |
||
| 105 | {
|
||
| 106 | $field = new Field(1, $testValue); |
||
| 107 | $this->assertArrayHasKey($arrayKey, $field->toArray(), 'Key not present in result array'); |
||
| 108 | $this->assertSame($field->toArray()[$arrayKey], $testValue, 'Different value in result array'); |
||
| 109 | $this->assertSame($field->getValue(), $testValue, 'Different getValue() return value'); |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | public function rangeValueTypesProvider() |
||
| 115 | {
|
||
| 116 | return [ |
||
| 117 | 'float range' => [ |
||
| 118 | [10.5, 13.5], |
||
| 119 | 'fvalueRangeFloat' |
||
| 120 | ], |
||
| 121 | 'int range' => [ |
||
| 122 | [10, 11], |
||
| 123 | 'fvalueRangeInt' |
||
| 124 | ], |
||
| 125 | 'date range' => [ |
||
| 126 | ['03-03-2016', '01-03-2017'], |
||
| 127 | 'fvalueRangeDate' |
||
| 128 | ], |
||
| 129 | ]; |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Test if various range types are properly handled |
||
| 135 | * @dataProvider rangeValueTypesProvider |
||
| 136 | */ |
||
| 137 | public function testRangeValues($testRange, $rangeKey) |
||
| 138 | {
|
||
| 139 | $expectedArrayKeys = [ |
||
| 140 | "{$rangeKey}Min",
|
||
| 141 | "{$rangeKey}Max"
|
||
| 142 | ]; |
||
| 143 | |||
| 144 | $field = new Field(2, $testRange); |
||
| 145 | $this->assertArrayHasKey( |
||
| 146 | $rangeKey, |
||
| 147 | $field->toArray(), |
||
| 148 | 'key doesn\'t exist in array representation' |
||
| 149 | ); |
||
| 150 | |||
| 151 | $this->assertSame( |
||
| 152 | $field->toArray()[$rangeKey], |
||
| 153 | array_combine($expectedArrayKeys, $testRange), |
||
| 154 | 'array representation contains wrong value' |
||
| 155 | ); |
||
| 156 | |||
| 157 | $this->assertSame( |
||
| 158 | $field->getValue(), |
||
| 159 | $testRange, |
||
| 160 | 'getValue() returns wrong value' |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Test if range types are properly handled when reversed |
||
| 167 | * @dataProvider rangeValueTypesProvider |
||
| 168 | */ |
||
| 169 | public function testRangeValuesReversed($testRange, $rangeKey) |
||
| 170 | {
|
||
| 171 | $expectedArrayKeys = [ |
||
| 172 | "{$rangeKey}Min",
|
||
| 173 | "{$rangeKey}Max"
|
||
| 174 | ]; |
||
| 175 | |||
| 176 | $reverseRange = array_reverse($testRange); |
||
| 177 | |||
| 178 | $field = new Field(2, $reverseRange); |
||
| 179 | $this->assertArrayHasKey( |
||
| 180 | $rangeKey, |
||
| 181 | $field->toArray(), |
||
| 182 | 'key doesn\'t exist in array representation' |
||
| 183 | ); |
||
| 184 | |||
| 185 | $this->assertSame( |
||
| 186 | $field->toArray()[$rangeKey], |
||
| 187 | array_combine($expectedArrayKeys, $testRange), |
||
| 188 | 'array representation contains wrong value' |
||
| 189 | ); |
||
| 190 | |||
| 191 | $this->assertSame( |
||
| 192 | $field->getValue(), |
||
| 193 | $testRange, |
||
| 194 | 'getValue() returns wrong value' |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Test forced value type - datetime |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function testDatetimeValue() |
||
| 204 | {
|
||
| 205 | $datetime = time(); |
||
| 206 | $field = new Field(1, $datetime, 'fvalueDatetime'); |
||
| 207 | $this->assertArrayHasKey('fvalueDatetime', $field->toArray());
|
||
| 208 | $this->assertSame($field->toArray()['fvalueDatetime'], $datetime); |
||
| 209 | $this->assertSame($field->getValue(), $datetime); |
||
| 210 | } |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * @expectedException InvalidArgumentException |
||
| 215 | * @expectedExceptionMessage Not supported value type: object; fid=1 |
||
| 216 | */ |
||
| 217 | public function testExceptionOnInvalidValue() |
||
| 218 | {
|
||
| 219 | new Field(1, (object)['foo' => 'bar']); |
||
| 220 | } |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @expectedException InvalidArgumentException |
||
| 225 | * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn |
||
| 226 | */ |
||
| 227 | public function testExceptionOnInvalidForcedValue() |
||
| 228 | {
|
||
| 229 | new Field(1, 'something', 'fvalueUnicorn'); |
||
| 230 | } |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Test fid value |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | public function testFid() |
||
| 238 | {
|
||
| 239 | $field = new Field(42, 'don\'t panic!'); |
||
| 240 | $this->assertSame($field->getFid(), 42); |
||
| 241 | } |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @dataProvider valueTypesProvider |
||
| 246 | */ |
||
| 247 | public function testCreatingFromArray($value, $key) |
||
| 248 | {
|
||
| 249 | $array = $this->defaultValues; |
||
| 250 | $array[$key] = $value; |
||
| 251 | $field = new Field(42); |
||
| 252 | $field->fromArray($array); |
||
| 253 | |||
| 254 | $this->assertSame($field->getValue(), $value); |
||
| 255 | $this->assertSame($field->getFid(), $this->defaultValues['fid']); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @expectedException InvalidArgumentException |
||
| 260 | * @expectedExceptionMessage Range array must have exactly 2 elements |
||
| 261 | */ |
||
| 262 | public function testExceptionOnInvalidRangeArrayItemCount() |
||
| 263 | {
|
||
| 264 | $field = new Field(12, [1, 2, 3]); |
||
| 265 | } |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * @expectedException InvalidArgumentException |
||
| 270 | * @expectedExceptionMessage Fid is required |
||
| 271 | */ |
||
| 272 | public function testExceptionOnFromArrayMissingFid() |
||
| 273 | {
|
||
| 274 | $fieldArray = $this->defaultValues; |
||
| 275 | unset($fieldArray['fid']); |
||
| 276 | $field = new Field(); |
||
| 277 | $field->fromArray($fieldArray); |
||
| 278 | } |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * @expectedException InvalidArgumentException |
||
| 283 | * @expectedExceptionMessage Unknown Field property: thisKeyIsInvalid |
||
| 284 | */ |
||
| 285 | public function testExceptionOnFromArrayInvalidArrayKey() |
||
| 286 | {
|
||
| 287 | $fieldArray = $this->defaultValues; |
||
| 288 | $fieldArray['thisKeyIsInvalid'] = 'whatever'; |
||
| 289 | $field = new Field(); |
||
| 290 | $field->fromArray($fieldArray); |
||
| 291 | } |
||
| 292 | |||
| 293 | |||
| 294 | public function testNullReturnValueWhenAllFieldsAreDefault() |
||
| 295 | {
|
||
| 296 | $field = new Field(); |
||
| 297 | $field->fromArray($this->defaultValues); |
||
| 298 | $this->assertNull($field->getValue()); |
||
| 299 | } |
||
| 300 | |||
| 301 | |||
| 302 | } |
||
| 303 |