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 EndAnonymousTypeTest extends TestCase |
||
| 10 | { |
||
| 11 | public function testSetBadRole() |
||
| 12 | { |
||
| 13 | $expected = 'Role must be a valid TSimpleIdentifier'; |
||
| 14 | $actual = null; |
||
| 15 | |||
| 16 | $foo = new EndAnonymousType(); |
||
| 17 | |||
| 18 | try { |
||
| 19 | $foo->setRole(' _ '); |
||
| 20 | } catch (\InvalidArgumentException $e) { |
||
| 21 | $actual = $e->getMessage(); |
||
| 22 | } |
||
| 23 | $this->assertEquals($expected, $actual); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function testSetBadEntitySet() |
||
| 27 | { |
||
| 28 | $expected = 'Entity set must be a valid TSimpleIdentifier'; |
||
| 29 | $actual = null; |
||
| 30 | |||
| 31 | $foo = new EndAnonymousType(); |
||
| 32 | |||
| 33 | try { |
||
| 34 | $foo->setEntitySet(' _ '); |
||
| 35 | } catch (\InvalidArgumentException $e) { |
||
| 36 | $actual = $e->getMessage(); |
||
| 37 | } |
||
| 38 | $this->assertEquals($expected, $actual); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testNewCreationIsNotOK() |
||
| 42 | { |
||
| 43 | $expected = 'Entity set must be a valid TSimpleIdentifier'; |
||
| 44 | $actual = null; |
||
| 45 | |||
| 46 | $foo = new EndAnonymousType(); |
||
| 47 | $this->assertFalse($foo->isOK($actual)); |
||
| 48 | $this->assertEquals($expected, $actual); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function testIsOkWithBadRole() |
||
| 52 | { |
||
| 53 | $expected = 'Role must be a valid TSimpleIdentifier'; |
||
| 54 | $actual = null; |
||
| 55 | |||
| 56 | $foo = m::mock(EndAnonymousType::class)->makePartial(); |
||
| 57 | $foo->shouldReceive('isTSimpleIdentifierValid')->andReturn(true, true, true, false)->times(4); |
||
| 58 | $foo->setEntitySet('UnitPrice'); |
||
| 59 | $foo->setRole(' _ '); |
||
| 60 | $this->assertFalse($foo->isOK($actual)); |
||
| 61 | $this->assertEquals($expected, $actual); |
||
| 62 | } |
||
| 63 | } |
||
| 64 |