|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Tests\Diff; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\StringValue; |
|
6
|
|
|
use Diff\DiffOp\Diff\Diff; |
|
7
|
|
|
use Diff\DiffOp\DiffOpAdd; |
|
8
|
|
|
use Diff\DiffOp\DiffOpChange; |
|
9
|
|
|
use Diff\DiffOp\DiffOpRemove; |
|
10
|
|
|
use Wikibase\DataModel\Services\Diff\StatementListPatcher; |
|
11
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
|
12
|
|
|
use Wikibase\DataModel\Snak\PropertySomeValueSnak; |
|
13
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
|
14
|
|
|
use Wikibase\DataModel\Statement\Statement; |
|
15
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @covers Wikibase\DataModel\Services\Diff\StatementListPatcher |
|
19
|
|
|
* |
|
20
|
|
|
* @license GPL-2.0+ |
|
21
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
22
|
|
|
* @author Thiemo Mättig |
|
23
|
|
|
*/ |
|
24
|
|
|
class StatementListPatcherTest extends \PHPUnit_Framework_TestCase { |
|
25
|
|
|
|
|
26
|
|
|
public function patchStatementListProvider() { |
|
27
|
|
|
$statement1 = new Statement( new PropertyNoValueSnak( 1 ) ); |
|
28
|
|
|
$statement2 = new Statement( new PropertyNoValueSnak( 2 ) ); |
|
29
|
|
|
|
|
30
|
|
|
return array( |
|
31
|
|
|
// Empty diffs |
|
32
|
|
|
array( |
|
33
|
|
|
new StatementList(), |
|
34
|
|
|
new Diff(), |
|
35
|
|
|
new StatementList() |
|
36
|
|
|
), |
|
37
|
|
|
array( |
|
38
|
|
|
new StatementList( $statement1 ), |
|
39
|
|
|
new Diff(), |
|
40
|
|
|
new StatementList( $statement1 ) |
|
41
|
|
|
), |
|
42
|
|
|
|
|
43
|
|
|
// Add operations |
|
44
|
|
|
array( |
|
45
|
|
|
new StatementList(), |
|
46
|
|
|
new Diff( array( new DiffOpAdd( $statement1 ) ), true ), |
|
47
|
|
|
new StatementList( $statement1 ) |
|
48
|
|
|
), |
|
49
|
|
|
array( |
|
50
|
|
|
new StatementList(), |
|
51
|
|
|
new Diff( array( new DiffOpAdd( $statement1 ) ), false ), |
|
52
|
|
|
new StatementList( $statement1 ) |
|
53
|
|
|
), |
|
54
|
|
|
array( |
|
55
|
|
|
new StatementList(), |
|
56
|
|
|
new Diff( array( new DiffOpAdd( $statement1 ) ) ), |
|
57
|
|
|
new StatementList( $statement1 ) |
|
58
|
|
|
), |
|
59
|
|
|
|
|
60
|
|
|
// Remove operations |
|
61
|
|
|
array( |
|
62
|
|
|
new StatementList( $statement1 ), |
|
63
|
|
|
new Diff( array( new DiffOpRemove( $statement1 ) ), true ), |
|
64
|
|
|
new StatementList() |
|
65
|
|
|
), |
|
66
|
|
|
array( |
|
67
|
|
|
new StatementList( $statement1 ), |
|
68
|
|
|
new Diff( array( new DiffOpRemove( $statement1 ) ), false ), |
|
69
|
|
|
new StatementList() |
|
70
|
|
|
), |
|
71
|
|
|
array( |
|
72
|
|
|
new StatementList( $statement1 ), |
|
73
|
|
|
new Diff( array( new DiffOpRemove( $statement1 ) ) ), |
|
74
|
|
|
new StatementList() |
|
75
|
|
|
), |
|
76
|
|
|
|
|
77
|
|
|
// Mixed operations |
|
78
|
|
|
array( |
|
79
|
|
|
new StatementList( $statement1 ), |
|
80
|
|
|
new Diff( array( |
|
81
|
|
|
new DiffOpRemove( $statement1 ), |
|
82
|
|
|
new DiffOpAdd( $statement2 ), |
|
83
|
|
|
) ), |
|
84
|
|
|
new StatementList( $statement2 ) |
|
85
|
|
|
), |
|
86
|
|
|
array( |
|
87
|
|
|
new StatementList( $statement1 ), |
|
88
|
|
|
new Diff( array( |
|
89
|
|
|
new DiffOpChange( $statement1, $statement2 ), |
|
90
|
|
|
) ), |
|
91
|
|
|
new StatementList( $statement2 ) |
|
92
|
|
|
), |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @dataProvider patchStatementListProvider |
|
98
|
|
|
*/ |
|
99
|
|
|
public function testPatchStatementList( |
|
100
|
|
|
StatementList $statements, |
|
101
|
|
|
Diff $patch, |
|
102
|
|
|
StatementList $expected |
|
103
|
|
|
) { |
|
104
|
|
|
$patcher = new StatementListPatcher(); |
|
105
|
|
|
$patcher->patchStatementList( $statements, $patch ); |
|
106
|
|
|
$this->assertEquals( $expected, $statements ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
|
111
|
|
|
*/ |
|
112
|
|
|
public function statementOrderProvider() { |
|
113
|
|
|
$statement1 = new Statement( new PropertyNoValueSnak( 1 ), null, null, 's1' ); |
|
114
|
|
|
$statement2 = new Statement( new PropertyNoValueSnak( 2 ), null, null, 's2' ); |
|
115
|
|
|
$statement3 = new Statement( new PropertyNoValueSnak( 3 ), null, null, 's3' ); |
|
116
|
|
|
|
|
117
|
|
|
return array( |
|
118
|
|
|
'Simple associative add' => array( |
|
119
|
|
|
new StatementList(), |
|
120
|
|
|
new Diff( array( |
|
121
|
|
|
's1' => new DiffOpAdd( $statement1 ), |
|
122
|
|
|
), true ), |
|
123
|
|
|
array( 's1' ) |
|
124
|
|
|
), |
|
125
|
|
|
'Simple non-associative add' => array( |
|
126
|
|
|
new StatementList(), |
|
127
|
|
|
new Diff( array( |
|
128
|
|
|
's1' => new DiffOpAdd( $statement1 ), |
|
129
|
|
|
), false ), |
|
130
|
|
|
array( 's1' ) |
|
131
|
|
|
), |
|
132
|
|
|
'Simple associative remove' => array( |
|
133
|
|
|
new StatementList( $statement1 ), |
|
134
|
|
|
new Diff( array( |
|
135
|
|
|
's1' => new DiffOpRemove( $statement1 ), |
|
136
|
|
|
), true ), |
|
137
|
|
|
array() |
|
138
|
|
|
), |
|
139
|
|
|
'Simple non-associative remove' => array( |
|
140
|
|
|
new StatementList( $statement1 ), |
|
141
|
|
|
new Diff( array( |
|
142
|
|
|
's1' => new DiffOpRemove( $statement1 ), |
|
143
|
|
|
), false ), |
|
144
|
|
|
array() |
|
145
|
|
|
), |
|
146
|
|
|
|
|
147
|
|
|
// Change operations |
|
148
|
|
|
'Remove and add' => array( |
|
149
|
|
|
new StatementList( $statement1 ), |
|
150
|
|
|
new Diff( array( |
|
151
|
|
|
's1' => new DiffOpRemove( $statement1 ), |
|
152
|
|
|
's2' => new DiffOpAdd( $statement2 ), |
|
153
|
|
|
) ), |
|
154
|
|
|
array( 's2' ) |
|
155
|
|
|
), |
|
156
|
|
|
'Add and remove' => array( |
|
157
|
|
|
new StatementList( $statement1 ), |
|
158
|
|
|
new Diff( array( |
|
159
|
|
|
's2' => new DiffOpAdd( $statement2 ), |
|
160
|
|
|
's1' => new DiffOpRemove( $statement1 ), |
|
161
|
|
|
) ), |
|
162
|
|
|
array( 's2' ) |
|
163
|
|
|
), |
|
164
|
|
|
'Simple associative replace' => array( |
|
165
|
|
|
new StatementList( $statement1 ), |
|
166
|
|
|
new Diff( array( |
|
167
|
|
|
's1' => new DiffOpChange( $statement1, $statement2 ), |
|
168
|
|
|
), true ), |
|
169
|
|
|
array( 's2' ) |
|
170
|
|
|
), |
|
171
|
|
|
'Simple non-associative replace' => array( |
|
172
|
|
|
new StatementList( $statement1 ), |
|
173
|
|
|
new Diff( array( |
|
174
|
|
|
's1' => new DiffOpChange( $statement1, $statement2 ), |
|
175
|
|
|
), false ), |
|
176
|
|
|
array( 's2' ) |
|
177
|
|
|
), |
|
178
|
|
|
'Replacing first element retains order' => array( |
|
179
|
|
|
new StatementList( $statement1, $statement2 ), |
|
180
|
|
|
new Diff( array( |
|
181
|
|
|
's1' => new DiffOpChange( $statement1, $statement3 ), |
|
182
|
|
|
) ), |
|
183
|
|
|
array( 's3', 's2' ) |
|
184
|
|
|
), |
|
185
|
|
|
'Replacing last element retains order' => array( |
|
186
|
|
|
new StatementList( $statement1, $statement2 ), |
|
187
|
|
|
new Diff( array( |
|
188
|
|
|
's2' => new DiffOpChange( $statement2, $statement3 ), |
|
189
|
|
|
) ), |
|
190
|
|
|
array( 's1', 's3' ) |
|
191
|
|
|
), |
|
192
|
|
|
|
|
193
|
|
|
// No-ops |
|
194
|
|
|
'Empty diff' => array( |
|
195
|
|
|
new StatementList( $statement1 ), |
|
196
|
|
|
new Diff(), |
|
197
|
|
|
array( 's1' ) |
|
198
|
|
|
), |
|
199
|
|
|
'Adding existing element is no-op' => array( |
|
200
|
|
|
new StatementList( $statement1 ), |
|
201
|
|
|
new Diff( array( |
|
202
|
|
|
's1' => new DiffOpAdd( $statement1 ), |
|
203
|
|
|
) ), |
|
204
|
|
|
array( 's1' ) |
|
205
|
|
|
), |
|
206
|
|
|
'Removing non-existing element is no-op' => array( |
|
207
|
|
|
new StatementList( $statement1 ), |
|
208
|
|
|
new Diff( array( |
|
209
|
|
|
's2' => new DiffOpRemove( $statement2 ), |
|
210
|
|
|
) ), |
|
211
|
|
|
array( 's1' ) |
|
212
|
|
|
), |
|
213
|
|
|
'Replacing non-existing element is no-op' => array( |
|
214
|
|
|
new StatementList( $statement1 ), |
|
215
|
|
|
new Diff( array( |
|
216
|
|
|
's2' => new DiffOpChange( $statement2, $statement3 ), |
|
217
|
|
|
) ), |
|
218
|
|
|
array( 's1' ) |
|
219
|
|
|
), |
|
220
|
|
|
); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @dataProvider statementOrderProvider |
|
225
|
|
|
*/ |
|
226
|
|
|
public function testStatementOrder( StatementList $statements, Diff $patch, array $expectedGuids ) { |
|
227
|
|
|
$patcher = new StatementListPatcher(); |
|
228
|
|
|
$patcher->patchStatementList( $statements, $patch ); |
|
229
|
|
|
|
|
230
|
|
|
$guids = array(); |
|
231
|
|
|
foreach ( $statements->toArray() as $statement ) { |
|
232
|
|
|
$guids[] = $statement->getGuid(); |
|
233
|
|
|
} |
|
234
|
|
|
$this->assertSame( $expectedGuids, $guids ); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function testGivenEmptyDiff_listIsReturnedAsIs() { |
|
238
|
|
|
$statements = new StatementList(); |
|
239
|
|
|
|
|
240
|
|
|
$this->assertListResultsFromPatch( $statements, $statements, new Diff() ); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
private function assertListResultsFromPatch( |
|
244
|
|
|
StatementList $expected, |
|
245
|
|
|
StatementList $statements, |
|
246
|
|
|
Diff $patch |
|
247
|
|
|
) { |
|
248
|
|
|
$patcher = new StatementListPatcher(); |
|
249
|
|
|
$patcher->patchStatementList( $statements, $patch ); |
|
250
|
|
|
$this->assertEquals( $expected, $statements ); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function testFoo() { |
|
254
|
|
|
$statement0 = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
255
|
|
|
$statement0->setGuid( 's0' ); |
|
256
|
|
|
|
|
257
|
|
|
$statement1 = new Statement( new PropertySomeValueSnak( 42 ) ); |
|
258
|
|
|
$statement1->setGuid( 's1' ); |
|
259
|
|
|
|
|
260
|
|
|
$statement2 = new Statement( new PropertyValueSnak( 42, new StringValue( 'ohi' ) ) ); |
|
261
|
|
|
$statement2->setGuid( 's2' ); |
|
262
|
|
|
|
|
263
|
|
|
$statement3 = new Statement( new PropertyNoValueSnak( 1 ) ); |
|
264
|
|
|
$statement3->setGuid( 's3' ); |
|
265
|
|
|
|
|
266
|
|
|
$patch = new Diff( array( |
|
267
|
|
|
's0' => new DiffOpRemove( $statement0 ), |
|
268
|
|
|
's2' => new DiffOpAdd( $statement2 ), |
|
269
|
|
|
's3' => new DiffOpAdd( $statement3 ) |
|
270
|
|
|
) ); |
|
271
|
|
|
|
|
272
|
|
|
$source = new StatementList(); |
|
273
|
|
|
$source->addStatement( $statement0 ); |
|
274
|
|
|
$source->addStatement( $statement1 ); |
|
275
|
|
|
|
|
276
|
|
|
$expected = new StatementList(); |
|
277
|
|
|
$expected->addStatement( $statement1 ); |
|
278
|
|
|
$expected->addStatement( $statement2 ); |
|
279
|
|
|
$expected->addStatement( $statement3 ); |
|
280
|
|
|
|
|
281
|
|
|
$this->assertListResultsFromPatch( $expected, $source, $patch ); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
} |
|
285
|
|
|
|