Completed
Push — develop ( ca634b...64da83 )
by Mathieu
02:06
created

CollectionTest::testOffsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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