1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Database\Tests\MediaWiki; |
4
|
|
|
|
5
|
|
|
use DatabaseBase; |
6
|
|
|
use Wikibase\Database\MediaWiki\DBConnectionProvider; |
7
|
|
|
use Wikibase\Database\MediaWiki\MediaWikiQueryInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Wikibase\Database\MediaWiki\MediaWikiQueryInterface |
11
|
|
|
* |
12
|
|
|
* @group Wikibase |
13
|
|
|
* @group WikibaseDatabase |
14
|
|
|
* @group WikibaseDatabaseMediawiki |
15
|
|
|
* |
16
|
|
|
* @licence GNU GPL v2+ |
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
18
|
|
|
*/ |
19
|
|
|
class MediaWikiQueryInterfaceTest extends \PHPUnit_Framework_TestCase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @dataProvider tableNameProvider |
23
|
|
|
* |
24
|
|
|
* @param string $tableName |
25
|
|
|
*/ |
26
|
|
|
public function testTableExists( $tableName ) { |
27
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
28
|
|
|
|
29
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
30
|
|
|
new DirectConnectionProvider( $connection ) |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$connection->expects( $this->once() ) |
34
|
|
|
->method( 'tableExists' ) |
35
|
|
|
->with( $this->equalTo( $tableName ) ); |
36
|
|
|
|
37
|
|
|
$queryInterface->tableExists( $tableName ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function tableNameProvider() { |
41
|
|
|
$argLists = array(); |
42
|
|
|
|
43
|
|
|
$argLists[] = array( 'user' ); |
44
|
|
|
$argLists[] = array( 'xdgxftjhreyetfytj' ); |
45
|
|
|
$argLists[] = array( 'a' ); |
46
|
|
|
$argLists[] = array( 'foo_bar_baz_bah' ); |
47
|
|
|
|
48
|
|
|
return $argLists; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider insertProvider |
53
|
|
|
*/ |
54
|
|
|
public function testInsert( $tableName, array $fieldValues ) { |
55
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
56
|
|
|
|
57
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
58
|
|
|
new DirectConnectionProvider( $connection ) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$connection->expects( $this->once() ) |
62
|
|
|
->method( 'insert' ) |
63
|
|
|
->with( |
64
|
|
|
$this->equalTo( $tableName ), |
65
|
|
|
$this->equalTo( $fieldValues ) |
66
|
|
|
) |
67
|
|
|
->will( $this->returnValue( true ) ); |
68
|
|
|
|
69
|
|
|
$queryInterface->insert( |
70
|
|
|
$tableName, |
71
|
|
|
$fieldValues |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @dataProvider insertProvider |
77
|
|
|
*/ |
78
|
|
|
public function testInsertFailure( $tableName, array $fieldValues ) { |
79
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
80
|
|
|
|
81
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
82
|
|
|
new DirectConnectionProvider( $connection ) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$connection->expects( $this->once() ) |
86
|
|
|
->method( 'insert' ) |
87
|
|
|
->will( $this->returnValue( false ) ); |
88
|
|
|
|
89
|
|
|
$this->setExpectedException( '\Wikibase\Database\QueryInterface\InsertFailedException' ); |
90
|
|
|
|
91
|
|
|
$queryInterface->insert( |
92
|
|
|
$tableName, |
93
|
|
|
$fieldValues |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function insertProvider() { |
98
|
|
|
$argLists = array(); |
99
|
|
|
|
100
|
|
|
$argLists[] = array( 'foo', array() ); |
101
|
|
|
$argLists[] = array( 'bar', array( 'intfield', ) ); |
102
|
|
|
$argLists[] = array( 'baz', array( 'intfield', 'textfield', ) ); |
103
|
|
|
|
104
|
|
|
return $argLists; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @dataProvider updateProvider |
109
|
|
|
*/ |
110
|
|
|
public function testUpdate( $tableName, array $newValues, array $conditions ) { |
111
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
112
|
|
|
|
113
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
114
|
|
|
new DirectConnectionProvider( $connection ) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$connection->expects( $this->once() ) |
118
|
|
|
->method( 'update' ) |
119
|
|
|
->with( |
120
|
|
|
$this->equalTo( $tableName ), |
121
|
|
|
$this->equalTo( $newValues ), |
122
|
|
|
$this->equalTo( $conditions ) |
123
|
|
|
) |
124
|
|
|
->will( $this->returnValue( true ) ); |
125
|
|
|
|
126
|
|
|
$queryInterface->update( |
127
|
|
|
$tableName, |
128
|
|
|
$newValues, |
129
|
|
|
$conditions |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @dataProvider updateProvider |
135
|
|
|
*/ |
136
|
|
|
public function testUpdateFailure( $tableName, array $newValues, array $conditions ) { |
137
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
138
|
|
|
|
139
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
140
|
|
|
new DirectConnectionProvider( $connection ) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$connection->expects( $this->once() ) |
144
|
|
|
->method( 'update' ) |
145
|
|
|
->will( $this->returnValue( false ) ); |
146
|
|
|
|
147
|
|
|
$this->setExpectedException( '\Wikibase\Database\QueryInterface\UpdateFailedException' ); |
148
|
|
|
|
149
|
|
|
$queryInterface->update( |
150
|
|
|
$tableName, |
151
|
|
|
$newValues, |
152
|
|
|
$conditions |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function updateProvider() { |
157
|
|
|
$argLists = array(); |
158
|
|
|
|
159
|
|
|
$argLists[] = array( |
160
|
|
|
'foo', |
161
|
|
|
array( 'intfield', 'textfield', ), |
162
|
|
|
array( |
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$argLists[] = array( |
167
|
|
|
'foo', |
168
|
|
|
array( 'textfield', ), |
169
|
|
|
array( 'intfield' ) |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$argLists[] = array( |
173
|
|
|
'foo', |
174
|
|
|
array( 'textfield', 'intfield', 'floatfield', ), |
175
|
|
|
array( 'textfield', 'floatfield', ) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
return $argLists; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @dataProvider deleteProvider |
183
|
|
|
*/ |
184
|
|
|
public function testDelete( $tableName, array $conditions ) { |
185
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
186
|
|
|
|
187
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
188
|
|
|
new DirectConnectionProvider( $connection ) |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$connection->expects( $this->once() ) |
192
|
|
|
->method( 'delete' ) |
193
|
|
|
->with( |
194
|
|
|
$this->equalTo( $tableName ), |
195
|
|
|
$this->equalTo( $conditions ) |
196
|
|
|
) |
197
|
|
|
->will( $this->returnValue( true ) ); |
198
|
|
|
|
199
|
|
|
$queryInterface->delete( $tableName, $conditions ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @dataProvider deleteProvider |
204
|
|
|
*/ |
205
|
|
|
public function testDeleteFailure( $tableName, array $conditions ) { |
206
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
207
|
|
|
|
208
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
209
|
|
|
new DirectConnectionProvider( $connection ) |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$connection->expects( $this->once() ) |
213
|
|
|
->method( 'delete' ) |
214
|
|
|
->will( $this->returnValue( false ) ); |
215
|
|
|
|
216
|
|
|
$this->setExpectedException( '\Wikibase\Database\Exception\DeleteFailedException' ); |
217
|
|
|
|
218
|
|
|
$queryInterface->delete( $tableName, $conditions ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function deleteProvider() { |
222
|
|
|
$argLists = array(); |
223
|
|
|
|
224
|
|
|
$argLists[] = array( 'foo', array() ); |
225
|
|
|
|
226
|
|
|
$argLists[] = array( 'bar', array( |
227
|
|
|
'intfield' => 42, |
228
|
|
|
) ); |
229
|
|
|
|
230
|
|
|
$argLists[] = array( 'baz', array( |
231
|
|
|
'intfield' => 42, |
232
|
|
|
'textfield' => '~=[,,_,,]:3', |
233
|
|
|
) ); |
234
|
|
|
|
235
|
|
|
return $argLists; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function testGetInsertId() { |
239
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
240
|
|
|
|
241
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
242
|
|
|
new DirectConnectionProvider( $connection ) |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
$connection->expects( $this->once() ) |
246
|
|
|
->method( 'insertId' ) |
247
|
|
|
->will( $this->returnValue( 42 ) ); |
248
|
|
|
|
249
|
|
|
$this->assertEquals( 42, $queryInterface->getInsertId() ); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @dataProvider selectProvider |
254
|
|
|
*/ |
255
|
|
|
public function testSelect( $tableName, array $fields, array $conditions ) { |
256
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
257
|
|
|
|
258
|
|
|
$queryInterface = new MediaWikiQueryInterface( |
259
|
|
|
new DirectConnectionProvider( $connection ) |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
$resultWrapper = $this->getMockBuilder( 'ResultWrapper' ) |
263
|
|
|
->disableOriginalConstructor()->getMock(); |
264
|
|
|
|
265
|
|
|
$connection->expects( $this->once() ) |
266
|
|
|
->method( 'select' ) |
267
|
|
|
->with( |
268
|
|
|
$this->equalTo( $tableName ), |
269
|
|
|
$this->equalTo( $fields ), |
270
|
|
|
$this->equalTo( $conditions ) |
271
|
|
|
) |
272
|
|
|
->will( $this->returnValue( $resultWrapper ) ); |
273
|
|
|
|
274
|
|
|
$queryInterface->select( $tableName, $fields, $conditions ); |
275
|
|
|
|
276
|
|
|
// Ideally we would have the select method result a mock ResultWrapper |
277
|
|
|
// and would assert if the data was present in the selection result. |
278
|
|
|
// It however seems somewhat impossible to create a mock of ResultWrapper. |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function selectProvider() { |
282
|
|
|
$argLists = array(); |
283
|
|
|
|
284
|
|
|
$argLists[] = array( |
285
|
|
|
'table', |
286
|
|
|
array( |
287
|
|
|
'foo', |
288
|
|
|
'bar', |
289
|
|
|
'baz', |
290
|
|
|
), |
291
|
|
|
array( |
292
|
|
|
'intfield' => 42, |
293
|
|
|
'strfield' => 'nyan', |
294
|
|
|
) |
295
|
|
|
); |
296
|
|
|
|
297
|
|
|
$argLists[] = array( |
298
|
|
|
'table', |
299
|
|
|
array( |
300
|
|
|
'foo', |
301
|
|
|
'bar', |
302
|
|
|
'baz', |
303
|
|
|
), |
304
|
|
|
array( |
305
|
|
|
) |
306
|
|
|
); |
307
|
|
|
|
308
|
|
|
$argLists[] = array( |
309
|
|
|
'onoez', |
310
|
|
|
array( |
311
|
|
|
'foo', |
312
|
|
|
), |
313
|
|
|
array( |
314
|
|
|
'intfield' => 42, |
315
|
|
|
) |
316
|
|
|
); |
317
|
|
|
|
318
|
|
|
return $argLists; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function testSelectFailure() { |
322
|
|
|
$this->setExpectedException( 'Wikibase\Database\Exception\SelectFailedException' ); |
323
|
|
|
$connection = $this->getMockBuilder( 'DatabaseMysql' )->disableOriginalConstructor()->getMock(); |
324
|
|
|
$connection->expects( $this->once() ) |
325
|
|
|
->method( 'select' ) |
326
|
|
|
->will( $this->returnValue( 'FOOBAR' ) ); |
327
|
|
|
|
328
|
|
|
$queryInterface = new MediaWikiQueryInterface( new DirectConnectionProvider( $connection ) ); |
329
|
|
|
$queryInterface->select( 'ham', array( 'egg' ), array( 'chips' ) ); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
class DirectConnectionProvider implements DBConnectionProvider { |
335
|
|
|
|
336
|
|
|
private $connection; |
337
|
|
|
|
338
|
|
|
public function __construct( DatabaseBase $connection ) { |
339
|
|
|
$this->connection = $connection; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @see DBConnectionProvider::getConnection |
344
|
|
|
* |
345
|
|
|
* @since 0.1 |
346
|
|
|
* |
347
|
|
|
* @return DatabaseBase |
348
|
|
|
*/ |
349
|
|
|
public function getConnection() { |
350
|
|
|
return $this->connection; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @see DBConnectionProvider::releaseConnection |
355
|
|
|
* |
356
|
|
|
* @since 0.1 |
357
|
|
|
*/ |
358
|
|
|
public function releaseConnection() { |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
|