Merge pull request #13192 from aydun/no_dao_free
[civicrm-core.git] / tests / phpunit / CRM / Utils / ArrayTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_ArrayTest
5 * @group headless
6 */
7 class CRM_Utils_ArrayTest extends CiviUnitTestCase {
8
9 public function testIndexArray() {
10 $inputs = array();
11 $inputs[] = array(
12 'lang' => 'en',
13 'msgid' => 'greeting',
14 'familiar' => FALSE,
15 'value' => 'Hello',
16 );
17 $inputs[] = array(
18 'lang' => 'en',
19 'msgid' => 'parting',
20 'value' => 'Goodbye',
21 );
22 $inputs[] = array(
23 'lang' => 'fr',
24 'msgid' => 'greeting',
25 'value' => 'Bon jour',
26 );
27 $inputs[] = array(
28 'lang' => 'fr',
29 'msgid' => 'parting',
30 'value' => 'Au revoir',
31 );
32 $inputs[] = array(
33 'lang' => 'en',
34 'msgid' => 'greeting',
35 'familiar' => TRUE,
36 'value' => 'Hey',
37 );
38 $inputs[] = array(
39 'msgid' => 'greeting',
40 'familiar' => TRUE,
41 'value' => 'Universal greeting',
42 );
43
44 $byLangMsgid = CRM_Utils_Array::index(array('lang', 'msgid'), $inputs);
45 $this->assertEquals($inputs[4], $byLangMsgid['en']['greeting']);
46 $this->assertEquals($inputs[1], $byLangMsgid['en']['parting']);
47 $this->assertEquals($inputs[2], $byLangMsgid['fr']['greeting']);
48 $this->assertEquals($inputs[3], $byLangMsgid['fr']['parting']);
49 $this->assertEquals($inputs[5], $byLangMsgid[NULL]['greeting']);
50 }
51
52 public function testCollect() {
53 $arr = array(
54 array('catWord' => 'cat', 'dogWord' => 'dog'),
55 array('catWord' => 'chat', 'dogWord' => 'chien'),
56 array('catWord' => 'gato'),
57 );
58 $expected = array('cat', 'chat', 'gato');
59 $this->assertEquals($expected, CRM_Utils_Array::collect('catWord', $arr));
60
61 $arr = array();
62 $arr['en'] = (object) array('catWord' => 'cat', 'dogWord' => 'dog');
63 $arr['fr'] = (object) array('catWord' => 'chat', 'dogWord' => 'chien');
64 $arr['es'] = (object) array('catWord' => 'gato');
65 $expected = array('en' => 'cat', 'fr' => 'chat', 'es' => 'gato');
66 $this->assertEquals($expected, CRM_Utils_Array::collect('catWord', $arr));
67 }
68
69 public function testProduct0() {
70 $actual = CRM_Utils_Array::product(
71 array(),
72 array('base data' => 1)
73 );
74 $this->assertEquals(array(
75 array('base data' => 1),
76 ), $actual);
77 }
78
79 public function testProduct1() {
80 $actual = CRM_Utils_Array::product(
81 array('dim1' => array('a', 'b')),
82 array('base data' => 1)
83 );
84 $this->assertEquals(array(
85 array('base data' => 1, 'dim1' => 'a'),
86 array('base data' => 1, 'dim1' => 'b'),
87 ), $actual);
88 }
89
90 public function testProduct3() {
91 $actual = CRM_Utils_Array::product(
92 array('dim1' => array('a', 'b'), 'dim2' => array('alpha', 'beta'), 'dim3' => array('one', 'two')),
93 array('base data' => 1)
94 );
95 $this->assertEquals(array(
96 array('base data' => 1, 'dim1' => 'a', 'dim2' => 'alpha', 'dim3' => 'one'),
97 array('base data' => 1, 'dim1' => 'a', 'dim2' => 'alpha', 'dim3' => 'two'),
98 array('base data' => 1, 'dim1' => 'a', 'dim2' => 'beta', 'dim3' => 'one'),
99 array('base data' => 1, 'dim1' => 'a', 'dim2' => 'beta', 'dim3' => 'two'),
100 array('base data' => 1, 'dim1' => 'b', 'dim2' => 'alpha', 'dim3' => 'one'),
101 array('base data' => 1, 'dim1' => 'b', 'dim2' => 'alpha', 'dim3' => 'two'),
102 array('base data' => 1, 'dim1' => 'b', 'dim2' => 'beta', 'dim3' => 'one'),
103 array('base data' => 1, 'dim1' => 'b', 'dim2' => 'beta', 'dim3' => 'two'),
104 ), $actual);
105 }
106
107 public function testIsSubset() {
108 $this->assertTrue(CRM_Utils_Array::isSubset(array(), array()));
109 $this->assertTrue(CRM_Utils_Array::isSubset(array('a'), array('a')));
110 $this->assertTrue(CRM_Utils_Array::isSubset(array('a'), array('b', 'a', 'c')));
111 $this->assertTrue(CRM_Utils_Array::isSubset(array('b', 'd'), array('a', 'b', 'c', 'd')));
112 $this->assertFalse(CRM_Utils_Array::isSubset(array('a'), array()));
113 $this->assertFalse(CRM_Utils_Array::isSubset(array('a'), array('b')));
114 $this->assertFalse(CRM_Utils_Array::isSubset(array('a'), array('b', 'c', 'd')));
115 }
116
117 public function testRemove() {
118 $data = array(
119 'one' => 1,
120 'two' => 2,
121 'three' => 3,
122 'four' => 4,
123 'five' => 5,
124 'six' => 6,
125 );
126 CRM_Utils_Array::remove($data, 'one', 'two', array('three', 'four'), 'five');
127 $this->assertEquals($data, array('six' => 6));
128 }
129
130 public function testGetSetPathParts() {
131 $arr = array(
132 'one' => '1',
133 'two' => array(
134 'half' => 2,
135 ),
136 );
137 $this->assertEquals('1', CRM_Utils_Array::pathGet($arr, array('one')));
138 $this->assertEquals('2', CRM_Utils_Array::pathGet($arr, array('two', 'half')));
139 $this->assertEquals(NULL, CRM_Utils_Array::pathGet($arr, array('zoo', 'half')));
140 CRM_Utils_Array::pathSet($arr, array('zoo', 'half'), '3');
141 $this->assertEquals(3, CRM_Utils_Array::pathGet($arr, array('zoo', 'half')));
142 $this->assertEquals(3, $arr['zoo']['half']);
143 }
144
145 public function getSortExamples() {
146 $red = array('label' => 'Red', 'id' => 1, 'weight' => '90');
147 $orange = array('label' => 'Orange', 'id' => 2, 'weight' => '70');
148 $yellow = array('label' => 'Yellow', 'id' => 3, 'weight' => '10');
149 $green = array('label' => 'Green', 'id' => 4, 'weight' => '70');
150 $blue = array('label' => 'Blue', 'id' => 5, 'weight' => '70');
151
152 $examples = array();
153 $examples[] = array(
154 array(
155 'r' => $red,
156 'y' => $yellow,
157 'g' => $green,
158 'o' => $orange,
159 'b' => $blue,
160 ),
161 'id',
162 array(
163 'r' => $red,
164 'o' => $orange,
165 'y' => $yellow,
166 'g' => $green,
167 'b' => $blue,
168 ),
169 );
170 $examples[] = array(
171 array(
172 'r' => $red,
173 'y' => $yellow,
174 'g' => $green,
175 'o' => $orange,
176 'b' => $blue,
177 ),
178 'label',
179 array(
180 'b' => $blue,
181 'g' => $green,
182 'o' => $orange,
183 'r' => $red,
184 'y' => $yellow,
185 ),
186 );
187 $examples[] = array(
188 array(
189 'r' => $red,
190 'g' => $green,
191 'y' => $yellow,
192 'o' => $orange,
193 'b' => $blue,
194 ),
195 array('weight', 'id'),
196 array(
197 'y' => $yellow,
198 'o' => $orange,
199 'g' => $green,
200 'b' => $blue,
201 'r' => $red,
202 ),
203 );
204
205 return $examples;
206 }
207
208 /**
209 * @param array $array
210 * @param string|array $field
211 * @param $expected
212 * @dataProvider getSortExamples
213 */
214 public function testCrmArraySortByField($array, $field, $expected) {
215 $actual = CRM_Utils_Array::crmArraySortByField($array, $field);
216
217 // assertEquals() has nicer error output, but it's not precise about order.
218 $this->assertEquals($expected, $actual);
219
220 $aIter = new ArrayIterator($actual);
221 $eIter = new ArrayIterator($expected);
222 $this->assertEquals($eIter->count(), $aIter->count());
223 $pos = 0;
224 while ($aIter->valid()) {
225 $this->assertEquals($eIter->key(), $aIter->key(), "Keys at offset $pos do not match");
226 $this->assertEquals($eIter->current(), $aIter->current(), "Values at offset $pos do not match");
227 $aIter->next();
228 $eIter->next();
229 $pos++;
230 }
231 }
232
233 public function getRecursiveIssetExamples() {
234 return [
235 [
236 [[[], [0, 1, 2], []]], [0, 1, 2], TRUE,
237 ],
238 [
239 [[[], [0, 1, 2], []]], [0, 1, 3], FALSE,
240 ],
241 [
242 [], ['foo'], FALSE,
243 ],
244 [
245 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'wrong'], FALSE,
246 ],
247 [
248 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right'], TRUE,
249 ],
250 [
251 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right', 'foo'], TRUE,
252 ],
253 ];
254 }
255
256 /**
257 * @param $array
258 * @param $path
259 * @param $expected
260 * @dataProvider getRecursiveIssetExamples
261 */
262 public function testRecursiveIsset($array, $path, $expected) {
263 $result = CRM_Utils_Array::pathIsset($array, $path);
264 $this->assertEquals($expected, $result);
265 }
266
267 public function getRecursiveValueExamples() {
268 return [
269 [
270 [[[], [0, 1, 2], []]], [0, 1, 2], NULL, 2,
271 ],
272 [
273 [[[], [0, 1, 2], []]], [0, 1, 3], NULL, NULL,
274 ],
275 [
276 [], ['foo'], FALSE, FALSE,
277 ],
278 [
279 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'wrong'], 'nada', 'nada',
280 ],
281 [
282 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right'], NULL, ['foo' => 1, 'bar' => 2]
283 ],
284 [
285 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right', 'foo'], NULL, 1,
286 ],
287 ];
288 }
289
290 /**
291 * @param $array
292 * @param $path
293 * @param $expected
294 * @dataProvider getRecursiveValueExamples
295 */
296 public function testRecursiveValue($array, $path, $default, $expected) {
297 $result = CRM_Utils_Array::pathGet($array, $path, $default);
298 $this->assertEquals($expected, $result);
299 }
300
301 /**
302 * Get values for build test.
303 */
304 public function getBuildValueExamples() {
305 return [
306 [
307 [], [0, 'email', 2, 'location'], [0 => ['email' => [2 => ['location' => 'llama']]]],
308 ],
309 [
310 ['foo', 'bar', [['donkey']]], [2, 0, 1], ['foo', 'bar', [['donkey', 'llama']]],
311 ],
312 [
313 ['a' => [1, 2, 3], 'b' => ['x' => [], 'y' => ['a' => 'donkey', 'b' => 'bear'], 'z' => [4, 5, 6]]], ['b', 'y', 'b'], ['a' => [1, 2, 3], 'b' => ['x' => [], 'y' => ['a' => 'donkey', 'b' => 'llama'], 'z' => [4, 5, 6]]],
314 ],
315 ];
316 }
317
318 /**
319 * Test the build recursive function.
320 *
321 * @param $path
322 * @param $expected
323 *
324 * @dataProvider getBuildValueExamples
325 */
326 public function testBuildRecursiveValue($source, $path, $expected) {
327 CRM_Utils_Array::pathSet($source, $path, 'llama');
328 $this->assertEquals($expected, $source);
329 }
330
331 /**
332 * Test the flatten function
333 */
334 public function testFlatten() {
335 $data = [
336 'my_array' => [
337 '0' => 'bar',
338 '1' => 'baz',
339 '2' => 'boz',
340 ],
341 'my_complex' => [
342 'dog' => 'woof',
343 'asdf' => [
344 'my_zero' => 0,
345 'my_int' => 1,
346 'my_null' => NULL,
347 'my_empty' => '',
348 ],
349 ],
350 'my_simple' => 999,
351 ];
352
353 $expected = [
354 'my_array.0' => 'bar',
355 'my_array.1' => 'baz',
356 'my_array.2' => 'boz',
357 'my_complex.dog' => 'woof',
358 'my_complex.asdf.my_zero' => 0,
359 'my_complex.asdf.my_int' => 1,
360 'my_complex.asdf.my_null' => NULL,
361 'my_complex.asdf.my_empty' => '',
362 'my_simple' => 999,
363 ];
364
365 $flat = [];
366 CRM_Utils_Array::flatten($data, $flat);
367 $this->assertEquals($flat, $expected);
368 }
369
370 }