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 |
||
| 2 | class CollectionTest extends \PHPUnit\Framework\TestCase |
||
| 3 | { |
||
| 4 | public function testConstruct() |
||
| 5 | { |
||
| 6 | $arr = array(1,2,3); |
||
| 7 | $collection = new \Suricate\Collection($arr); |
||
| 8 | $this->assertEquals($arr, $collection->getItems()); |
||
| 9 | } |
||
| 10 | |||
| 11 | public function testIsEmpty() |
||
| 12 | { |
||
| 13 | $arr = array(); |
||
| 14 | $collection = new \Suricate\Collection($arr); |
||
| 15 | $this->assertEquals(true, $collection->isEmpty()); |
||
| 16 | } |
||
| 17 | |||
| 18 | public function testUnique() |
||
| 19 | { |
||
| 20 | $arr = array(1, 2, 1, 3); |
||
| 21 | $collection = new \Suricate\Collection($arr); |
||
| 22 | $this->assertEquals([0 => 1, 1 => 2, 3 => 3], $collection->unique()->getItems()); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function testCount() |
||
| 30 | } |
||
| 31 | |||
| 32 | public function testPaginate() |
||
| 33 | { |
||
| 34 | $arr = array(1, 2, 3, 4, 5); |
||
| 35 | $collection = new \Suricate\Collection($arr); |
||
| 36 | |||
| 37 | |||
| 38 | $collection->paginate(1, 3); |
||
| 39 | $this->assertEquals(['page' => 3, 'nbItems' => 5, 'nbPages' => 5], $collection->pagination); |
||
| 40 | $this->assertEquals([3], $collection->getItems()); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testGetPossibleValuesFor() |
||
| 44 | { |
||
| 45 | $arr = [ |
||
| 46 | ['id' => 40, 'name' => 'name 1'], |
||
| 47 | ['id' => 20, 'name' => 'name 2'], |
||
| 48 | ['id' => 35, 'name' => 'name 3'], |
||
| 49 | ]; |
||
| 50 | $collection = new \Suricate\Collection($arr); |
||
| 51 | |||
| 52 | $this->assertEquals([40 => 'name 1', 20 => 'name 2', 35 =>'name 3'], $collection->getPossibleValuesFor('name', 'id')); |
||
| 53 | $this->assertEquals(['name 1', 'name 2', 'name 3'], $collection->getPossibleValuesFor('name')); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testFirst() |
||
| 57 | { |
||
| 58 | $arr = array(4, 5, 6); |
||
| 59 | $collection = new \Suricate\Collection($arr); |
||
| 60 | $this->assertEquals(4, $collection->first()); |
||
| 61 | |||
| 62 | $collection = new \Suricate\Collection([]); |
||
| 63 | $this->assertNull($collection->first()); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function testLast() |
||
| 67 | { |
||
| 68 | $arr = array(1, 2, 3); |
||
| 69 | $collection = new \Suricate\Collection($arr); |
||
| 70 | $this->assertEquals(3, $collection->last()); |
||
| 71 | |||
| 72 | |||
| 73 | $collection = new \Suricate\Collection(); |
||
| 74 | $this->assertEquals(null, $collection->last()); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function testSum() |
||
| 78 | { |
||
| 79 | $arr = array(1, 2, 3); |
||
| 80 | $collection = new \Suricate\Collection($arr); |
||
| 81 | $this->assertEquals(6, $collection->sum()); |
||
| 82 | |||
| 83 | $arr = [ |
||
| 84 | ['id' => 10, 'name' => 'azerty'], |
||
| 85 | ['id' => 2, 'name' => 'qsdfg'], |
||
| 86 | ['id' => 3, 'name' => 'wxcvbn'] |
||
| 87 | ]; |
||
| 88 | $collection = new \Suricate\Collection($arr); |
||
| 89 | $this->assertEquals(15, $collection->sum('id')); |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | public function testHas() |
||
| 94 | { |
||
| 95 | $arr = array('a' => 1, 'b' => 2, 'c' => 3); |
||
| 96 | $collection = new \Suricate\Collection($arr); |
||
| 97 | $this->assertEquals(true, $collection->has('b')); |
||
| 98 | $this->assertEquals(false, $collection->has('d')); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function testKeys() |
||
| 102 | { |
||
| 103 | $arr = array('a' => 1, 'b' => 2, 'c' => 3); |
||
| 104 | $collection = new \Suricate\Collection($arr); |
||
| 105 | $this->assertEquals(['a', 'b', 'c'], $collection->keys()); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function testPrepend() |
||
| 109 | { |
||
| 110 | $arr = [4, 5, 6]; |
||
| 111 | $collection = new \Suricate\Collection($arr); |
||
| 112 | $collection->prepend(99); |
||
| 113 | $this->assertEquals([99, 4, 5, 6], $collection->getItems()); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function testPut() |
||
| 117 | { |
||
| 118 | $arr = array('a' => 1, 'b' => 2, 'c' => 3); |
||
| 119 | $collection = new \Suricate\Collection($arr); |
||
| 120 | $collection->put('z', 99); |
||
| 121 | $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'z' => 99], $collection->getItems()); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function testShift() |
||
| 125 | { |
||
| 126 | $arr = array('a' => 1, 'b' => 2, 'c' => 3); |
||
| 127 | $collection = new \Suricate\Collection($arr); |
||
| 128 | $shifted = $collection->shift(); |
||
| 129 | $this->assertEquals(1, $shifted); |
||
| 130 | $this->assertEquals(['b' => 2, 'c' => 3], $collection->getItems()); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function testPop() |
||
| 134 | { |
||
| 135 | $arr = array('a' => 1, 'b' => 2, 'c' => 3); |
||
| 136 | $collection = new \Suricate\Collection($arr); |
||
| 137 | $popped = $collection->pop(); |
||
| 138 | $this->assertEquals(3, $popped); |
||
| 139 | $this->assertEquals(['a' => 1, 'b' => 2], $collection->getItems()); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function testReverse() |
||
| 143 | { |
||
| 144 | $arr = array('a' => 1, 'b' => 2, 'c' => 3); |
||
| 145 | $collection = new \Suricate\Collection($arr); |
||
| 146 | $reversed = $collection->reverse(); |
||
| 147 | $this->assertEquals(['c' => 3, 'b' => 2, 'a' => 1], $reversed->getItems()); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function testReduce() |
||
| 175 | } |
||
| 176 | |||
| 177 | public function testSlice() |
||
| 178 | { |
||
| 179 | // Basic slice |
||
| 180 | $arr = ['a' => 1, 'b' => 2, 'c' => 3]; |
||
| 181 | $collection = new \Suricate\Collection($arr); |
||
| 182 | $sliced = $collection->slice(1, 2); |
||
| 183 | |||
| 184 | $this->assertEquals(['b' => 2, 'c' => 3], $sliced->getItems()); |
||
| 185 | |||
| 186 | // Slice with numeric keys |
||
| 187 | $arr = [1, 2, 3]; |
||
| 188 | $collection = new \Suricate\Collection($arr); |
||
| 189 | $sliced = $collection->slice(1, 2); |
||
| 190 | |||
| 191 | $this->assertEquals([2, 3], $sliced->getItems()); |
||
| 192 | |||
| 295 |