|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class HelperArrayTest |
|
5
|
|
|
* |
|
6
|
|
|
* @coversDefaultClass HelperArray |
|
7
|
|
|
*/ |
|
8
|
|
|
class HelperArrayTest extends PHPUnit_Framework_TestCase { |
|
9
|
|
|
|
|
10
|
|
|
public function setUp() { |
|
11
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function tearDown() { |
|
15
|
|
|
parent::tearDown(); // TODO: Change the autogenerated stub |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function dataStringToArray() { |
|
19
|
|
|
return array( |
|
20
|
|
|
array(123, ',', array()), // Only string would be converted to array |
|
21
|
|
|
array('', ',', array()), // Empty data would be converted to empty array() |
|
22
|
|
|
array('123', ',', array('123')), // One element string |
|
23
|
|
|
array('123,234', ',', array('123', '234')), // Two element string |
|
24
|
|
|
array('123;234', ';', array('123', '234')), // Specified delimiter |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $value |
|
30
|
|
|
* @param string $delimiter |
|
31
|
|
|
* @param array $expected |
|
32
|
|
|
* |
|
33
|
|
|
* @dataProvider dataStringToArray |
|
34
|
|
|
* |
|
35
|
|
|
* @covers ::stringToArray |
|
36
|
|
|
*/ |
|
37
|
|
|
public function testStringToArray($value, $delimiter, $expected) { |
|
38
|
|
|
$this->assertEquals($expected, HelperArray::stringToArray($value, $delimiter)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
public function dataMakeArray() { |
|
43
|
|
|
return array( |
|
44
|
|
|
array(array('test'), 0, array('test')), // Straightforward: just array |
|
45
|
|
|
array('test', 0, array(0 => 'test')), // Value with default index |
|
46
|
|
|
array('test', 1, array(1 => 'test')), // Value with non-default index |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @dataProvider dataMakeArray |
|
52
|
|
|
* |
|
53
|
|
|
* @covers ::makeArrayRef |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testMakeArrayRef($value, $index, $expected) { |
|
56
|
|
|
HelperArray::makeArrayRef($value, $index); |
|
57
|
|
|
$this->assertEquals($expected, $value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @dataProvider dataMakeArray |
|
62
|
|
|
* |
|
63
|
|
|
* @covers ::makeArray |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testMakeArray($value, $index, $expected) { |
|
66
|
|
|
$this->assertEquals($expected, HelperArray::makeArray($value, $index)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function dataFilter() { |
|
70
|
|
|
$callback = function ($value) { |
|
71
|
|
|
return !empty($value); |
|
72
|
|
|
}; |
|
73
|
|
|
|
|
74
|
|
|
return array( |
|
75
|
|
|
array(1, $callback, array()), // Not array |
|
76
|
|
|
array(array(), $callback, array()), // Empty array |
|
77
|
|
|
array(array(''), $callback, array()), // Not empty array with one empty element |
|
78
|
|
|
array(array('0', ''), $callback, array()), // Not empty array with both filterable elements |
|
79
|
|
|
array(array('test', ''), $callback, array(0 => 'test')), // Not empty array with one filterable element |
|
80
|
|
|
array(array('test1', '', 'test2'), $callback, array(0 => 'test1', 1 => 'test2')), |
|
81
|
|
|
array(array('test1', 'test', 'test2'), $callback, array(0 => 'test1', 1 => 'test', 2 => 'test2')), |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $value |
|
87
|
|
|
* @param callable $callback |
|
88
|
|
|
* @param array $expected |
|
89
|
|
|
* |
|
90
|
|
|
* @dataProvider dataFilter |
|
91
|
|
|
* |
|
92
|
|
|
* @covers ::filter |
|
93
|
|
|
*/ |
|
94
|
|
|
public function testFilter($value, $callback, $expected) { |
|
95
|
|
|
$this->assertEquals($expected, HelperArray::filter($value, $callback)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param mixed $value |
|
100
|
|
|
* @param callable $callback |
|
101
|
|
|
* @param array $expected |
|
102
|
|
|
* |
|
103
|
|
|
* @dataProvider dataFilter |
|
104
|
|
|
* |
|
105
|
|
|
* @covers ::filterEmpty |
|
106
|
|
|
* @covers Validators::isNotEmpty |
|
107
|
|
|
* @covers Validators::isNotEmptyByRef |
|
108
|
|
|
* @covers ::filter |
|
109
|
|
|
*/ |
|
110
|
|
|
public function testFilterEmpty($value, $callback, $expected) { |
|
111
|
|
|
$this->assertEquals($expected, HelperArray::filterEmpty($value)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @covers ::stringToArrayFilterEmpty |
|
117
|
|
|
* @covers ::stringToArray |
|
118
|
|
|
* @covers ::filterEmpty |
|
119
|
|
|
* @covers Validators::isNotEmpty |
|
120
|
|
|
* @covers ::filter |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testStringToArrayFilterEmpty() { |
|
123
|
|
|
// Not string |
|
124
|
|
|
$this->assertEquals(array(), HelperArray::stringToArrayFilterEmpty(1)); |
|
125
|
|
|
|
|
126
|
|
|
// Empty string |
|
127
|
|
|
$this->assertEquals(array(), HelperArray::stringToArrayFilterEmpty('')); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @covers ::merge |
|
132
|
|
|
*/ |
|
133
|
|
|
public function testMerge() { |
|
134
|
|
|
// Testing ARRAY_REPLACE mode |
|
135
|
|
|
$array1 = array('a' => 'b'); |
|
136
|
|
|
$array2 = array('c' => 'd', 0 => 10); |
|
137
|
|
|
HelperArray::merge($array1, $array2); |
|
138
|
|
|
$this->assertEquals($array2, $array1); |
|
139
|
|
|
|
|
140
|
|
|
// Testing ARRAY_MERGE mode |
|
141
|
|
|
// String keyed value should be replaced |
|
142
|
|
|
// Integer keyed value should be added and integer keys should be recalculated |
|
143
|
|
|
$array2 = array('c' => 'e', 0 => 20); |
|
144
|
|
|
HelperArray::merge($array1, $array2, HelperArray::MERGE_PHP); |
|
145
|
|
|
$this->assertEquals(array('c' => 'e', 0 => 10, 1 => 20), $array1); |
|
146
|
|
|
|
|
147
|
|
|
// First array is not an array |
|
148
|
|
|
$array1 = 1; |
|
149
|
|
|
// Element int(20) should have key recalculated and become key(2) |
|
150
|
|
|
$array2 = array('c' => 'd', 1 => 10, 5 => 20); |
|
151
|
|
|
HelperArray::merge($array1, $array2, HelperArray::MERGE_PHP); |
|
152
|
|
|
$this->assertEquals(array(0 => 1, 'c' => 'd', 1 => 10, 2 => 20), $array1); |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @covers ::keyExistsOr |
|
158
|
|
|
*/ |
|
159
|
|
|
public function testKeyExistsOr() { |
|
160
|
|
|
$array = array('a' => 'b'); |
|
161
|
|
|
$this->assertEquals('b', HelperArray::keyExistsOr($array, 'a', 'q')); |
|
162
|
|
|
$this->assertEquals('q', HelperArray::keyExistsOr($array, 'c', 'q')); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function dataCloneDeep() { |
|
166
|
|
|
return array( |
|
167
|
|
|
array(HelperArray::CLONE_ARRAY_NONE, true, 2, true, 6), |
|
168
|
|
|
array(HelperArray::CLONE_ARRAY_SHALLOW, false, 1, true, 6), |
|
169
|
|
|
array(HelperArray::CLONE_ARRAY_RECURSIVE, false, 1, false, 5), |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
protected function helpCloneDeep($source, $destination, $isSameObject, $value, $valueAfterChange) { |
|
174
|
|
|
// Checking 0-level object |
|
175
|
|
|
$this->assertEquals($isSameObject, $source === $destination); |
|
176
|
|
|
|
|
177
|
|
|
// Checking property 'test' of 0-level object |
|
178
|
|
|
$this->assertEquals($source->test, $destination->test); |
|
179
|
|
|
$this->assertEquals($value, $destination->test); |
|
180
|
|
|
$this->assertEquals($value, $source->test); |
|
181
|
|
|
|
|
182
|
|
|
// Changing property of 0-level and checking result |
|
183
|
|
|
$destination->test = $value + 1; |
|
184
|
|
|
$this->assertEquals($value + 1, $destination->test); |
|
185
|
|
|
$this->assertEquals($valueAfterChange, $source->test); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @dataProvider dataCloneDeep |
|
191
|
|
|
* |
|
192
|
|
|
* @covers ::cloneDeep |
|
193
|
|
|
*/ |
|
194
|
|
|
public function testCloneDeep($deep, $l0Eq, $l0Prop, $l1Eq, $l1Prop) { |
|
195
|
|
|
// Init values |
|
196
|
|
|
$source = array(new StdClass(), array(new StdClass())); |
|
197
|
|
|
$source[0]->test = 1; |
|
198
|
|
|
$source[1][0]->test = 5; |
|
199
|
|
|
$destination = $source; |
|
200
|
|
|
|
|
201
|
|
|
// Making clone |
|
202
|
|
|
HelperArray::cloneDeep($destination, $deep); |
|
203
|
|
|
$this->helpCloneDeep($source[0], $destination[0], $l0Eq, 1, $l0Prop); |
|
204
|
|
|
$this->helpCloneDeep($source[1][0], $destination[1][0], $l1Eq, 5, $l1Prop); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|