|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Test; |
| 5 | + |
| 6 | +use nixn\php\Arr; |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +final class ArrTest extends TestCase |
| 11 | +{ |
| 12 | + use HandleTrait; |
| 13 | + |
| 14 | + public static function provider_pick(): array |
| 15 | + { |
| 16 | + $array = [0, 1, 2, 'a' => 'a', 'b' => 'b', 'c' => 'c']; |
| 17 | + return [ |
| 18 | + [[$array], []], |
| 19 | + [[$array, 3, 'd'], []], |
| 20 | + [[$array, 1], [1 => 1]], |
| 21 | + [[$array, 1, 2, 3, 'c', 'd'], [1 => 1, 2 => 2, 'c' => 'c']], |
| 22 | + ]; |
| 23 | + } |
| 24 | + |
| 25 | + #[DataProvider('provider_pick')] |
| 26 | + public function test_pick(array $args, mixed $result): void |
| 27 | + { |
| 28 | + $this->assertSame($result, Arr::pick(...$args)); |
| 29 | + } |
| 30 | + |
| 31 | + public static function provider_find_by(): array |
| 32 | + { |
| 33 | + $array = [0, 2, 4, 'a' => 'A', 'b' => 'B', 'c' => 'C']; |
| 34 | + return [ |
| 35 | + [[$array, fn($v, $k) => is_int($v) && $v > 0], 2], |
| 36 | + [[$array, fn($v, $k) => is_int($v) && $v > 2, true], 2], |
| 37 | + [[$array, fn($v, $k) => $k === 'a'], 'A'], |
| 38 | + [[$array, fn($v, $k) => $v === 'B', true], 'b'], |
| 39 | + [[$array, fn($v, $k) => false], null], |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + #[DataProvider('provider_find_by')] |
| 44 | + public function test_find_by(array $args, mixed $result): void |
| 45 | + { |
| 46 | + $this->assertSame($result, Arr::find_by(...$args)); |
| 47 | + } |
| 48 | + |
| 49 | + public static function provider_reduce(): array |
| 50 | + { |
| 51 | + $add = fn(int $carry, int $next) => $carry + $next; |
| 52 | + return [ |
| 53 | + [[[], $add], -1, 'exception' => [\RuntimeException::class, '/no elements and no initial value/']], |
| 54 | + [[[], $add, 1], 1], |
| 55 | + [[[], $add, 1, true], 1], |
| 56 | + [[[2], $add], 2], |
| 57 | + [[[2], $add, 1], 3], |
| 58 | + [[[2], $add, 1, true], 2], |
| 59 | + [[[2, 3], $add], 5], |
| 60 | + [[[2, 3], $add, 1], 6], |
| 61 | + [[[2, 3], $add, 1, true], 5], |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + #[DataProvider('provider_reduce')] |
| 66 | + public function test_reduce(array $args, mixed $result, string $message = '', ?array $exception = null): void |
| 67 | + { |
| 68 | + $this->handle_exception($exception); |
| 69 | + $this->assertSame($result, Arr::reduce(...$args), $message); |
| 70 | + } |
| 71 | + |
| 72 | + public static function provider_kvjoin(): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + [[[]], ''], |
| 76 | + [[['a' => 'A']], 'a=A'], |
| 77 | + [[['a' => 'A'], ';'], 'a=A'], |
| 78 | + [[['a' => 'A'], ';', ':'], 'a:A'], |
| 79 | + [[['a' => 'A', 'b' => 'B']], 'a=A, b=B'], |
| 80 | + [[['a' => 'A', 'b' => 'B'], ';'], 'a=A;b=B'], |
| 81 | + [[['a' => 'A', 'b' => 'B'], ';', ':'], 'a:A;b:B'], |
| 82 | + ]; |
| 83 | + } |
| 84 | + |
| 85 | + #[DataProvider('provider_kvjoin')] |
| 86 | + public function test_kvjoin(array $args, string $result, string $message = ''): void |
| 87 | + { |
| 88 | + $this->assertSame($result, Arr::kvjoin(...$args), $message); |
| 89 | + } |
| 90 | + |
| 91 | + public static function provider_first(): array |
| 92 | + { |
| 93 | + $none = ['k' => null, 'v' => null]; |
| 94 | + return [ |
| 95 | + [[], $none], |
| 96 | + [[1], ['k' => 0, 'v' => 1]], |
| 97 | + [[1, 2], ['k' => 0, 'v' => 1]], |
| 98 | + [[1 => 1, 2], ['k' => 1, 'v' => 1]], |
| 99 | + [['a' => 1, 'b' => 2], ['k' => 'a', 'v' => 1]], |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + #[DataProvider('provider_first')] |
| 104 | + public function test_first(array $data, array $result, string $message = ''): void |
| 105 | + { |
| 106 | + $this->assertSame($result, Arr::first($data), $message); |
| 107 | + } |
| 108 | + |
| 109 | + public static function provider_find(): array |
| 110 | + { |
| 111 | + $array = [0, 2, 4, 'a' => 'A', 'b' => 'B', 'c' => 'C']; |
| 112 | + $none = ['k' => null, 'v' => null]; |
| 113 | + return [ |
| 114 | + [[$array], $none], |
| 115 | + [[$array, 0], ['k' => 0, 'v' => 0]], |
| 116 | + [[$array, 1], ['k' => 1, 'v' => 2]], |
| 117 | + [[$array, 3], $none], |
| 118 | + [[$array, 0, 1], ['k' => 0, 'v' => 0]], |
| 119 | + [[$array, 1, 2], ['k' => 1, 'v' => 2]], |
| 120 | + [[$array, 'a', 'b', 'c'], ['k' => 'a', 'v' => 'A']], |
| 121 | + [[$array, 'x'], $none], |
| 122 | + [[$array, 'x', 3], $none], |
| 123 | + [[$array, 'a', 2, 'b'], ['k' => 2, 'v' => 4]], |
| 124 | + [[$array, 3, 'b'], ['k' => 'b', 'v' => 'B']], |
| 125 | + [[$array, 3, 'b', 2], ['k' => 2, 'v' => 4]], |
| 126 | + ]; |
| 127 | + } |
| 128 | + |
| 129 | + #[DataProvider('provider_find')] |
| 130 | + public function test_find(array $args, mixed $result, string $message = ''): void |
| 131 | + { |
| 132 | + $this->assertSame($result, Arr::find(...$args), $message); |
| 133 | + } |
| 134 | +} |
0 commit comments