|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ahc\Underscore\Tests; |
| 4 | + |
| 5 | +use Ahc\Underscore\UnderscoreArray as _; |
| 6 | + |
| 7 | +class UnderscoreArrayTest extends \PHPUnit_Framework_TestCase |
| 8 | +{ |
| 9 | + public function test_first_last() |
| 10 | + { |
| 11 | + $array = range(rand(5, 10), rand(15, 20)); |
| 12 | + |
| 13 | + $this->assertSame($array[0], _::_($array)->first(), 'first'); |
| 14 | + $this->assertSame(array_reverse($array)[0], _::_($array)->last(), 'last'); |
| 15 | + |
| 16 | + $array = ['x' => ['first'], 'z' => 'last']; |
| 17 | + |
| 18 | + $this->assertSame($array['x'], _::_($array)->head(), 'first'); |
| 19 | + $this->assertSame($array['z'], _::_($array)->tail(), 'last'); |
| 20 | + |
| 21 | + $array = range(1, 5); |
| 22 | + |
| 23 | + $this->assertSame([1, 2, 3], _::_($array)->take(3), 'first 3'); |
| 24 | + $this->assertSame([1, 2, 3, 4, 5], _::_($array)->first(6), 'first 6 (n + 1)'); |
| 25 | + $this->assertSame([2 => 3, 3 => 4, 4 => 5], _::_($array)->drop(3), 'last 3'); |
| 26 | + $this->assertSame([1, 2, 3, 4, 5], _::_($array)->last(6), 'last 6 (n + 1)'); |
| 27 | + } |
| 28 | + |
| 29 | + public function test_compact() |
| 30 | + { |
| 31 | + $array = [0, 'a', '', [], 2, [1]]; |
| 32 | + |
| 33 | + $this->assertSame([1 => 'a', 4 => 2, 5 => [1]], _::_($array)->compact()->get(), 'first'); |
| 34 | + } |
| 35 | + |
| 36 | + public function test_flatten() |
| 37 | + { |
| 38 | + $array = [0, 'a', '', [[1, [2]]], 'b', [[[3]], 4, 'c', new _([5, 'd'])]]; |
| 39 | + |
| 40 | + $this->assertSame( |
| 41 | + [0, 'a', '', 1, 2, 'b', 3, 4, 'c', 5, 'd'], |
| 42 | + _::_($array)->flatten()->get(), |
| 43 | + 'flatten' |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_unique_uniq() |
| 48 | + { |
| 49 | + $array = [0, 'a', '', 1, '', 0, 2, 'a', 3, 1]; |
| 50 | + |
| 51 | + $this->assertSame( |
| 52 | + [0, 'a', '', 1, 6 => 2, 8 => 3], |
| 53 | + _::_($array)->unique()->get(), |
| 54 | + 'unique' |
| 55 | + ); |
| 56 | + |
| 57 | + $array = ['a', '', 'a', 1, '', 0, 1, 'b', 3, 2]; |
| 58 | + |
| 59 | + $this->assertSame( |
| 60 | + ['a', '', 3 => 1, 5 => 0, 7 => 'b', 8 => 3, 9 => 2], |
| 61 | + _::_($array)->uniq(function ($i) { |
| 62 | + return $i; |
| 63 | + })->get(), |
| 64 | + 'uniq' |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_difference_without() |
| 69 | + { |
| 70 | + $array = [1, 2, 1, 'a' => 3, 'b' => [4]]; |
| 71 | + |
| 72 | + $this->assertSame( |
| 73 | + [1 => 2, 'a' => 3], |
| 74 | + _::_($array)->difference([1, [4]])->get(), |
| 75 | + 'difference' |
| 76 | + ); |
| 77 | + |
| 78 | + $this->assertSame( |
| 79 | + ['a' => 3, 'b' => [4]], |
| 80 | + _::_($array)->without([1, 2])->get(), |
| 81 | + 'without' |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + public function test_union() |
| 86 | + { |
| 87 | + $array = [1, 2, 'a' => 3]; |
| 88 | + |
| 89 | + $this->assertSame( |
| 90 | + [1, 2, 'a' => 4, 3, 'b' => [5]], |
| 91 | + _::_($array)->union([3, 'a' => 4, 'b' => [5]])->get(), |
| 92 | + 'union' |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + public function test_intersection() |
| 97 | + { |
| 98 | + $array = [1, 2, 'a' => 3]; |
| 99 | + |
| 100 | + $this->assertSame( |
| 101 | + [1 => 2, 'a' => 3], |
| 102 | + _::_($array)->intersection([2, 'a' => 3, 3])->get(), |
| 103 | + 'intersection' |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + public function test_zip() |
| 108 | + { |
| 109 | + $array = [1, 2, 'a' => 3, 'b' => 'B']; |
| 110 | + |
| 111 | + $this->assertSame( |
| 112 | + [[1, 2], [2, 4], 'a' => [3, 5], 'b' => ['B', null]], |
| 113 | + _::_($array)->zip([2, 4, 'a' => 5])->get(), |
| 114 | + 'zip' |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + public function test_object() |
| 119 | + { |
| 120 | + $array = [[1, 2], 'a' => 3, 'b' => 'B']; |
| 121 | + |
| 122 | + foreach (_::_($array)->object() as $index => $value) { |
| 123 | + $this->assertTrue(is_object($value)); |
| 124 | + $this->assertSame($index, $value->index); |
| 125 | + $this->assertSame($array[$index], $value->value); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public function test_firstIndex_lastIndex() |
| 130 | + { |
| 131 | + $array = _::_([[1, 2], 'a' => 3, 'x' => 4, 'y' => 2, 'b' => 'B']); |
| 132 | + |
| 133 | + $this->assertSame(0, $array->firstIndex()); |
| 134 | + $this->assertSame('b', $array->lastIndex()); |
| 135 | + |
| 136 | + $this->assertSame('x', $array->firstIndex(function ($i) { |
| 137 | + return is_numeric($i) && $i % 2 === 0; |
| 138 | + })); |
| 139 | + $this->assertSame('y', $array->lastIndex(function ($i) { |
| 140 | + return is_numeric($i) && $i % 2 === 0; |
| 141 | + })); |
| 142 | + } |
| 143 | + |
| 144 | + public function test_indexOf_lastIndexOf() |
| 145 | + { |
| 146 | + $array = _::_([[1, 2], 'a' => 2, 'x' => 4, 'y' => 2, 'b' => 'B']); |
| 147 | + |
| 148 | + $this->assertSame('a', $array->indexOf(2)); |
| 149 | + $this->assertSame('y', $array->lastIndexOf(2)); |
| 150 | + } |
| 151 | + |
| 152 | + public function test_range() |
| 153 | + { |
| 154 | + $this->assertSame([4, 5, 6, 7, 8, 9], _::_()->range(4, 9)->get()); |
| 155 | + $this->assertSame([10, 12, 14, 16, 18], _::_()->range(10, 18, 2)->get()); |
| 156 | + $this->assertSame([20, 19, 18, 17, 16], _::_()->range(20, 16, -1)->get()); |
| 157 | + } |
| 158 | +} |
0 commit comments