OnArrayTest.php 9.57 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/12/16
 * Time: 9:28 AM
 */

9
namespace qfq;
10
11
12
13
14

require_once(__DIR__ . '/../../qfq/helper/OnArray.php');


class OnArrayTest extends \PHPUnit_Framework_TestCase {
15

16
17
    public function testSortByKey() {
        $unsorted = [
Carsten  Rose's avatar
Carsten Rose committed
18
19
20
21
            'a' => 'z',
            '1' => 'y',
            'Aa' => 'y0',
            'zZ' => 'X9',
Carsten  Rose's avatar
Carsten Rose committed
22
            '0000' => 'h',
23
24
25
26
        ];

        $sorted = [
            '0000' => 'h',
Carsten  Rose's avatar
Carsten Rose committed
27
            '1' => 'y',
28
            'Aa' => 'y0',
Carsten  Rose's avatar
Carsten Rose committed
29
            'a' => 'z',
Carsten  Rose's avatar
Carsten Rose committed
30
            'zZ' => 'X9',
31
32
33
        ];

        $expected = $unsorted;
34
        OnArray::sortKey($expected);
35
36
37
        $this->assertEquals($sorted, $expected);

        $expected = array();
38
        OnArray::sortKey($expected);
39
40
41
        $this->assertEquals(array(), $expected);

    }
42
43
44
45
46

    public function testTrimArray() {
        $raw = ['hello', '"next"', '"without trailing', 'without leading"', ' with whitespace ', '" with tick and whitespace "', ''];
        $expected = ['hello', 'next', 'without trailing', 'without leading', ' with whitespace ', ' with tick and whitespace ', ''];

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        $this->assertEquals($expected, OnArray::trimArray($raw, '"'));
    }

    public function testRemoveEmptyElementsFromArray() {
        $this->assertEquals(array(), OnArray::removeEmptyElementsFromArray(array()));

        $expected = array('Hello world');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($expected));

        $expected = array('Hello world', 'Blue sky');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($expected));

        $raw2 = array('Hello world', '', 'Blue sky');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($raw2));

        $raw2 = array('Hello world', 'Blue sky', '');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($raw2));

        $raw2 = array('', 'Hello world', 'Blue sky');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($raw2));


        $expected = array('first' => 'Hello world');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($expected));

        $expected = array('first' => 'Hello world', 'second' => 'Blue sky');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($expected));

        $raw2 = array('first' => 'Hello world', 'second' => '', 'third' => 'Blue sky');
        $expected = array('first' => 'Hello world', 'third' => 'Blue sky');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($raw2));

        $raw2 = array('first' => 'Hello world', 'second' => 'Blue sky', 'third' => '');
        $expected = array('first' => 'Hello world', 'second' => 'Blue sky');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($raw2));

        $raw2 = array('first' => '', 'second' => 'Hello world', 'third' => 'Blue sky');
        $expected = array('second' => 'Hello world', 'third' => 'Blue sky');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($raw2));


        $raw2 = array('first' => '', '' => 'Hello world', 'third' => 'Blue sky');
        $expected = array('' => 'Hello world', 'third' => 'Blue sky');
        $this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($raw2));

92
    }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

    public function testCopyValuesIfNotAlreadyExist() {

        $this->assertEquals(array(), OnArray::copyArrayItemsIfNotAlreadyExist(array(), array(), array()));

        $this->assertEquals(array(), OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], array(), array()));

        $this->assertEquals(array(), OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], array(), ['b']));

        $this->assertEquals(['a' => 'base'], OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], ['a' => 'base'], ['unknown']));

        $this->assertEquals(['b' => 'base', 'a' => 'something'], OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], ['b' => 'base'], ['a']));

        $this->assertEquals(['a' => 'base'], OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], ['a' => 'base'], ['a']));

    }

    public function testGetArrayItems() {
        $this->assertEquals(array(), OnArray::getArrayItems(array(), array()));
        $this->assertEquals(array(), OnArray::getArrayItems(['a' => 'hello'], array()));
        $this->assertEquals(array(), OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], array()));
        $this->assertEquals(array(), OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['c']));
        $this->assertEquals(['a' => 'hello'], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['a']));
        $this->assertEquals(['a' => 'hello', 'b' => 'world'], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['a', 'b']));

        $this->assertEquals(array(), OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['c'], false));
        $this->assertEquals(['c' => ''], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['c'], true));

        $this->assertEquals(['a' => 'hello'], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['a', 'c'], false));
        $this->assertEquals(['a' => 'hello', 'c' => ''], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['a', 'c'], true));
Carsten  Rose's avatar
Carsten Rose committed
123
124
125
126
127
128
129
130
131
132
133

        $this->assertEquals(['a' => 'hello', 'b' => '0'], OnArray::getArrayItems(['a' => 'hello', 'b' => '0'], ['a', 'b'], false));
        $this->assertEquals(['a' => 'hello', 'b' => '0'], OnArray::getArrayItems(['a' => 'hello', 'b' => 0], ['a', 'b'], false));

        $this->assertEquals(['a' => 'hello'], OnArray::getArrayItems(['a' => 'hello', 'b' => '0'], ['a', 'b'], false, true));
        $this->assertEquals(['a' => 'hello'], OnArray::getArrayItems(['a' => 'hello', 'b' => 0], ['a', 'b'], false, true));

        $this->assertEquals(['a' => 'hello', 'b' => ''], OnArray::getArrayItems(['a' => 'hello', 'b' => ''], ['a', 'b'], false, false, false));
        $this->assertEquals(['a' => 'hello'], OnArray::getArrayItems(['a' => 'hello', 'b' => ''], ['a', 'b'], false, false, true));


134
    }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

    public function testArrayValueReplace() {
        $this->assertEquals(array(), OnArray::arrayValueReplace(array(), '', ''));
        $this->assertEquals(array(), OnArray::arrayValueReplace(array(), 'a', ''));
        $this->assertEquals(array(), OnArray::arrayValueReplace(array(), '', 'b'));
        $this->assertEquals(array(), OnArray::arrayValueReplace(array(), 'a', 'b'));

        $src = ['fruit1' => 'apple', 'fruit2' => 'cherry', 'fruit3' => 'banana'];
        $this->assertEquals($src, OnArray::arrayValueReplace($src, '', ''));
        $this->assertEquals($src, OnArray::arrayValueReplace($src, 'Z', ''));
        $this->assertEquals($src, OnArray::arrayValueReplace($src, 'Z', 'Y'));

        $expected = ['fruit1' => 'Apple', 'fruit2' => 'cherry', 'fruit3' => 'bAnAnA'];
        $this->assertEquals($expected, OnArray::arrayValueReplace($src, 'a', 'A'));
    }
Carsten  Rose's avatar
Carsten Rose committed
150
151
152
153
154

    public function testGetArrayItemKeyNameStartWith() {
        $this->assertEquals(array(), OnArray::getArrayItemKeyNameStartWith(array(), ''));
        $this->assertEquals(['a' => 'hello'], OnArray::getArrayItemKeyNameStartWith(['a' => 'hello'], ''));
        $this->assertEquals(array(), OnArray::getArrayItemKeyNameStartWith(['a' => 'hello'], 'b'));
155
156
//        $this->assertEquals([0 => 'hello'], OnArray::getArrayItemKeyNameStartWith(['a' => 'hello'], 'a'));
        $this->assertEquals(['' => 'hello'], OnArray::getArrayItemKeyNameStartWith(['a' => 'hello'], 'a'));
Carsten  Rose's avatar
Carsten Rose committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
        $this->assertEquals(['b' => 'hello'], OnArray::getArrayItemKeyNameStartWith(['ab' => 'hello'], 'a'));
        $this->assertEquals(array(), OnArray::getArrayItemKeyNameStartWith(['ba' => 'hello'], 'a'));
        $this->assertEquals(['a' => 'is', 'b' => 'john'], OnArray::getArrayItemKeyNameStartWith(['1_a' => 'my', '1_b' => 'name', '2_a' => 'is', '2_b' => 'john'], '2_'));
        $this->assertEquals(['a' => 'is', 'b' => 'john'], OnArray::getArrayItemKeyNameStartWith(['1z_a' => 'my', '1z_b' => 'name', '2z_a' => 'is', '2z_b' => 'john'], '2z_'));

    }

    public function testArrayEscapeshellarg() {
        $this->assertEquals(array(), OnArray::arrayEscapeshellarg(array()));
        $this->assertEquals(['name' => "'john'"], OnArray::arrayEscapeshellarg(['name' => 'john']));
        $this->assertEquals(['name' => "'jo\"hn'"], OnArray::arrayEscapeshellarg(['name' => 'jo"hn']));
        $this->assertEquals(['name' => "'john'", 'surname' => "'doe'"], OnArray::arrayEscapeshellarg(['name' => 'john', 'surname' => 'doe']));
        $this->assertEquals(['name' => "'john'", 'sub' => ['surname' => "'doe'"]], OnArray::arrayEscapeshellarg(['name' => 'john', 'sub' => ['surname' => 'doe']]));
    }
171
172
173
174
175
176
177
178
179
180
181

    public function testArrayKeyNameRemoveLeadingUnderscore() {
        $this->assertEquals(array(), OnArray::keyNameRemoveLeadingUnderscore(array()));
        $this->assertEquals(['name' => 'john'], OnArray::keyNameRemoveLeadingUnderscore(['name' => 'john']));
        $this->assertEquals(['name' => 'john', 'surname' => 'doe'], OnArray::keyNameRemoveLeadingUnderscore(['name' => 'john', 'surname' => 'doe']));
        $this->assertEquals(['name' => '_john'], OnArray::keyNameRemoveLeadingUnderscore(['_name' => '_john']));
        $this->assertEquals(['name' => 'john', 'surname' => 'doe'], OnArray::keyNameRemoveLeadingUnderscore(['_name' => 'john', 'surname' => 'doe']));
        $this->assertEquals(['name' => 'john', 'surname' => 'doe'], OnArray::keyNameRemoveLeadingUnderscore(['name' => 'john', '_surname' => 'doe']));
        $this->assertEquals(['name' => 'john', 'surname' => 'doe'], OnArray::keyNameRemoveLeadingUnderscore(['_name' => 'john', '_surname' => 'doe']));

    }
182
}