|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ahc\Underscore\Tests; |
| 4 | + |
| 5 | +use Ahc\Underscore\UnderscoreCollection as _; |
| 6 | + |
| 7 | +class UnderscoreCollectionTest extends \PHPUnit_Framework_TestCase |
| 8 | +{ |
| 9 | + public function test_array_json_props() |
| 10 | + { |
| 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->assertSame(5, $_->size()); |
| 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'); |
| 47 | + |
| 48 | + $this->assertSame(null, $_->get(5), 'get non existing key'); |
| 49 | + } |
| 50 | + |
| 51 | + public function test_each() |
| 52 | + { |
| 53 | + $answers = []; |
| 54 | + _::_([1, 2, 3])->each(function ($num) use (&$answers) { |
| 55 | + $answers[] = $num * 5; |
| 56 | + }); |
| 57 | + |
| 58 | + $this->assertSame([5, 10, 15], $answers, 'callback applied on each member'); |
| 59 | + $this->assertCount(3, $answers, 'callback applied exactly 3 times'); |
| 60 | + |
| 61 | + $answers = []; |
| 62 | + _::_(['one' => 1, 'two' => 2, 'three' => 3])->each(function ($num, $index) use (&$answers) { |
| 63 | + $answers[] = $index; |
| 64 | + }); |
| 65 | + |
| 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 ($sum, $num) { |
| 87 | + return $num + $sum; |
| 88 | + }, 0); |
| 89 | + |
| 90 | + $this->assertSame(6, $sum, 'sum by reduce'); |
| 91 | + |
| 92 | + $sum = _::_([1, 2, 3])->foldl(function ($sum, $num) { |
| 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 ($prod, $num) { |
| 99 | + return $prod * $num; |
| 100 | + }, 1); |
| 101 | + |
| 102 | + $this->assertSame(24, $prod, 'prod by reduce with initial 1'); |
| 103 | + |
| 104 | + $concat = _::_([1, 2, 3, 4])->inject(function ($concat, $num) { |
| 105 | + return $concat . $num; |
| 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 ($sum, $num) { |
| 114 | + return $num + $sum; |
| 115 | + }, 0); |
| 116 | + |
| 117 | + $this->assertSame(6, $sum, 'sum by reduceRight'); |
| 118 | + |
| 119 | + $concat = _::_([1, 2, 3, 4])->foldr(function ($concat, $num) { |
| 120 | + return $concat . $num; |
| 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'); |
| 245 | + } |
| 246 | + |
| 247 | + public function test_max_min() |
| 248 | + { |
| 249 | + $list = _::_([['a' => 1, 'b' => 2], ['a' => 2, 'b' => 3], ['a' => 0, 'b' => 1]]); |
| 250 | + |
| 251 | + $this->assertSame(2, $list->max('a'), 'max a = 2'); |
| 252 | + $this->assertSame(3, $list->max('b'), 'max a = 3'); |
| 253 | + $this->assertSame(0, $list->min('a'), 'min a = 0'); |
| 254 | + $this->assertSame(1, $list->min('b'), 'min b = 1'); |
| 255 | + |
| 256 | + $this->assertSame(5, $list->max(function ($i) { |
| 257 | + return $i['a'] + $i['b']; |
| 258 | + }), 'max sum of a and b'); |
| 259 | + |
| 260 | + $this->assertSame(1, $list->min(function ($i) { |
| 261 | + return $i['b'] - $i['a']; |
| 262 | + }), 'max diff of b and a'); |
| 263 | + |
| 264 | + $list = _::_([1, 99, 9, -10, 1000, false, 0, 'string', -99, 10000, 87, null]); |
| 265 | + |
| 266 | + $this->assertSame(10000, $list->max(), 'max = 10000'); |
| 267 | + $this->assertSame(-99, $list->min(), 'min = -99'); |
| 268 | + } |
| 269 | + |
| 270 | + public function test_shuffle() |
| 271 | + { |
| 272 | + $pool = range(1, 5); |
| 273 | + $shuf = _::_($pool)->shuffle()->get(); |
| 274 | + |
| 275 | + foreach ($shuf as $key => $value) { |
| 276 | + $this->assertArrayHasKey($key, $pool, 'shuffled item is one from pool'); |
| 277 | + $this->assertSame($pool[$key], $value, 'The values are the same as in pool'); |
| 278 | + } |
| 279 | + |
| 280 | + $this->assertSame(count($pool), count($shuf), 'Should have exact counts'); |
| 281 | + } |
| 282 | + |
| 283 | + public function test_sample() |
| 284 | + { |
| 285 | + $pool = range(10, 5, -1); |
| 286 | + |
| 287 | + foreach ([1, 2, 3] as $n) { |
| 288 | + $samp = _::_($pool)->sample($n)->get(); |
| 289 | + |
| 290 | + foreach ($samp as $key => $value) { |
| 291 | + $this->assertArrayHasKey($key, $pool, 'sampled item is one from pool'); |
| 292 | + $this->assertSame($pool[$key], $value, 'The values are the same as in pool'); |
| 293 | + } |
| 294 | + |
| 295 | + $this->assertCount($n, $samp, 'The count should be the one specified'); |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + public function test_sortBy() |
| 300 | + { |
| 301 | + $sort = $init = range(1, 15); |
| 302 | + $sort = _::_($sort)->shuffle()->get(); |
| 303 | + |
| 304 | + $this->assertNotSame($init, $sort, 'Should be random'); |
| 305 | + |
| 306 | + $sort = _::_($sort)->sortBy(null)->get(); |
| 307 | + |
| 308 | + $this->assertSame($init, $sort, 'Should be sorted'); |
| 309 | + |
| 310 | + $list = _::_([['a' => 1, 'b' => 2], ['a' => 2, 'b' => 3], ['a' => 0, 'b' => 1]]); |
| 311 | + |
| 312 | + $byA = $list->sortBy('a')->get(); |
| 313 | + $this->assertSame( |
| 314 | + [2 => ['a' => 0, 'b' => 1], 0 => ['a' => 1, 'b' => 2], 1 => ['a' => 2, 'b' => 3]], |
| 315 | + $byA, 'sort by a' |
| 316 | + ); |
| 317 | + |
| 318 | + $byAB = $list->sortBy(function ($i) { |
| 319 | + return $i['a'] + $i['b']; |
| 320 | + })->get(); |
| 321 | + |
| 322 | + $this->assertSame( |
| 323 | + [2 => ['a' => 0, 'b' => 1], 0 => ['a' => 1, 'b' => 2], 1 => ['a' => 2, 'b' => 3]], |
| 324 | + $byAB, 'sort by a+b' |
| 325 | + ); |
| 326 | + } |
| 327 | + |
| 328 | + public function test_groupBy_indexBy_countBy() |
| 329 | + { |
| 330 | + $list = _::_([ |
| 331 | + ['a' => 0, 'b' => 1, 'c' => 1], |
| 332 | + ['a' => true, 'b' => false, 'c' => 'c'], |
| 333 | + ['a' => 2, 'b' => 1, 'c' => 2], |
| 334 | + ['a' => 1, 'b' => null, 'c' => 0], |
| 335 | + ]); |
| 336 | + |
| 337 | + $grpByA = $list->groupBy('a')->get(); |
| 338 | + $idxByA = $list->indexBy('a')->get(); |
| 339 | + $cntByA = $list->countBy('a')->get(); |
| 340 | + |
| 341 | + $this->assertSame([ |
| 342 | + 0 => [0 => ['a' => 0, 'b' => 1, 'c' => 1]], |
| 343 | + 1 => [1 => ['a' => true, 'b' => false, 'c' => 'c'], 3 => ['a' => 1, 'b' => null, 'c' => 0]], |
| 344 | + 2 => [2 => ['a' => 2, 'b' => 1, 'c' => 2]], |
| 345 | + ], $grpByA, 'groupBy a'); |
| 346 | + |
| 347 | + $this->assertSame([ |
| 348 | + 0 => ['a' => 0, 'b' => 1, 'c' => 1], |
| 349 | + 1 => ['a' => 1, 'b' => null, 'c' => 0], |
| 350 | + 2 => ['a' => 2, 'b' => 1, 'c' => 2], |
| 351 | + ], $idxByA, 'indexBy a'); |
| 352 | + |
| 353 | + $this->assertSame([ |
| 354 | + 0 => 1, |
| 355 | + 1 => 2, |
| 356 | + 2 => 1, |
| 357 | + ], $cntByA, 'countBy a'); |
| 358 | + } |
| 359 | + |
| 360 | + public function test_toArray() |
| 361 | + { |
| 362 | + $array = [['deep' => 1, 'ok'], 'shallow', 0, false]; |
| 363 | + |
| 364 | + $this->assertSame($array, _::_($array)->toArray()); |
| 365 | + } |
| 366 | + |
| 367 | + public function test_partition() |
| 368 | + { |
| 369 | + $nums = _::_(range(1, 10)); |
| 370 | + $oddEvn = $nums->partition(function ($i) { |
| 371 | + return $i % 2; |
| 372 | + })->get(); |
| 373 | + $evnOdd = $nums->partition(function ($i) { |
| 374 | + return $i % 2 == 0; |
| 375 | + })->get(); |
| 376 | + |
| 377 | + $this->assertCount(2, $oddEvn, '2 partitions'); |
| 378 | + $this->assertArrayHasKey(0, $oddEvn, 'odd partition'); |
| 379 | + $this->assertArrayHasKey(1, $oddEvn, 'even partition'); |
| 380 | + |
| 381 | + $this->assertSame([1, 3, 5, 7, 9], $oddEvn[0], 'odds'); |
| 382 | + $this->assertSame([1, 3, 5, 7, 9], $oddEvn[0], 'odds'); |
| 383 | + $this->assertSame($evnOdd[1], $oddEvn[0], 'odds crosscheck'); |
| 384 | + $this->assertSame($evnOdd[0], $oddEvn[1], 'evens crosscheck'); |
| 385 | + } |
| 386 | +} |
0 commit comments