1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Diff\Tests\Differ; |
4
|
|
|
|
5
|
|
|
use Diff\ArrayComparer\NativeArrayComparer; |
6
|
|
|
use Diff\ArrayComparer\StrictArrayComparer; |
7
|
|
|
use Diff\Differ\Differ; |
8
|
|
|
use Diff\Differ\ListDiffer; |
9
|
|
|
use Diff\DiffOp\DiffOpAdd; |
10
|
|
|
use Diff\DiffOp\DiffOpRemove; |
11
|
|
|
use Diff\Tests\DiffTestCase; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers Diff\Differ\ListDiffer |
15
|
|
|
* |
16
|
|
|
* @group Diff |
17
|
|
|
* @group Differ |
18
|
|
|
* |
19
|
|
|
* @license GPL-2.0+ |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
class ListDifferTest extends DiffTestCase { |
23
|
|
|
|
24
|
|
|
public function arrayComparerProvider() { |
25
|
|
|
$add = array( new DiffOpAdd( 1 ) ); |
26
|
|
|
|
27
|
|
|
return array( |
28
|
|
|
'null' => array( null, $add ), |
29
|
|
|
'native object' => array( new NativeArrayComparer(), array() ), |
30
|
|
|
'strict object' => array( new StrictArrayComparer(), $add ), |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @dataProvider arrayComparerProvider |
36
|
|
|
*/ |
37
|
|
|
public function testConstructor( $arrayComparer, array $expected ) { |
38
|
|
|
$differ = new ListDiffer( $arrayComparer ); |
39
|
|
|
$diff = $differ->doDiff( array( 1 ), array( 1, 1 ) ); |
40
|
|
|
$this->assertEquals( $expected, $diff ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns those that both work for native and strict mode. |
45
|
|
|
*/ |
46
|
|
|
private function getCommonArgLists() { |
47
|
|
|
$argLists = array(); |
48
|
|
|
|
49
|
|
|
$old = array(); |
50
|
|
|
$new = array(); |
51
|
|
|
$expected = array(); |
52
|
|
|
|
53
|
|
|
$argLists[] = array( $old, $new, $expected, |
54
|
|
|
'There should be no difference between empty arrays' ); |
55
|
|
|
|
56
|
|
|
$old = array( 42 ); |
57
|
|
|
$new = array( 42 ); |
58
|
|
|
$expected = array(); |
59
|
|
|
|
60
|
|
|
$argLists[] = array( $old, $new, $expected, |
61
|
|
|
'There should be no difference between arrays with the same element' ); |
62
|
|
|
|
63
|
|
|
$old = array( 42, 'ohi', 4.2, false ); |
64
|
|
|
$new = array( 42, 'ohi', 4.2, false ); |
65
|
|
|
$expected = array(); |
66
|
|
|
|
67
|
|
|
$argLists[] = array( $old, $new, $expected, |
68
|
|
|
'There should be no difference between arrays with the same elements' ); |
69
|
|
|
|
70
|
|
|
$old = array( 42, 'ohi', 4.2, false ); |
71
|
|
|
$new = array( false, 4.2, 'ohi', 42 ); |
72
|
|
|
$expected = array(); |
73
|
|
|
|
74
|
|
|
$argLists[] = array( $old, $new, $expected, |
75
|
|
|
'There should be no difference between arrays with the same elements even when not ordered the same' ); |
76
|
|
|
|
77
|
|
|
$old = array(); |
78
|
|
|
$new = array( 42 ); |
79
|
|
|
$expected = array( new DiffOpAdd( 42 ) ); |
80
|
|
|
|
81
|
|
|
$argLists[] = array( $old, $new, $expected, |
82
|
|
|
'An array with a single element should be an add operation different from an empty array' ); |
83
|
|
|
|
84
|
|
|
$old = array( 42 ); |
85
|
|
|
$new = array(); |
86
|
|
|
$expected = array( new DiffOpRemove( 42 ) ); |
87
|
|
|
|
88
|
|
|
$argLists[] = array( $old, $new, $expected, |
89
|
|
|
'An empty array should be a remove operation different from an array with one element' ); |
90
|
|
|
|
91
|
|
|
$old = array( 1 ); |
92
|
|
|
$new = array( 2 ); |
93
|
|
|
$expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); |
94
|
|
|
|
95
|
|
|
$argLists[] = array( $old, $new, $expected, |
96
|
|
|
'Two arrays with a single different element should differ by an add and a remove op' ); |
97
|
|
|
|
98
|
|
|
$old = array( 9001, 42, 1, 0 ); |
99
|
|
|
$new = array( 9001, 2, 0, 42 ); |
100
|
|
|
$expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); |
101
|
|
|
|
102
|
|
|
$argLists[] = array( |
103
|
|
|
$old, |
104
|
|
|
$new, |
105
|
|
|
$expected, |
106
|
|
|
'Two arrays with a single different element should differ by an add' |
107
|
|
|
. 'and a remove op even when they share identical elements' |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
return $argLists; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function toDiffProvider() { |
114
|
|
|
$argLists = $this->getCommonArgLists(); |
115
|
|
|
|
116
|
|
|
$old = array( 42, 42 ); |
117
|
|
|
$new = array( 42 ); |
118
|
|
|
$expected = array( new DiffOpRemove( 42 ) ); |
119
|
|
|
|
120
|
|
|
$argLists[] = array( $old, $new, $expected, |
121
|
|
|
'[42, 42] to [42] should [rem(42)]' ); |
122
|
|
|
|
123
|
|
|
$old = array( 42 ); |
124
|
|
|
$new = array( 42, 42 ); |
125
|
|
|
$expected = array( new DiffOpAdd( 42 ) ); |
126
|
|
|
|
127
|
|
|
$argLists[] = array( $old, $new, $expected, |
128
|
|
|
'[42] to [42, 42] should [add(42)]' ); |
129
|
|
|
|
130
|
|
|
$old = array( '42' ); |
131
|
|
|
$new = array( 42 ); |
132
|
|
|
$expected = array( new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) ); |
133
|
|
|
|
134
|
|
|
$argLists[] = array( $old, $new, $expected, |
135
|
|
|
'["42"] to [42] should [rem("42"), add(42)]' ); |
136
|
|
|
|
137
|
|
|
$old = array( array( 1 ) ); |
138
|
|
|
$new = array( array( 2 ) ); |
139
|
|
|
$expected = array( new DiffOpRemove( array( 1 ) ), new DiffOpAdd( array( 2 ) ) ); |
140
|
|
|
|
141
|
|
|
$argLists[] = array( $old, $new, $expected, |
142
|
|
|
'[[1]] to [[2]] should [rem([1]), add([2])]' ); |
143
|
|
|
|
144
|
|
|
$old = array( array( 2 ) ); |
145
|
|
|
$new = array( array( 2 ) ); |
146
|
|
|
$expected = array(); |
147
|
|
|
|
148
|
|
|
$argLists[] = array( $old, $new, $expected, |
149
|
|
|
'[[2]] to [[2]] should result in an empty diff' ); |
150
|
|
|
|
151
|
|
|
// test "soft" object comparison |
152
|
|
|
$obj1 = new \stdClass(); |
153
|
|
|
$obj2 = new \stdClass(); |
154
|
|
|
$objX = new \stdClass(); |
155
|
|
|
|
156
|
|
|
$obj1->test = 'Test'; |
157
|
|
|
$obj2->test = 'Test'; |
158
|
|
|
$objX->xest = 'Test'; |
159
|
|
|
|
160
|
|
|
$old = array( $obj1 ); |
161
|
|
|
$new = array( $obj2 ); |
162
|
|
|
$expected = array( ); |
163
|
|
|
|
164
|
|
|
$argLists[] = array( $old, $new, $expected, |
165
|
|
|
'Two arrays containing equivalent objects should result in an empty diff' ); |
166
|
|
|
|
167
|
|
|
$old = array( $obj1 ); |
168
|
|
|
$new = array( $objX ); |
169
|
|
|
$expected = array( new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX ) ); |
170
|
|
|
|
171
|
|
|
$argLists[] = array( $old, $new, $expected, |
172
|
|
|
'Two arrays containing different objects of the same type should result in an add and a remove op.' ); |
173
|
|
|
|
174
|
|
|
return $argLists; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @dataProvider toDiffProvider |
179
|
|
|
*/ |
180
|
|
|
public function testDoDiff( $old, $new, $expected, $message = '' ) { |
181
|
|
|
$this->doTestDiff( new ListDiffer(), $old, $new, $expected, $message ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function toDiffNativeProvider() { |
185
|
|
|
$argLists = $this->getCommonArgLists(); |
186
|
|
|
|
187
|
|
|
$old = array( '42' ); |
188
|
|
|
$new = array( 42 ); |
189
|
|
|
$expected = array(); |
190
|
|
|
|
191
|
|
|
$argLists[] = array( $old, $new, $expected, |
192
|
|
|
'["42"] to [42] should result in an empty diff' ); |
193
|
|
|
|
194
|
|
|
$old = array( 42, 42 ); |
195
|
|
|
$new = array( 42 ); |
196
|
|
|
$expected = array(); |
197
|
|
|
|
198
|
|
|
$argLists[] = array( $old, $new, $expected, |
199
|
|
|
'[42, 42] to [42] should result in an empty diff' ); |
200
|
|
|
|
201
|
|
|
$old = array( 42 ); |
202
|
|
|
$new = array( 42, 42 ); |
203
|
|
|
$expected = array(); |
204
|
|
|
|
205
|
|
|
$argLists[] = array( $old, $new, $expected, |
206
|
|
|
'[42] to [42, 42] should result in an empty diff' ); |
207
|
|
|
|
208
|
|
|
// TODO: test toString()-based object comparison |
209
|
|
|
|
210
|
|
|
return $argLists; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @dataProvider toDiffNativeProvider |
215
|
|
|
*/ |
216
|
|
|
public function testDoNativeDiff( $old, $new, $expected, $message = '' ) { |
217
|
|
|
$this->doTestDiff( new ListDiffer( new NativeArrayComparer() ), $old, $new, $expected, $message ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private function doTestDiff( Differ $differ, $old, $new, $expected, $message ) { |
221
|
|
|
$actual = $differ->doDiff( $old, $new ); |
222
|
|
|
|
223
|
|
|
$this->assertArrayEquals( $expected, $actual, false, false, $message ); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function testDiffCallsArrayComparatorCorrectly() { |
227
|
|
|
$arrayComparer = $this->createMock( 'Diff\ArrayComparer\ArrayComparer' ); |
228
|
|
|
|
229
|
|
|
$arrayComparer->expects( $this->exactly( 2 ) ) |
230
|
|
|
->method( 'diffArrays' ) |
231
|
|
|
->with( |
232
|
|
|
$this->equalTo( array( 42 ) ), |
233
|
|
|
$this->equalTo( array( 42 ) ) |
234
|
|
|
) |
235
|
|
|
->will( $this->returnValue( array() ) ); |
236
|
|
|
|
237
|
|
|
$differ = new ListDiffer( $arrayComparer ); |
238
|
|
|
|
239
|
|
|
$differ->doDiff( array( 42 ), array( 42 ) ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|