2 require_once 'CiviTest/CiviUnitTestCase.php';
3 class CRM_Utils_ArrayTest
extends CiviUnitTestCase
{
4 function testBreakReference() {
5 // Get a reference and make a change
6 $fooRef1 = self
::returnByReference();
7 $this->assertEquals('original', $fooRef1['foo']);
8 $fooRef1['foo'] = 'modified';
10 // Make sure that the referenced item was actually changed
11 $fooRef2 = self
::returnByReference();
12 $this->assertEquals('modified', $fooRef1['foo']);
13 $this->assertEquals('original', $fooRef2['foo']);
15 // Get a non-reference, make a change, and make sure the references were unaffected.
16 $fooNonReference = CRM_Utils_Array
::breakReference(self
::returnByReference());
17 $fooNonReference['foo'] = 'privately-modified';
18 $this->assertEquals('modified', $fooRef1['foo']);
19 $this->assertEquals('original', $fooRef2['foo']);
20 $this->assertEquals('privately-modified', $fooNonReference['foo']);
23 private function &returnByReference() {
26 $foo['foo'] = 'original';
31 function testIndexArray() {
35 'msgid' => 'greeting',
46 'msgid' => 'greeting',
52 'value' => 'Au revoir'
56 'msgid' => 'greeting',
61 $byLangMsgid = CRM_Utils_Array
::index(array('lang', 'msgid'), $inputs);
62 $this->assertEquals($inputs[4], $byLangMsgid['en']['greeting']);
63 $this->assertEquals($inputs[1], $byLangMsgid['en']['parting']);
64 $this->assertEquals($inputs[2], $byLangMsgid['fr']['greeting']);
65 $this->assertEquals($inputs[3], $byLangMsgid['fr']['parting']);
68 function testCollect() {
70 array('catWord' => 'cat', 'dogWord' => 'dog'),
71 array('catWord' => 'chat', 'dogWord' => 'chien'),
72 array('catWord' => 'gato'),
74 $expected = array('cat', 'chat', 'gato');
75 $this->assertEquals($expected, CRM_Utils_Array
::collect('catWord', $arr));
78 $arr['en']= (object) array('catWord' => 'cat', 'dogWord' => 'dog');
79 $arr['fr']= (object) array('catWord' => 'chat', 'dogWord' => 'chien');
80 $arr['es']= (object) array('catWord' => 'gato');
81 $expected = array('en' => 'cat', 'fr' => 'chat', 'es' => 'gato');
82 $this->assertEquals($expected, CRM_Utils_Array
::collect('catWord', $arr));