|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
require_once 'include/utils/array_utils.php'; |
|
4
|
|
|
class array_utilsTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
public function testvar_export_helper() { |
|
8
|
|
|
|
|
9
|
|
|
//execute the method and test if it returns expected values |
|
10
|
|
|
|
|
11
|
|
|
$tempArray = Array( "Key1" => "value1" , "Key2" => "value2" ); |
|
12
|
|
|
|
|
13
|
|
|
$expected = "array (\n 'Key1' => 'value1',\n 'Key2' => 'value2',\n)"; |
|
14
|
|
|
$actual = var_export_helper($tempArray); |
|
15
|
|
|
$this->assertSame($actual,$expected); |
|
16
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function testoverride_value_to_string(){ |
|
20
|
|
|
|
|
21
|
|
|
//execute the method and test if it returns expected values |
|
22
|
|
|
|
|
23
|
|
|
$expected = "\$array_name['value_name'] = 'value';"; |
|
24
|
|
|
$actual = override_value_to_string('array_name', 'value_name', 'value'); |
|
25
|
|
|
$this->assertSame($actual,$expected); |
|
26
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testadd_blank_option(){ |
|
30
|
|
|
|
|
31
|
|
|
//execute the method with array not having any blank key value pair. function will return an array with blank key value pair added. |
|
32
|
|
|
$tempArray = Array( "Key1" => "value1" , "Key2" => "value2" ); |
|
33
|
|
|
$expected = Array( "" => "" , "Key1" => "value1" , "Key2" => "value2" ); |
|
34
|
|
|
|
|
35
|
|
|
$actual = add_blank_option($tempArray); |
|
36
|
|
|
$this->assertSame($actual,$expected); |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
//execute the method with array having a blank key value pair. function will return the same array back without any change. |
|
40
|
|
|
$tempArray = Array("" => "" , "Key1" => "value1" , "Key2" => "value2" ); |
|
41
|
|
|
$expected = Array( "" => "" , "Key1" => "value1" , "Key2" => "value2" ); |
|
42
|
|
|
|
|
43
|
|
|
$actual = add_blank_option($tempArray); |
|
44
|
|
|
$this->assertSame($actual,$expected); |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
public function testoverride_value_to_string_recursive(){ |
|
50
|
|
|
|
|
51
|
|
|
//execute the method and test if it returns expected values |
|
52
|
|
|
|
|
53
|
|
|
//without keys |
|
54
|
|
|
$tempArray = Array( "Key1" => "value1" , "Key2" => "value2" ); |
|
55
|
|
|
$expected = "\$tempArray=array (\n 'Key1' => 'value1',\n 'Key2' => 'value2',\n);"; |
|
56
|
|
|
$actual = override_value_to_string_recursive('','tempArray',$tempArray); |
|
57
|
|
|
$this->assertSame($actual,$expected); |
|
58
|
|
|
|
|
59
|
|
|
//with keys |
|
60
|
|
|
$tempArray = Array(); |
|
61
|
|
|
$tempArray["Key1"]["Key2"] = Array( "Key3" => "value" , "Key4" => "value" ); |
|
62
|
|
|
$expected = "\$tempArray['key1']['key2']=array (\n 'Key1' => \n array (\n 'Key2' => \n array (\n 'Key3' => 'value',\n 'Key4' => 'value',\n ),\n ),\n);"; |
|
63
|
|
|
$actual = override_value_to_string_recursive(array('key1','key2'),'tempArray',$tempArray); |
|
64
|
|
|
//var_dump( nl2br($actual)); |
|
|
|
|
|
|
65
|
|
|
$this->assertSame($actual,$expected); |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testoverride_recursive_helper(){ |
|
70
|
|
|
|
|
71
|
|
|
//execute the method and test if it returns expected values |
|
72
|
|
|
|
|
73
|
|
|
//without keys |
|
74
|
|
|
$tempArray = Array( "Key1" => "value1" , "Key2" => "value2" ); |
|
75
|
|
|
$expected = "=array (\n 'Key1' => 'value1',\n 'Key2' => 'value2',\n);"; |
|
76
|
|
|
$actual = override_recursive_helper('','tempArray',$tempArray); |
|
77
|
|
|
$this->assertSame($actual,$expected); |
|
78
|
|
|
|
|
79
|
|
|
//with keys |
|
80
|
|
|
$tempArray = Array(); |
|
81
|
|
|
$tempArray["Key1"]["Key2"] = Array( "Key3" => "value" , "Key4" => "value" ); |
|
82
|
|
|
$expected = "['key1']['key2']=array (\n 'Key1' => \n array (\n 'Key2' => \n array (\n 'Key3' => 'value',\n 'Key4' => 'value',\n ),\n ),\n);"; |
|
83
|
|
|
$actual = override_recursive_helper(array('key1','key2'),'tempArray',$tempArray); |
|
84
|
|
|
$this->assertSame($actual,$expected); |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testoverride_value_to_string_recursive2() { |
|
90
|
|
|
|
|
91
|
|
|
//execute the method and test if it returns expected values |
|
92
|
|
|
|
|
93
|
|
|
//null array |
|
94
|
|
|
$expected = Null; |
|
95
|
|
|
$actual = override_value_to_string_recursive2( 'tempArray', 'key1','',false); |
|
96
|
|
|
$this->assertSame($actual,$expected); |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
//simple array |
|
100
|
|
|
$tempArray = Array( "Key1" => "value1" , "Key2" => "value2" ); |
|
101
|
|
|
$expected = "\$['tempArray']['Key1'] = 'value1';\n$['tempArray']['Key2'] = 'value2';\n"; |
|
102
|
|
|
$actual = override_value_to_string_recursive2( '', 'tempArray', $tempArray); |
|
103
|
|
|
$this->assertSame($actual,$expected); |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
//complex array |
|
107
|
|
|
$tempArray = Array(); |
|
108
|
|
|
$tempArray["Key1"]["Key2"] = Array( "Key3" => "value" , "Key4" => "value" ); |
|
109
|
|
|
$expected = "\$tempArray['key1']['Key2']['Key3'] = 'value';\n\$tempArray['key1']['Key2']['Key4'] = 'value';\n"; |
|
110
|
|
|
$actual = override_value_to_string_recursive2('tempArray','key1',$tempArray["Key1"]); |
|
111
|
|
|
$this->assertSame($actual,$expected); |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
public function testobject_to_array_recursive() |
|
117
|
|
|
{ |
|
118
|
|
|
//execute the method and test if it returns expected values |
|
119
|
|
|
|
|
120
|
|
|
//test invalid input |
|
121
|
|
|
$obj = ''; |
|
122
|
|
|
$expected = ''; |
|
123
|
|
|
$actual = object_to_array_recursive($obj); |
|
124
|
|
|
$this->assertSame($actual,$expected); |
|
125
|
|
|
|
|
126
|
|
|
//test with a valid object |
|
127
|
|
|
$obj = new TimeDate(); |
|
128
|
|
|
$expected = Array('dbDayFormat' => 'Y-m-d', 'dbTimeFormat' => 'H:i:s', 'allow_cache' => true); |
|
129
|
|
|
$actual = object_to_array_recursive($obj); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertSame($actual,$expected); |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testdeepArrayDiff() { |
|
136
|
|
|
|
|
137
|
|
|
//execute the method and test if it returns expected values |
|
138
|
|
|
|
|
139
|
|
|
//same simple arrays |
|
140
|
|
|
$tempArray1 = Array( "Key1" => "value1" , "Key2" => "value2" ); |
|
141
|
|
|
$tempArray2 = Array( "Key1" => "value1" , "Key2" => "value2" ); |
|
142
|
|
|
$expected = Array(); |
|
143
|
|
|
$actual = deepArrayDiff( $tempArray1, $tempArray2); |
|
144
|
|
|
$this->assertSame($actual,$expected); |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
//different simple arrays |
|
148
|
|
|
$tempArray1 = Array( "Key1" => "value1" , "Key2" => "value2" ); |
|
149
|
|
|
$tempArray2 = Array( "Key1" => "value1" , "Key2" => "value3" ); |
|
150
|
|
|
$expected = Array( "Key2" => "value2"); |
|
151
|
|
|
$actual = deepArrayDiff( $tempArray1, $tempArray2); |
|
152
|
|
|
$this->assertSame($actual,$expected); |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
//same complex arrays |
|
156
|
|
|
$tempArray1 = Array("Key1" => Array( "Key2" => "value2" , "Key3" => "value3" )); |
|
157
|
|
|
$tempArray2 = Array("Key1" => Array( "Key2" => "value2" , "Key3" => "value3" )); |
|
158
|
|
|
$expected = Array(); |
|
159
|
|
|
$actual = deepArrayDiff( $tempArray1, $tempArray2); |
|
160
|
|
|
$this->assertSame($actual,$expected); |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
//complex arrays with different root node |
|
164
|
|
|
$tempArray1 = Array("Key1" => Array( "Key2" => "value2" , "Key3" => "value3" )); |
|
165
|
|
|
$tempArray2 = Array("Key2" => Array( "Key2" => "value2" , "Key3" => "value3" )); |
|
166
|
|
|
$expected = Array("Key1" => Array( "Key2" => "value2" , "Key3" => "value3" )); |
|
167
|
|
|
$actual = deepArrayDiff( $tempArray1, $tempArray2); |
|
168
|
|
|
$this->assertSame($actual,$expected); |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
//complex arrays with different child node |
|
172
|
|
|
$tempArray1 = Array("Key1" => Array( "Key2" => "value2" , "Key3" => "value3" )); |
|
173
|
|
|
$tempArray2 = Array("Key1" => Array( "Key2" => "value2" , "Key4" => "value4" )); |
|
174
|
|
|
$expected = Array("Key1" => Array( "Key3" => "value3" )); |
|
175
|
|
|
$actual = deepArrayDiff( $tempArray1, $tempArray2); |
|
176
|
|
|
$this->assertSame($actual,$expected); |
|
177
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
public function testsetDeepArrayValue() { |
|
182
|
|
|
|
|
183
|
|
|
//execute the method and test if it returns expected values |
|
184
|
|
|
|
|
185
|
|
|
//add to existing array |
|
186
|
|
|
$tempArray = Array("Key1" => Array( "Key2" => "value2" , "Key3" => "value3" )); |
|
187
|
|
|
$expected = Array("Key1" => Array( "Key2" => "value2" , "Key3" => "value3" ),"key4" => "value4" ); |
|
188
|
|
|
setDeepArrayValue( $tempArray, "key4","value4"); |
|
189
|
|
|
$this->assertSame($tempArray,$expected); |
|
190
|
|
|
|
|
191
|
|
|
//add to empty array |
|
192
|
|
|
$tempArray = Array(); |
|
193
|
|
|
$expected = Array("key1" => Array( "key2" => Array("key3" => "value3")) ); |
|
194
|
|
|
setDeepArrayValue( $tempArray, "key1_key2_key3","value3"); |
|
195
|
|
|
//var_dump($tempArray); |
|
196
|
|
|
$this->assertSame($tempArray,$expected); |
|
197
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function testarray_merge_values() { |
|
201
|
|
|
|
|
202
|
|
|
//execute the method and test if it returns expected values |
|
203
|
|
|
|
|
204
|
|
|
//try with two different length arrays |
|
205
|
|
|
$tempArray1 = Array("v1", "v2" , "v3" ); |
|
206
|
|
|
$tempArray2 = Array("v4", "v5" ); |
|
207
|
|
|
$actual = array_merge_values( $tempArray1, $tempArray2); |
|
208
|
|
|
$this->assertFalse($actual); |
|
209
|
|
|
|
|
210
|
|
|
//try with same length arrays. |
|
211
|
|
|
$tempArray1 = Array("v1", "v2" , "v3" ); |
|
212
|
|
|
$tempArray2 = Array("v4", "v5" , "v6" ); |
|
213
|
|
|
$expected = array("v1v4", "v2v5", "v3v6"); |
|
214
|
|
|
$actual = array_merge_values( $tempArray1, $tempArray2); |
|
215
|
|
|
$this->assertSame($expected,$actual); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
public function testarray_search_insensitive() |
|
220
|
|
|
{ |
|
221
|
|
|
//execute the method and test if it returns expected value |
|
222
|
|
|
|
|
223
|
|
|
//test with invalid input |
|
224
|
|
|
$tempArray =""; |
|
225
|
|
|
$actual = array_search_insensitive('',$tempArray); |
|
226
|
|
|
$this->assertFalse($actual); |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
//test with invalid needle.. |
|
230
|
|
|
$tempArray = Array("Key1" => "value1", "Key2" => "value2" , "Key3" => "value3" ,"key4" => "value4" ); |
|
231
|
|
|
$actual = array_search_insensitive('', $tempArray); |
|
232
|
|
|
$this->assertFalse($actual); |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
//test with valid needle and haystack. |
|
236
|
|
|
$tempArray = Array("Key1" => "value1", "Key2" => "value2" , "Key3" => "value3" ,"key4" => "value4" ); |
|
237
|
|
|
$actual = array_search_insensitive('value4', $tempArray); |
|
238
|
|
|
$this->assertTrue($actual); |
|
239
|
|
|
|
|
240
|
|
|
//var_dump($actual); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function testget() { |
|
244
|
|
|
|
|
245
|
|
|
//execute the method and test if it returns expected values |
|
246
|
|
|
|
|
247
|
|
|
//test for a top level key |
|
248
|
|
|
$tempArray = new SugarArray(Array("Key1" => Array("Key2" => "value2" , "Key3" => "value3") ,"key4" => "value4" )); |
|
249
|
|
|
$expected = "value4"; |
|
250
|
|
|
$actual = $tempArray->get('key4'); |
|
251
|
|
|
$this->assertSame($expected,$actual); |
|
252
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
//test for a child level key with dot notation |
|
255
|
|
|
$tempArray = new SugarArray(Array("key1" => Array("key2" => "value2" , "key3" => "value3") ,"key4" => "value4" )); |
|
256
|
|
|
$expected = "value3"; |
|
257
|
|
|
$actual = $tempArray->get('key1.key3'); |
|
258
|
|
|
$this->assertSame($expected,$actual); |
|
259
|
|
|
|
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function teststaticGet() { |
|
263
|
|
|
|
|
264
|
|
|
//execute the method and test if it returns expected values |
|
265
|
|
|
|
|
266
|
|
|
//test for a top level key |
|
267
|
|
|
$haystack = Array("key1" => Array("key2" => "value2" , "key3" => "value3") ,"key4" => "value4" ); |
|
268
|
|
|
$expected = "value4"; |
|
269
|
|
|
$actual = SugarArray::staticGet($haystack,"key4"); |
|
270
|
|
|
$this->assertSame($expected,$actual); |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
//test for a child level key with dot notation |
|
274
|
|
|
$haystack = Array("key1" => Array("key2" => "value2" , "key3" => "value3") ,"key4" => "value4" ); |
|
275
|
|
|
$expected = "value3"; |
|
276
|
|
|
$actual = SugarArray::staticGet($haystack,'key1.key3'); |
|
277
|
|
|
$this->assertSame($expected,$actual); |
|
278
|
|
|
|
|
279
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.