|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ahc\Underscore\Tests; |
| 4 | + |
| 5 | +use Ahc\Underscore\UnderscoreArray as _; |
| 6 | + |
| 7 | +class ArrayTest 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)->first(), 'first'); |
| 19 | + $this->assertSame($array['z'], _::_($array)->last(), 'last'); |
| 20 | + } |
| 21 | + |
| 22 | + public function test_compact() |
| 23 | + { |
| 24 | + $array = [0, 'a', '', [], 2, [1]]; |
| 25 | + |
| 26 | + $this->assertSame([1 => 'a', 4 => 2, 5 => [1]], _::_($array)->compact()->get(), 'first'); |
| 27 | + } |
| 28 | + |
| 29 | + public function test_flatten() |
| 30 | + { |
| 31 | + $array = [0, 'a', '', [[1, [2]]], 'b', [[[3]], 4, 'c']]; |
| 32 | + |
| 33 | + $this->assertSame( |
| 34 | + [0, 'a', '', 1, 2, 'b', 3, 4, 'c'], |
| 35 | + _::_($array)->flatten()->get(), |
| 36 | + 'flatten' |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_unique_uniq() |
| 41 | + { |
| 42 | + $array = [0, 'a', '', 1, '', 0, 2, 'a', 3, 1]; |
| 43 | + |
| 44 | + $this->assertSame( |
| 45 | + [0, 'a', '', 1, 6 => 2, 8 => 3], |
| 46 | + _::_($array)->unique()->get(), |
| 47 | + 'unique' |
| 48 | + ); |
| 49 | + |
| 50 | + $array = ['a', '', 'a', 1, '', 0, 1, 'b', 3, 2]; |
| 51 | + |
| 52 | + $this->assertSame( |
| 53 | + ['a', '', 3 => 1, 5 => 0, 7 => 'b', 8 => 3, 9 => 2], |
| 54 | + _::_($array)->uniq(function ($i) { |
| 55 | + return $i; |
| 56 | + })->get(), |
| 57 | + 'uniq' |
| 58 | + ); |
| 59 | + } |
| 60 | +} |
0 commit comments