|
5 | 5 |
|
6 | 6 | use PHPUnit\Framework\TestCase;
|
7 | 7 | use function BrenoRoosevelt\get_path;
|
| 8 | +use function BrenoRoosevelt\has_path; |
8 | 9 | use function BrenoRoosevelt\set_path;
|
| 10 | +use function BrenoRoosevelt\unset_path; |
9 | 11 |
|
10 | 12 | class DotNotationTest extends TestCase
|
11 | 13 | {
|
@@ -126,4 +128,153 @@ public function testGetPath(array $items, string $path, $default, $expected): vo
|
126 | 128 | $actual = get_path($items, $path, $default);
|
127 | 129 | $this->assertEquals($expected, $actual);
|
128 | 130 | }
|
| 131 | + |
| 132 | + public function hasPathProvider(): array |
| 133 | + { |
| 134 | + $abcdNull = ['a' => ['b' => ['c' => ['d' => null]]]]; |
| 135 | + return [ |
| 136 | + 'case_1' => [ |
| 137 | + [], |
| 138 | + '', |
| 139 | + false |
| 140 | + ], |
| 141 | + 'case_2' => [ |
| 142 | + [1, 2, 3], |
| 143 | + '0', |
| 144 | + true |
| 145 | + ], |
| 146 | + 'case_3' => [ |
| 147 | + [1, 2, 3], |
| 148 | + '2', |
| 149 | + true |
| 150 | + ], |
| 151 | + 'case_4' => [ |
| 152 | + [[[[1, 2, 3]]]], |
| 153 | + '0.0.0.2', |
| 154 | + true |
| 155 | + ], |
| 156 | + 'case_5' => [ |
| 157 | + [[[[1, 2, 3]]]], |
| 158 | + '0.0.0.3', |
| 159 | + false |
| 160 | + ], |
| 161 | + 'case_6' => [ |
| 162 | + [[[[1, 2, 3]]]], |
| 163 | + '0.0', |
| 164 | + true |
| 165 | + ], |
| 166 | + 'case_7' => [ |
| 167 | + [[[0, 1 => [1, 2], [1, 2, 3]]]], |
| 168 | + '0.0.1', |
| 169 | + true |
| 170 | + ], |
| 171 | + 'case_8' => [ |
| 172 | + $abcdNull, |
| 173 | + '0', |
| 174 | + false |
| 175 | + ], |
| 176 | + 'case_9' => [ |
| 177 | + $abcdNull, |
| 178 | + 'a', |
| 179 | + true |
| 180 | + ], |
| 181 | + 'case_10' => [ |
| 182 | + $abcdNull, |
| 183 | + 'a.b', |
| 184 | + true |
| 185 | + ], |
| 186 | + 'case_11' => [ |
| 187 | + $abcdNull, |
| 188 | + 'a.b.c', |
| 189 | + true |
| 190 | + ], |
| 191 | + 'case_12' => [ |
| 192 | + $abcdNull, |
| 193 | + 'a.b.c.d', |
| 194 | + true |
| 195 | + ], |
| 196 | + 'case_13' => [ |
| 197 | + ['a' => null], |
| 198 | + 'a', |
| 199 | + true |
| 200 | + ], |
| 201 | + 'case_14' => [ |
| 202 | + ['a' => ['b' => null]], |
| 203 | + 'a.b', |
| 204 | + true |
| 205 | + ], |
| 206 | + 'case_15' => [ |
| 207 | + ['a' => ['b' => false]], |
| 208 | + 'a.b', |
| 209 | + true |
| 210 | + ], |
| 211 | + 'case_16' => [ |
| 212 | + ['a' => ['b' => 0]], |
| 213 | + 'a.b', |
| 214 | + true |
| 215 | + ], |
| 216 | + ]; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * @param array $items |
| 221 | + * @param string $path |
| 222 | + * @param $expected |
| 223 | + * @return void |
| 224 | + * @dataProvider hasPathProvider |
| 225 | + */ |
| 226 | + public function testHasPath(array $items, string $path, $expected) |
| 227 | + { |
| 228 | + $actual = has_path($items, $path); |
| 229 | + $this->assertEquals($expected, $actual); |
| 230 | + } |
| 231 | + |
| 232 | + public function unsetPathProvider(): array |
| 233 | + { |
| 234 | + return [ |
| 235 | + 'case_1' => [ |
| 236 | + [], |
| 237 | + '', |
| 238 | + [] |
| 239 | + ], |
| 240 | + 'case_2' => [ |
| 241 | + [1, 2, 3], |
| 242 | + '3', |
| 243 | + [1, 2, 3] |
| 244 | + ], |
| 245 | + 'case_3' => [ |
| 246 | + [1, 2, 3], |
| 247 | + '2', |
| 248 | + [1, 2] |
| 249 | + ], |
| 250 | + 'case_4' => [ |
| 251 | + [1, 2, 3], |
| 252 | + '0', |
| 253 | + [1 => 2, 2 => 3] |
| 254 | + ], |
| 255 | + 'case_5' => [ |
| 256 | + [[[[1, 2, 3]]]], |
| 257 | + '0.0.0.1', |
| 258 | + [[[[1, 2 => 3]]]], |
| 259 | + ], |
| 260 | + 'case_6' => [ |
| 261 | + [['x' => [[1, 2 => 3]]], 9], |
| 262 | + '0.x', |
| 263 | + [[], 1 => 9], |
| 264 | + ], |
| 265 | + ]; |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * @param array $items |
| 270 | + * @param string $path |
| 271 | + * @param $expected |
| 272 | + * @return void |
| 273 | + * @dataProvider unsetPathProvider |
| 274 | + */ |
| 275 | + public function testUnsetPath(array $items, string $path, $expected) |
| 276 | + { |
| 277 | + unset_path($items, $path); |
| 278 | + $this->assertEquals($expected, $items); |
| 279 | + } |
129 | 280 | }
|
0 commit comments