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 |
||
| 7 | class TypedArrayTest extends \PHPUnit_Framework_TestCase |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @expectedException \TypeError |
||
| 11 | */ |
||
| 12 | public function testWithBool() { |
||
| 13 | |||
| 14 | $typedArray = new TypedArray( function($obj){ return is_bool($obj);}); |
||
| 15 | $this->assertEquals(0, $typedArray->count()); |
||
| 16 | |||
| 17 | $this->assertFalse($typedArray->add("test")); |
||
| 18 | $this->assertFalse($typedArray->add(new \stdClass())); |
||
| 19 | |||
| 20 | $this->assertNotFalse($typedArray->add(true)); |
||
| 21 | $this->assertEquals(1, $typedArray->count()); |
||
| 22 | $this->assertFalse($typedArray->contains(false)); |
||
| 23 | $this->assertTrue($typedArray->contains(true)); |
||
| 24 | |||
| 25 | $this->assertNotFalse($typedArray->add(false)); |
||
| 26 | $this->assertEquals(2, $typedArray->count()); |
||
| 27 | $this->assertTrue($typedArray->contains(false)); |
||
| 28 | $this->assertTrue($typedArray->contains(true)); |
||
| 29 | |||
| 30 | $this->assertFalse($typedArray->delete(42)); |
||
| 31 | $this->assertFalse($typedArray->delete(6)); |
||
| 32 | |||
| 33 | $total = $typedArray->count(); |
||
| 34 | $typedArray->delete(0); |
||
| 35 | $this->assertEquals($total -1 , $typedArray->count()); |
||
| 36 | |||
| 37 | $typedArray->contains(42); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @expectedException \TypeError |
||
| 42 | */ |
||
| 43 | public function testWithString() { |
||
| 44 | $typedArray = new TypedArray( function($obj){ return is_string($obj);}); |
||
| 45 | |||
| 46 | |||
| 47 | $this->assertFalse($typedArray->add(true)); |
||
| 48 | $this->assertFalse($typedArray->add(new \stdClass())); |
||
| 49 | $this->assertFalse($typedArray->add(42)); |
||
| 50 | |||
| 51 | $this->assertNotFalse($typedArray->add('plop')); |
||
| 52 | $this->assertFalse($typedArray->contains('123')); |
||
| 53 | $this->assertFalse($typedArray->contains('PloP')); |
||
| 54 | $this->assertTrue($typedArray->contains('plop')); |
||
| 55 | |||
| 56 | $this->assertNotFalse($typedArray->add("123")); |
||
| 57 | $this->assertTrue($typedArray->contains('123')); |
||
| 58 | $this->assertTrue($typedArray->contains('plop')); |
||
| 59 | |||
| 60 | $typedArray->contains(42); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @expectedException \TypeError |
||
| 65 | */ |
||
| 66 | public function testWithObject() { |
||
| 67 | |||
| 68 | $mockBuilder = $this->getMockBuilder('\PDO')->disableOriginalConstructor(); |
||
| 69 | $typedArray = new TypedArray('\PDO'); |
||
| 70 | $this->assertNotFalse($typedArray->add($mockBuilder->getMock())); |
||
| 71 | |||
| 72 | $this->assertFalse($typedArray->add(true)); |
||
| 73 | $this->assertFalse($typedArray->add(new \stdClass())); |
||
| 74 | $this->assertFalse($typedArray->add(42)); |
||
| 75 | |||
| 76 | $typedArray->delete('plpo'); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testWithObjectImplementEquatable() { |
||
| 80 | |||
| 81 | $mock = $this->getMockBuilder('Ducatel\PHPCollection\Base\Equatable')->getMock(); |
||
| 82 | $mock->method('equals') |
||
| 83 | ->willReturn(true); |
||
| 84 | |||
| 85 | $typedArray = new TypedArray('Ducatel\PHPCollection\Base\Equatable'); |
||
| 86 | $this->assertNotFalse($typedArray->add($mock)); |
||
| 87 | $this->assertFalse($typedArray->add(true)); |
||
| 88 | $this->assertFalse($typedArray->add(new \stdClass())); |
||
| 89 | $this->assertFalse($typedArray->add(42)); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testWithCustomEqualsFunction() { |
||
| 93 | |||
| 94 | $isStringFct = function($obj){ return is_string($obj);}; |
||
| 95 | $isEquals = function ($obj1, $obj2){ return (strcasecmp($obj1, $obj2) == 0);}; |
||
| 96 | $typedArray = new TypedArray($isStringFct, $isEquals); |
||
| 97 | |||
| 98 | $this->assertFalse($typedArray->add(true)); |
||
| 99 | $this->assertFalse($typedArray->add(new \stdClass())); |
||
| 100 | $this->assertFalse($typedArray->add(42)); |
||
| 101 | |||
| 102 | $this->assertNotFalse($typedArray->add('plop')); |
||
| 103 | $this->assertFalse($typedArray->contains('123')); |
||
| 104 | $this->assertTrue($typedArray->contains('plop')); |
||
| 105 | $this->assertTrue($typedArray->contains('PloP')); |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | } |
||
| 110 |