OnArrayTest.php 6.09 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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

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


class OnArrayTest extends \PHPUnit_Framework_TestCase {
    public function testSortByKey() {
        $unsorted = [
            'a' => 'z',
            '1' => 'y',
            'Aa' => 'y0',
            'zZ' => 'X9',
            '0000' => 'h'
        ];

        $sorted = [
            '0000' => 'h',
            '1' => 'y',
            'Aa' => 'y0',
            'a' => 'z',
            'zZ' => 'X9'
        ];

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

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

    }
41
42
43
44
45

    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 ', ''];

46
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
        $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));

91
    }
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));
    }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

    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'));
    }
138
}