|
6 | 6 |
|
7 | 7 | class CollectionTest extends \PHPUnit_Framework_TestCase
|
8 | 8 | {
|
9 |
| - public function test_each() |
| 9 | + public function test_array_json_props() |
10 | 10 | {
|
11 |
| - _::_([1, 2, 3])->each(function ($num, $i) { |
12 |
| - $this->assertSame($num, $i + 1, 'each iterators provide value and iteration count'); |
13 |
| - }); |
| 11 | + $_ = _::_([9, 'a' => 'Apple', 5, 8, 'c' => 'Cat']); |
| 12 | + |
| 13 | + $this->assertSame('Apple', $_['a']); |
| 14 | + $this->assertSame(8, $_[2]); |
| 15 | + $this->assertTrue(isset($_['c'])); |
| 16 | + $this->assertFalse(isset($_['D'])); |
| 17 | + $this->assertCount(5, $_); |
| 18 | + |
| 19 | + unset($_['c']); |
| 20 | + |
| 21 | + $this->assertSame(4, count($_)); |
| 22 | + |
| 23 | + $_['d'] = 'Dog'; // Set new |
| 24 | + $_[0] = 8; // Override |
| 25 | + |
| 26 | + $this->assertCount(5, $_); |
| 27 | + |
| 28 | + $json = json_encode($data = [8, 'a' => 'Apple', 5, 8, 'd' => 'Dog']); |
| 29 | + $this->assertSame($json, json_encode($_)); |
| 30 | + $this->assertSame($json, (string) $_); |
| 31 | + |
| 32 | + foreach ($_ as $key => $value) { |
| 33 | + $this->assertSame($data[$key], $value); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @expectedException \PHPUnit_Framework_Error_Notice |
| 39 | + * @expectedExceptionMessage Undefined offset: 5 |
| 40 | + */ |
| 41 | + public function test_get() |
| 42 | + { |
| 43 | + $_ = _::_([1, 5, 9]); |
| 44 | + |
| 45 | + $this->assertSame([1, 5, 9], $_->get(), 'get all'); |
| 46 | + $this->assertSame(5, $_->get(1), 'get by key'); |
14 | 47 |
|
| 48 | + $this->assertSame(null, $_->get(5), 'get non existing key'); |
| 49 | + } |
| 50 | + |
| 51 | + public function test_each() |
| 52 | + { |
15 | 53 | $answers = [];
|
16 |
| - $count = 0; |
17 |
| - _::_([1, 2, 3])->each(function ($num) use (&$answers, &$count) { |
| 54 | + _::_([1, 2, 3])->each(function ($num) use (&$answers) { |
18 | 55 | $answers[] = $num * 5;
|
19 |
| - $count++; |
20 | 56 | });
|
21 | 57 |
|
22 |
| - $this->assertSame($answers, [5, 10, 15], 'callback applied on each member'); |
23 |
| - $this->assertSame($count, 3, 'callback applied exactly 3 times'); |
| 58 | + $this->assertSame([5, 10, 15], $answers, 'callback applied on each member'); |
| 59 | + $this->assertCount(3, $answers, 'callback applied exactly 3 times'); |
24 | 60 |
|
25 | 61 | $answers = [];
|
26 | 62 | _::_(['one' => 1, 'two' => 2, 'three' => 3])->each(function ($num, $index) use (&$answers) {
|
27 | 63 | $answers[] = $index;
|
28 | 64 | });
|
29 | 65 |
|
30 |
| - $this->assertSame($answers, ['one', 'two', 'three'], 'callback applied on each member of assoc array'); |
| 66 | + $this->assertSame(['one', 'two', 'three'], $answers, 'callback applied on each member of assoc array'); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_map_collect() |
| 70 | + { |
| 71 | + $mapped = _::_([1, 2, 3])->map(function ($num) { |
| 72 | + return $num * 2; |
| 73 | + }); |
| 74 | + |
| 75 | + $this->assertSame([2, 4, 6], $mapped->get(), 'callback applied on each member'); |
| 76 | + |
| 77 | + $mapped = _::_([['a' => 1], ['a' => 2]])->collect(function ($row) { |
| 78 | + return $row['a']; |
| 79 | + }); |
| 80 | + |
| 81 | + $this->assertSame([1, 2], $mapped->get(), 'map prop'); |
| 82 | + } |
| 83 | + |
| 84 | + public function test_reduce_foldl_inject() |
| 85 | + { |
| 86 | + $sum = _::_([1, 2, 3])->reduce(function ($num, $sum) { |
| 87 | + return $num + $sum; |
| 88 | + }, 0); |
| 89 | + |
| 90 | + $this->assertSame(6, $sum, 'sum by reduce'); |
| 91 | + |
| 92 | + $sum = _::_([1, 2, 3])->foldl(function ($num, $sum) { |
| 93 | + return $num + $sum; |
| 94 | + }, 10); |
| 95 | + |
| 96 | + $this->assertSame(10 + 6, $sum, 'sum by reduce with initial 10'); |
| 97 | + |
| 98 | + $prod = _::_([1, 2, 3, 4])->inject(function ($num, $sum) { |
| 99 | + return $num * $sum; |
| 100 | + }, 1); |
| 101 | + |
| 102 | + $this->assertSame(24, $prod, 'prod by reduce with initial 1'); |
| 103 | + |
| 104 | + $concat = _::_([1, 2, 3, 4])->inject(function ($num, $concat) { |
| 105 | + return $num . $concat; |
| 106 | + }, ''); |
| 107 | + |
| 108 | + $this->assertSame('1234', $concat, 'concat by reduce'); |
| 109 | + } |
| 110 | + |
| 111 | + public function test_reduceRight_foldr() |
| 112 | + { |
| 113 | + $sum = _::_([1, 2, 3])->reduce(function ($num, $sum) { |
| 114 | + return $num + $sum; |
| 115 | + }, 0); |
| 116 | + |
| 117 | + $this->assertSame(6, $sum, 'sum by reduceRight'); |
| 118 | + |
| 119 | + $concat = _::_([1, 2, 3, 4])->foldr(function ($num, $concat) { |
| 120 | + return $num . $concat; |
| 121 | + }, ''); |
| 122 | + |
| 123 | + $this->assertSame('4321', $concat, 'concat by reduceRight'); |
| 124 | + } |
| 125 | + |
| 126 | + public function test_find_detect() |
| 127 | + { |
| 128 | + $num = _::_([1, 2, 4, 3])->find(function ($num) { |
| 129 | + return $num > 2; |
| 130 | + }); |
| 131 | + |
| 132 | + $this->assertSame(4, $num, 'first num gt 2'); |
| 133 | + |
| 134 | + $num = _::_([1, 2, 3])->detect(function ($num) { |
| 135 | + return $num > 4; |
| 136 | + }); |
| 137 | + |
| 138 | + $this->assertNull($num, 'first num gt 5 doesnt exist'); |
| 139 | + } |
| 140 | + |
| 141 | + public function test_filter_select() |
| 142 | + { |
| 143 | + $gt2 = _::_([1, 2, 4, 0, 3])->filter(function ($num) { |
| 144 | + return $num > 2; |
| 145 | + }); |
| 146 | + |
| 147 | + $this->assertSame([4, 3], array_values($gt2->get()), 'nums gt 2'); |
| 148 | + |
| 149 | + $odds = _::_([1, 2, 3, 4, 5, 7, 6])->select(function ($num) { |
| 150 | + return $num % 2 === 1; |
| 151 | + }); |
| 152 | + |
| 153 | + $this->assertSame([1, 3, 5, 7], array_values($odds->get()), 'odd nums'); |
| 154 | + } |
| 155 | + |
| 156 | + public function test_reject() |
| 157 | + { |
| 158 | + $evens = _::_([1, 2, 3, 4, 5, 7, 6])->reject(function ($num) { |
| 159 | + return $num % 2 !== 0; |
| 160 | + }); |
| 161 | + |
| 162 | + $this->assertSame([2, 4, 6], array_values($evens->get()), 'even nums'); |
| 163 | + } |
| 164 | + |
| 165 | + public function test_every_all() |
| 166 | + { |
| 167 | + $gt0 = _::_([1, 2, 3, 4])->every(function ($num) { |
| 168 | + return $num > 0; |
| 169 | + }); |
| 170 | + |
| 171 | + $this->assertTrue($gt0, 'every nums gt 0'); |
| 172 | + |
| 173 | + $lt0 = _::_([1, 2, 3, 4])->all(function ($num) { |
| 174 | + return $num < 0; |
| 175 | + }); |
| 176 | + |
| 177 | + $this->assertFalse($lt0, 'every nums lt 0'); |
| 178 | + } |
| 179 | + |
| 180 | + public function test_some_any() |
| 181 | + { |
| 182 | + $pos = _::_([1, 2, 0, 4, -1])->some(function ($num) { |
| 183 | + return $num > 0; |
| 184 | + }); |
| 185 | + |
| 186 | + $this->assertTrue($pos, 'some positive numbers'); |
| 187 | + |
| 188 | + $neg = _::_([1, 2, 4])->any(function ($num) { |
| 189 | + return $num < 0; |
| 190 | + }); |
| 191 | + |
| 192 | + $this->assertFalse($neg, 'no any neg num'); |
| 193 | + } |
| 194 | + |
| 195 | + public function test_contains_includes() |
| 196 | + { |
| 197 | + $contains = _::_([1, 2, 4])->contains(2); |
| 198 | + |
| 199 | + $this->assertTrue($contains, 'contains 2'); |
| 200 | + |
| 201 | + $includes = _::_([1, 2, 4])->includes(-3); |
| 202 | + |
| 203 | + $this->assertFalse($includes, 'doesnt include -3'); |
| 204 | + } |
| 205 | + |
| 206 | + public function test_invoke() |
| 207 | + { |
| 208 | + $sum = _::_([1, 2, 4])->invoke(function () { |
| 209 | + return array_sum(func_get_args()); |
| 210 | + }); |
| 211 | + |
| 212 | + $this->assertSame(7, $sum, 'sum items by invoke fn'); |
| 213 | + } |
| 214 | + |
| 215 | + public function test_pluck() |
| 216 | + { |
| 217 | + $people = _::_([['name' => 'moe', 'age' => 30], ['name' => 'curly']]); |
| 218 | + $names = $people->pluck('name')->get(); |
| 219 | + $ages = $people->pluck('age')->get(); |
| 220 | + |
| 221 | + $this->assertSame(['moe', 'curly'], $names, 'pluck names'); |
| 222 | + $this->assertSame([30], $ages, 'pluck ages'); |
| 223 | + } |
| 224 | + |
| 225 | + public function test_where() |
| 226 | + { |
| 227 | + $list = _::_([['a' => 1, 'b' => 2], ['a' => 2, 'b' => 2], ['a' => 1, 'b' => 3]]); |
| 228 | + $a1 = $list->where(['a' => 1])->get(); |
| 229 | + $a1b2 = $list->where(['a' => 1, 'b' => 2])->get(); |
| 230 | + $c3 = $list->where(['c' => 3])->get(); |
| 231 | + |
| 232 | + $this->assertSame([['a' => 1, 'b' => 2], 2 => ['a' => 1, 'b' => 3]], $a1, 'where a = 1'); |
| 233 | + $this->assertSame([['a' => 1, 'b' => 2]], $a1b2, 'where a = 1 and b = 2'); |
| 234 | + $this->assertSame([], $c3, 'where c = 3'); |
| 235 | + } |
| 236 | + |
| 237 | + public function test_findWhere() |
| 238 | + { |
| 239 | + $list = _::_([['a' => 1, 'b' => 2], ['a' => 2, 'b' => 2], ['a' => 1, 'b' => 3]]); |
| 240 | + $b3 = $list->findWhere(['b' => 3]); |
| 241 | + $a2b1 = $list->findWhere(['a' => 2, 'b' => 1]); |
| 242 | + |
| 243 | + $this->assertSame(['a' => 1, 'b' => 3], $b3, 'findwhere b = 3'); |
| 244 | + $this->assertNull($a2b1, 'where a = 2 and b = 1'); |
31 | 245 | }
|
32 | 246 | }
|
0 commit comments