]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/utils/SugarArrayUtilsTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / include / utils / SugarArrayUtilsTest.php
1 <?php 
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37  
38 require_once 'include/utils/array_utils.php';
39
40 class SugarArrayUtilsTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42         
43         public function test_array_merge_values()
44         {       
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");
49                 
50                 $expectedResult12 = array("ax","by","cz");
51                 $expectedResult13 = array("a1", "b2", "c3");
52                 $expectedResult14 = false;
53                 
54                 
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));
58                         
59         }
60         
61         
62         public function test_array_search_insensitive()
63         {
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();
68                 
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));     
73         }
74         
75         public function test_object_to_array_recursive()
76         {
77                 $simple = new SimpleObejct();
78                 
79                 $notSimple = new NotSimpleObject();
80                 $notSimple->setFoo(new SimpleObejct());
81                 $notObject = "foo";
82                 
83                 $simpleExpected = array('foo'=>'bar', 'b'=>1);
84                 $notSimpleExpected = array('foo'=>array('foo'=>'bar', 'b'=>1), 'b'=>1);
85                 $notObjectExpected = 'foo';
86                 
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));
90                 
91         }
92         
93         public function test_overide_value_to_string()
94         {
95                 $name = 'name';
96                 $value_name = 1;
97                 $value = 4;
98                 
99                 $expected = '$name[1] = 4;';
100                 
101                 $this->assertEquals($expected, override_value_to_string($name, $value_name, $value));
102                 
103         }
104         
105         //To do: test eval == true.
106         public function test_override_value_to_string_recursive()
107         {
108                 $key_names = array(1, 2, 3, 4, 5);
109                 global $array_name; 
110                 $array_name= 'name';
111                 $value = 'foo';
112         
113                 
114                 $expectedNoEval = '$name[1][2][3][4][5]='."'".'foo'."';";
115                 $expectedEval = true;
116                 
117                 $this->assertEquals($expectedNoEval, override_value_to_string_recursive($key_names, $array_name, $value));
118                 global $name;
119                 
120                 $array = override_value_to_string_recursive($key_names, $array_name, $value, true);
121         } 
122         
123         
124         //array_name is never used in this function...
125         public function test_override_recursive_helper()
126         {
127                 $key_names = array(1, 2, 3, 4, 5);
128                 $array_name = 'name';
129                 $value = 'foo';
130                 
131                 $expected = '$name[1][2][3][4][5]='."'".$value."';";
132                 
133                 $this->assertEquals($expected, override_value_to_string_recursive($key_names, $array_name, $value));    
134         } 
135
136         //Todo: hit the if statement
137         public function test_setDeepArrayValue()
138         {
139                 $arrayActualSimple = array(1=>'a');
140                 setDeepArrayValue($arrayActualSimple, 1, 'b');
141                 $arrayExpectedSimple = array(1=>'b');
142                 
143                 $this->assertEquals($arrayExpectedSimple, $arrayActualSimple);  
144         }       
145 }
146
147 class SimpleObejct
148 {
149         public $foo = 'bar';
150         public $b = 1;
151 }
152
153 class NotSimpleObject
154 {
155         public $foo;
156         public $b = 1;
157         public function setFoo($input)
158         {
159                 $this->foo = $input;
160         }
161 }