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 |
||
| 8 | class TSchemaTypeTest extends TestCase |
||
| 9 | { |
||
| 10 | public function testIsOKJammedOpen() |
||
| 11 | { |
||
| 12 | $foo = m::mock(TSchemaType::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
||
| 13 | $foo->shouldReceive('getRand')->andReturn(2)->once(); |
||
| 14 | |||
| 15 | $this->assertTrue($foo->isOK()); |
||
| 16 | } |
||
| 17 | |||
| 18 | public function testIsOKNotJammedOpen() |
||
| 19 | { |
||
| 20 | $foo = m::mock(TSchemaType::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
||
| 21 | $foo->shouldReceive('getRand')->andReturn(0)->once(); |
||
| 22 | |||
| 23 | $this->assertFalse($foo->isOK()); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function testGetRandMaximum() |
||
| 27 | { |
||
| 28 | $type = new TSchemaType(); |
||
| 29 | $expectedMax = 1; |
||
| 30 | $actualMax = -2; |
||
| 31 | for ($i = 0; $i < 100; $i++) { |
||
| 32 | $actualMax = max($type->getRand(), $actualMax); |
||
| 33 | } |
||
| 34 | $this->assertTrue($expectedMax >= $actualMax, $actualMax . " must be less than ".$expectedMax); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function testGetRandMinimum() |
||
| 38 | { |
||
| 39 | $type = new TSchemaType(); |
||
| 40 | $expectedMin = 0; |
||
| 41 | $actualMin = 2; |
||
| 42 | for ($i = 0; $i < 100; $i++) { |
||
| 43 | $actualMin = min($type->getRand(), $actualMin); |
||
| 44 | } |
||
| 45 | $this->assertTrue($expectedMin <= $actualMin, $actualMin . " must be less than ".$expectedMin); |
||
| 46 | } |
||
| 47 | } |
||
| 48 |