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 TEntityPropertyTypeTest extends TestCase |
||
| 10 | { |
||
| 11 | public function testSetNullDefaultValue() |
||
| 12 | { |
||
| 13 | $foo = new TEntityPropertyType(); |
||
| 14 | $foo->setDefaultValue(null); |
||
| 15 | $this->assertEquals(null, $foo->getDefaultValue()); |
||
| 16 | } |
||
| 17 | |||
| 18 | public function testSetStringDefaultValue() |
||
| 19 | { |
||
| 20 | $foo = new TEntityPropertyType(); |
||
| 21 | $foo->setDefaultValue('abc'); |
||
| 22 | $this->assertEquals('abc', $foo->getDefaultValue()); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function testSetNumericDefaultValue() |
||
| 26 | { |
||
| 27 | $foo = new TEntityPropertyType(); |
||
| 28 | $foo->setDefaultValue(1234); |
||
| 29 | $this->assertEquals('1234', $foo->getDefaultValue()); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function testSetObjectDefaultValue() |
||
| 33 | { |
||
| 34 | $expected = 'Default value must be resolvable to a string'; |
||
| 35 | $actual = null; |
||
| 36 | |||
| 37 | $foo = new TEntityPropertyType(); |
||
| 38 | try { |
||
| 39 | $foo->setDefaultValue(new \DateTime()); |
||
| 40 | } catch (\InvalidArgumentException $e) { |
||
| 41 | $actual = $e->getMessage(); |
||
| 42 | } |
||
| 43 | $this->assertEquals($expected, $actual); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function testSetEmptyArrayDefaultValue() |
||
| 47 | { |
||
| 48 | $expected = 'Default value must be resolvable to a string'; |
||
| 49 | $actual = null; |
||
| 50 | |||
| 51 | $foo = new TEntityPropertyType(); |
||
| 52 | try { |
||
| 53 | $foo->setDefaultValue([]); |
||
| 54 | } catch (\InvalidArgumentException $e) { |
||
| 55 | $actual = $e->getMessage(); |
||
| 56 | } |
||
| 57 | $this->assertEquals($expected, $actual); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function testSetNonEmptyArrayDefaultValue() |
||
| 61 | { |
||
| 62 | $expected = 'Default value must be resolvable to a string'; |
||
| 63 | $actual = null; |
||
| 64 | |||
| 65 | $foo = new TEntityPropertyType(); |
||
| 66 | try { |
||
| 67 | $foo->setDefaultValue(['a']); |
||
| 68 | } catch (\InvalidArgumentException $e) { |
||
| 69 | $actual = $e->getMessage(); |
||
| 70 | } |
||
| 71 | $this->assertEquals($expected, $actual); |
||
| 72 | } |
||
| 73 | } |
||
| 74 |