Completed
Push — develop ( 2e1df2...27dbda )
by Mathieu
03:22
created

CollectionTest::testReduce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
class CollectionTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 testFirst()
33
    {
34
        $arr = array(4, 5, 6);
35
        $collection = new \Suricate\Collection($arr);
36
        $this->assertEquals(4, $collection->first());
37
    }
38
39
    public function testLast()
40
    {
41
        $arr = array(1, 2, 3);
42
        $collection = new \Suricate\Collection($arr);
43
        $this->assertEquals(3, $collection->last());
44
45
46
        $collection = new \Suricate\Collection();
47
        $this->assertEquals(null, $collection->last());
48
    }
49
50
    public function testSum()
51
    {
52
        $arr = array(1, 2, 3);
53
        $collection = new \Suricate\Collection($arr);
54
        $this->assertEquals(6, $collection->sum());
55
    }
56
57
    public function testHas()
58
    {
59
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
60
        $collection = new \Suricate\Collection($arr);
61
        $this->assertEquals(true, $collection->has('b'));
62
        $this->assertEquals(false, $collection->has('d'));
63
    }
64
65
    public function testKeys()
66
    {
67
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
68
        $collection = new \Suricate\Collection($arr);
69
        $this->assertEquals(['a', 'b', 'c'], $collection->keys());
70
    }
71
72
    public function testPrepend()
73
    {
74
        $arr = [4, 5, 6];
75
        $collection = new \Suricate\Collection($arr);
76
        $collection->prepend(99);
77
        $this->assertEquals([99, 4, 5, 6], $collection->getItems());
78
    }
79
    
80
    public function testPut()
81
    {
82
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
83
        $collection = new \Suricate\Collection($arr);
84
        $collection->put('z', 99);
85
        $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'z' => 99], $collection->getItems());
86
    }
87
88
    public function testShift()
89
    {
90
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
91
        $collection = new \Suricate\Collection($arr);
92
        $shifted = $collection->shift();
93
        $this->assertEquals(1, $shifted);
94
        $this->assertEquals(['b' => 2, 'c' => 3], $collection->getItems());
95
    }
96
97
    public function testPop()
98
    {
99
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
100
        $collection = new \Suricate\Collection($arr);
101
        $popped = $collection->pop();
102
        $this->assertEquals(3, $popped);
103
        $this->assertEquals(['a' => 1, 'b' => 2], $collection->getItems());
104
    }
105
106
    public function testReverse()
107
    {
108
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
109
        $collection = new \Suricate\Collection($arr);
110
        $reversed = $collection->reverse();
111
        $this->assertEquals(['c' => 3, 'b' => 2, 'a' => 1], $reversed->getItems());
112
    }
113
114
    public function testReduce()
115
    {
116
        // Basic reduce
117
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
118
        $collection = new \Suricate\Collection($arr);
119
        $callback = function ($carry, $item) {
120
            $carry += $item;
121
122
            return $carry;
123
        };
124
        $reduced = $collection->reduce($callback);
125
    
126
        $this->assertEquals(6, $reduced);
127
128
        // reduce with initial value
129
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
130
        $collection = new \Suricate\Collection($arr);
131
        $callback = function ($carry, $item) {
132
            $carry += $item;
133
134
            return $carry;
135
        };
136
        $reduced = $collection->reduce($callback, 100);
137
    
138
        $this->assertEquals(106, $reduced);
139
    }
140
141
    public function testSlice()
142
    {
143
        // Basic slice
144
        $arr = ['a' => 1, 'b' => 2, 'c' => 3];
145
        $collection = new \Suricate\Collection($arr);
146
        $sliced = $collection->slice(1, 2);
147
    
148
        $this->assertEquals(['b' => 2, 'c' => 3], $sliced->getItems());
149
150
        // Slice with numeric keys
151
        $arr = [1, 2, 3];
152
        $collection = new \Suricate\Collection($arr);
153
        $sliced = $collection->slice(1, 2);
154
    
155
        $this->assertEquals([2, 3], $sliced->getItems());
156
157
        // Slice preserve keys
158
        $arr = [1, 2, 3];
159
        $collection = new \Suricate\Collection($arr);
160
        $sliced = $collection->slice(1, 2, true);
161
    
162
        $this->assertEquals([1 => 2, 2 => 3], $sliced->getItems());
163
    }
164
    
165
    public function testTake()
166
    {
167
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
168
        $collection = new \Suricate\Collection($arr);
169
        $taken = $collection->take(2);
170
        $this->assertEquals(['a' => 1, 'b' => 2], $taken->getItems());
171
    }
172
173
    public function testFilter()
174
    {
175
        $arr = array('a' => 1, 'b' => 2, 'c' => 3);
176
        $collection = new \Suricate\Collection($arr);
177
        $this->assertEquals(
178
            ['b' => 2],
179
            $collection->filter(function ($value) {
180
                return ($value % 2) === 0;
181
            })->getItems()
182
        );
183
    }
184
185
    public function testPush()
186
    {
187
        $arr = array(1, 2, 3);
188
        $collection = new \Suricate\Collection($arr);
189
        $this->assertEquals([1, 2, 3, 4], $collection->push(4)->getItems());
190
    }
191
}
192