|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Axstrad library. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Dan Kempster <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Dan Kempster <[email protected]> |
|
11
|
|
|
* @package Axstrad\Common |
|
12
|
|
|
* @subpackage Tests |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace Axstrad\Common\Tests\Util\ArrayUtil; |
|
15
|
|
|
|
|
16
|
|
|
use Axstrad\Common\Util\ArrayUtil; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Axstrad\Common\Tests\Util\ArrayUtil\MergeRecursiveDistinctTest |
|
20
|
|
|
* |
|
21
|
|
|
* @cover Axstrad\Common\Util\ArrayUtil::mergeRecursiveDistinct |
|
22
|
|
|
* @group unittest |
|
23
|
|
|
*/ |
|
24
|
|
|
class MergeRecursiveDistinctTest extends \PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testNumericIndexesAreAppended() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->assertEquals( |
|
31
|
|
|
array( |
|
32
|
|
|
'one', 'two' |
|
33
|
|
|
), |
|
34
|
|
|
ArrayUtil::mergeRecursiveDistinct(array('one'), array('two')) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testDuplicateIndexesAreReplaced() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->assertEquals( |
|
43
|
|
|
array( |
|
44
|
|
|
'one'=>'three' |
|
45
|
|
|
), |
|
46
|
|
|
ArrayUtil::mergeRecursiveDistinct(array('one'=>'two'), array('one'=>'three')) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testArgumentsAreConvertedToArrays() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->assertEquals( |
|
55
|
|
|
array('one', 'two'), |
|
56
|
|
|
ArrayUtil::mergeRecursiveDistinct('one', 'two') |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testAcceptsAnyNumberOfArguments() |
|
63
|
|
|
{ |
|
64
|
|
|
$assertion = array(); |
|
65
|
|
|
$arguments = array(); |
|
66
|
|
|
for ($x=1; $x<=100; $x++) { |
|
67
|
|
|
$arguments[] = array('test '.$x); |
|
68
|
|
|
$assertion[] = 'test '.$x; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals( |
|
72
|
|
|
$assertion, |
|
73
|
|
|
call_user_func_array('Axstrad\Common\Util\ArrayUtil::mergeRecursiveDistinct', $arguments) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testArraysAreMergedRecursivly() |
|
80
|
|
|
{ |
|
81
|
|
|
$array1 = array( |
|
82
|
|
|
'one'=>array('one'=>'bar1'), |
|
83
|
|
|
'two'=>'foo1', |
|
84
|
|
|
); |
|
85
|
|
|
$array2 = array( |
|
86
|
|
|
'one'=>array('two'=>'bar2'), |
|
87
|
|
|
'three'=>'foo2' |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals( |
|
91
|
|
|
array( |
|
92
|
|
|
'one' => array( |
|
93
|
|
|
'one' => 'bar1', |
|
94
|
|
|
'two' => 'bar2' |
|
95
|
|
|
), |
|
96
|
|
|
'two' => 'foo1', |
|
97
|
|
|
'three' => 'foo2', |
|
98
|
|
|
), |
|
99
|
|
|
ArrayUtil::mergeRecursiveDistinct($array1, $array2) |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
*/ |
|
105
|
|
|
public function testIfOneArgumentIsArrayTreatAllAsArray() |
|
106
|
|
|
{ |
|
107
|
|
|
$array1 = array('replace'=>'one'); |
|
108
|
|
|
$array2 = array('replace'=>array('two')); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertEquals( |
|
111
|
|
|
array( |
|
112
|
|
|
'replace'=>array('one','two'), |
|
113
|
|
|
), |
|
114
|
|
|
ArrayUtil::mergeRecursiveDistinct($array1, $array2) |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
*/ |
|
120
|
|
|
public function testValuesOfIntegerKeysAreAppendedOnlyIfTheirValueDoesntAlreadyExistsWithIntegerKey() |
|
121
|
|
|
{ |
|
122
|
|
|
$array1 = array(0=>'bar', '1' => 'foo'); |
|
123
|
|
|
$array2 = array(2=>'bar', '3' => 'foo'); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals( |
|
126
|
|
|
array( |
|
127
|
|
|
0 => 'bar', |
|
128
|
|
|
1 => 'foo', |
|
129
|
|
|
), |
|
130
|
|
|
ArrayUtil::mergeRecursiveDistinct($array1, $array2) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|