Files
2025-05-12 15:45:02 +08:00

49 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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']));
}
}