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 |
||
| 12 | { |
||
| 13 | $expected = "Name must be a valid TSimpleIdentifier"; |
||
| 14 | $actual = null; |
||
| 15 | |||
| 16 | $foo = new EntitySetAnonymousType(); |
||
| 17 | $name = " _ "; |
||
| 18 | |||
| 19 | try { |
||
| 20 | $foo->setName($name); |
||
| 21 | } catch (\InvalidArgumentException $e) { |
||
| 22 | $actual = $e->getMessage(); |
||
| 23 | } |
||
| 24 | $this->assertEquals($expected, $actual); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testSetBadEntityType() |
||
| 28 | { |
||
| 29 | $expected = "Entity type must be a valid TQualifiedName"; |
||
| 30 | $actual = null; |
||
| 31 | |||
| 32 | $foo = new EntitySetAnonymousType(); |
||
| 33 | $name = " _ "; |
||
| 34 | |||
| 35 | try { |
||
| 36 | $foo->setEntityType($name); |
||
| 37 | } catch (\InvalidArgumentException $e) { |
||
| 38 | $actual = $e->getMessage(); |
||
| 39 | } |
||
| 40 | $this->assertEquals($expected, $actual); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testSetBadGetterAccess() |
||
| 44 | { |
||
| 45 | $expected = "Getter access must be a valid TAccess"; |
||
| 46 | $actual = null; |
||
| 47 | |||
| 48 | $foo = new EntitySetAnonymousType(); |
||
| 49 | $name = " _ "; |
||
| 50 | |||
| 51 | try { |
||
| 52 | $foo->setGetterAccess($name); |
||
| 53 | } catch (\InvalidArgumentException $e) { |
||
| 54 | $actual = $e->getMessage(); |
||
| 55 | } |
||
| 56 | $this->assertEquals($expected, $actual); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testIsTEntitySetAttributesOKBadName() |
||
| 60 | { |
||
| 61 | $expectedStarts = "Name must be a valid TSimpleIdentifier"; |
||
| 62 | $expectedEnds = "AlgoWeb_ODataMetadata_MetadataV3_edm_EntityContainer_EntitySetAnonymousType"; |
||
| 63 | $actual = null; |
||
| 64 | |||
| 65 | $foo = m::mock(EntitySetAnonymousType::class)->makePartial(); |
||
| 66 | $foo->shouldReceive('isTSimpleIdentifierValid')->andReturn(true, false)->twice(); |
||
| 67 | |||
| 68 | $foo->setName(" _ "); |
||
| 69 | |||
| 70 | $this->assertFalse($foo->isTEntitySetAttributesOK($actual)); |
||
| 71 | $this->assertStringStartsWith($expectedStarts, $actual); |
||
| 72 | $this->assertStringEndsWith($expectedEnds, $actual); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function testIsTEntitySetAttributesOKBadEntityType() |
||
| 76 | { |
||
| 77 | $expectedStarts = "Entity type must be a valid TQualifiedName"; |
||
| 78 | $expectedEnds = "AlgoWeb_ODataMetadata_MetadataV3_edm_EntityContainer_EntitySetAnonymousType"; |
||
| 79 | $actual = null; |
||
| 80 | |||
| 81 | $foo = m::mock(EntitySetAnonymousType::class)->makePartial(); |
||
| 82 | $foo->shouldReceive('isTQualifiedNameValid')->andReturn(true, false)->twice(); |
||
| 83 | |||
| 84 | $foo->setEntityType(" _ "); |
||
| 85 | |||
| 86 | $this->assertFalse($foo->isTEntitySetAttributesOK($actual)); |
||
| 87 | $this->assertStringStartsWith($expectedStarts, $actual); |
||
| 88 | $this->assertStringEndsWith($expectedEnds, $actual); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testIsTEntitySetAttributesOKBadGetterAccess() |
||
| 92 | { |
||
| 93 | $expectedStarts = "Getter access must be a valid TAccess"; |
||
| 94 | $expectedEnds = "AlgoWeb_ODataMetadata_MetadataV3_edm_EntityContainer_EntitySetAnonymousType"; |
||
| 95 | $actual = null; |
||
| 96 | |||
| 97 | $foo = m::mock(EntitySetAnonymousType::class)->makePartial(); |
||
| 98 | $foo->shouldReceive('isTAccessOk')->andReturn(true, false)->twice(); |
||
| 99 | |||
| 100 | $foo->setGetterAccess(" _ "); |
||
| 101 | |||
| 102 | $this->assertFalse($foo->isTEntitySetAttributesOK($actual)); |
||
| 103 | $this->assertStringStartsWith($expectedStarts, $actual); |
||
| 104 | $this->assertStringEndsWith($expectedEnds, $actual); |
||
| 105 | } |
||
| 106 | } |
||
| 107 |