2 require_once 'CiviTest/CiviUnitTestCase.php';
5 * 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(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']);
52 public function testCollect() {
54 array('catWord' => 'cat', 'dogWord' => 'dog'),
55 array('catWord' => 'chat', 'dogWord' => 'chien'),
56 array('catWord' => 'gato'),
58 $expected = array('cat', 'chat', 'gato');
59 $this->assertEquals($expected, CRM_Utils_Array
::collect('catWord', $arr));
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));
69 public function testProduct0() {
70 $actual = CRM_Utils_Array
::product(
72 array('base data' => 1)
74 $this->assertEquals(array(
75 array('base data' => 1),
79 public function testProduct1() {
80 $actual = CRM_Utils_Array
::product(
81 array('dim1' => array('a', 'b')),
82 array('base data' => 1)
84 $this->assertEquals(array(
85 array('base data' => 1, 'dim1' => 'a'),
86 array('base data' => 1, 'dim1' => 'b'),
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)
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'),
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')));
117 public function testRemove() {
126 CRM_Utils_Array
::remove($data, 'one', 'two', array('three', 'four'), 'five');
127 $this->assertEquals($data, array('six' => 6));