|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace BrenoRoosevelt\Tests; |
| 5 | + |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use function BrenoRoosevelt\exactly; |
| 8 | +use const BrenoRoosevelt\CALLBACK_USE_BOTH; |
| 9 | +use const BrenoRoosevelt\CALLBACK_USE_KEY; |
| 10 | +use const BrenoRoosevelt\CALLBACK_USE_VALUE; |
| 11 | + |
| 12 | +class ExactlyTest extends TestCase |
| 13 | +{ |
| 14 | + public function exactlyProvider(): array |
| 15 | + { |
| 16 | + return [ |
| 17 | + 'case_1' => [ |
| 18 | + 0, // n |
| 19 | + ['a', 'b', 'c'], // collection |
| 20 | + fn($el) => false, // callback |
| 21 | + CALLBACK_USE_VALUE, // mode |
| 22 | + true, // expected |
| 23 | + ], |
| 24 | + 'case_2' => [ |
| 25 | + 3, |
| 26 | + ['a', 'b', 'c'], |
| 27 | + fn($el) => true, |
| 28 | + CALLBACK_USE_VALUE, |
| 29 | + true, |
| 30 | + ], |
| 31 | + 'case_3' => [ |
| 32 | + 1, |
| 33 | + [], |
| 34 | + fn($el) => true, |
| 35 | + CALLBACK_USE_VALUE, |
| 36 | + false, |
| 37 | + ], |
| 38 | + 'case_3_1' => [ |
| 39 | + 0, |
| 40 | + [], |
| 41 | + fn($el) => true, |
| 42 | + CALLBACK_USE_VALUE, |
| 43 | + true, |
| 44 | + ], |
| 45 | + 'case_3_2' => [ |
| 46 | + -1, |
| 47 | + [], |
| 48 | + fn($el) => true, |
| 49 | + CALLBACK_USE_VALUE, |
| 50 | + false, |
| 51 | + ], |
| 52 | + 'case_4' => [ |
| 53 | + 1, |
| 54 | + [1, 2, 3, 4], |
| 55 | + fn($el) => $el % 2 === 0, |
| 56 | + CALLBACK_USE_VALUE, |
| 57 | + false, |
| 58 | + ], |
| 59 | + 'case_5' => [ |
| 60 | + 2, |
| 61 | + [1, 2, 3, 4], |
| 62 | + fn($el) => $el % 2 === 0, |
| 63 | + CALLBACK_USE_VALUE, |
| 64 | + true, |
| 65 | + ], |
| 66 | + 'case_6' => [ |
| 67 | + 3, |
| 68 | + [1, 2, 3, 4], |
| 69 | + fn($key) => is_integer($key), |
| 70 | + CALLBACK_USE_KEY, |
| 71 | + false, |
| 72 | + ], |
| 73 | + 'case_6_1' => [ |
| 74 | + 1, |
| 75 | + [1, 2, 3, 4, 'a' => 5], |
| 76 | + fn($key) => is_string($key), |
| 77 | + CALLBACK_USE_KEY, |
| 78 | + true, |
| 79 | + ], |
| 80 | + 'case_7' => [ |
| 81 | + 1, |
| 82 | + ['a' => 1, 'b' => 2, 'c' => 3, 4], |
| 83 | + fn($v, $key) => is_integer($key) && is_integer($v), |
| 84 | + CALLBACK_USE_BOTH, |
| 85 | + true, |
| 86 | + ] |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param int $n |
| 92 | + * @param iterable $items |
| 93 | + * @param callable $callback |
| 94 | + * @param int $mode |
| 95 | + * @param bool $expected |
| 96 | + * @return void |
| 97 | + * @dataProvider exactlyProvider |
| 98 | + */ |
| 99 | + public function testExactly(int $n, iterable $items, callable $callback, int $mode, bool $expected): void |
| 100 | + { |
| 101 | + $actual = exactly($n, $items, $callback, $mode); |
| 102 | + $this->assertEquals($expected, $actual); |
| 103 | + } |
| 104 | +} |
0 commit comments