Fixed: Not selecting a datalabel used to issue a notice(undefined offset)
[phpmyadmin/ammaryasirr.git] / test / libraries / core / PMA_array_test.php
blobaae83324bcd9c4095b6ae32a8ce361c10b00ccea
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Test for PMA_array_read(), PMA_array_write(), PMA_array_remove(), PMA_array_merge_recursive(),
5 * PMA_arrayWalkRecursive() from libraries/core.lib.php
7 * @package phpMyAdmin-test
8 */
11 * Include to test.
13 require_once 'libraries/core.lib.php';
15 class PMA_array_test extends PHPUnit_Framework_TestCase
17 function testPMA_array_read()
19 $arr = array(
20 "int" => 1,
21 "str" => "str_val",
22 "arr" => array('val1', 'val2', 'val3'),
23 "sarr" => array('arr1' => array(1, 2, 3), array(3, array('a', 'b', 'c'), 4))
26 $this->assertEquals(PMA_array_read('int', $arr), $arr['int']);
28 $this->assertEquals(PMA_array_read('str', $arr), $arr['str']);
30 $this->assertEquals(PMA_array_read('arr/0', $arr), $arr['arr'][0]);
32 $this->assertEquals(PMA_array_read('arr/1', $arr), $arr['arr'][1]);
34 $this->assertEquals(PMA_array_read('arr/2', $arr), $arr['arr'][2]);
36 $this->assertEquals(PMA_array_read('sarr/arr1/0', $arr), $arr['sarr']['arr1'][0]);
38 $this->assertEquals(PMA_array_read('sarr/arr1/1', $arr), $arr['sarr']['arr1'][1]);
40 $this->assertEquals(PMA_array_read('sarr/arr1/2', $arr), $arr['sarr']['arr1'][2]);
42 $this->assertEquals(PMA_array_read('sarr/0/0', $arr), $arr['sarr'][0][0]);
44 $this->assertEquals(PMA_array_read('sarr/0/1', $arr), $arr['sarr'][0][1]);
46 $this->assertEquals(PMA_array_read('sarr/0/1/2', $arr), $arr['sarr'][0][1][2]);
48 $this->assertEquals(PMA_array_read('sarr/not_exiting/1', $arr), null);
50 $this->assertEquals(PMA_array_read('sarr/not_exiting/1', $arr, 0), 0);
52 $this->assertEquals(PMA_array_read('sarr/not_exiting/1', $arr, 'defailt_val'), 'defailt_val');
55 function testPMA_array_write()
57 $arr = array(
58 "int" => 1,
59 "str" => "str_val",
60 "arr" => array('val1', 'val2', 'val3'),
61 "sarr" => array('arr1' => array(1, 2, 3), array(3, array('a', 'b', 'c'), 4))
64 PMA_array_write('int', $arr, 5);
65 $this->assertEquals($arr['int'], 5);
67 PMA_array_write('str', $arr, '_str');
68 $this->assertEquals($arr['str'], '_str');
70 PMA_array_write('arr/0', $arr, 'val_arr_0');
71 $this->assertEquals($arr['arr'][0], 'val_arr_0');
73 PMA_array_write('arr/1', $arr, 'val_arr_1');
74 $this->assertEquals($arr['arr'][1], 'val_arr_1');
76 PMA_array_write('arr/2', $arr, 'val_arr_2');
77 $this->assertEquals($arr['arr'][2], 'val_arr_2');
79 PMA_array_write('sarr/arr1/0', $arr, 'val_sarr_arr_0');
80 $this->assertEquals($arr['sarr']['arr1'][0], 'val_sarr_arr_0');
82 PMA_array_write('sarr/arr1/1', $arr, 'val_sarr_arr_1');
83 $this->assertEquals($arr['sarr']['arr1'][1], 'val_sarr_arr_1');
85 PMA_array_write('sarr/arr1/2', $arr, 'val_sarr_arr_2');
86 $this->assertEquals($arr['sarr']['arr1'][2], 'val_sarr_arr_2');
88 PMA_array_write('sarr/0/0', $arr, 5);
89 $this->assertEquals($arr['sarr'][0][0], 5);
91 PMA_array_write('sarr/0/1/0', $arr, 'e');
92 $this->assertEquals($arr['sarr'][0][1][0], 'e');
94 PMA_array_write('sarr/not_existing/1', $arr, 'some_val');
95 $this->assertEquals($arr['sarr']['not_existing'][1], 'some_val');
97 PMA_array_write('sarr/0/2', $arr, NULL);
98 $this->assertNull($arr['sarr'][0][2]);
101 function testPMA_array_remove()
103 $arr = array(
104 "int" => 1,
105 "str" => "str_val",
106 "arr" => array('val1', 'val2', 'val3'),
107 "sarr" => array('arr1' => array(1, 2, 3), array(3, array('a', 'b', 'c'), 4))
110 PMA_array_remove('int', $arr);
111 $this->assertArrayNotHasKey('int', $arr);
113 PMA_array_remove('str', $arr);
114 $this->assertArrayNotHasKey('str', $arr);
116 PMA_array_remove('arr/0', $arr);
117 $this->assertArrayNotHasKey(0, $arr['arr']);
119 PMA_array_remove('arr/1', $arr);
120 $this->assertArrayNotHasKey(1, $arr['arr']);
122 PMA_array_remove('arr/2', $arr);
123 $this->assertArrayNotHasKey('arr', $arr);
125 $tmp_arr = $arr;
126 PMA_array_remove('sarr/not_existing/1', $arr);
127 $this->assertEquals($tmp_arr, $arr);
129 PMA_array_remove('sarr/arr1/0', $arr);
130 $this->assertArrayNotHasKey(0, $arr['sarr']['arr1']);
132 PMA_array_remove('sarr/arr1/1', $arr);
133 $this->assertArrayNotHasKey(1, $arr['sarr']['arr1']);
135 PMA_array_remove('sarr/arr1/2', $arr);
136 $this->assertArrayNotHasKey('arr1', $arr['sarr']);
138 PMA_array_remove('sarr/0/0', $arr);
139 $this->assertArrayNotHasKey(0, $arr['sarr'][0]);
141 PMA_array_remove('sarr/0/1/0', $arr);
142 $this->assertArrayNotHasKey(0, $arr['sarr'][0][1]);
144 PMA_array_remove('sarr/0/1/1', $arr);
145 $this->assertArrayNotHasKey(1, $arr['sarr'][0][1]);
147 PMA_array_remove('sarr/0/1/2', $arr);
148 $this->assertArrayNotHasKey(1, $arr['sarr'][0]);
150 PMA_array_remove('sarr/0/2', $arr);
152 $this->assertEmpty($arr);
155 function testPMA_array_merge_recursive()
157 $arr1 = array('key1' => 1, 'key2' => 2.3, 'key3' => 'str3');
158 $arr2 = array('key1' => 4, 'key2' => 5, 'key3' => 6);
159 $arr3 = array('key4' => 7, 'key5' => 'str8', 'key6' => 9);
160 $arr4 = array(1, 2, 3);
162 $this->assertFalse(PMA_array_merge_recursive());
164 $this->assertEquals(PMA_array_merge_recursive($arr1), $arr1);
166 $this->assertEquals(PMA_array_merge_recursive($arr1, 'str'), 'str');
168 $this->assertEquals(PMA_array_merge_recursive('str1', $arr2), $arr2);
170 $this->assertEquals(PMA_array_merge_recursive($arr1, $arr2), array('key1' => 4, 'key2' => 5, 'key3' => 6));
172 $this->assertEquals(PMA_array_merge_recursive($arr1, $arr3), array('key1' => 1, 'key2' => 2.3, 'key3' => 'str3', 'key4' => 7, 'key5' => 'str8', 'key6' => 9));
174 $this->assertEquals(PMA_array_merge_recursive($arr2, $arr4), array(1, 2, 3));
176 $this->assertEquals(PMA_array_merge_recursive($arr1, $arr2, $arr3), array('key1' => 4, 'key2' => 5, 'key3' => 6, 'key4' => 7, 'key5' => 'str8', 'key6' => 9));
180 function testPMA_arrayWalkRecursive()
182 function fConcat($var)
184 return 'val: ' . $var . ' processed';
187 $arr = array(1, 2, 3, 4);
188 $target = array('val: 1 processed','val: 2 processed','val: 3 processed','val: 4 processed');
190 PMA_arrayWalkRecursive($arr, 'fConcat');
191 $this->assertEquals($arr, $target);
195 * @depends testPMA_arrayWalkRecursive
197 function testPMA_arrayWalkRecursiveNotProcessIntKeys()
199 function fAdd($var)
201 return ++$var;
204 $arr = array(1, 2, 3, 4);
205 $target = array(2, 3, 4, 5);
207 PMA_arrayWalkRecursive($arr, 'fAdd', true);
208 $this->assertEquals($arr, $target);
212 * @depends testPMA_arrayWalkRecursiveNotProcessIntKeys
214 function testPMA_arrayWalkRecursiveSubArray()
216 $arr = array("key1"=>'val1', 'key2'=>array('skey1'=>'sval1','skey2'=>'sval2'),'key3'=>'val3');
217 $target = array('key1'=>'val: val1 processed', 'key2'=> array('skey1'=>'val: sval1 processed', 'skey2'=>'val: sval2 processed'),'key3'=>'val: val3 processed');
219 PMA_arrayWalkRecursive($arr, 'fConcat');
220 $this->assertEquals($arr, $target);
223 function testPMA_arrayWalkRecursiveApplyToKeysStripSlashes()
225 $arr = array("key\\1"=>'v\\\\al1', 'k\\ey2'=>array('s\\\\key1'=>'sval\\1','s\\k\\ey2'=>'s\\v\\al2'),'key3'=>'val3');
226 $target = array("key1"=>'val1', 'key2'=>array('skey1'=>'sval1','skey2'=>'sval2'),'key3'=>'val3');
228 PMA_arrayWalkRecursive($arr, 'stripslashes',true);
229 $this->assertEquals($arr, $target);