Skip to content

Commit e103040

Browse files
committed
add some tests for array helper
1 parent eabe6c6 commit e103040

File tree

2 files changed

+70
-28
lines changed

2 files changed

+70
-28
lines changed

src/Arr/ArrayHelper.php

+28-28
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static function valueToUpper(array $arr): array
176176
* 将数组中的值全部转为大写或小写
177177
*
178178
* @param array|iterable $arr
179-
* @param bool $toUpper
179+
* @param bool $toUpper
180180
*
181181
* @return array
182182
*/
@@ -242,9 +242,9 @@ public static function valueExistsOne($check, array $sampleArr): bool
242242
* ******* 不区分大小写,检查 一个或多个值是否 全存在数组中 *******
243243
* 有一个不存在即返回 false
244244
*
245-
* @param string|array $need
246-
* @param array $arr 只能检查一维数组
247-
* @param bool $type 是否同时验证类型
245+
* @param string|array $need
246+
* @param array|iterable $arr 只能检查一维数组
247+
* @param bool $type 是否同时验证类型
248248
*
249249
* @return bool | string 不存在的会返回 检查到的 字段,判断时 请使用 ArrHelper::existsAll($need,$arr)===true 来验证是否全存在
250250
*/
@@ -273,9 +273,9 @@ public static function existsAll($need, $arr, bool $type = false)
273273
* ******* 不区分大小写,检查 一个或多个值是否存在数组中 *******
274274
* 有一个存在就返回 true 都不存在 return false
275275
*
276-
* @param string|array $need
276+
* @param string|array $need
277277
* @param array|iterable $arr 只能检查一维数组
278-
* @param bool $type 是否同时验证类型
278+
* @param bool $type 是否同时验证类型
279279
*
280280
* @return bool
281281
*/
@@ -327,7 +327,7 @@ public static function getKeyMaxWidth(array $data, bool $excludeInt = true): int
327327
foreach ($data as $key => $value) {
328328
// key is not a integer
329329
if (!$excludeInt || !is_numeric($key)) {
330-
$width = mb_strlen($key, 'UTF-8');
330+
$width = mb_strlen((string)$key, 'UTF-8');
331331
$maxWidth = $width > $maxWidth ? $width : $maxWidth;
332332
}
333333
}
@@ -356,7 +356,7 @@ public static function getMaxWidth(array $keys, bool $excludeInt = true): int
356356
foreach ($keys as $key) {
357357
// key is not a integer
358358
if (!$excludeInt || !is_numeric($key)) {
359-
$keyWidth = mb_strlen($key, 'UTF-8');
359+
$keyWidth = mb_strlen((string)$key, 'UTF-8');
360360
$maxWidth = $keyWidth > $maxWidth ? $keyWidth : $maxWidth;
361361
}
362362
}
@@ -617,8 +617,8 @@ public static function prepend(array $array, $value, $key = null): array
617617
* remove the $key of the $arr, and return value.
618618
*
619619
* @param string|int $key
620-
* @param array $arr
621-
* @param mixed $default
620+
* @param array $arr
621+
* @param mixed $default
622622
*
623623
* @return mixed
624624
*/
@@ -637,9 +637,9 @@ public static function remove(array &$arr, $key, $default = null)
637637
/**
638638
* Get a value from the array, and remove it.
639639
*
640-
* @param array|ArrayAccess $array
641-
* @param string|int $key
642-
* @param mixed $default
640+
* @param array|ArrayAccess $array
641+
* @param string|int $key
642+
* @param mixed $default
643643
*
644644
* @return mixed
645645
*/
@@ -711,13 +711,13 @@ public static function wrap($value): array
711711
/**
712712
* array 递归 转换成 字符串
713713
*
714-
* @param array $array
715-
* @param int $length
716-
* @param int $cycles 至多循环六次 $num >= 6
717-
* @param bool $showKey
718-
* @param bool $addMark
719-
* @param string $separator
720-
* @param string $string
714+
* @param array $array
715+
* @param int $length
716+
* @param int $cycles 至多循环六次 $num >= 6
717+
* @param bool $showKey
718+
* @param bool $addMark
719+
* @param string $separator
720+
* @param string $string
721721
*
722722
* @return string
723723
*/
@@ -749,14 +749,14 @@ public static function toString(
749749

750750
if (is_array($value)) {
751751
$string .= $keyStr . 'Array(' . self::toString(
752-
$value,
753-
$length,
754-
$cycles,
755-
$showKey,
756-
$addMark,
757-
$separator,
758-
$string
759-
) . ')' . $separator;
752+
$value,
753+
$length,
754+
$cycles,
755+
$showKey,
756+
$addMark,
757+
$separator,
758+
$string
759+
) . ')' . $separator;
760760
} elseif (is_object($value)) {
761761
$string .= $keyStr . 'Object(' . get_class($value) . ')' . $separator;
762762
} elseif (is_resource($value)) {

test/Arr/ArrHelperTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest\Arr;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Toolkit\Stdlib\Arr;
7+
use function array_keys;
8+
9+
/**
10+
* class ArrHelperTest
11+
*/
12+
class ArrHelperTest extends TestCase
13+
{
14+
public function testGetKeyMaxWidth(): void
15+
{
16+
$data = [
17+
'key1' => 'value1',
18+
'key2-test' => 'value2',
19+
];
20+
21+
$this->assertSame(9, Arr::getKeyMaxWidth($data));
22+
$this->assertSame(9, Arr::getKeyMaxWidth($data, false));
23+
24+
$data = [
25+
'key1' => 'value1',
26+
'34430' => 'value2'
27+
];
28+
29+
$this->assertSame(4, Arr::getKeyMaxWidth($data));
30+
$this->assertSame(5, Arr::getKeyMaxWidth($data, false));
31+
$this->assertSame(5, Arr::getMaxWidth(array_keys($data), false));
32+
33+
$data = [
34+
'key1' => 'value1',
35+
34430 => 'value2'
36+
];
37+
38+
$this->assertSame(4, Arr::getKeyMaxWidth($data));
39+
$this->assertSame(5, Arr::getKeyMaxWidth($data, false));
40+
$this->assertSame(5, Arr::getMaxWidth(array_keys($data), false));
41+
}
42+
}

0 commit comments

Comments
 (0)