]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/utils/SugarArrayMergeTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / utils / SugarArrayMergeTest.php
1 <?php
2 require_once 'include/utils.php';
3
4 class SugarArrayMergeTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     /**
7      * @group bug17142
8      */
9     public function testSubArrayOrderIsPreserved() 
10     {
11         $array1 = array(
12             'dog' => array(
13                 'dog1' => 'dog1',
14                 'dog2' => 'dog2',
15                 'dog3' => 'dog3',
16                 'dog4' => 'dog4',
17                 )
18             );
19         
20         $array2 = array(
21             'dog' => array(
22                 'dog2' => 'dog2',
23                 'dog1' => 'dog1',
24                 'dog3' => 'dog3',
25                 'dog4' => 'dog4',
26                 )
27             );
28         
29         $results = sugarArrayMerge($array1,$array2);
30         
31         $keys1 = array_keys($results['dog']);
32         $keys2 = array_keys($array2['dog']);
33         
34         for ( $i = 0; $i < 4; $i++ ) {
35             $this->assertEquals($keys1[$i],$keys2[$i]);
36         }
37     }
38     
39     public function testSugarArrayMergeMergesTwoArraysWithLikeKeysOverwritingExistingKeys()
40     {
41         $foo = array(
42             'one' => 123,
43             'two' => 123,
44             'foo' => array(
45                 'int' => 123,
46                 'foo' => 'bar',
47             ),
48         );
49         $bar = array(
50             'one' => 123,
51             'two' => 321,
52             'foo' => array(
53                 'int' => 123,
54                 'bar' => 'foo',
55             ),
56         );
57         
58         $expected = array(
59             'one' => 123, 
60             'two' => 321,
61             'foo' => array(
62                 'int' => 123,
63                 'foo' => 'bar',
64                 'bar' => 'foo',
65             ),
66         );
67         $this->assertEquals(sugarArrayMerge($foo, $bar), $expected);
68         // insure that internal functions can't duplicate behavior
69         $this->assertNotEquals(array_merge($foo, $bar), $expected);
70         $this->assertNotEquals(array_merge_recursive($foo, $bar), $expected);
71     }
72 }