4 * Class CRM_Utils_ArrayTest
7 class CRM_Utils_ArrayTest
extends CiviUnitTestCase
{
9 public function testIndexArray() {
13 'msgid' => 'greeting',
24 'msgid' => 'greeting',
25 'value' => 'Bon jour',
30 'value' => 'Au revoir',
34 'msgid' => 'greeting',
39 'msgid' => 'greeting',
41 'value' => 'Universal greeting',
44 $byLangMsgid = CRM_Utils_Array
::index(['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']);
52 public function testCollect() {
54 ['catWord' => 'cat', 'dogWord' => 'dog'],
55 ['catWord' => 'chat', 'dogWord' => 'chien'],
56 ['catWord' => 'gato'],
58 $expected = ['cat', 'chat', 'gato'];
59 $this->assertEquals($expected, CRM_Utils_Array
::collect('catWord', $arr));
62 $arr['en'] = (object) ['catWord' => 'cat', 'dogWord' => 'dog'];
63 $arr['fr'] = (object) ['catWord' => 'chat', 'dogWord' => 'chien'];
64 $arr['es'] = (object) ['catWord' => 'gato'];
65 $expected = ['en' => 'cat', 'fr' => 'chat', 'es' => 'gato'];
66 $this->assertEquals($expected, CRM_Utils_Array
::collect('catWord', $arr));
69 public function testProduct0() {
70 $actual = CRM_Utils_Array
::product(
79 public function testProduct1() {
80 $actual = CRM_Utils_Array
::product(
81 ['dim1' => ['a', 'b']],
85 ['base data' => 1, 'dim1' => 'a'],
86 ['base data' => 1, 'dim1' => 'b'],
90 public function testProduct3() {
91 $actual = CRM_Utils_Array
::product(
92 ['dim1' => ['a', 'b'], 'dim2' => ['alpha', 'beta'], 'dim3' => ['one', 'two']],
96 ['base data' => 1, 'dim1' => 'a', 'dim2' => 'alpha', 'dim3' => 'one'],
97 ['base data' => 1, 'dim1' => 'a', 'dim2' => 'alpha', 'dim3' => 'two'],
98 ['base data' => 1, 'dim1' => 'a', 'dim2' => 'beta', 'dim3' => 'one'],
99 ['base data' => 1, 'dim1' => 'a', 'dim2' => 'beta', 'dim3' => 'two'],
100 ['base data' => 1, 'dim1' => 'b', 'dim2' => 'alpha', 'dim3' => 'one'],
101 ['base data' => 1, 'dim1' => 'b', 'dim2' => 'alpha', 'dim3' => 'two'],
102 ['base data' => 1, 'dim1' => 'b', 'dim2' => 'beta', 'dim3' => 'one'],
103 ['base data' => 1, 'dim1' => 'b', 'dim2' => 'beta', 'dim3' => 'two'],
107 public function testIsSubset() {
108 $this->assertTrue(CRM_Utils_Array
::isSubset([], []));
109 $this->assertTrue(CRM_Utils_Array
::isSubset(['a'], ['a']));
110 $this->assertTrue(CRM_Utils_Array
::isSubset(['a'], ['b', 'a', 'c']));
111 $this->assertTrue(CRM_Utils_Array
::isSubset(['b', 'd'], ['a', 'b', 'c', 'd']));
112 $this->assertFalse(CRM_Utils_Array
::isSubset(['a'], []));
113 $this->assertFalse(CRM_Utils_Array
::isSubset(['a'], ['b']));
114 $this->assertFalse(CRM_Utils_Array
::isSubset(['a'], ['b', 'c', 'd']));
117 public function testRemove() {
126 CRM_Utils_Array
::remove($data, 'one', 'two', ['three', 'four'], 'five');
127 $this->assertEquals($data, ['six' => 6]);
130 public function testGetSetPathParts() {
137 $this->assertEquals('1', CRM_Utils_Array
::pathGet($arr, ['one']));
138 $this->assertEquals('2', CRM_Utils_Array
::pathGet($arr, ['two', 'half']));
139 $this->assertEquals(NULL, CRM_Utils_Array
::pathGet($arr, ['zoo', 'half']));
140 CRM_Utils_Array
::pathSet($arr, ['zoo', 'half'], '3');
141 $this->assertEquals(3, CRM_Utils_Array
::pathGet($arr, ['zoo', 'half']));
142 $this->assertEquals(3, $arr['zoo']['half']);
145 public function getSortExamples() {
146 $red = ['label' => 'Red', 'id' => 1, 'weight' => '90'];
147 $orange = ['label' => 'Orange', 'id' => 2, 'weight' => '70'];
148 $yellow = ['label' => 'Yellow', 'id' => 3, 'weight' => '10'];
149 $green = ['label' => 'Green', 'id' => 4, 'weight' => '70'];
150 $blue = ['label' => 'Blue', 'id' => 5, 'weight' => '70'];
209 * @param array $array
210 * @param string|array $field
212 * @dataProvider getSortExamples
214 public function testCrmArraySortByField($array, $field, $expected) {
215 $actual = CRM_Utils_Array
::crmArraySortByField($array, $field);
217 // assertEquals() has nicer error output, but it's not precise about order.
218 $this->assertEquals($expected, $actual);
220 $aIter = new ArrayIterator($actual);
221 $eIter = new ArrayIterator($expected);
222 $this->assertEquals($eIter->count(), $aIter->count());
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");
233 public function getRecursiveIssetExamples() {
236 [[[], [0, 1, 2], []]], [0, 1, 2], TRUE,
239 [[[], [0, 1, 2], []]], [0, 1, 3], FALSE,
245 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'wrong'], FALSE,
248 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right'], TRUE,
251 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right', 'foo'], TRUE,
260 * @dataProvider getRecursiveIssetExamples
262 public function testRecursiveIsset($array, $path, $expected) {
263 $result = CRM_Utils_Array
::pathIsset($array, $path);
264 $this->assertEquals($expected, $result);
267 public function getRecursiveValueExamples() {
270 [[[], [0, 1, 2], []]], [0, 1, 2], NULL, 2,
273 [[[], [0, 1, 2], []]], [0, 1, 3], NULL, NULL,
276 [], ['foo'], FALSE, FALSE,
279 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'wrong'], 'nada', 'nada',
282 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right'], NULL, ['foo' => 1, 'bar' => 2],
285 [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right', 'foo'], NULL, 1,
295 * @dataProvider getRecursiveValueExamples
297 public function testRecursiveValue($array, $path, $default, $expected) {
298 $result = CRM_Utils_Array
::pathGet($array, $path, $default);
299 $this->assertEquals($expected, $result);
303 * Get values for build test.
305 public function getBuildValueExamples() {
308 [], [0, 'email', 2, 'location'], [0 => ['email' => [2 => ['location' => 'llama']]]],
311 ['foo', 'bar', [['donkey']]], [2, 0, 1], ['foo', 'bar', [['donkey', 'llama']]],
314 ['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]]],
320 * Test the build recursive function.
326 * @dataProvider getBuildValueExamples
328 public function testBuildRecursiveValue($source, $path, $expected) {
329 CRM_Utils_Array
::pathSet($source, $path, 'llama');
330 $this->assertEquals($expected, $source);
334 * Test the flatten function
336 public function testFlatten() {
356 'my_array.0' => 'bar',
357 'my_array.1' => 'baz',
358 'my_array.2' => 'boz',
359 'my_complex.dog' => 'woof',
360 'my_complex.asdf.my_zero' => 0,
361 'my_complex.asdf.my_int' => 1,
362 'my_complex.asdf.my_null' => NULL,
363 'my_complex.asdf.my_empty' => '',
368 CRM_Utils_Array
::flatten($data, $flat);
369 $this->assertEquals($flat, $expected);