Skip to content

Commit 286a353

Browse files
committed
Add Arr tests
1 parent c88bb29 commit 286a353

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

tests/unit/Utility/ArrTest.php

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,41 @@
66

77
final class ArrTest extends \Lkrms\Tests\TestCase
88
{
9+
/**
10+
* @dataProvider firstProvider
11+
*
12+
* @param mixed $expected
13+
* @param mixed[] $array
14+
*/
15+
public function testFirst($expected, array $array): void
16+
{
17+
$this->assertSame($expected, Arr::first($array));
18+
}
19+
20+
/**
21+
* @return array<array{mixed,mixed[]}>
22+
*/
23+
public static function firstProvider(): array
24+
{
25+
$object1 = new \stdClass();
26+
$object2 = new \stdClass();
27+
return [
28+
[null, []],
29+
[null, [null]],
30+
[true, [true, false]],
31+
[false, [false, true]],
32+
[0, [0, 1, 2]],
33+
[2, [2, 1, 0]],
34+
[0, [
35+
2 => 0,
36+
1 => 1,
37+
0 => 2,
38+
]],
39+
[$object1, [$object1, $object2]],
40+
[$object2, [$object2, $object1]],
41+
];
42+
}
43+
944
/**
1045
* @dataProvider implodeProvider
1146
*
@@ -194,6 +229,161 @@ public static function ofStringProvider(): array
194229
];
195230
}
196231

232+
/**
233+
* @dataProvider lastProvider
234+
*
235+
* @param mixed $expected
236+
* @param mixed[] $array
237+
*/
238+
public function testLast($expected, array $array): void
239+
{
240+
$this->assertSame($expected, Arr::last($array));
241+
}
242+
243+
/**
244+
* @return array<array{mixed,mixed[]}>
245+
*/
246+
public static function lastProvider(): array
247+
{
248+
$object1 = new \stdClass();
249+
$object2 = new \stdClass();
250+
return [
251+
[null, []],
252+
[null, [null]],
253+
[false, [true, false]],
254+
[true, [false, true]],
255+
[2, [0, 1, 2]],
256+
[0, [2, 1, 0]],
257+
[2, [
258+
2 => 0,
259+
1 => 1,
260+
0 => 2,
261+
]],
262+
[$object2, [$object1, $object2]],
263+
[$object1, [$object2, $object1]],
264+
];
265+
}
266+
267+
/**
268+
* @dataProvider popProvider
269+
*
270+
* @param mixed[] $expected
271+
* @param mixed $popped
272+
* @param mixed[] $array
273+
*/
274+
public function testPop(array $expected, $popped, array $array): void
275+
{
276+
// @phpstan-ignore-next-line
277+
$this->assertSame($expected, Arr::pop($array, $actualShifted));
278+
$this->assertSame($popped, $actualShifted);
279+
}
280+
281+
/**
282+
* @return array<array{mixed[],mixed,mixed[]}>
283+
*/
284+
public static function popProvider(): array
285+
{
286+
return [
287+
[['a', 'b'], 'c', ['a', 'b', 'c']],
288+
[[0, 1], 2, [0, 1, 2]],
289+
[[false], true, [false, true]],
290+
[[true], false, [true, false]],
291+
[[], null, [null]],
292+
[[], null, []],
293+
[['a' => 'foo'], 'bar', ['a' => 'foo', 'b' => 'bar']],
294+
];
295+
}
296+
297+
/**
298+
* @dataProvider pushProvider
299+
*
300+
* @param mixed[] $expected
301+
* @param mixed[] $array
302+
* @param mixed ...$values
303+
*/
304+
public function testPush(array $expected, array $array, ...$values): void
305+
{
306+
$this->assertSame($expected, Arr::push($array, ...$values));
307+
}
308+
309+
/**
310+
* @return array<array{mixed[],mixed[],...}>
311+
*/
312+
public static function pushProvider(): array
313+
{
314+
return [
315+
[['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c'], 'd', 'e'],
316+
[[0, 1, 2, 3, 4], [0, 1, 2], 3, 4],
317+
[[false, true, true, false], [false, true], true, false],
318+
[[true, false, false, true], [true, false], false, true],
319+
[[], []],
320+
[[null], [], null],
321+
[[false], [], false],
322+
[[0], [], 0],
323+
[['a' => 'foo', 'b' => 'bar', 'baz', 'qux'], ['a' => 'foo', 'b' => 'bar'], 'baz', 'qux'],
324+
];
325+
}
326+
327+
/**
328+
* @dataProvider shiftProvider
329+
*
330+
* @param mixed[] $expected
331+
* @param mixed $shifted
332+
* @param mixed[] $array
333+
*/
334+
public function testShift(array $expected, $shifted, array $array): void
335+
{
336+
// @phpstan-ignore-next-line
337+
$this->assertSame($expected, Arr::shift($array, $actualShifted));
338+
$this->assertSame($shifted, $actualShifted);
339+
}
340+
341+
/**
342+
* @return array<array{mixed[],mixed,mixed[]}>
343+
*/
344+
public static function shiftProvider(): array
345+
{
346+
return [
347+
[['b', 'c'], 'a', ['a', 'b', 'c']],
348+
[[1, 2], 0, [0, 1, 2]],
349+
[[true], false, [false, true]],
350+
[[false], true, [true, false]],
351+
[[], null, [null]],
352+
[[], null, []],
353+
[['b' => 'bar'], 'foo', ['a' => 'foo', 'b' => 'bar']],
354+
];
355+
}
356+
357+
/**
358+
* @dataProvider unshiftProvider
359+
*
360+
* @param mixed[] $expected
361+
* @param mixed[] $array
362+
* @param mixed ...$values
363+
*/
364+
public function testUnshift(array $expected, array $array, ...$values): void
365+
{
366+
$this->assertSame($expected, Arr::unshift($array, ...$values));
367+
}
368+
369+
/**
370+
* @return array<array{mixed[],mixed[],...}>
371+
*/
372+
public static function unshiftProvider(): array
373+
{
374+
return [
375+
[['d', 'e', 'a', 'b', 'c'], ['a', 'b', 'c'], 'd', 'e'],
376+
[[3, 4, 0, 1, 2], [0, 1, 2], 3, 4],
377+
[[true, false, false, true], [false, true], true, false],
378+
[[false, true, true, false], [true, false], false, true],
379+
[[], []],
380+
[[null], [], null],
381+
[[false], [], false],
382+
[[0], [], 0],
383+
[['baz', 'qux', 'a' => 'foo', 'b' => 'bar'], ['a' => 'foo', 'b' => 'bar'], 'baz', 'qux'],
384+
];
385+
}
386+
197387
/**
198388
* @dataProvider unwrapProvider
199389
*

0 commit comments

Comments
 (0)