mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-15 06:40:59 +08:00
Add test coverage of yii\helpers\BaseArrayHelper (#14205)
This commit is contained in:

committed by
Alexander Makarov

parent
ac70262bfc
commit
21a7a68e45
@ -51,6 +51,10 @@ class ArrayHelperTest extends TestCase
|
||||
|
||||
public function testToArray()
|
||||
{
|
||||
$dataArrayable = $this->getMock('yii\\base\\Arrayable');
|
||||
$dataArrayable->method('toArray')->willReturn([]);
|
||||
$this->assertEquals([], ArrayHelper::toArray($dataArrayable));
|
||||
$this->assertEquals(['foo'], ArrayHelper::toArray('foo'));
|
||||
$object = new Post1;
|
||||
$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
|
||||
$object = new Post2;
|
||||
@ -187,6 +191,11 @@ class ArrayHelperTest extends TestCase
|
||||
|
||||
public function testMultisort()
|
||||
{
|
||||
// empty key
|
||||
$dataEmpty = [];
|
||||
ArrayHelper::multisort($dataEmpty, '');
|
||||
$this->assertEquals([], $dataEmpty);
|
||||
|
||||
// single key
|
||||
$array = [
|
||||
['name' => 'b', 'age' => 3],
|
||||
@ -327,6 +336,24 @@ class ArrayHelperTest extends TestCase
|
||||
'- Enh: test3',
|
||||
], $changelog);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \yii\base\InvalidParamException
|
||||
*/
|
||||
public function testMultisortInvalidParamExceptionDirection()
|
||||
{
|
||||
$data = ['foo' => 'bar'];
|
||||
ArrayHelper::multisort($data, ['foo'], []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \yii\base\InvalidParamException
|
||||
*/
|
||||
public function testMultisortInvalidParamExceptionSortFlag()
|
||||
{
|
||||
$data = ['foo' => 'bar'];
|
||||
ArrayHelper::multisort($data, ['foo'], ['foo'], []);
|
||||
}
|
||||
|
||||
public function testMerge()
|
||||
{
|
||||
@ -358,6 +385,7 @@ class ArrayHelperTest extends TestCase
|
||||
'features' => [
|
||||
'debug',
|
||||
],
|
||||
'foo',
|
||||
];
|
||||
|
||||
$result = ArrayHelper::merge($a, $b, $c);
|
||||
@ -373,6 +401,7 @@ class ArrayHelperTest extends TestCase
|
||||
'gii',
|
||||
'debug',
|
||||
],
|
||||
'foo',
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
@ -769,6 +798,7 @@ class ArrayHelperTest extends TestCase
|
||||
$this->assertFalse(ArrayHelper::isAssociative('test'));
|
||||
$this->assertFalse(ArrayHelper::isAssociative([]));
|
||||
$this->assertFalse(ArrayHelper::isAssociative([1, 2, 3]));
|
||||
$this->assertFalse(ArrayHelper::isAssociative([1], false));
|
||||
$this->assertTrue(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test']));
|
||||
$this->assertFalse(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test', 3]));
|
||||
$this->assertTrue(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test', 3], false));
|
||||
@ -781,6 +811,7 @@ class ArrayHelperTest extends TestCase
|
||||
$this->assertTrue(ArrayHelper::isIndexed([1, 2, 3]));
|
||||
$this->assertTrue(ArrayHelper::isIndexed([2 => 'a', 3 => 'b']));
|
||||
$this->assertFalse(ArrayHelper::isIndexed([2 => 'a', 3 => 'b'], true));
|
||||
$this->assertFalse(ArrayHelper::isIndexed(['a' => 'b'], false));
|
||||
}
|
||||
|
||||
public function testHtmlEncode()
|
||||
@ -961,5 +992,6 @@ class ArrayHelperTest extends TestCase
|
||||
]);
|
||||
$this->assertEquals(ArrayHelper::filter($array, ['X']), []);
|
||||
$this->assertEquals(ArrayHelper::filter($array, ['X.Y']), []);
|
||||
$this->assertEquals(ArrayHelper::filter($array, ['A.X']), []);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user