Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-11-03-17-29-16
[civicrm-core.git] / tests / phpunit / CRM / Utils / ArrayTest.php
1 <?php
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';
13
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']);
18
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']);
25 }
26
27 private function &returnByReference() {
28 static $foo;
29 if ($foo === NULL) {
30 $foo['foo'] = 'original';
31 }
32 return $foo;
33 }
34
35 function testIndexArray() {
36 $inputs = array();
37 $inputs[] = array(
38 'lang' => 'en',
39 'msgid' => 'greeting',
40 'familiar' => false,
41 'value' => 'Hello'
42 );
43 $inputs[] = array(
44 'lang' => 'en',
45 'msgid' => 'parting',
46 'value' => 'Goodbye'
47 );
48 $inputs[] = array(
49 'lang' => 'fr',
50 'msgid' => 'greeting',
51 'value' => 'Bon jour'
52 );
53 $inputs[] = array(
54 'lang' => 'fr',
55 'msgid' => 'parting',
56 'value' => 'Au revoir'
57 );
58 $inputs[] = array(
59 'lang' => 'en',
60 'msgid' => 'greeting',
61 'familiar' => true,
62 'value' => 'Hey'
63 );
64
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']);
70 }
71
72 function testCollect() {
73 $arr = array(
74 array('catWord' => 'cat', 'dogWord' => 'dog'),
75 array('catWord' => 'chat', 'dogWord' => 'chien'),
76 array('catWord' => 'gato'),
77 );
78 $expected = array('cat', 'chat', 'gato');
79 $this->assertEquals($expected, CRM_Utils_Array::collect('catWord', $arr));
80
81 $arr = array();
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));
87 }
88
89 }