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()
|
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;
|
$object = new Post1;
|
||||||
$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
|
$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
|
||||||
$object = new Post2;
|
$object = new Post2;
|
||||||
@ -187,6 +191,11 @@ class ArrayHelperTest extends TestCase
|
|||||||
|
|
||||||
public function testMultisort()
|
public function testMultisort()
|
||||||
{
|
{
|
||||||
|
// empty key
|
||||||
|
$dataEmpty = [];
|
||||||
|
ArrayHelper::multisort($dataEmpty, '');
|
||||||
|
$this->assertEquals([], $dataEmpty);
|
||||||
|
|
||||||
// single key
|
// single key
|
||||||
$array = [
|
$array = [
|
||||||
['name' => 'b', 'age' => 3],
|
['name' => 'b', 'age' => 3],
|
||||||
@ -327,6 +336,24 @@ class ArrayHelperTest extends TestCase
|
|||||||
'- Enh: test3',
|
'- Enh: test3',
|
||||||
], $changelog);
|
], $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()
|
public function testMerge()
|
||||||
{
|
{
|
||||||
@ -358,6 +385,7 @@ class ArrayHelperTest extends TestCase
|
|||||||
'features' => [
|
'features' => [
|
||||||
'debug',
|
'debug',
|
||||||
],
|
],
|
||||||
|
'foo',
|
||||||
];
|
];
|
||||||
|
|
||||||
$result = ArrayHelper::merge($a, $b, $c);
|
$result = ArrayHelper::merge($a, $b, $c);
|
||||||
@ -373,6 +401,7 @@ class ArrayHelperTest extends TestCase
|
|||||||
'gii',
|
'gii',
|
||||||
'debug',
|
'debug',
|
||||||
],
|
],
|
||||||
|
'foo',
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
@ -769,6 +798,7 @@ class ArrayHelperTest extends TestCase
|
|||||||
$this->assertFalse(ArrayHelper::isAssociative('test'));
|
$this->assertFalse(ArrayHelper::isAssociative('test'));
|
||||||
$this->assertFalse(ArrayHelper::isAssociative([]));
|
$this->assertFalse(ArrayHelper::isAssociative([]));
|
||||||
$this->assertFalse(ArrayHelper::isAssociative([1, 2, 3]));
|
$this->assertFalse(ArrayHelper::isAssociative([1, 2, 3]));
|
||||||
|
$this->assertFalse(ArrayHelper::isAssociative([1], false));
|
||||||
$this->assertTrue(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test']));
|
$this->assertTrue(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test']));
|
||||||
$this->assertFalse(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test', 3]));
|
$this->assertFalse(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test', 3]));
|
||||||
$this->assertTrue(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test', 3], false));
|
$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([1, 2, 3]));
|
||||||
$this->assertTrue(ArrayHelper::isIndexed([2 => 'a', 3 => 'b']));
|
$this->assertTrue(ArrayHelper::isIndexed([2 => 'a', 3 => 'b']));
|
||||||
$this->assertFalse(ArrayHelper::isIndexed([2 => 'a', 3 => 'b'], true));
|
$this->assertFalse(ArrayHelper::isIndexed([2 => 'a', 3 => 'b'], true));
|
||||||
|
$this->assertFalse(ArrayHelper::isIndexed(['a' => 'b'], false));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHtmlEncode()
|
public function testHtmlEncode()
|
||||||
@ -961,5 +992,6 @@ class ArrayHelperTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
$this->assertEquals(ArrayHelper::filter($array, ['X']), []);
|
$this->assertEquals(ArrayHelper::filter($array, ['X']), []);
|
||||||
$this->assertEquals(ArrayHelper::filter($array, ['X.Y']), []);
|
$this->assertEquals(ArrayHelper::filter($array, ['X.Y']), []);
|
||||||
|
$this->assertEquals(ArrayHelper::filter($array, ['A.X']), []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user