ArrayUtilsTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 116
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B hasTypeKeysProvider() 0 29 1
A testHasStringKeys() 0 4 1
A testHasIntegerKeys() 0 4 1
A findValueByKeysProvider() 0 18 1
A testFindValueByKeys() 0 4 1
A flattenProvider() 0 12 1
A testFlatten() 0 4 1
A renameKeyProvider() 0 11 1
A testRenameKey() 0 4 1
1
<?php
2
namespace Bedd\Common;
3
4
/**
5
 * ArrayUtilsTest
6
 */
7
class ArrayUtilsTest extends TestCase
8
{
9
    public function hasTypeKeysProvider()
10
    {
11
        $arr_keys_mixed = ['a'=>3, 1=>'Test', '_foo'=>'bar'];
12
        $arr_keys_string = ['a'=>'a', 'b'=>'b', 'c'=>'c'];
13
        $arr_keys_int = ['a', 'b', 'c'];
14
        return [
15
            //$input, $only, $allow_empty, $exprected_string, $expected_int
16
            //only strings
17
            [$arr_keys_string, false, false, true, false],
18
            [$arr_keys_string, true, false, true, false],
19
            [$arr_keys_string, true, true, true, false],
20
            [$arr_keys_string, false, true, true, false],
21
            //only ints
22
            [$arr_keys_int, false, false, false, true],
23
            [$arr_keys_int, true, false, false, true],
24
            [$arr_keys_int, true, true, false, true],
25
            [$arr_keys_int, false, true, false, true],
26
            //mixed
27
            [$arr_keys_mixed, false, false, true, true],
28
            [$arr_keys_mixed, true, false, false, false],
29
            [$arr_keys_mixed, true, true, false, false],
30
            [$arr_keys_mixed, false, true, true, true],
31
            //empty
32
            [[], false, false, false, false],
33
            [[], true, false, false, false],
34
            [[], true, true, true, true],
35
            [[], false, true, true, true],
36
        ];
37
    }
38
    /**
39
     * Test for Bedd\Common\ArrayUtils::hasStringKeys
40
     * @dataProvider hasTypeKeysProvider
41
     */
42
    public function testHasStringKeys($input, $only, $allow_empty, $exprected_string)
43
    {
44
        $this->assertEquals($exprected_string, ArrayUtils::hasStringKeys($input, $only, $allow_empty));
45
    }
46
    /**
47
     * Test for Bedd\Common\ArrayUtils::hasStringKeys
48
     * @dataProvider hasTypeKeysProvider
49
     */
50
    public function testHasIntegerKeys($input, $only, $allow_empty, $exprected_string, $exprected_int)
51
    {
52
        $this->assertEquals($exprected_int, ArrayUtils::hasIntegerKeys($input, $only, $allow_empty));
53
    }
54
55
    public function findValueByKeysProvider()
56
    {
57
        $input = ['name'=>'Sven', 2 => 'Alter', 'foo'=>'bar', 'FOO'=>'BAR'];
58
        return [
59
            //$input, $keys, $default, $expected
60
            [$input, [], null, null],
61
            [$input, [], false, false],
62
            [$input, [], 'Wurst', 'Wurst'],
63
            [$input, ['name'], null, 'Sven'],
64
            [$input, ['name', 'foo'], null, 'Sven'],
65
            [$input, ['Name', 'foo'], null, 'bar'],
66
            [$input, ['Name', 'foo'], 'Hans', 'bar'],
67
            [$input, ['foo', 'name'], null, 'bar'],
68
            [$input, ['Foo', 2], null, 'Alter'],
69
            [$input, ['foo', 2], null, 'bar'],
70
            [$input, ['Foo', 'FOO'], null, 'BAR'],
71
        ];
72
    }
73
    /**
74
     * Test for Bedd\Common\ArrayUtils::findValueByKeys
75
     * @dataProvider findValueByKeysProvider
76
     */
77
    public function testFindValueByKeys(array $input, array $keys, $default, $exprected)
78
    {
79
        $this->assertEquals($exprected, ArrayUtils::findValueByKeys($input, $keys, $default));
80
    }
81
82
    public function flattenProvider()
83
    {
84
        return [
85
            //$input, $preserve_keys, $expected
86
            [[], true, []],
87
            [[], false, []],
88
            [['name' => 'Sven', 'Name' => 'Hans', 'Wurst'], false, ['Sven', 'Hans', 'Wurst']],
89
            [['name' => 'Sven', 'Name' => 'Hans', 'Wurst'], true, ['name' => 'Sven', 'Name' => 'Hans', 'Wurst']],
90
            [['a', [['b']],['c'],[[[['d']]]]], false, ['a', 'b', 'c', 'd']],
91
            [['a', [['b']],['c'],[[[['d']]]], 'd'], false, ['a', 'b', 'c', 'd', 'd']],
92
        ];
93
    }
94
    /**
95
     * Test for Bedd\Common\ArrayUtils::flatten
96
     * @dataProvider flattenProvider
97
     */
98
    public function testFlatten($input, $preserve_keys, $expected)
99
    {
100
        $this->assertEquals($expected, ArrayUtils::flatten($input, $preserve_keys));
101
    }
102
    
103
    public function renameKeyProvider()
104
    {
105
        return [
106
            //$input, $old_key, $new_key, $expected
107
            [['name'=>'Sven'], 'test', 'test', ['name'=>'Sven']],
108
            [['name'=>'Sven'], 'test', 'test2', ['name'=>'Sven']],
109
            [['name'=>'Sven'], 'name', 'Name', ['Name'=>'Sven']],
110
            [['name'=>'Sven', 'alter' => 20], 'alter', 'age', ['name'=>'Sven', 'age' => 20]],
111
            [['name'=>'Sven', 'alter' => 20, 'test'=>'Test'], 'alter', 'age', ['name'=>'Sven', 'age' => 20, 'test'=>'Test']],
112
        ];
113
    }
114
    /**
115
     * Test for Bedd\Common\ArrayUtils::renameKey
116
     * @dataProvider renameKeyProvider
117
     */
118
    public function testRenameKey($input, $old_key, $new_key, $expected)
119
    {
120
        $this->assertEquals($expected, ArrayUtils::renameKey($input, $old_key, $new_key));
121
    }
122
}
123