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