2 require_once 'CiviTest/CiviUnitTestCase.php';
3 class CRM_Utils_ArrayTest
extends CiviUnitTestCase
{
4 //@todo make BAO enotice compliant & remove the line below
5 // WARNING - NEVER COPY & PASTE $_eNoticeCompliant = FALSE
6 // new test classes should be compliant.
7 public $_eNoticeCompliant = FALSE;
8 function testBreakReference() {
9 // Get a reference and make a change
10 $fooRef1 = self
::returnByReference();
11 $this->assertEquals('original', $fooRef1['foo']);
12 $fooRef1['foo'] = 'modified';
14 // Make sure that the referenced item was actually changed
15 $fooRef2 = self
::returnByReference();
16 $this->assertEquals('modified', $fooRef1['foo']);
17 $this->assertEquals('original', $fooRef2['foo']);
19 // Get a non-reference, make a change, and make sure the references were unaffected.
20 $fooNonReference = CRM_Utils_Array
::breakReference(self
::returnByReference());
21 $fooNonReference['foo'] = 'privately-modified';
22 $this->assertEquals('modified', $fooRef1['foo']);
23 $this->assertEquals('original', $fooRef2['foo']);
24 $this->assertEquals('privately-modified', $fooNonReference['foo']);
27 private function &returnByReference() {
30 $foo['foo'] = 'original';
35 function testIndexArray() {
39 'msgid' => 'greeting',
50 'msgid' => 'greeting',
56 'value' => 'Au revoir'
60 'msgid' => 'greeting',
65 $byLangMsgid = CRM_Utils_Array
::index(array('lang', 'msgid'), $inputs);
66 $this->assertEquals($inputs[4], $byLangMsgid['en']['greeting']);
67 $this->assertEquals($inputs[1], $byLangMsgid['en']['parting']);
68 $this->assertEquals($inputs[2], $byLangMsgid['fr']['greeting']);
69 $this->assertEquals($inputs[3], $byLangMsgid['fr']['parting']);
72 function testCollect() {
74 array('catWord' => 'cat', 'dogWord' => 'dog'),
75 array('catWord' => 'chat', 'dogWord' => 'chien'),
76 array('catWord' => 'gato'),
78 $expected = array('cat', 'chat', 'gato');
79 $this->assertEquals($expected, CRM_Utils_Array
::collect('catWord', $arr));
82 $arr['en']= (object) array('catWord' => 'cat', 'dogWord' => 'dog');
83 $arr['fr']= (object) array('catWord' => 'chat', 'dogWord' => 'chien');
84 $arr['es']= (object) array('catWord' => 'gato');
85 $expected = array('en' => 'cat', 'fr' => 'chat', 'es' => 'gato');
86 $this->assertEquals($expected, CRM_Utils_Array
::collect('catWord', $arr));