Completed
Push — master ( 617702...1dd00f )
by mw
36:38 queued 19:59
created

DatabaseTest::testDisableEnableTransactions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 29
nc 1
nop 0
dl 0
loc 41
rs 8.8571
c 1
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\MediaWiki;
4
5
use SMW\MediaWiki\Database;
6
7
/**
8
 * @covers \SMW\MediaWiki\Database
9
 * @group semantic-mediawiki
10
 *
11
 * @license GNU GPL v2+
12
 * @since 1.9.0.2
13
 *
14
 * @author mwjames
15
 */
16
class DatabaseTest extends \PHPUnit_Framework_TestCase {
17
18
	public function testCanConstruct() {
19
20
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
21
			->disableOriginalConstructor()
22
			->getMock();
23
24
		$this->assertInstanceOf(
25
			'\SMW\MediaWiki\Database',
26
			new Database( $connectionProvider )
27
		);
28
	}
29
30
	public function testNumRowsMethod() {
31
32
		$database = $this->getMockBuilder( '\DatabaseBase' )
33
			->disableOriginalConstructor()
34
			->setMethods( array( 'numRows' ) )
35
			->getMockForAbstractClass();
36
37
		$database->expects( $this->once() )
38
			->method( 'numRows' )
39
			->with( $this->equalTo( 'Fuyu' ) )
40
			->will( $this->returnValue( 1 ) );
41
42
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
43
			->disableOriginalConstructor()
44
			->getMock();
45
46
		$connectionProvider->expects( $this->atLeastOnce() )
47
			->method( 'getConnection' )
48
			->will( $this->returnValue( $database ) );
49
50
		$instance = new Database( $connectionProvider );
51
52
		$this->assertEquals(
53
			1,
54
			$instance->numRows( 'Fuyu' )
55
		);
56
	}
57
58
	public function testAddQuotesMethod() {
59
60
		$database = $this->getMockBuilder( '\DatabaseBase' )
61
			->disableOriginalConstructor()
62
			->setMethods( array( 'addQuotes' ) )
63
			->getMockForAbstractClass();
64
65
		$database->expects( $this->once() )
66
			->method( 'addQuotes' )
67
			->with( $this->equalTo( 'Fan' ) )
68
			->will( $this->returnValue( 'Fan' ) );
69
70
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
71
			->disableOriginalConstructor()
72
			->getMock();
73
74
		$connectionProvider->expects( $this->atLeastOnce() )
75
			->method( 'getConnection' )
76
			->will( $this->returnValue( $database ) );
77
78
		$instance = new Database( $connectionProvider );
79
80
		$this->assertEquals(
81
			'Fan',
82
			$instance->addQuotes( 'Fan' )
83
		);
84
	}
85
86
	/**
87
	 * @dataProvider dbTypeProvider
88
	 */
89
	public function testTableNameMethod( $type ) {
90
91
		$database = $this->getMockBuilder( '\DatabaseBase' )
92
			->disableOriginalConstructor()
93
			->setMethods( array( 'tableName', 'getType' ) )
94
			->getMockForAbstractClass();
95
96
		$database->expects( $this->any() )
97
			->method( 'tableName' )
98
			->with( $this->equalTo( 'Foo' ) )
99
			->will( $this->returnValue( 'Foo' ) );
100
101
		$database->expects( $this->once() )
102
			->method( 'getType' )
103
			->will( $this->returnValue( $type ) );
104
105
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
106
			->disableOriginalConstructor()
107
			->getMock();
108
109
		$connectionProvider->expects( $this->atLeastOnce() )
110
			->method( 'getConnection' )
111
			->will( $this->returnValue( $database ) );
112
113
		$instance = new Database( $connectionProvider );
114
		$instance->setDBPrefix( 'bar_' );
115
116
		$expected = $type === 'sqlite' ? 'bar_Foo' : 'Foo';
117
118
		$this->assertEquals(
119
			$expected,
120
			$instance->tableName( 'Foo' )
121
		);
122
	}
123
124
	public function testSelectMethod() {
125
126
		$resultWrapper = $this->getMockBuilder( 'ResultWrapper' )
127
			->disableOriginalConstructor()
128
			->getMock();
129
130
		$database = $this->getMockBuilder( '\DatabaseBase' )
131
			->disableOriginalConstructor()
132
			->setMethods( array( 'select' ) )
133
			->getMockForAbstractClass();
134
135
		$database->expects( $this->once() )
136
			->method( 'select' )
137
			->will( $this->returnValue( $resultWrapper ) );
138
139
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
140
			->disableOriginalConstructor()
141
			->getMock();
142
143
		$connectionProvider->expects( $this->atLeastOnce() )
144
			->method( 'getConnection' )
145
			->will( $this->returnValue( $database ) );
146
147
		$instance = new Database( $connectionProvider );
148
149
		$this->assertInstanceOf(
150
			'ResultWrapper',
151
			$instance->select( 'Foo', 'Bar', '', __METHOD__ )
152
		);
153
	}
154
155
	public function testSelectFieldMethod() {
156
157
		$database = $this->getMockBuilder( '\DatabaseBase' )
158
			->disableOriginalConstructor()
159
			->setMethods( array( 'selectField' ) )
160
			->getMockForAbstractClass();
161
162
		$database->expects( $this->once() )
163
			->method( 'selectField' )
164
			->with( $this->equalTo( 'Foo' ) )
165
			->will( $this->returnValue( 'Bar' ) );
166
167
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
168
			->disableOriginalConstructor()
169
			->getMock();
170
171
		$connectionProvider->expects( $this->atLeastOnce() )
172
			->method( 'getConnection' )
173
			->will( $this->returnValue( $database ) );
174
175
		$instance = new Database( $connectionProvider );
176
177
		$this->assertEquals(
178
			'Bar',
179
			$instance->selectField( 'Foo', 'Bar', '', __METHOD__, array() )
180
		);
181
	}
182
183
	/**
184
	 * @dataProvider querySqliteProvider
185
	 */
186
	public function testQueryOnSQLite( $query, $expected ) {
187
188
		$resultWrapper = $this->getMockBuilder( 'ResultWrapper' )
189
			->disableOriginalConstructor()
190
			->getMock();
191
192
		$read = $this->getMockBuilder( '\DatabaseBase' )
193
			->disableOriginalConstructor()
194
			->setMethods( array( 'getType' ) )
195
			->getMockForAbstractClass();
196
197
		$read->expects( $this->any() )
198
			->method( 'getType' )
199
			->will( $this->returnValue( 'sqlite' ) );
200
201
		$readConnectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
202
			->disableOriginalConstructor()
203
			->getMock();
204
205
		$readConnectionProvider->expects( $this->atLeastOnce() )
206
			->method( 'getConnection' )
207
			->will( $this->returnValue( $read ) );
208
209
		$write = $this->getMockBuilder( '\DatabaseBase' )
210
			->disableOriginalConstructor()
211
			->setMethods( array( 'query' ) )
212
			->getMockForAbstractClass();
213
214
		$write->expects( $this->once() )
215
			->method( 'query' )
216
			->with( $this->equalTo( $expected ) )
217
			->will( $this->returnValue( $resultWrapper ) );
218
219
		$writeConnectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
220
			->disableOriginalConstructor()
221
			->getMock();
222
223
		$writeConnectionProvider->expects( $this->atLeastOnce() )
224
			->method( 'getConnection' )
225
			->will( $this->returnValue( $write ) );
226
227
		$instance = new Database( $readConnectionProvider, $writeConnectionProvider );
228
229
		$this->assertInstanceOf(
230
			'ResultWrapper',
231
			$instance->query( $query )
232
		);
233
	}
234
235
	public function querySqliteProvider() {
236
237
		$provider = array(
238
			array( 'TEMPORARY', 'TEMP' ),
239
			array( 'RAND', 'RANDOM' ),
240
			array( 'ENGINE=MEMORY', '' ),
241
			array( 'DROP TEMP', 'DROP' )
242
		);
243
244
		return $provider;
245
	}
246
247
	public function testSelectThrowsException() {
248
249
		$database = $this->getMockBuilder( '\DatabaseBase' )
250
			->disableOriginalConstructor()
251
			->setMethods( array( 'select' ) )
252
			->getMockForAbstractClass();
253
254
		$database->expects( $this->once() )
255
			->method( 'select' );
256
257
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
258
			->disableOriginalConstructor()
259
			->getMock();
260
261
		$connectionProvider->expects( $this->atLeastOnce() )
262
			->method( 'getConnection' )
263
			->will( $this->returnValue( $database ) );
264
265
		$instance = new Database( $connectionProvider );
266
267
		$this->setExpectedException( 'RuntimeException' );
268
269
		$this->assertInstanceOf(
270
			'ResultWrapper',
271
			$instance->select( 'Foo', 'Bar', '', __METHOD__ )
272
		);
273
	}
274
275
	public function testQueryThrowsException() {
276
277
		$database = $this->getMockBuilder( '\DatabaseBase' )
278
			->disableOriginalConstructor()
279
			->setMethods( array( 'query' ) )
280
			->getMockForAbstractClass();
281
282
		$databaseException = new \DBError( $database, 'foo' );
283
284
		$database->expects( $this->any() )
285
			->method( 'query' )
286
			->will( $this->throwException( $databaseException ) );
287
288
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
289
			->disableOriginalConstructor()
290
			->getMock();
291
292
		$connectionProvider->expects( $this->atLeastOnce() )
293
			->method( 'getConnection' )
294
			->will( $this->returnValue( $database ) );
295
296
		$instance = new Database( $connectionProvider );
297
298
		$this->setExpectedException( 'RuntimeException' );
299
300
		$this->assertInstanceOf(
301
			'ResultWrapper',
302
			$instance->query( 'Foo', __METHOD__ )
303
		);
304
	}
305
306
	public function testBeginTransaction() {
307
308
		$database = $this->getMockBuilder( '\DatabaseBase' )
309
			->disableOriginalConstructor()
310
			->getMockForAbstractClass();
311
312
		$readConnectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
313
			->disableOriginalConstructor()
314
			->getMock();
315
316
		$writeConnectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
317
			->disableOriginalConstructor()
318
			->getMock();
319
320
		$writeConnectionProvider->expects( $this->once() )
321
			->method( 'getConnection' )
322
			->will( $this->returnValue( $database ) );
323
324
		$instance = new Database(
325
			$readConnectionProvider,
326
			$writeConnectionProvider
327
		);
328
329
		// Can't reach the `DatabaseBase::begin` with a mock because
330
		// it is declared as final
331
		$instance->beginTransaction( __METHOD__ );
332
	}
333
334
	public function testCommitTransaction() {
335
336
		$database = $this->getMockBuilder( '\DatabaseBase' )
337
			->disableOriginalConstructor()
338
			->getMockForAbstractClass();
339
340
		$database->expects( $this->any() )
341
			->method( 'isOpen' )
342
			->will( $this->returnValue( true ) );
343
344
		$readConnectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
345
			->disableOriginalConstructor()
346
			->getMock();
347
348
		$writeConnectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
349
			->disableOriginalConstructor()
350
			->getMock();
351
352
		$writeConnectionProvider->expects( $this->atLeastOnce() )
353
			->method( 'getConnection' )
354
			->will( $this->returnValue( $database ) );
355
356
		$instance = new Database(
357
			$readConnectionProvider,
358
			$writeConnectionProvider
359
		);
360
361
		// Can't reach the `DatabaseBase::begin`/`DatabaseBase::commit`
362
		// with a mock because it is declared as final
363
		$instance->beginTransaction( __METHOD__ );
364
		$instance->commitTransaction( __METHOD__ );
365
	}
366
367
	public function testDisableEnableTransactions() {
368
369
		$database = $this->getMockBuilder( '\DatabaseBase' )
370
			->disableOriginalConstructor()
371
			->setMethods( array( 'getFlag', 'clearFlag', 'setFlag' ) )
372
			->getMockForAbstractClass();
373
374
		$database->expects( $this->any() )
375
			->method( 'getType' )
376
			->will( $this->returnValue( 'mysql' ) );
377
378
		$database->expects( $this->any() )
379
			->method( 'getFlag' )
380
			->will( $this->returnValue( true ) );
381
382
		$database->expects( $this->once() )
383
			->method( 'clearFlag' );
384
385
		$database->expects( $this->once() )
386
			->method( 'setFlag' );
387
388
		$readConnectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
389
			->disableOriginalConstructor()
390
			->getMock();
391
392
		$writeConnectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
393
			->disableOriginalConstructor()
394
			->getMock();
395
396
		$writeConnectionProvider->expects( $this->atLeastOnce() )
397
			->method( 'getConnection' )
398
			->will( $this->returnValue( $database ) );
399
400
		$instance = new Database(
401
			$readConnectionProvider,
402
			$writeConnectionProvider
403
		);
404
405
		$instance->disableTransactions();
406
		$instance->enableTransactions();
407
	}
408
409
	public function testMissingWriteConnectionThrowsException() {
410
411
		$connectionProvider = $this->getMockBuilder( '\SMW\DBConnectionProvider' )
412
			->disableOriginalConstructor()
413
			->getMock();
414
415
		$instance = new Database( $connectionProvider );
416
417
		$this->setExpectedException( 'RuntimeException' );
418
		$instance->tableExists( 'Foo' );
419
	}
420
421
	public function dbTypeProvider() {
422
		return array(
423
			array( 'mysql' ),
424
			array( 'sqlite' ),
425
			array( 'postgres' )
426
		);
427
	}
428
429
}
430