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 TEntityTypeTypeTest extends TestCase |
||
| 10 | { |
||
| 11 | public function testSetBaseTypeBadData() |
||
| 12 | { |
||
| 13 | $expected = 'Base type must be a valid TQualifiedName'; |
||
| 14 | $actual = null; |
||
| 15 | $foo = new TEntityTypeType(); |
||
| 16 | $baseType = ' _ '; |
||
| 17 | |||
| 18 | try { |
||
| 19 | $foo->setBaseType($baseType); |
||
| 20 | } catch (\InvalidArgumentException $e) { |
||
| 21 | $actual = $e->getMessage(); |
||
| 22 | } |
||
| 23 | $this->assertEquals($expected, $actual); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function testIsDerivableTypeAttributesValidNewCreation() |
||
| 27 | { |
||
| 28 | $expected = 'Name cannot be null: AlgoWeb\\ODataMetadata\\MetadataV3\\edm\\TEntityTypeType'; |
||
| 29 | $actual = null; |
||
| 30 | $foo = new TEntityTypeType(); |
||
| 31 | |||
| 32 | try { |
||
| 33 | $foo->isTDerivableTypeAttributesValid($actual); |
||
| 34 | } catch (\InvalidArgumentException $e) { |
||
| 35 | $actual = $e->getMessage(); |
||
| 36 | } |
||
| 37 | $this->assertEquals($expected, $actual); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testIsDerivableTypeAttributesValidNameGoodBaseTypeNotGood() |
||
| 41 | { |
||
| 42 | $expectedStarts = 'Base type must be a valid TQualifiedName:'; |
||
| 43 | $expectedEnds = 'AlgoWeb_ODataMetadata_MetadataV3_edm_TEntityTypeType'; |
||
| 44 | $actual = null; |
||
| 45 | $foo = m::mock(TEntityTypeType::class)->makePartial(); |
||
| 46 | $foo->shouldReceive('isTQualifiedNameValid')->withAnyArgs()->andReturn(true, false)->twice(); |
||
| 47 | $foo->shouldReceive('isTTypeAttributesValid')->andReturn(true)->atLeast(1); |
||
| 48 | |||
| 49 | $foo->setName('Name'); |
||
| 50 | $foo->setBaseType('baseType'); |
||
| 51 | |||
| 52 | try { |
||
| 53 | $foo->isTDerivableTypeAttributesValid($actual); |
||
| 54 | } catch (\InvalidArgumentException $e) { |
||
| 55 | $actual = $e->getMessage(); |
||
| 56 | } |
||
| 57 | $this->assertStringStartsWith($expectedStarts, $actual); |
||
| 58 | $this->assertStringEndsWith($expectedEnds, $actual); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testSetNameNull() |
||
| 62 | { |
||
| 63 | $expected = 'Name cannot be null'; |
||
| 64 | $actual = null; |
||
| 65 | $name = null; |
||
| 66 | |||
| 67 | $foo = new TEntityTypeType(); |
||
| 68 | |||
| 69 | try { |
||
| 70 | $foo->setName($name); |
||
| 71 | } catch (\InvalidArgumentException $e) { |
||
| 72 | $actual = $e->getMessage(); |
||
| 73 | } |
||
| 74 | $this->assertEquals($expected, $actual); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function testSetNameNonString() |
||
| 78 | { |
||
| 79 | $expected = 'Name must be a valid TSimpleIdentifier'; |
||
| 80 | $actual = null; |
||
| 81 | $name = new \DateTime(); |
||
| 82 | |||
| 83 | $foo = new TEntityTypeType(); |
||
| 84 | |||
| 85 | try { |
||
| 86 | $foo->setName($name); |
||
| 87 | } catch (\InvalidArgumentException $e) { |
||
| 88 | $actual = $e->getMessage(); |
||
| 89 | } |
||
| 90 | $this->assertEquals($expected, $actual); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testIsTTypeAttributesValidNameNotSimpleIdentifier() |
||
| 94 | { |
||
| 95 | $expectedStarts = 'Name must be a valid TSimpleIdentifier:'; |
||
| 96 | $expectedEnds = 'AlgoWeb_ODataMetadata_MetadataV3_edm_TEntityTypeType'; |
||
| 97 | $actual = null; |
||
| 98 | $name = ' _ '; |
||
| 99 | |||
| 100 | $foo = m::mock(TEntityTypeType::class)->makePartial(); |
||
| 101 | $foo->shouldReceive('isTSimpleIdentifierValid')->withAnyArgs()->andReturn(true, false)->twice(); |
||
| 102 | $foo->setName($name); |
||
| 103 | |||
| 104 | $foo->isTTypeAttributesValid($actual); |
||
| 105 | $this->assertStringStartsWith($expectedStarts, $actual); |
||
| 106 | $this->assertStringEndsWith($expectedEnds, $actual); |
||
| 107 | } |
||
| 108 | } |
||
| 109 |