2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once 'include/utils/array_utils.php';
40 class SugarArrayUtilsTest extends Sugar_PHPUnit_Framework_TestCase
43 public function test_array_merge_values()
45 $array1 = array("a","b","c");
46 $array2 = array("x","y","z");
47 $array3 = array(1, 2, 3);
48 $array4 = array("a", "b", "c", "d", "e");
50 $expectedResult12 = array("ax","by","cz");
51 $expectedResult13 = array("a1", "b2", "c3");
52 $expectedResult14 = false;
55 $this->assertEquals($expectedResult12, array_merge_values($array1, $array2));
56 $this->assertEquals($expectedResult13, array_merge_values($array1, $array3));
57 $this->assertEquals($expectedResult14, array_merge_values($array1, $array4));
62 public function test_array_search_insensitive()
64 $arrayLowerCase = array("alpha","bravo","charlie","delta","echo");
65 $arrayUpperCase = array("ALPHA", "BRAVO", "CHARLIE", "DELTA", "ECHO");
66 $arrayMixed = array("Alpha","Bravo","Charlie", "Delta", "Echo");
67 $arrayEmpty = array();
69 $this->assertTrue(array_search_insensitive("delta", $arrayLowerCase));
70 $this->assertTrue(array_search_insensitive("delta", $arrayUpperCase));
71 $this->assertTrue(array_search_insensitive("delta", $arrayMixed));
72 $this->assertFalse(array_search_insensitive("delta", $arrayEmpty));
75 public function test_object_to_array_recursive()
77 $simple = new SimpleObejct();
79 $notSimple = new NotSimpleObject();
80 $notSimple->setFoo(new SimpleObejct());
83 $simpleExpected = array('foo'=>'bar', 'b'=>1);
84 $notSimpleExpected = array('foo'=>array('foo'=>'bar', 'b'=>1), 'b'=>1);
85 $notObjectExpected = 'foo';
87 $this->assertEquals($simpleExpected, object_to_array_recursive($simple));
88 $this->assertEquals($notSimpleExpected, object_to_array_recursive($notSimple));
89 $this->assertEquals($notObjectExpected, object_to_array_recursive($notObject));
93 public function test_overide_value_to_string()
99 $expected = '$name[1] = 4;';
101 $this->assertEquals($expected, override_value_to_string($name, $value_name, $value));
105 //To do: test eval == true.
106 public function test_override_value_to_string_recursive()
108 $key_names = array(1, 2, 3, 4, 5);
114 $expectedNoEval = '$name[1][2][3][4][5]='."'".'foo'."';";
115 $expectedEval = true;
117 $this->assertEquals($expectedNoEval, override_value_to_string_recursive($key_names, $array_name, $value));
120 $array = override_value_to_string_recursive($key_names, $array_name, $value, true);
124 //array_name is never used in this function...
125 public function test_override_recursive_helper()
127 $key_names = array(1, 2, 3, 4, 5);
128 $array_name = 'name';
131 $expected = '$name[1][2][3][4][5]='."'".$value."';";
133 $this->assertEquals($expected, override_value_to_string_recursive($key_names, $array_name, $value));
136 //Todo: hit the if statement
137 public function test_setDeepArrayValue()
139 $arrayActualSimple = array(1=>'a');
140 setDeepArrayValue($arrayActualSimple, 1, 'b');
141 $arrayExpectedSimple = array(1=>'b');
143 $this->assertEquals($arrayExpectedSimple, $arrayActualSimple);
153 class NotSimpleObject
157 public function setFoo($input)