|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Polyfills\MbTrimPolyfill\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +class ArrayFindTest extends TestCase { |
| 8 | + |
| 9 | + public function testArrayFind() { |
| 10 | + $array1 = [ |
| 11 | + "a" => 1, |
| 12 | + "b" => 2, |
| 13 | + "c" => 3, |
| 14 | + "d" => 4, |
| 15 | + "e" => 5, |
| 16 | + ]; |
| 17 | + |
| 18 | + $array2 = [ |
| 19 | + 1, 2, 3, 4, 5, |
| 20 | + ]; |
| 21 | + |
| 22 | + $evenFunction = static function($input) { |
| 23 | + return $input % 2 === 0; |
| 24 | + }; |
| 25 | + |
| 26 | + self::assertSame(array_find(['apple', 'banana'], static function($value, $key) { |
| 27 | + return $value === 'banana'; |
| 28 | + }), 'banana'); |
| 29 | + |
| 30 | + self::assertSame(2, array_find($array1, $evenFunction)); |
| 31 | + |
| 32 | + self::assertNull(array_find($array2, static function($value) { |
| 33 | + return $value > 5; |
| 34 | + })); |
| 35 | + |
| 36 | + self::assertNull(array_find($array2, static function($value) { |
| 37 | + return $value > 5; |
| 38 | + })); |
| 39 | + |
| 40 | + self::assertNull(array_find([], static function($value) { |
| 41 | + return true; |
| 42 | + })); |
| 43 | + |
| 44 | + self::assertSame(3, array_find($array1, static function($value, $key) { |
| 45 | + return $key === "c"; |
| 46 | + })); |
| 47 | + |
| 48 | + self::assertNull(array_find($array1, static function($value) { |
| 49 | + return false; |
| 50 | + })); |
| 51 | + } |
| 52 | + |
| 53 | + public function testArrayFindKey() { |
| 54 | + $array1 = [ |
| 55 | + "a" => 1, |
| 56 | + "b" => 2, |
| 57 | + "c" => 3, |
| 58 | + "d" => 4, |
| 59 | + "e" => 5, |
| 60 | + ]; |
| 61 | + |
| 62 | + $array2 = [ |
| 63 | + 1, 2, 3, 4, 5, |
| 64 | + ]; |
| 65 | + |
| 66 | + $evenFunction = static function($input) { |
| 67 | + return $input % 2 === 0; |
| 68 | + }; |
| 69 | + |
| 70 | + self::assertSame("d", array_find_key($array1, static function($value) { |
| 71 | + return $value > 3; |
| 72 | + })); |
| 73 | + |
| 74 | + self::assertSame(3, array_find_key($array2, static function($value) { |
| 75 | + return $value > 3; |
| 76 | + })); |
| 77 | + |
| 78 | + self::assertSame(1, array_find_key($array2, $evenFunction)); |
| 79 | + |
| 80 | + self::assertNull(array_find_key($array2, static function($value) { |
| 81 | + return $value > 5; |
| 82 | + })); |
| 83 | + |
| 84 | + self::assertNull(array_find_key([], static function($value) { |
| 85 | + return true; |
| 86 | + })); |
| 87 | + |
| 88 | + self::assertSame("c", array_find_key($array1, static function($value, $key) { |
| 89 | + return $key === "c"; |
| 90 | + })); |
| 91 | + |
| 92 | + self::assertNull(array_find_key($array2, static function($value, $key) { |
| 93 | + return false; |
| 94 | + })); |
| 95 | + } |
| 96 | + |
| 97 | + public function testArrayAny() { |
| 98 | + $array1 = [ |
| 99 | + "a" => 1, |
| 100 | + "b" => 2, |
| 101 | + "c" => 3, |
| 102 | + "d" => 4, |
| 103 | + "e" => 5, |
| 104 | + ]; |
| 105 | + |
| 106 | + $array2 = [ |
| 107 | + 1, 2, 3, 4, 5, |
| 108 | + ]; |
| 109 | + |
| 110 | + $evenFunction = static function($input) { |
| 111 | + return $input % 2 === 0; |
| 112 | + }; |
| 113 | + |
| 114 | + self::assertTrue(array_any($array1, static function($value) { |
| 115 | + return $value > 3; |
| 116 | + })); |
| 117 | + |
| 118 | + self::assertTrue(array_any($array2, static function($value) { |
| 119 | + return $value > 3; |
| 120 | + })); |
| 121 | + |
| 122 | + self::assertTrue(array_any($array2, $evenFunction)); |
| 123 | + |
| 124 | + self::assertFalse(array_any($array2, static function($value) { |
| 125 | + return $value > 5; |
| 126 | + })); |
| 127 | + |
| 128 | + self::assertFalse(array_any([], static function($value) { |
| 129 | + return true; |
| 130 | + })); |
| 131 | + |
| 132 | + self::assertTrue(array_any($array1, static function($value, $key) { |
| 133 | + return $key === "c"; |
| 134 | + })); |
| 135 | + |
| 136 | + self::assertFalse(array_any($array2, static function($value, $key) { |
| 137 | + return false; |
| 138 | + })); |
| 139 | + } |
| 140 | + |
| 141 | + public function testArrayAll() { |
| 142 | + $array1 = [ |
| 143 | + "a" => 1, |
| 144 | + "b" => 2, |
| 145 | + "c" => 3, |
| 146 | + "d" => 4, |
| 147 | + "e" => 5, |
| 148 | + ]; |
| 149 | + |
| 150 | + $array2 = [ |
| 151 | + 1, 2, 3, 4, 5, |
| 152 | + ]; |
| 153 | + |
| 154 | + $evenFunction = static function($input) { |
| 155 | + return $input % 2 === 0; |
| 156 | + }; |
| 157 | + |
| 158 | + self::assertTrue(array_all($array1, static function($value) { |
| 159 | + return $value > 0; |
| 160 | + })); |
| 161 | + |
| 162 | + self::assertTrue(array_all($array1, static function($value) { |
| 163 | + return $value > 0; |
| 164 | + })); |
| 165 | + |
| 166 | + self::assertFalse(array_all($array2, static function($value) { |
| 167 | + return $value > 1; |
| 168 | + })); |
| 169 | + |
| 170 | + self::assertFalse(array_all($array2, $evenFunction)); |
| 171 | + |
| 172 | + self::assertTrue(array_all([], static function($value) { |
| 173 | + return true; |
| 174 | + })); |
| 175 | + |
| 176 | + self::assertTrue(array_all([], static function($value) { |
| 177 | + return false; |
| 178 | + })); |
| 179 | + |
| 180 | + self::assertFalse(array_all($array2, static function($value) { |
| 181 | + return $value > 5; |
| 182 | + })); |
| 183 | + } |
| 184 | +} |
0 commit comments