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 |
||
| 9 | class TValueTermTypeTest extends TestCase |
||
| 10 | { |
||
| 11 | public function testSetGetNullableRoundTrip() |
||
| 12 | { |
||
| 13 | $foo = new TValueTermType(); |
||
| 14 | $foo->setNullable(true); |
||
| 15 | $this->assertTrue($foo->getNullable()); |
||
| 16 | } |
||
| 17 | |||
| 18 | public function testSetGetNullableRoundTripNotBool() |
||
| 19 | { |
||
| 20 | $foo = new TValueTermType(); |
||
| 21 | $foo->setNullable('nullable'); |
||
| 22 | $this->assertTrue($foo->getNullable()); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function testSetBadMaxLength() |
||
| 26 | { |
||
| 27 | $expected = 'Input must be numeric'; |
||
| 28 | $actual = null; |
||
| 29 | |||
| 30 | $foo = new TValueTermType(); |
||
| 31 | |||
| 32 | try { |
||
| 33 | $foo->setMaxLength(' _ '); |
||
| 34 | } catch (\InvalidArgumentException $e) { |
||
| 35 | $actual = $e->getMessage(); |
||
| 36 | } |
||
| 37 | $this->assertEquals($expected, $actual); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testSetGetMaxLengthRoundTrip() |
||
| 41 | { |
||
| 42 | $expected = '11'; |
||
| 43 | $actual = null; |
||
| 44 | |||
| 45 | $foo = new TValueTermType(); |
||
| 46 | $foo->setMaxLength($expected); |
||
| 47 | $actual = $foo->getMaxLength(); |
||
| 48 | |||
| 49 | $this->assertEquals($expected, $actual); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function testSetNullMaxLength() |
||
| 53 | { |
||
| 54 | $foo = new TValueTermType(); |
||
| 55 | $foo->setMaxLength(null); |
||
| 56 | $this->assertNull($foo->getMaxLength()); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testSetBadFixedLength() |
||
| 60 | { |
||
| 61 | $expected = 'Fixed length must be a valid TFixedLengthFacet'; |
||
| 62 | $actual = null; |
||
| 63 | |||
| 64 | $foo = new TValueTermType(); |
||
| 65 | |||
| 66 | try { |
||
| 67 | $foo->setFixedLength(' _ '); |
||
| 68 | } catch (\InvalidArgumentException $e) { |
||
| 69 | $actual = $e->getMessage(); |
||
| 70 | } |
||
| 71 | $this->assertEquals($expected, $actual); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function testSetGetFixedLengthRoundTrip() |
||
| 75 | { |
||
| 76 | $expected = true; |
||
| 77 | $actual = null; |
||
| 78 | |||
| 79 | $foo = new TValueTermType(); |
||
| 80 | $foo->setFixedLength($expected); |
||
| 81 | $actual = $foo->getFixedLength(); |
||
| 82 | |||
| 83 | $this->assertEquals($expected, $actual); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function testSetBadPrecision() |
||
| 87 | { |
||
| 88 | $expected = 'Input must be numeric'; |
||
| 89 | $actual = null; |
||
| 90 | |||
| 91 | $foo = new TValueTermType(); |
||
| 92 | |||
| 93 | try { |
||
| 94 | $foo->setPrecision(' _ '); |
||
| 95 | } catch (\InvalidArgumentException $e) { |
||
| 96 | $actual = $e->getMessage(); |
||
| 97 | } |
||
| 98 | $this->assertEquals($expected, $actual); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function testSetGetPrecisionRoundTrip() |
||
| 102 | { |
||
| 103 | $expected = '11'; |
||
| 104 | $actual = null; |
||
| 105 | |||
| 106 | $foo = new TValueTermType(); |
||
| 107 | $foo->setPrecision($expected); |
||
| 108 | $actual = $foo->getPrecision(); |
||
| 109 | |||
| 110 | $this->assertEquals($expected, $actual); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function testSetNullPrecision() |
||
| 114 | { |
||
| 115 | $foo = new TValueTermType(); |
||
| 116 | $foo->setPrecision(null); |
||
| 117 | $this->assertNull($foo->getPrecision()); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testSetBadScale() |
||
| 121 | { |
||
| 122 | $expected = 'Input must be numeric'; |
||
| 123 | $actual = null; |
||
| 124 | |||
| 125 | $foo = new TValueTermType(); |
||
| 126 | |||
| 127 | try { |
||
| 128 | $foo->setScale(' _ '); |
||
| 129 | } catch (\InvalidArgumentException $e) { |
||
| 130 | $actual = $e->getMessage(); |
||
| 131 | } |
||
| 132 | $this->assertEquals($expected, $actual); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function testSetGetScaleRoundTrip() |
||
| 136 | { |
||
| 137 | $expected = '11'; |
||
| 138 | $actual = null; |
||
| 139 | |||
| 140 | $foo = new TValueTermType(); |
||
| 141 | $foo->setScale($expected); |
||
| 142 | $actual = $foo->getScale(); |
||
| 143 | |||
| 144 | $this->assertEquals($expected, $actual); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testSetNullScale() |
||
| 148 | { |
||
| 149 | $foo = new TValueTermType(); |
||
| 150 | $foo->setScale(null); |
||
| 151 | $this->assertNull($foo->getScale()); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function testSetBadUnicode() |
||
| 155 | { |
||
| 156 | $expected = 'Unicode must be a valid TUnicodeFacet'; |
||
| 157 | $actual = null; |
||
| 158 | |||
| 159 | $foo = new TValueTermType(); |
||
| 160 | |||
| 161 | try { |
||
| 162 | $foo->setUnicode(' _ '); |
||
| 163 | } catch (\InvalidArgumentException $e) { |
||
| 164 | $actual = $e->getMessage(); |
||
| 165 | } |
||
| 166 | $this->assertEquals($expected, $actual); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function testSetGetUnicodeRoundTrip() |
||
| 170 | { |
||
| 171 | $expected = true; |
||
| 172 | $actual = null; |
||
| 173 | |||
| 174 | $foo = new TValueTermType(); |
||
| 175 | $foo->setUnicode($expected); |
||
| 176 | $actual = $foo->getUnicode(); |
||
| 177 | |||
| 178 | $this->assertEquals($expected, $actual); |
||
| 179 | } |
||
| 180 | |||
| 181 | public function testSetBadCollation() |
||
| 182 | { |
||
| 183 | $expected = 'Collation must be a valid TCollationFacet'; |
||
| 184 | $actual = null; |
||
| 185 | |||
| 186 | $foo = new TValueTermType(); |
||
| 187 | |||
| 188 | try { |
||
| 189 | $foo->setCollation(new \DateTime()); |
||
| 190 | } catch (\InvalidArgumentException $e) { |
||
| 191 | $actual = $e->getMessage(); |
||
| 192 | } |
||
| 193 | $this->assertEquals($expected, $actual); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function testSetGetCollationRoundTrip() |
||
| 197 | { |
||
| 198 | $expected = 'swedish'; |
||
| 199 | $actual = null; |
||
| 200 | |||
| 201 | $foo = new TValueTermType(); |
||
| 202 | $foo->setCollation($expected); |
||
| 203 | $actual = $foo->getCollation(); |
||
| 204 | |||
| 205 | $this->assertEquals($expected, $actual); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function testSetBadSRID() |
||
| 209 | { |
||
| 210 | $expected = 'Input must be numeric'; |
||
| 211 | $actual = null; |
||
| 212 | |||
| 213 | $foo = new TValueTermType(); |
||
| 214 | |||
| 215 | try { |
||
| 216 | $foo->setSRID(' _ '); |
||
| 217 | } catch (\InvalidArgumentException $e) { |
||
| 218 | $actual = $e->getMessage(); |
||
| 219 | } |
||
| 220 | $this->assertEquals($expected, $actual); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function testSetGetSRIDRoundTrip() |
||
| 224 | { |
||
| 225 | $expected = '11'; |
||
| 226 | $actual = null; |
||
| 227 | |||
| 228 | $foo = new TValueTermType(); |
||
| 229 | $foo->setSRID($expected); |
||
| 230 | $actual = $foo->getSRID(); |
||
| 231 | |||
| 232 | $this->assertEquals($expected, $actual); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function testSetNullSRID() |
||
| 236 | { |
||
| 237 | $expected = 'Input must be a string'; |
||
| 238 | $actual = null; |
||
| 239 | |||
| 240 | $foo = new TValueTermType(); |
||
| 241 | try { |
||
| 242 | $foo->setSRID(null); |
||
| 243 | } catch (\InvalidArgumentException $e) { |
||
| 244 | $actual = $e->getMessage(); |
||
| 245 | } |
||
| 246 | $this->assertEquals($expected, $actual); |
||
| 247 | } |
||
| 248 | |||
| 249 | public function testIsTFacetAttributeValidNewCreation() |
||
| 250 | { |
||
| 251 | $expected = 'Nullable must be boolean: AlgoWeb\ODataMetadata\MetadataV3\edm\TValueTermType'; |
||
| 252 | $actual = null; |
||
| 253 | |||
| 254 | $foo = new TValueTermType(); |
||
| 255 | $this->assertFalse($foo->isTFacetAttributesTraitValid($actual)); |
||
| 256 | $this->assertEquals($expected, $actual); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function testIsTFacetAttributeValidNullableSet() |
||
| 260 | { |
||
| 261 | $expected = ''; |
||
| 262 | $actual = null; |
||
| 263 | |||
| 264 | $foo = new TValueTermType(); |
||
| 265 | $foo->setNullable(true); |
||
| 266 | $this->assertTrue($foo->isTFacetAttributesTraitValid($actual)); |
||
| 267 | $this->assertEquals($expected, $actual); |
||
| 268 | } |
||
| 269 | |||
| 270 | public function testIsTFacetAttributeValidBadCollation() |
||
| 271 | { |
||
| 272 | $expected = 'Collation must be a valid TCollationFacet:'; |
||
| 273 | $actual = null; |
||
| 274 | |||
| 275 | $foo = m::mock(TValueTermType::class)->makePartial(); |
||
| 276 | $foo->setNullable(true); |
||
| 277 | $foo->shouldReceive('isTCollationFacetValid')->andReturn(true, false)->twice(); |
||
| 278 | |||
| 279 | $foo->setCollation(' _ '); |
||
| 280 | $this->assertFalse($foo->isTFacetAttributesTraitValid($actual)); |
||
| 281 | $this->assertStringStartsWith($expected, $actual); |
||
| 282 | } |
||
| 283 | |||
| 284 | public function testIsTFacetAttributeValidBadMaxLength() |
||
| 285 | { |
||
| 286 | $expected = 'Max length must be a valid TMaxLengthFacet: '; |
||
| 287 | $actual = null; |
||
| 288 | |||
| 289 | $foo = m::mock(TValueTermType::class)->makePartial(); |
||
| 290 | $foo->setNullable(true); |
||
| 291 | $foo->shouldReceive('isTMaxLengthFacetValid')->andReturn(true, false)->twice(); |
||
| 292 | |||
| 293 | $foo->setMaxLength(' _ '); |
||
| 294 | $this->assertFalse($foo->isTFacetAttributesTraitValid($actual)); |
||
| 295 | $this->assertStringStartsWith($expected, $actual); |
||
| 296 | } |
||
| 297 | |||
| 298 | public function testIsTFacetAttributeValidBadFixedLength() |
||
| 299 | { |
||
| 300 | $expected = 'Fixed length must be a valid TFixedLengthFacet: '; |
||
| 301 | $actual = null; |
||
| 302 | |||
| 303 | $foo = m::mock(TValueTermType::class)->makePartial(); |
||
| 304 | $foo->setNullable(true); |
||
| 305 | $foo->shouldReceive('isTIsFixedLengthFacetTraitValid')->andReturn(true, false)->twice(); |
||
| 306 | |||
| 307 | $foo->setFixedLength(' _ '); |
||
| 308 | $this->assertFalse($foo->isTFacetAttributesTraitValid($actual)); |
||
| 309 | $this->assertStringStartsWith($expected, $actual); |
||
| 310 | } |
||
| 311 | |||
| 312 | public function testIsTFacetAttributeValidBadPrecision() |
||
| 313 | { |
||
| 314 | $expected = 'Precision must be a valid TPrecisionFacet: '; |
||
| 315 | $actual = null; |
||
| 316 | |||
| 317 | $foo = m::mock(TValueTermType::class)->makePartial(); |
||
| 318 | $foo->setNullable(true); |
||
| 319 | $foo->shouldReceive('isTPrecisionFacetValid')->andReturn(true, false)->twice(); |
||
| 320 | |||
| 321 | $foo->setPrecision(' _ '); |
||
| 322 | $this->assertFalse($foo->isTFacetAttributesTraitValid($actual)); |
||
| 323 | $this->assertStringStartsWith($expected, $actual); |
||
| 324 | } |
||
| 325 | |||
| 326 | public function testIsTFacetAttributeValidBadScale() |
||
| 327 | { |
||
| 328 | $expected = 'Scale must be a valid TScaleFacet: '; |
||
| 329 | $actual = null; |
||
| 330 | |||
| 331 | $foo = m::mock(TValueTermType::class)->makePartial(); |
||
| 332 | $foo->setNullable(true); |
||
| 333 | $foo->shouldReceive('isTScaleFacetValid')->andReturn(true, false)->twice(); |
||
| 334 | |||
| 335 | $foo->setScale(' _ '); |
||
| 336 | $this->assertFalse($foo->isTFacetAttributesTraitValid($actual)); |
||
| 337 | $this->assertStringStartsWith($expected, $actual); |
||
| 338 | } |
||
| 339 | |||
| 340 | public function testIsTFacetAttributeValidBadSRID() |
||
| 341 | { |
||
| 342 | $expected = 'SRID must be a valid TSridFacet: '; |
||
| 343 | $actual = null; |
||
| 344 | |||
| 345 | $foo = m::mock(TValueTermType::class)->makePartial(); |
||
| 346 | $foo->setNullable(true); |
||
| 347 | $foo->shouldReceive('isTSridFacetValid')->andReturn(true, false)->twice(); |
||
| 348 | |||
| 349 | $foo->setSRID(' _ '); |
||
| 350 | $this->assertFalse($foo->isTFacetAttributesTraitValid($actual)); |
||
| 351 | $this->assertStringStartsWith($expected, $actual); |
||
| 352 | } |
||
| 353 | |||
| 354 | public function testIsTFacetAttributeValidBadUnicode() |
||
| 355 | { |
||
| 356 | $expected = 'Unicode must be a valid TUnicodeFacet: '; |
||
| 357 | $actual = null; |
||
| 358 | |||
| 359 | $foo = m::mock(TValueTermType::class)->makePartial(); |
||
| 360 | $foo->setNullable(true); |
||
| 361 | $foo->shouldReceive('isTIsUnicodeFacetTraitValid')->andReturn(true, false)->twice(); |
||
| 362 | |||
| 363 | $foo->setUnicode(' _ '); |
||
| 364 | $this->assertFalse($foo->isTFacetAttributesTraitValid($actual)); |
||
| 365 | $this->assertStringStartsWith($expected, $actual); |
||
| 366 | } |
||
| 367 | } |
||
| 368 |