Completed
Push — release/v1.0.0 ( 9b8046...eee318 )
by Edward
11:43
created

CollectionTest::testSlice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * File CollectionTest.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\Tests\Collection;
8
9
use Epfremme\Collection\Collection;
10
11
/**
12
 * Class CollectionTest
13
 *
14
 * @package Epfremme\Tests\Collection
15
 */
16
class CollectionTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @var Collection
20
     */
21
    private $collection;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function setup()
27
    {
28
        parent::setup();
29
30
        $this->collection = new Collection([1,2,3]);
31
    }
32
33
    /**
34
     * Test pushing element onto collection
35
     *
36
     * @return void
37
     */
38
    public function testPush()
39
    {
40
        $this->collection->push(4);
41
42
        $this->assertCount(4, $this->collection);
43
        $this->assertTrue($this->collection->contains(4));
44
        $this->assertEquals(4, $this->collection->get(3));
45
    }
46
47
    /**
48
     * Test popping element from collection
49
     *
50
     * @return void
51
     */
52
    public function testPop()
53
    {
54
        $element = $this->collection->pop();
55
56
        $this->assertCount(2, $this->collection);
57
        $this->assertFalse($this->collection->contains(3));
58
        $this->assertEquals(3, $element);
59
    }
60
61
    /**
62
     * Test shifting element from collection
63
     *
64
     * @return void
65
     */
66
    public function testShift()
67
    {
68
        $element = $this->collection->shift();
69
70
        $this->assertCount(2, $this->collection);
71
        $this->assertFalse($this->collection->contains(1));
72
        $this->assertEquals(1, $element);
73
    }
74
75
    /**
76
     * Test un-shifting element onto collection
77
     *
78
     * @return void
79
     */
80
    public function testUnshift()
81
    {
82
        $this->collection->unshift(0);
83
84
        $this->assertCount(4, $this->collection);
85
        $this->assertTrue($this->collection->contains(0));
86
        $this->assertEquals(0, $this->collection->get(0));
87
    }
88
89
    /**
90
     * Test adding element to collection
91
     *
92
     * @return void
93
     */
94
    public function testAdd()
95
    {
96
        $collection = $this->collection->add(7);
97
98
        $this->assertCount(4, $this->collection);
99
        $this->assertEquals(7, $this->collection->get(3));
100
        $this->assertSame($this->collection, $collection);
101
    }
102
103
    /**
104
     * Test removing element from array
105
     *
106
     * @return void
107
     */
108
    public function testRemove()
109
    {
110
        $success = $this->collection->remove(3);
111
112
        $this->assertTrue($success);
113
        $this->assertCount(2, $this->collection);
114
        $this->assertFalse($this->collection->contains(3));
115
    }
116
117
    /**
118
     * Test removing key not in collection
119
     *
120
     * @return void
121
     */
122
    public function testRemoveUnsetElement()
123
    {
124
        $success = $this->collection->remove(7);
125
126
        $this->assertFalse($success);
127
    }
128
129
    /**
130
     * Test setting value at key
131
     *
132
     * @return void
133
     */
134
    public function testSet()
135
    {
136
        $collection = $this->collection->set(1, 7);
137
138
        $this->assertEquals(7, $this->collection->get(1));
139
        $this->assertSame($this->collection, $collection);
140
141
        $this->collection->set(6, 9);
142
        $this->assertEquals(9, $this->collection->get(6));
143
    }
144
145
    /**
146
     * Test creating slice of collection
147
     *
148
     * @return void
149
     */
150
    public function testSlice()
151
    {
152
        $slice = $this->collection->slice(1, 1);
153
154
        $this->assertInstanceOf(Collection::class, $slice);
155
        $this->assertAttributeEquals([1 => 2], 'elements', $slice);
156
        $this->assertAttributeEquals([1, 2, 3], 'elements', $this->collection);
157
    }
158
159
    /**
160
     * Test slice without args returns empty collection
161
     *
162
     * @return void
163
     */
164
    public function testSliceWithoutArgs()
165
    {
166
        $slice = $this->collection->slice();
167
168
        $this->assertInstanceOf(Collection::class, $slice);
169
        $this->assertAttributeEquals([], 'elements', $slice);
170
        $this->assertAttributeEquals([1, 2, 3], 'elements', $this->collection);
171
    }
172
173
    /**
174
     * Test splicing elements from collection
175
     *
176
     * @return void
177
     */
178
    public function testSplice()
179
    {
180
        $slice = $this->collection->splice(1, 1);
181
182
        $this->assertInstanceOf(Collection::class, $slice);
183
        $this->assertAttributeEquals([2], 'elements', $slice);
184
        $this->assertAttributeEquals([1, 3], 'elements', $this->collection);
185
    }
186
187
    /**
188
     * Test splicing elements from collection
189
     *
190
     * @return void
191
     */
192
    public function testSpliceWithoutArgs()
193
    {
194
        $slice = $this->collection->splice();
195
196
        $this->assertInstanceOf(Collection::class, $slice);
197
        $this->assertAttributeEquals([], 'elements', $slice);
198
        $this->assertAttributeEquals([1, 2, 3], 'elements', $this->collection);
199
    }
200
201
    /**
202
     * Test splicing single element into collection
203
     *
204
     * @return void
205
     */
206
    public function testSpliceWithScalarReplacement()
207
    {
208
        $slice = $this->collection->splice(1, 1, 7);
209
210
        $this->assertInstanceOf(Collection::class, $slice);
211
        $this->assertAttributeEquals([2], 'elements', $slice);
212
        $this->assertAttributeEquals([1, 7, 3], 'elements', $this->collection);
213
    }
214
215
    /**
216
     * Test splicing array of elements into collection
217
     *
218
     * @return void
219
     */
220
    public function testSpliceWithArrayOfReplacements()
221
    {
222
        $slice = $this->collection->splice(1, 1, [7,8,9]);
223
224
        $this->assertInstanceOf(Collection::class, $slice);
225
        $this->assertAttributeEquals([2], 'elements', $slice);
226
        $this->assertAttributeEquals([1, 7, 8, 9, 3], 'elements', $this->collection);
227
    }
228
229
    /**
230
     * Test mapping callback to new collection
231
     *
232
     * @return void
233
     */
234
    public function testMap()
235
    {
236
        $collection = $this->collection->map(function($value, $key) {
237
            return 2 * $value;
238
        });
239
240
        $this->assertAttributeEquals([2,4,6], 'elements', $collection);
241
        $this->assertAttributeEquals([1,2,3], 'elements', $this->collection);
242
    }
243
244
    /**
245
     * Test collection reduction method
246
     *
247
     * @return void
248
     */
249
    public function testReduce()
250
    {
251
        $result = $this->collection->reduce(function($carry, $value) {
252
            return $carry + $value;
253
        });
254
255
        $this->assertEquals(6, $result);
256
    }
257
258
    /**
259
     * Test sorting collection by callback
260
     *
261
     * @return void
262
     */
263
    public function testSort()
264
    {
265
        $this->collection->sort(function($a, $b) {
266
            if ($a === $b) {
267
                return 0;
268
            }
269
270
            return $a > $b ? -1 : 1;
271
        });
272
273
        $this->assertAttributeEquals([3,2,1], 'elements', $this->collection);
274
    }
275
276
    /**
277
     * Test sorting collection by callback and retaining element keys/indicies
278
     *
279
     * @return void
280
     */
281
    public function testAsort()
282
    {
283
        $this->collection->asort(function($a, $b) {
284
            if ($a === $b) {
285
                return 0;
286
            }
287
288
            return $a > $b ? -1 : 1;
289
        });
290
291
        $this->assertAttributeEquals([2 => 3, 1 => 2, 0 => 1], 'elements', $this->collection);
292
    }
293
294
    /**
295
     * Test applying a callback to each element of the collection
296
     *
297
     * @reutrn void
298
     */
299
    public function testEach()
300
    {
301
        $success = $this->collection->each(function($key, $value) {
302
            $this->assertTrue($this->collection->keyExists($key));
303
            $this->assertEquals($value, $this->collection->get($key));
304
305
            return $this->collection->set($key, $value * 2);
306
        });
307
308
        $this->assertTrue($success);
309
        $this->assertAttributeEquals([2,4,6], 'elements', $this->collection);
310
    }
311
312
    /**
313
     * Test each method breaks when receiving false from callback
314
     *
315
     * @return void
316
     */
317
    public function testEachWithEarlyReturn()
318
    {
319
        $success = $this->collection->each(function($key, $value) {
320
            return $key === 1;
321
        });
322
323
        $this->assertFalse($success);
324
    }
325
}
326