Passed
Push — develop ( f9f106...a8d80f )
by Mathieu
01:37
created

CollectionTest::testHas()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
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 testGetPossibleValuesFor()
51
    {
52
        $arr = [
53
            ['id' => 40, 'name' => 'name 1'],
54
            ['id' => 20, 'name' => 'name 2'],
55
            ['id' => 35, 'name' => 'name 3'],
56
        ];
57
        $collection = new \Suricate\Collection($arr);
58
59
        $this->assertEquals([40 => 'name 1', 20 => 'name 2', 35 =>'name 3'], $collection->getPossibleValuesFor('name', 'id'));
60
        $this->assertEquals(['name 1', 'name 2', 'name 3'], $collection->getPossibleValuesFor('name'));
61
    }
62
63
    public function testFirst()
64
    {
65
        $arr = array(4, 5, 6);
66
        $collection = new \Suricate\Collection($arr);
67
        $this->assertEquals(4, $collection->first());
68
69
        $collection = new \Suricate\Collection([]);
70
        $this->assertNull($collection->first());
71
    }
72
73
    public function testLast()
74
    {
75
        $arr = array(1, 2, 3);
76
        $collection = new \Suricate\Collection($arr);
77
        $this->assertEquals(3, $collection->last());
78
79
80
        $collection = new \Suricate\Collection();
81
        $this->assertEquals(null, $collection->last());
82
    }
83
84
    public function testSum()
85
    {
86
        $arr = array(1, 2, 3);
87
        $collection = new \Suricate\Collection($arr);
88
        $this->assertEquals(6, $collection->sum());
89
90
        $arr = [
91
            ['id' => 10, 'name' => 'azerty'],
92
            ['id' => 2, 'name' => 'qsdfg'],
93
            ['id' => 3, 'name' => 'wxcvbn']
94
        ];
95
        $collection = new \Suricate\Collection($arr);
96
        $this->assertEquals(15, $collection->sum('id'));
97
98
    }
99
100
    public function testHas()
101
    {
102
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
103
        $collection = new \Suricate\Collection($arr);
104
        $this->assertEquals(true, $collection->has('b'));
105
        $this->assertEquals(false, $collection->has('d'));
106
    }
107
108
    public function testKeys()
109
    {
110
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
111
        $collection = new \Suricate\Collection($arr);
112
        $this->assertEquals(['a', 'b', 'c'], $collection->keys());
113
    }
114
115
    public function testPrepend()
116
    {
117
        $arr = [4, 5, 6];
118
        $collection = new \Suricate\Collection($arr);
119
        $collection->prepend(99);
120
        $this->assertEquals([99, 4, 5, 6], $collection->getItems());
121
    }
122
    
123
    public function testPut()
124
    {
125
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
126
        $collection = new \Suricate\Collection($arr);
127
        $collection->put('z', 99);
128
        $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'z' => 99], $collection->getItems());
129
    }
130
131
    public function testShift()
132
    {
133
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
134
        $collection = new \Suricate\Collection($arr);
135
        $shifted = $collection->shift();
136
        $this->assertEquals(1, $shifted);
137
        $this->assertEquals(['b' => 2, 'c' => 3], $collection->getItems());
138
    }
139
140
    public function testPop()
141
    {
142
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
143
        $collection = new \Suricate\Collection($arr);
144
        $popped = $collection->pop();
145
        $this->assertEquals(3, $popped);
146
        $this->assertEquals(['a' => 1, 'b' => 2], $collection->getItems());
147
    }
148
149
    public function testReverse()
150
    {
151
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
152
        $collection = new \Suricate\Collection($arr);
153
        $reversed = $collection->reverse();
154
        $this->assertEquals(['c' => 3, 'b' => 2, 'a' => 1], $reversed->getItems());
155
    }
156
157
    public function testReduce()
158
    {
159
        // Basic reduce
160
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
161
        $collection = new \Suricate\Collection($arr);
162
        $callback = function ($carry, $item) {
163
            $carry += $item;
164
165
            return $carry;
166
        };
167
        $reduced = $collection->reduce($callback);
168
    
169
        $this->assertEquals(6, $reduced);
170
171
        // reduce with initial value
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, 100);
180
    
181
        $this->assertEquals(106, $reduced);
182
    }
183
184
    public function testSlice()
185
    {
186
        // Basic slice
187
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
188
        $collection = new \Suricate\Collection($arr);
189
        $sliced = $collection->slice(1, 2);
190
    
191
        $this->assertEquals(['b' => 2, 'c' => 3], $sliced->getItems());
192
193
        // Slice with numeric keys
194
        $arr = [1, 2, 3];
195
        $collection = new \Suricate\Collection($arr);
196
        $sliced = $collection->slice(1, 2);
197
    
198
        $this->assertEquals([2, 3], $sliced->getItems());
199
200
        // Slice preserve keys
201
        $arr = [1, 2, 3];
202
        $collection = new \Suricate\Collection($arr);
203
        $sliced = $collection->slice(1, 2, true);
204
    
205
        $this->assertEquals([1 => 2, 2 => 3], $sliced->getItems());
206
    }
207
    
208
    public function testTake()
209
    {
210
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
211
        $collection = new \Suricate\Collection($arr);
212
        $taken = $collection->take(2);
213
        $this->assertEquals(['a' => 1, 'b' => 2], $taken->getItems());
214
    }
215
216
    public function testFilter()
217
    {
218
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
219
        $collection = new \Suricate\Collection($arr);
220
        $this->assertEquals(
221
            ['b' => 2],
222
            $collection->filter(function ($value) {
223
                return ($value % 2) === 0;
224
            })->getItems()
225
        );
226
    }
227
228
    public function testPush()
229
    {
230
        $arr = array(1, 2, 3);
231
        $collection = new \Suricate\Collection($arr);
232
        $this->assertEquals([1, 2, 3, 4], $collection->push(4)->getItems());
233
    }
234
235
    public function testGetValuesFor()
236
    {
237
        $arr = [
238
            ['id' => 1, 'name' => 'azerty'],
239
            ['id' => 2, 'name' => 'qsdfg'],
240
            ['id' => 3, 'name' => 'wxcvbn']
241
        ];
242
        $collection = new \Suricate\Collection($arr);
243
        $this->assertEquals([
244
            'azerty',
245
            'qsdfg',
246
            'wxcvbn'],
247
            $collection->getValuesFor('name')
248
        );
249
    }
250
251
    public function testSearch()
252
    {
253
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
254
        $collection = new \Suricate\Collection($arr);
255
        $this->assertEquals('c', $collection->search('3'));
256
        $this->assertEquals('c', $collection->search(3, true));
257
        $this->assertFalse($collection->search(22));
258
        $this->assertFalse($collection->search('3', true));
259
    }
260
261
    public function testOffsetExists()
262
    {
263
        $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => null, 'z' => 99];
264
265
        $collection = new \Suricate\Collection($arr);
266
        $this->assertTrue($collection->offsetExists('a'));
267
        $this->assertTrue($collection->offsetExists('d'));
268
        $this->assertFalse($collection->offsetExists('e'));
269
    }
270
271
    public function testOffsetGet()
272
    {
273
        $arr = [1, 2, 3];
274
275
        $collection = new \Suricate\Collection($arr);
276
277
        $this->assertNull($collection->offsetGet(4));
278
        $this->assertSame(2, $collection->offsetGet(1));
279
    }
280
281
    public function testOffsetSet()
282
    {
283
        $arr = [1, 2, 3];
284
285
        $collection = new \Suricate\Collection($arr);
286
        $collection->offsetSet(null, 4);
287
        $this->assertSame([1, 2, 3, 4], $collection->getItems());
288
289
        $collection->offsetSet('a', 5);
290
        $this->assertSame([1, 2, 3, 4, 'a' => 5], $collection->getItems());
291
    }
292
293
    public function testOffsetUnset()
294
    {
295
        $arr = [1, 2, 3];
296
297
        $collection = new \Suricate\Collection($arr);
298
        $collection->offsetUnset(1);
299
        $this->assertSame([0 => 1, 2 => 3], $collection->getItems());
300
    }
301
}
302