提交的内容
This commit is contained in:
46
vendor/topthink/think-helper/tests/ArrTest.php
vendored
Executable file → Normal file
46
vendor/topthink/think-helper/tests/ArrTest.php
vendored
Executable file → Normal file
@ -55,7 +55,7 @@ class ArrTest extends TestCase
|
||||
|
||||
public function testDivide()
|
||||
{
|
||||
list($keys, $values) = Arr::divide(['name' => 'ThinkPHP']);
|
||||
[$keys, $values] = Arr::divide(['name' => 'ThinkPHP']);
|
||||
$this->assertSame(['name'], $keys);
|
||||
$this->assertSame(['ThinkPHP'], $values);
|
||||
}
|
||||
@ -109,7 +109,7 @@ class ArrTest extends TestCase
|
||||
public function testLast()
|
||||
{
|
||||
$array = [100, 200, 300];
|
||||
$last = Arr::last($array, function ($value) {
|
||||
$last = Arr::last($array, function ($value) {
|
||||
return $value < 250;
|
||||
});
|
||||
$this->assertSame(200, $last);
|
||||
@ -234,17 +234,17 @@ class ArrTest extends TestCase
|
||||
public function testPull()
|
||||
{
|
||||
$array = ['name' => 'ThinkPHP', 'price' => 100];
|
||||
$name = Arr::pull($array, 'name');
|
||||
$name = Arr::pull($array, 'name');
|
||||
$this->assertSame('ThinkPHP', $name);
|
||||
$this->assertSame(['price' => 100], $array);
|
||||
// Only works on first level keys
|
||||
$array = ['i@example.com' => 'Joe', 'jack@localhost' => 'Jane'];
|
||||
$name = Arr::pull($array, 'i@example.com');
|
||||
$name = Arr::pull($array, 'i@example.com');
|
||||
$this->assertSame('Joe', $name);
|
||||
$this->assertSame(['jack@localhost' => 'Jane'], $array);
|
||||
// Does not work for nested keys
|
||||
$array = ['emails' => ['i@example.com' => 'Joe', 'jack@localhost' => 'Jane']];
|
||||
$name = Arr::pull($array, 'emails.i@example.com');
|
||||
$name = Arr::pull($array, 'emails.i@example.com');
|
||||
$this->assertNull($name);
|
||||
$this->assertSame(['emails' => ['i@example.com' => 'Joe', 'jack@localhost' => 'Jane']], $array);
|
||||
}
|
||||
@ -331,12 +331,42 @@ class ArrTest extends TestCase
|
||||
|
||||
public function testWrap()
|
||||
{
|
||||
$string = 'a';
|
||||
$array = ['a'];
|
||||
$object = new stdClass();
|
||||
$string = 'a';
|
||||
$array = ['a'];
|
||||
$object = new stdClass();
|
||||
$object->value = 'a';
|
||||
$this->assertSame(['a'], Arr::wrap($string));
|
||||
$this->assertSame($array, Arr::wrap($array));
|
||||
$this->assertSame([$object], Arr::wrap($object));
|
||||
}
|
||||
|
||||
public function testMergeDeep()
|
||||
{
|
||||
$this->assertSame(
|
||||
[
|
||||
'a' => [
|
||||
'c' => [2],
|
||||
'e' => 5,
|
||||
'f' => 4,
|
||||
],
|
||||
'x' => 3,
|
||||
],
|
||||
Arr::mergeDeep(
|
||||
[
|
||||
'a' => [
|
||||
'c' => [1],
|
||||
'e' => 5,
|
||||
],
|
||||
'x' => 4,
|
||||
],
|
||||
[
|
||||
'a' => [
|
||||
'c' => [2],
|
||||
'f' => 4,
|
||||
],
|
||||
'x' => 3,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
0
vendor/topthink/think-helper/tests/CollectionTest.php
vendored
Executable file → Normal file
0
vendor/topthink/think-helper/tests/CollectionTest.php
vendored
Executable file → Normal file
48
vendor/topthink/think-helper/tests/IsAssocTest.php
vendored
Normal file
48
vendor/topthink/think-helper/tests/IsAssocTest.php
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use think\helper\Arr;
|
||||
|
||||
class IsAssocTest extends TestCase
|
||||
{
|
||||
public function testEmptyArray()
|
||||
{
|
||||
// 空数组不是关联数组
|
||||
$this->assertFalse(Arr::isAssoc([]));
|
||||
}
|
||||
|
||||
public function testSequentialArray()
|
||||
{
|
||||
// 顺序索引数组不是关联数组
|
||||
$this->assertFalse(Arr::isAssoc([1, 2, 3]));
|
||||
$this->assertFalse(Arr::isAssoc(['a', 'b', 'c']));
|
||||
$this->assertFalse(Arr::isAssoc([null, false, true]));
|
||||
}
|
||||
|
||||
public function testNonSequentialArray()
|
||||
{
|
||||
// 非顺序索引数组是关联数组
|
||||
$this->assertTrue(Arr::isAssoc([1 => 'a', 0 => 'b'])); // 键顺序不是0,1
|
||||
$this->assertTrue(Arr::isAssoc([1 => 'a', 2 => 'b'])); // 不是从0开始
|
||||
$this->assertTrue(Arr::isAssoc([0 => 'a', 2 => 'b'])); // 不连续
|
||||
}
|
||||
|
||||
public function testStringKeys()
|
||||
{
|
||||
// 字符串键的数组是关联数组
|
||||
$this->assertTrue(Arr::isAssoc(['a' => 1, 'b' => 2]));
|
||||
// 注意:PHP会将字符串数字键'0'、'1'自动转换为整数键0、1
|
||||
// 所以这个实际上是顺序索引数组,不是关联数组
|
||||
$this->assertFalse(Arr::isAssoc(['0' => 'a', '1' => 'b']));
|
||||
$this->assertTrue(Arr::isAssoc(['a' => 'a', 0 => 'b'])); // 混合键
|
||||
}
|
||||
|
||||
public function testMixedKeys()
|
||||
{
|
||||
// 混合键类型的数组是关联数组
|
||||
$this->assertTrue(Arr::isAssoc([0 => 'a', 'b' => 'b']));
|
||||
$this->assertTrue(Arr::isAssoc(['a' => 1, 2 => 'b']));
|
||||
}
|
||||
|
||||
}
|
||||
69
vendor/topthink/think-helper/tests/MergeDeepTest.php
vendored
Normal file
69
vendor/topthink/think-helper/tests/MergeDeepTest.php
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use think\helper\Arr;
|
||||
|
||||
class MergeDeepTest extends TestCase
|
||||
{
|
||||
public function testMergeDeepWithAssociativeArrays()
|
||||
{
|
||||
// 测试关联数组的递归合并
|
||||
$array1 = ['a' => ['b' => 2], 'c' => 3];
|
||||
$array2 = ['a' => ['b' => 4, 'd' => 5], 'e' => 6];
|
||||
|
||||
$result = Arr::mergeDeep($array1, $array2);
|
||||
|
||||
$expected = [
|
||||
'a' => ['b' => 4, 'd' => 5],
|
||||
'c' => 3,
|
||||
'e' => 6
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testMergeDeepWithIndexedArrays()
|
||||
{
|
||||
// 测试索引数组的覆盖
|
||||
$array1 = ['a' => [1, 2, 3], 'b' => 2];
|
||||
$array2 = ['a' => [4, 5], 'c' => 3];
|
||||
|
||||
$result = Arr::mergeDeep($array1, $array2);
|
||||
|
||||
$expected = [
|
||||
'a' => [4, 5], // 索引数组应该被完全覆盖
|
||||
'b' => 2,
|
||||
'c' => 3
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testMergeDeepWithMixedArrays()
|
||||
{
|
||||
// 测试混合数组类型
|
||||
$array1 = [
|
||||
'a' => ['b' => 2, 'c' => 3],
|
||||
'd' => [1, 2, 3],
|
||||
'e' => 4
|
||||
];
|
||||
|
||||
$array2 = [
|
||||
'a' => ['b' => 5, 'f' => 6],
|
||||
'd' => [7, 8],
|
||||
'g' => 9
|
||||
];
|
||||
|
||||
$result = Arr::mergeDeep($array1, $array2);
|
||||
|
||||
$expected = [
|
||||
'a' => ['b' => 5, 'c' => 3, 'f' => 6], // 关联数组递归合并
|
||||
'd' => [7, 8], // 索引数组被覆盖
|
||||
'e' => 4,
|
||||
'g' => 9
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
0
vendor/topthink/think-helper/tests/StrTest.php
vendored
Executable file → Normal file
0
vendor/topthink/think-helper/tests/StrTest.php
vendored
Executable file → Normal file
0
vendor/topthink/think-helper/tests/TestCase.php
vendored
Executable file → Normal file
0
vendor/topthink/think-helper/tests/TestCase.php
vendored
Executable file → Normal file
Reference in New Issue
Block a user