1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Diff\Tests\Patcher; |
4
|
|
|
|
5
|
|
|
use Diff\DiffOp\Diff\Diff; |
6
|
|
|
use Diff\DiffOp\DiffOpAdd; |
7
|
|
|
use Diff\DiffOp\DiffOpRemove; |
8
|
|
|
use Diff\Patcher\ListPatcher; |
9
|
|
|
use Diff\Patcher\Patcher; |
10
|
|
|
use Diff\Tests\DiffTestCase; |
11
|
|
|
use stdClass; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers Diff\Patcher\ListPatcher |
15
|
|
|
* @covers Diff\Patcher\ThrowingPatcher |
16
|
|
|
* |
17
|
|
|
* @group Diff |
18
|
|
|
* @group DiffPatcher |
19
|
|
|
* |
20
|
|
|
* @license GPL-2.0+ |
21
|
|
|
* @author Jeroen De Dauw < [email protected] > |
22
|
|
|
*/ |
23
|
|
|
class ListPatcherTest extends DiffTestCase { |
24
|
|
|
|
25
|
|
|
public function patchProvider() { |
26
|
|
|
$argLists = array(); |
27
|
|
|
|
28
|
|
|
$patcher = new ListPatcher(); |
29
|
|
|
$base = array(); |
30
|
|
|
$diff = new Diff(); |
31
|
|
|
$expected = array(); |
32
|
|
|
|
33
|
|
|
$argLists[] = array( $patcher, $base, $diff, $expected ); |
34
|
|
|
|
35
|
|
|
$patcher = new ListPatcher(); |
36
|
|
|
$base = array( 4, 2 ); |
37
|
|
|
$diff = new Diff(); |
38
|
|
|
$expected = array( 4, 2 ); |
39
|
|
|
|
40
|
|
|
$argLists[] = array( $patcher, $base, $diff, $expected ); |
41
|
|
|
|
42
|
|
|
$patcher = new ListPatcher(); |
43
|
|
|
$base = array(); |
44
|
|
|
$diff = new Diff( array( |
45
|
|
|
new DiffOpAdd( 9001 ) |
46
|
|
|
) ); |
47
|
|
|
$expected = array( 9001 ); |
48
|
|
|
|
49
|
|
|
$argLists[] = array( $patcher, $base, $diff, $expected ); |
50
|
|
|
|
51
|
|
|
$patcher = new ListPatcher(); |
52
|
|
|
$base = array( 4, 2 ); |
53
|
|
|
$diff = new Diff( array( |
54
|
|
|
new DiffOpAdd( 9001 ) |
55
|
|
|
) ); |
56
|
|
|
$expected = array( 4, 2, 9001 ); |
57
|
|
|
|
58
|
|
|
$argLists[] = array( $patcher, $base, $diff, $expected ); |
59
|
|
|
|
60
|
|
|
$patcher = new ListPatcher(); |
61
|
|
|
$base = array( 4, 2 ); |
62
|
|
|
$diff = new Diff( array( |
63
|
|
|
new DiffOpAdd( 9001 ), |
64
|
|
|
new DiffOpAdd( 9002 ), |
65
|
|
|
new DiffOpAdd( 2 ) |
66
|
|
|
) ); |
67
|
|
|
$expected = array( 4, 2, 9001, 9002, 2 ); |
68
|
|
|
|
69
|
|
|
$argLists[] = array( $patcher, $base, $diff, $expected ); |
70
|
|
|
|
71
|
|
|
$patcher = new ListPatcher(); |
72
|
|
|
$base = array( 0, 1, 2, 3, 4 ); |
73
|
|
|
$diff = new Diff( array( |
74
|
|
|
new DiffOpRemove( 2 ), |
75
|
|
|
new DiffOpRemove( 3 ), |
76
|
|
|
) ); |
77
|
|
|
$expected = array( 0, 1, 4 ); |
78
|
|
|
|
79
|
|
|
$argLists[] = array( $patcher, $base, $diff, $expected ); |
80
|
|
|
|
81
|
|
|
$patcher = new ListPatcher(); |
82
|
|
|
$base = array( 0, 1, 2, 2, 2, 3, 4 ); |
83
|
|
|
$diff = new Diff( array( |
84
|
|
|
new DiffOpRemove( 2 ), |
85
|
|
|
new DiffOpRemove( 3 ), |
86
|
|
|
new DiffOpAdd( 6 ), |
87
|
|
|
new DiffOpRemove( 2 ), |
88
|
|
|
) ); |
89
|
|
|
$expected = array( 0, 1, 2, 4, 6 ); |
90
|
|
|
|
91
|
|
|
$argLists[] = array( $patcher, $base, $diff, $expected ); |
92
|
|
|
|
93
|
|
|
$patcher = new ListPatcher(); |
94
|
|
|
$base = array( |
95
|
|
|
$this->newObject( 'foo' ), |
96
|
|
|
$this->newObject( 'bar' ), |
97
|
|
|
); |
98
|
|
|
$diff = new Diff( array( |
99
|
|
|
new DiffOpRemove( $this->newObject( 'foo' ) ), |
100
|
|
|
new DiffOpAdd( $this->newObject( 'baz' ) ), |
101
|
|
|
) ); |
102
|
|
|
$expected = array( $this->newObject( 'bar' ), $this->newObject( 'baz' ) ); |
103
|
|
|
|
104
|
|
|
$argLists[] = array( $patcher, $base, $diff, $expected ); |
105
|
|
|
|
106
|
|
|
return $argLists; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function newObject( $value ) { |
110
|
|
|
$object = new stdClass(); |
111
|
|
|
$object->element = $value; |
112
|
|
|
return $object; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @dataProvider patchProvider |
117
|
|
|
* |
118
|
|
|
* @param Patcher $patcher |
119
|
|
|
* @param array $base |
120
|
|
|
* @param Diff $diff |
121
|
|
|
* @param array $expected |
122
|
|
|
*/ |
123
|
|
|
public function testPatch( Patcher $patcher, array $base, Diff $diff, array $expected ) { |
124
|
|
|
$actual = $patcher->patch( $base, $diff ); |
125
|
|
|
|
126
|
|
|
$this->assertArrayEquals( $expected, $actual ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @dataProvider getApplicableDiffProvider |
131
|
|
|
* |
132
|
|
|
* @param Diff $diff |
133
|
|
|
* @param array $currentObject |
134
|
|
|
* @param Diff $expected |
135
|
|
|
* @param string|null $message |
136
|
|
|
*/ |
137
|
|
|
public function testGetApplicableDiff( Diff $diff, array $currentObject, Diff $expected, $message = null ) { |
138
|
|
|
$patcher = new ListPatcher(); |
139
|
|
|
$actual = $patcher->getApplicableDiff( $currentObject, $diff ); |
140
|
|
|
|
141
|
|
|
$this->assertEquals( $expected->getOperations(), $actual->getOperations(), $message ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getApplicableDiffProvider() { |
145
|
|
|
// Diff, current object, expected |
146
|
|
|
$argLists = array(); |
147
|
|
|
|
148
|
|
|
$diff = new Diff( array(), false ); |
149
|
|
|
$currentObject = array(); |
150
|
|
|
$expected = clone $diff; |
151
|
|
|
|
152
|
|
|
$argLists[] = array( $diff, $currentObject, $expected, 'Empty diff should remain empty on empty base' ); |
153
|
|
|
|
154
|
|
|
$diff = new Diff( array(), false ); |
155
|
|
|
|
156
|
|
|
$currentObject = array( 'foo' => 0, 'bar' => 1 ); |
157
|
|
|
|
158
|
|
|
$expected = clone $diff; |
159
|
|
|
|
160
|
|
|
$argLists[] = array( $diff, $currentObject, $expected, 'Empty diff should remain empty on non-empty base' ); |
161
|
|
|
|
162
|
|
|
$diff = new Diff( array( |
163
|
|
|
new DiffOpRemove( 9001 ), |
164
|
|
|
), false ); |
165
|
|
|
|
166
|
|
|
$currentObject = array(); |
167
|
|
|
|
168
|
|
|
$expected = new Diff( array(), false ); |
169
|
|
|
|
170
|
|
|
$argLists[] = array( $diff, $currentObject, $expected, 'Remove ops should be removed on empty base' ); |
171
|
|
|
|
172
|
|
|
$diff = new Diff( array( |
173
|
|
|
new DiffOpAdd( 42 ), |
174
|
|
|
new DiffOpRemove( 9001 ), |
175
|
|
|
), false ); |
176
|
|
|
|
177
|
|
|
$currentObject = array(); |
178
|
|
|
|
179
|
|
|
$expected = new Diff( array( |
180
|
|
|
new DiffOpAdd( 42 ), |
181
|
|
|
), true ); |
182
|
|
|
|
183
|
|
|
$argLists[] = array( |
184
|
|
|
$diff, |
185
|
|
|
$currentObject, |
186
|
|
|
$expected, |
187
|
|
|
'Add ops not present in the base should be retained (ListDiff)' |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$diff = new Diff( array( |
191
|
|
|
new DiffOpAdd( 42 ), |
192
|
|
|
new DiffOpRemove( 9001 ), |
193
|
|
|
), false ); |
194
|
|
|
|
195
|
|
|
$currentObject = array( 1, 42, 9001 ); |
196
|
|
|
|
197
|
|
|
$expected = new Diff( array( |
198
|
|
|
new DiffOpAdd( 42 ), |
199
|
|
|
new DiffOpRemove( 9001 ), |
200
|
|
|
), false ); |
201
|
|
|
|
202
|
|
|
$argLists[] = array( |
203
|
|
|
$diff, |
204
|
|
|
$currentObject, |
205
|
|
|
$expected, |
206
|
|
|
'Add ops with values present in the base should be retained in ListDiff' |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
$diff = new Diff( array( |
210
|
|
|
new DiffOpAdd( 42 ), |
211
|
|
|
), false ); |
212
|
|
|
|
213
|
|
|
$currentObject = array(); |
214
|
|
|
|
215
|
|
|
$expected = clone $diff; |
216
|
|
|
|
217
|
|
|
$argLists[] = array( |
218
|
|
|
$diff, |
219
|
|
|
$currentObject, |
220
|
|
|
$expected, |
221
|
|
|
'list diffs containing only add ops should be retained even when not in the base' |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
$diff = new Diff( array( |
225
|
|
|
new DiffOpRemove( 42 ), |
226
|
|
|
new DiffOpRemove( 9001 ), |
227
|
|
|
), false ); |
228
|
|
|
|
229
|
|
|
$currentObject = array( |
230
|
|
|
42, |
231
|
|
|
72010, |
232
|
|
|
9001, |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
$expected = clone $diff; |
236
|
|
|
|
237
|
|
|
$argLists[] = array( |
238
|
|
|
$diff, |
239
|
|
|
$currentObject, |
240
|
|
|
$expected, |
241
|
|
|
'list diffs containing only remove ops should be retained when present in the base' |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
return $argLists; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testPatchMapRaisesError() { |
248
|
|
|
$patcher = new ListPatcher(); |
249
|
|
|
|
250
|
|
|
$patcher->patch( array(), new Diff( array(), true ) ); |
251
|
|
|
|
252
|
|
|
$patcher->throwErrors(); |
253
|
|
|
$this->setExpectedException( 'Diff\Patcher\PatcherException' ); |
254
|
|
|
|
255
|
|
|
$patcher->patch( array(), new Diff( array(), true ) ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|