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 | class TEdmxTypeTest extends TestCase |
||
| 13 | { |
||
| 14 | public function testSetEmptyVersion() |
||
| 15 | { |
||
| 16 | $version = ' '; |
||
| 17 | $foo = new TEdmxType(); |
||
| 18 | |||
| 19 | $expected = 'Version cannot be null or empty'; |
||
| 20 | $actual = null; |
||
| 21 | |||
| 22 | try { |
||
| 23 | $foo->setVersion($version); |
||
| 24 | } catch (\InvalidArgumentException $e) { |
||
| 25 | $actual = $e->getMessage(); |
||
| 26 | } |
||
| 27 | $this->assertEquals($expected, $actual); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function testSetDataServiceTypeNotOK() |
||
| 31 | { |
||
| 32 | $dataServiceType = m::mock(TDataServicesType::class); |
||
| 33 | $dataServiceType->shouldReceive('isOK')->andReturn(false)->once(); |
||
| 34 | $foo = new TEdmxType(); |
||
| 35 | |||
| 36 | $expected = ''; |
||
| 37 | $actual = null; |
||
| 38 | |||
| 39 | try { |
||
| 40 | $foo->setDataServiceType($dataServiceType); |
||
| 41 | } catch (\InvalidArgumentException $e) { |
||
| 42 | $actual = $e->getMessage(); |
||
| 43 | } |
||
| 44 | $this->assertEquals($expected, $actual); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testSetDesignerTypeNotOK() |
||
| 48 | { |
||
| 49 | $designer = m::mock(TDesignerType::class); |
||
| 50 | $designer->shouldReceive('isOK')->andReturn(false)->once(); |
||
| 51 | $foo = new TEdmxType(); |
||
| 52 | |||
| 53 | $expected = ''; |
||
| 54 | $actual = null; |
||
| 55 | |||
| 56 | try { |
||
| 57 | $foo->setDesigner($designer); |
||
| 58 | } catch (\InvalidArgumentException $e) { |
||
| 59 | $actual = $e->getMessage(); |
||
| 60 | } |
||
| 61 | $this->assertEquals($expected, $actual); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function testSetDesignerTypeOK() |
||
| 65 | { |
||
| 66 | $designer = m::mock(TDesignerType::class); |
||
| 67 | $designer->shouldReceive('isOK')->andReturn(true)->twice(); |
||
| 68 | $foo = new TEdmxType(); |
||
| 69 | |||
| 70 | $foo->setDesigner($designer); |
||
| 71 | $this->assertTrue($foo->getDesigner()->isOK()); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function testSetRuntimeNotOK() |
||
| 75 | { |
||
| 76 | $runtime = m::mock(TRuntimeType::class); |
||
| 77 | $runtime->shouldReceive('isOK')->andReturn(false)->once(); |
||
| 78 | $foo = new TEdmxType(); |
||
| 79 | |||
| 80 | $expected = ''; |
||
| 81 | $actual = null; |
||
| 82 | |||
| 83 | try { |
||
| 84 | $foo->setRuntime($runtime); |
||
| 85 | } catch (\InvalidArgumentException $e) { |
||
| 86 | $actual = $e->getMessage(); |
||
| 87 | } |
||
| 88 | $this->assertEquals($expected, $actual); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testSetRuntimeOK() |
||
| 92 | { |
||
| 93 | $runtime = m::mock(TRuntimeType::class); |
||
| 94 | $runtime->shouldReceive('isOK')->andReturn(true)->twice(); |
||
| 95 | $foo = new TEdmxType(); |
||
| 96 | |||
| 97 | $foo->setRuntime($runtime); |
||
| 98 | $this->assertTrue($foo->getRuntime()->isOK()); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function testIsNewObjectOK() |
||
| 102 | { |
||
| 103 | $expected = 'Version cannot be null or empty'; |
||
| 104 | $actual = null; |
||
| 105 | $foo = new TEdmxType(); |
||
| 106 | |||
| 107 | $this->assertFalse($foo->isOK($actual)); |
||
| 108 | $this->assertEquals($expected, $actual); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function testIsObjectWithoutDataServiceOK() |
||
| 112 | { |
||
| 113 | $expected = 'Data service type cannot be null'; |
||
| 114 | $actual = null; |
||
| 115 | $foo = new TEdmxType(); |
||
| 116 | $foo->setVersion('1.5'); |
||
| 117 | |||
| 118 | $this->assertFalse($foo->isOK($actual)); |
||
| 119 | $this->assertEquals($expected, $actual); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testIsObjectWithBadDataServiceOK() |
||
| 123 | { |
||
| 124 | $unwanted2 = 'Version cannot be null or empty'; |
||
| 125 | $actual = null; |
||
| 126 | $dataServiceType = m::mock(TDataServicesType::class); |
||
| 127 | $dataServiceType->shouldReceive('isOK')->andReturn(true, false)->twice(); |
||
| 128 | $foo = new TEdmxType(); |
||
| 129 | $foo->setVersion('1.5'); |
||
| 130 | $foo->setDataServiceType($dataServiceType); |
||
| 131 | |||
| 132 | $this->assertFalse($foo->isOK($actual)); |
||
| 133 | $this->assertNotEquals($unwanted2, $actual); |
||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | public function testIsObjectWithBadDesignerServiceOK() |
||
| 138 | { |
||
| 139 | $unwanted = 'Data service type cannot be null'; |
||
| 140 | $unwanted2 = 'Version cannot be null or empty'; |
||
| 141 | $actual = null; |
||
| 142 | $designer = m::mock(TDesignerType::class); |
||
| 143 | $designer->shouldReceive('isOK')->andReturn(true, false)->twice(); |
||
| 144 | $dataServiceType = m::mock(TDataServicesType::class); |
||
| 145 | $dataServiceType->shouldReceive('isOK')->andReturn(true)->once(); |
||
| 146 | $foo = new TEdmxType(); |
||
| 147 | $foo->setVersion('1.5'); |
||
| 148 | $foo->setDataServiceType($dataServiceType); |
||
| 149 | $foo->setDesigner($designer); |
||
| 150 | |||
| 151 | $this->assertFalse($foo->isOK($actual)); |
||
| 152 | $this->assertNotEquals($unwanted, $actual); |
||
| 153 | $this->assertNotEquals($unwanted2, $actual); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function testIsObjectWithBadRuntimeServiceOK() |
||
| 157 | { |
||
| 158 | $unwanted = 'Data service type cannot be null'; |
||
| 159 | $unwanted2 = 'Version cannot be null or empty'; |
||
| 160 | $actual = null; |
||
| 161 | $runtime = m::mock(TRuntimeType::class); |
||
| 162 | $runtime->shouldReceive('isOK')->andReturn(true, false)->twice(); |
||
| 163 | $dataServiceType = m::mock(TDataServicesType::class); |
||
| 164 | $dataServiceType->shouldReceive('isOK')->andReturn(true)->once(); |
||
| 165 | $foo = new TEdmxType(); |
||
| 166 | $foo->setVersion('1.5'); |
||
| 167 | $foo->setDataServiceType($dataServiceType); |
||
| 168 | $foo->setRuntime($runtime); |
||
| 169 | |||
| 170 | $this->assertFalse($foo->isOK($actual)); |
||
| 171 | $this->assertNotEquals($unwanted, $actual); |
||
| 172 | $this->assertNotEquals($unwanted2, $actual); |
||
| 173 | } |
||
| 174 | } |
||
| 175 |