|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Nette\Utils\Arrays; |
| 6 | +use Tester\Assert; |
| 7 | + |
| 8 | +require __DIR__ . '/../bootstrap.php'; |
| 9 | + |
| 10 | + |
| 11 | +test('no predicate', function () { |
| 12 | + Assert::null(Arrays::firstKey([])); |
| 13 | + Assert::same(0, Arrays::firstKey([null])); |
| 14 | + Assert::same(0, Arrays::firstKey([1, 2, 3])); |
| 15 | + Assert::same(5, Arrays::firstKey([5 => 1, 2, 3])); |
| 16 | +}); |
| 17 | + |
| 18 | +test('internal array pointer is not affected', function () { |
| 19 | + $arr = [1, 2, 3]; |
| 20 | + end($arr); |
| 21 | + Assert::same(0, Arrays::firstKey($arr)); |
| 22 | + Assert::same(3, current($arr)); |
| 23 | +}); |
| 24 | + |
| 25 | +test('with predicate', function () { |
| 26 | + Assert::null(Arrays::firstKey([], fn() => true)); |
| 27 | + Assert::null(Arrays::firstKey([], fn() => false)); |
| 28 | + Assert::null(Arrays::firstKey(['' => 'x'], fn() => false)); |
| 29 | + Assert::same(0, Arrays::firstKey([null], fn() => true)); |
| 30 | + Assert::null(Arrays::firstKey([null], fn() => false)); |
| 31 | + Assert::same(0, Arrays::firstKey([1, 2, 3], fn() => true)); |
| 32 | + Assert::null(Arrays::firstKey([1, 2, 3], fn() => false)); |
| 33 | + Assert::same(2, Arrays::firstKey([1, 2, 3], fn($v) => $v > 2)); |
| 34 | + Assert::same(0, Arrays::firstKey([1, 2, 3], fn($v) => $v < 2)); |
| 35 | +}); |
| 36 | + |
| 37 | +test('predicate arguments', function () { |
| 38 | + Arrays::firstKey([2 => 'x'], fn() => Assert::same(['x', 2, [2 => 'x']], func_get_args())); |
| 39 | +}); |
0 commit comments