CollectionTest   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 306
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 2
Metric Value
eloc 159
c 9
b 0
f 2
dl 0
loc 306
rs 10
wmc 27

27 Methods

Rating   Name   Duplication   Size   Complexity  
A testReduce() 0 25 1
A testOffsetSet() 0 10 1
A testOffsetGet() 0 8 1
A testPop() 0 7 1
A testSum() 0 13 1
A testLast() 0 8 1
A testOffsetExists() 0 8 1
A testReverse() 0 8 1
A testPut() 0 8 1
A testHas() 0 6 1
A testFirst() 0 8 1
A testTake() 0 6 1
A testCount() 0 5 1
A testSearch() 0 8 1
A testKeys() 0 5 1
A testGetPossibleValuesFor() 0 16 1
A testFilter() 0 11 1
A testPrepend() 0 6 1
A testOffsetUnset() 0 7 1
A testShift() 0 7 1
A testPush() 0 5 1
A testGetValuesFor() 0 11 1
A testPaginate() 0 11 1
A testSlice() 0 22 1
A testUnique() 0 7 1
A testIsEmpty() 0 5 1
A testConstruct() 0 5 1
1
<?php
2
3
/**
4
 * @SuppressWarnings("TooManyPublicMethods")
5
 **/
6
class CollectionTest extends \PHPUnit\Framework\TestCase
7
{
8
    public function testConstruct()
9
    {
10
        $arr = [1, 2, 3];
11
        $collection = new \Suricate\Collection($arr);
12
        $this->assertEquals($arr, $collection->getItems());
13
    }
14
15
    public function testIsEmpty()
16
    {
17
        $arr = array();
18
        $collection = new \Suricate\Collection($arr);
19
        $this->assertEquals(true, $collection->isEmpty());
20
    }
21
22
    public function testUnique()
23
    {
24
        $arr = [1, 2, 1, 3];
25
        $collection = new \Suricate\Collection($arr);
26
        $this->assertEquals(
27
            [0 => 1, 1 => 2, 3 => 3],
28
            $collection->unique()->getItems()
29
        );
30
    }
31
32
    public function testCount()
33
    {
34
        $arr = [1, 2, 3];
35
        $collection = new \Suricate\Collection($arr);
36
        $this->assertEquals(3, $collection->count());
37
    }
38
39
    public function testPaginate()
40
    {
41
        $arr = [1, 2, 3, 4, 5];
42
        $collection = new \Suricate\Collection($arr);
43
44
        $collection->paginate(1, 3);
45
        $this->assertEquals(
46
            ['page' => 3, 'nbItems' => 5, 'nbPages' => 5],
47
            $collection->pagination
48
        );
49
        $this->assertEquals([3], $collection->getItems());
50
    }
51
52
    public function testGetPossibleValuesFor()
53
    {
54
        $arr = [
55
            ['id' => 40, 'name' => 'name 1'],
56
            ['id' => 20, 'name' => 'name 2'],
57
            ['id' => 35, 'name' => 'name 3']
58
        ];
59
        $collection = new \Suricate\Collection($arr);
60
61
        $this->assertEquals(
62
            [40 => 'name 1', 20 => 'name 2', 35 => 'name 3'],
63
            $collection->getPossibleValuesFor('name', 'id')
64
        );
65
        $this->assertEquals(
66
            ['name 1', 'name 2', 'name 3'],
67
            $collection->getPossibleValuesFor('name')
68
        );
69
    }
70
71
    public function testFirst()
72
    {
73
        $arr = array(4, 5, 6);
74
        $collection = new \Suricate\Collection($arr);
75
        $this->assertEquals(4, $collection->first());
76
77
        $collection = new \Suricate\Collection([]);
78
        $this->assertNull($collection->first());
79
    }
80
81
    public function testLast()
82
    {
83
        $arr = array(1, 2, 3);
84
        $collection = new \Suricate\Collection($arr);
85
        $this->assertEquals(3, $collection->last());
86
87
        $collection = new \Suricate\Collection();
88
        $this->assertEquals(null, $collection->last());
89
    }
90
91
    public function testSum()
92
    {
93
        $arr = array(1, 2, 3);
94
        $collection = new \Suricate\Collection($arr);
95
        $this->assertEquals(6, $collection->sum());
96
97
        $arr = [
98
            ['id' => 10, 'name' => 'azerty'],
99
            ['id' => 2, 'name' => 'qsdfg'],
100
            ['id' => 3, 'name' => 'wxcvbn']
101
        ];
102
        $collection = new \Suricate\Collection($arr);
103
        $this->assertEquals(15, $collection->sum('id'));
104
    }
105
106
    public function testHas()
107
    {
108
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
109
        $collection = new \Suricate\Collection($arr);
110
        $this->assertEquals(true, $collection->has('b'));
111
        $this->assertEquals(false, $collection->has('d'));
112
    }
113
114
    public function testKeys()
115
    {
116
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
117
        $collection = new \Suricate\Collection($arr);
118
        $this->assertEquals(['a', 'b', 'c'], $collection->keys());
119
    }
120
121
    public function testPrepend()
122
    {
123
        $arr = [4, 5, 6];
124
        $collection = new \Suricate\Collection($arr);
125
        $collection->prepend(99);
126
        $this->assertEquals([99, 4, 5, 6], $collection->getItems());
127
    }
128
129
    public function testPut()
130
    {
131
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
132
        $collection = new \Suricate\Collection($arr);
133
        $collection->put('z', 99);
134
        $this->assertEquals(
135
            ['a' => 1, 'b' => 2, 'c' => 3, 'z' => 99],
136
            $collection->getItems()
137
        );
138
    }
139
140
    public function testShift()
141
    {
142
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
143
        $collection = new \Suricate\Collection($arr);
144
        $shifted = $collection->shift();
145
        $this->assertEquals(1, $shifted);
146
        $this->assertEquals(['b' => 2, 'c' => 3], $collection->getItems());
147
    }
148
149
    public function testPop()
150
    {
151
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
152
        $collection = new \Suricate\Collection($arr);
153
        $popped = $collection->pop();
154
        $this->assertEquals(3, $popped);
155
        $this->assertEquals(['a' => 1, 'b' => 2], $collection->getItems());
156
    }
157
158
    public function testReverse()
159
    {
160
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
161
        $collection = new \Suricate\Collection($arr);
162
        $reversed = $collection->reverse();
163
        $this->assertEquals(
164
            ['c' => 3, 'b' => 2, 'a' => 1],
165
            $reversed->getItems()
166
        );
167
    }
168
169
    public function testReduce()
170
    {
171
        // Basic reduce
172
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
173
        $collection = new \Suricate\Collection($arr);
174
        $callback = function ($carry, $item) {
175
            $carry += $item;
176
177
            return $carry;
178
        };
179
        $reduced = $collection->reduce($callback);
180
181
        $this->assertEquals(6, $reduced);
182
183
        // reduce with initial value
184
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
185
        $collection = new \Suricate\Collection($arr);
186
        $callback = function ($carry, $item) {
187
            $carry += $item;
188
189
            return $carry;
190
        };
191
        $reduced = $collection->reduce($callback, 100);
192
193
        $this->assertEquals(106, $reduced);
194
    }
195
196
    public function testSlice()
197
    {
198
        // Basic slice
199
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
200
        $collection = new \Suricate\Collection($arr);
201
        $sliced = $collection->slice(1, 2);
202
203
        $this->assertEquals(['b' => 2, 'c' => 3], $sliced->getItems());
204
205
        // Slice with numeric keys
206
        $arr = [1, 2, 3];
207
        $collection = new \Suricate\Collection($arr);
208
        $sliced = $collection->slice(1, 2);
209
210
        $this->assertEquals([2, 3], $sliced->getItems());
211
212
        // Slice preserve keys
213
        $arr = [1, 2, 3];
214
        $collection = new \Suricate\Collection($arr);
215
        $sliced = $collection->slice(1, 2, true);
216
217
        $this->assertEquals([1 => 2, 2 => 3], $sliced->getItems());
218
    }
219
220
    public function testTake()
221
    {
222
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
223
        $collection = new \Suricate\Collection($arr);
224
        $taken = $collection->take(2);
225
        $this->assertEquals(['a' => 1, 'b' => 2], $taken->getItems());
226
    }
227
228
    public function testFilter()
229
    {
230
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
231
        $collection = new \Suricate\Collection($arr);
232
        $this->assertEquals(
233
            ['b' => 2],
234
            $collection
235
                ->filter(function ($value) {
236
                    return $value % 2 === 0;
237
                })
238
                ->getItems()
239
        );
240
    }
241
242
    public function testPush()
243
    {
244
        $arr = array(1, 2, 3);
245
        $collection = new \Suricate\Collection($arr);
246
        $this->assertEquals([1, 2, 3, 4], $collection->push(4)->getItems());
247
    }
248
249
    public function testGetValuesFor()
250
    {
251
        $arr = [
252
            ['id' => 1, 'name' => 'azerty'],
253
            ['id' => 2, 'name' => 'qsdfg'],
254
            ['id' => 3, 'name' => 'wxcvbn']
255
        ];
256
        $collection = new \Suricate\Collection($arr);
257
        $this->assertEquals(
258
            ['azerty', 'qsdfg', 'wxcvbn'],
259
            $collection->getValuesFor('name')
260
        );
261
    }
262
263
    public function testSearch()
264
    {
265
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
266
        $collection = new \Suricate\Collection($arr);
267
        $this->assertEquals('c', $collection->search('3'));
268
        $this->assertEquals('c', $collection->search(3, true));
269
        $this->assertFalse($collection->search(22));
270
        $this->assertFalse($collection->search('3', true));
271
    }
272
273
    public function testOffsetExists()
274
    {
275
        $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => null, 'z' => 99];
276
277
        $collection = new \Suricate\Collection($arr);
278
        $this->assertTrue($collection->offsetExists('a'));
279
        $this->assertTrue($collection->offsetExists('d'));
280
        $this->assertFalse($collection->offsetExists('e'));
281
    }
282
283
    public function testOffsetGet()
284
    {
285
        $arr = [1, 2, 3];
286
287
        $collection = new \Suricate\Collection($arr);
288
289
        $this->assertNull($collection->offsetGet(4));
290
        $this->assertSame(2, $collection->offsetGet(1));
291
    }
292
293
    public function testOffsetSet()
294
    {
295
        $arr = [1, 2, 3];
296
297
        $collection = new \Suricate\Collection($arr);
298
        $collection->offsetSet(null, 4);
299
        $this->assertSame([1, 2, 3, 4], $collection->getItems());
300
301
        $collection->offsetSet('a', 5);
302
        $this->assertSame([1, 2, 3, 4, 'a' => 5], $collection->getItems());
303
    }
304
305
    public function testOffsetUnset()
306
    {
307
        $arr = [1, 2, 3];
308
309
        $collection = new \Suricate\Collection($arr);
310
        $collection->offsetUnset(1);
311
        $this->assertSame([0 => 1, 2 => 3], $collection->getItems());
312
    }
313
}
314