Skip to content

Commit 20924c8

Browse files
committed
Coverage 100%!
1 parent 2ff6647 commit 20924c8

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/Util.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ public static function normalizeIndex(int $index, int $containerLength, bool $th
3737
* Check if an array is sequential (indexed from 0 to n-1).
3838
*
3939
* @param array<mixed> $source The array to check for sequential indexing.
40+
* @param bool $forceCustomImplementation Flag only for tests.
4041
*
4142
* @return bool Returns true if the array has sequential indexing, false otherwise.
4243
*/
43-
public static function isArraySequential(array $source): bool
44+
public static function isArraySequential(array $source, bool $forceCustomImplementation = false): bool
4445
{
45-
if (!function_exists('array_is_list')) {
46-
return array_keys($source) === range(0, count($source) - 1);
46+
if (!function_exists('array_is_list') || $forceCustomImplementation) {
47+
return \count($source) === 0 || array_keys($source) === range(0, count($source) - 1);
4748
}
4849
return array_is_list($source);
4950
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smoren\ArrayView\Tests\Unit\Utils;
6+
7+
use Smoren\ArrayView\Exceptions\IndexError;
8+
use Smoren\ArrayView\Util;
9+
10+
class IsArraySequentialTest extends \Codeception\Test\Unit
11+
{
12+
/**
13+
* @dataProvider dataProviderForIsSequentialTrue
14+
*/
15+
public function testIsSequentialTrue(array $source)
16+
{
17+
$this->assertTrue(Util::isArraySequential($source));
18+
$this->assertTrue(Util::isArraySequential($source, true));
19+
}
20+
21+
/**
22+
* @dataProvider dataProviderForIsSequentialFalse
23+
*/
24+
public function testIsSequentialFalse(array $source)
25+
{
26+
$this->assertFalse(Util::isArraySequential($source));
27+
$this->assertFalse(Util::isArraySequential($source, true));
28+
}
29+
30+
public function dataProviderForIsSequentialTrue(): array
31+
{
32+
return [
33+
[[]],
34+
[['']],
35+
[[null]],
36+
[[1]],
37+
[['1']],
38+
[['test']],
39+
[[1, 2, 3]],
40+
[[0 => 1, 1 => 2, 2 => 3]],
41+
[['0' => 1, 1 => 2, 2 => 3]],
42+
[['0' => 1, '1' => 2, '2' => 3]],
43+
[[10, '20', 30.5]],
44+
];
45+
}
46+
47+
public function dataProviderForIsSequentialFalse(): array
48+
{
49+
return [
50+
[['a' => 1]],
51+
[[1 => 1]],
52+
[[0 => 1, 1 => 2, 3 => 3]],
53+
[[0 => 1, 1 => 2, 2 => 3, 'test' => 123]],
54+
[[1 => 2, 2 => 3]],
55+
[[1 => 2, 3 => 3, 'a' => 1000]],
56+
];
57+
}
58+
}

0 commit comments

Comments
 (0)