|
1
|
|
|
<?php |
|
2
|
|
|
namespace Bedd\Common; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* ArrayUtilsTest |
|
6
|
|
|
*/ |
|
7
|
|
|
class ArrayUtilsTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Test for Bedd\Common\ArrayUtils::column |
|
11
|
|
|
*/ |
|
12
|
|
|
public function testColumn() |
|
13
|
|
|
{ |
|
14
|
|
|
$array = [ |
|
15
|
|
|
['id'=>1, 'name'=>'Sven'], |
|
16
|
|
|
['id'=>2, 'name'=>'Hans'], |
|
17
|
|
|
['id'=>5, 'name'=>'Peter'], |
|
18
|
|
|
]; |
|
19
|
|
|
$this->assertEquals(['Sven', 'Hans', 'Peter'], ArrayUtils::column($array, 'name')); |
|
20
|
|
|
$this->assertEquals([1, 2, 5], ArrayUtils::column($array, 'id')); |
|
21
|
|
|
$this->assertEquals([1=>'Sven', 2=>'Hans', 5=>'Peter'], ArrayUtils::column($array, 'name', 'id')); |
|
22
|
|
|
$this->assertEquals(count(ArrayUtils::column($array, 'id')), 3); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Test for Bedd\Common\ArrayUtils::hasStringKeys |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testHasStringKeys() |
|
29
|
|
|
{ |
|
30
|
|
|
$array1 = ['a'=>true,2=>true]; |
|
31
|
|
|
$array2 = ['test', 'test', 'test']; |
|
32
|
|
|
$this->assertEquals(true, ArrayUtils::hasStringKeys($array1, false, false)); |
|
33
|
|
|
$this->assertEquals(false, ArrayUtils::hasStringKeys($array1, true, false)); |
|
34
|
|
|
$this->assertEquals(false, ArrayUtils::hasStringKeys($array2)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Test for Bedd\Common\ArrayUtils::hasStringKeys |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testHasIntegerKeys() |
|
41
|
|
|
{ |
|
42
|
|
|
$array1 = ['a'=>true,2=>true]; |
|
43
|
|
|
$array2 = ['test', 'test', 'test']; |
|
44
|
|
|
$this->assertEquals(true, ArrayUtils::hasIntegerKeys($array1, false)); |
|
45
|
|
|
$this->assertEquals(false, ArrayUtils::hasIntegerKeys($array1, true)); |
|
46
|
|
|
$this->assertEquals(true, ArrayUtils::hasIntegerKeys($array2)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Test for Bedd\Common\ArrayUtils::findValueByKeys |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testFindValueByKeys() |
|
53
|
|
|
{ |
|
54
|
|
|
$arr1 = ['name' => 'Sven', 'Name' => 'Max']; |
|
55
|
|
|
$this->assertEquals(null, ArrayUtils::findValueByKeys($arr1, ['test', 'test2'])); |
|
56
|
|
|
$this->assertEquals('Max', ArrayUtils::findValueByKeys($arr1, ['test', 'Name'])); |
|
57
|
|
|
$this->assertEquals('Karl', ArrayUtils::findValueByKeys($arr1, ['test', 'test2'], 'Karl')); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Test for Bedd\Common\ArrayUtils::flatten |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testFlatten() |
|
64
|
|
|
{ |
|
65
|
|
|
$arr1 = ['name' => 'Sven', 'Name' => 'Max']; |
|
66
|
|
|
$arr2 = ['a', [['b']],['c'],[[[['d']]]]]; |
|
67
|
|
|
$this->assertEquals($arr1, ArrayUtils::flatten($arr1, true)); |
|
68
|
|
|
$this->assertEquals(['Sven', 'Max'], ArrayUtils::flatten($arr1, false)); |
|
69
|
|
|
$this->assertEquals(['a', 'b', 'c', 'd'], ArrayUtils::flatten($arr2)); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|