Passed
Push — develop ( ae181d...dce625 )
by Mathieu
01:47
created

CollectionTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
eloc 123
dl 0
loc 241
rs 10
c 6
b 0
f 1
wmc 23

23 Methods

Rating   Name   Duplication   Size   Complexity  
A testCount() 0 5 1
A testUnique() 0 5 1
A testIsEmpty() 0 5 1
A testConstruct() 0 5 1
A testReduce() 0 25 1
A testPop() 0 7 1
A testSum() 0 13 1
A testLast() 0 9 1
A testPut() 0 6 1
A testReverse() 0 6 1
A testHas() 0 6 1
A testFirst() 0 5 1
A testKeys() 0 5 1
A testTake() 0 6 1
A testSearch() 0 8 1
A testPrepend() 0 6 1
A testFilter() 0 9 1
A testShift() 0 7 1
A testGetValuesFor() 0 13 1
A testPush() 0 5 1
A testPaginate() 0 9 1
A testSlice() 0 22 1
A testGetIterator() 0 5 1
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()
26
    {
27
        $arr = array(1, 2, 3);
28
        $collection = new \Suricate\Collection($arr);
29
        $this->assertEquals(3, $collection->count());
30
    }
31
32
    public function testGetIterator()
33
    {
34
        $arr = array(1, 2, 3);
35
        $collection = new \Suricate\Collection($arr);
36
        $this->assertEquals(new \ArrayIterator($arr), $collection->getIterator());
37
    }
38
39
    public function testPaginate()
40
    {
41
        $arr = array(1, 2, 3, 4, 5);
42
        $collection = new \Suricate\Collection($arr);
43
        
44
45
        $collection->paginate(1, 3);
46
        $this->assertEquals(['page' => 3, 'nbItems' => 5, 'nbPages' => 5], $collection->pagination);
47
        $this->assertEquals([3], $collection->getItems());
48
    }
49
50
    public function testFirst()
51
    {
52
        $arr = array(4, 5, 6);
53
        $collection = new \Suricate\Collection($arr);
54
        $this->assertEquals(4, $collection->first());
55
    }
56
57
    public function testLast()
58
    {
59
        $arr = array(1, 2, 3);
60
        $collection = new \Suricate\Collection($arr);
61
        $this->assertEquals(3, $collection->last());
62
63
64
        $collection = new \Suricate\Collection();
65
        $this->assertEquals(null, $collection->last());
66
    }
67
68
    public function testSum()
69
    {
70
        $arr = array(1, 2, 3);
71
        $collection = new \Suricate\Collection($arr);
72
        $this->assertEquals(6, $collection->sum());
73
74
        $arr = [
75
            ['id' => 10, 'name' => 'azerty'],
76
            ['id' => 2, 'name' => 'qsdfg'],
77
            ['id' => 3, 'name' => 'wxcvbn']
78
        ];
79
        $collection = new \Suricate\Collection($arr);
80
        $this->assertEquals(15, $collection->sum('id'));
81
82
    }
83
84
    public function testHas()
85
    {
86
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
87
        $collection = new \Suricate\Collection($arr);
88
        $this->assertEquals(true, $collection->has('b'));
89
        $this->assertEquals(false, $collection->has('d'));
90
    }
91
92
    public function testKeys()
93
    {
94
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
95
        $collection = new \Suricate\Collection($arr);
96
        $this->assertEquals(['a', 'b', 'c'], $collection->keys());
97
    }
98
99
    public function testPrepend()
100
    {
101
        $arr = [4, 5, 6];
102
        $collection = new \Suricate\Collection($arr);
103
        $collection->prepend(99);
104
        $this->assertEquals([99, 4, 5, 6], $collection->getItems());
105
    }
106
    
107
    public function testPut()
108
    {
109
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
110
        $collection = new \Suricate\Collection($arr);
111
        $collection->put('z', 99);
112
        $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'z' => 99], $collection->getItems());
113
    }
114
115
    public function testShift()
116
    {
117
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
118
        $collection = new \Suricate\Collection($arr);
119
        $shifted = $collection->shift();
120
        $this->assertEquals(1, $shifted);
121
        $this->assertEquals(['b' => 2, 'c' => 3], $collection->getItems());
122
    }
123
124
    public function testPop()
125
    {
126
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
127
        $collection = new \Suricate\Collection($arr);
128
        $popped = $collection->pop();
129
        $this->assertEquals(3, $popped);
130
        $this->assertEquals(['a' => 1, 'b' => 2], $collection->getItems());
131
    }
132
133
    public function testReverse()
134
    {
135
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
136
        $collection = new \Suricate\Collection($arr);
137
        $reversed = $collection->reverse();
138
        $this->assertEquals(['c' => 3, 'b' => 2, 'a' => 1], $reversed->getItems());
139
    }
140
141
    public function testReduce()
142
    {
143
        // Basic reduce
144
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
145
        $collection = new \Suricate\Collection($arr);
146
        $callback = function ($carry, $item) {
147
            $carry += $item;
148
149
            return $carry;
150
        };
151
        $reduced = $collection->reduce($callback);
152
    
153
        $this->assertEquals(6, $reduced);
154
155
        // reduce with initial value
156
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
157
        $collection = new \Suricate\Collection($arr);
158
        $callback = function ($carry, $item) {
159
            $carry += $item;
160
161
            return $carry;
162
        };
163
        $reduced = $collection->reduce($callback, 100);
164
    
165
        $this->assertEquals(106, $reduced);
166
    }
167
168
    public function testSlice()
169
    {
170
        // Basic slice
171
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
172
        $collection = new \Suricate\Collection($arr);
173
        $sliced = $collection->slice(1, 2);
174
    
175
        $this->assertEquals(['b' => 2, 'c' => 3], $sliced->getItems());
176
177
        // Slice with numeric keys
178
        $arr = [1, 2, 3];
179
        $collection = new \Suricate\Collection($arr);
180
        $sliced = $collection->slice(1, 2);
181
    
182
        $this->assertEquals([2, 3], $sliced->getItems());
183
184
        // Slice preserve keys
185
        $arr = [1, 2, 3];
186
        $collection = new \Suricate\Collection($arr);
187
        $sliced = $collection->slice(1, 2, true);
188
    
189
        $this->assertEquals([1 => 2, 2 => 3], $sliced->getItems());
190
    }
191
    
192
    public function testTake()
193
    {
194
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
195
        $collection = new \Suricate\Collection($arr);
196
        $taken = $collection->take(2);
197
        $this->assertEquals(['a' => 1, 'b' => 2], $taken->getItems());
198
    }
199
200
    public function testFilter()
201
    {
202
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
203
        $collection = new \Suricate\Collection($arr);
204
        $this->assertEquals(
205
            ['b' => 2],
206
            $collection->filter(function ($value) {
207
                return ($value % 2) === 0;
208
            })->getItems()
209
        );
210
    }
211
212
    public function testPush()
213
    {
214
        $arr = array(1, 2, 3);
215
        $collection = new \Suricate\Collection($arr);
216
        $this->assertEquals([1, 2, 3, 4], $collection->push(4)->getItems());
217
    }
218
219
    public function testGetValuesFor()
220
    {
221
        $arr = [
222
            ['id' => 1, 'name' => 'azerty'],
223
            ['id' => 2, 'name' => 'qsdfg'],
224
            ['id' => 3, 'name' => 'wxcvbn']
225
        ];
226
        $collection = new \Suricate\Collection($arr);
227
        $this->assertEquals([
228
            'azerty',
229
            'qsdfg',
230
            'wxcvbn'],
231
            $collection->getValuesFor('name')
232
        );
233
    }
234
235
    public function testSearch()
236
    {
237
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
238
        $collection = new \Suricate\Collection($arr);
239
        $this->assertEquals('c', $collection->search('3'));
240
        $this->assertEquals('c', $collection->search(3, true));
241
        $this->assertFalse($collection->search(22));
242
        $this->assertFalse($collection->search('3', true));
243
    }
244
}
245