444a946e3909537ad30fe1d0476c70e1a3a1ab0a
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / database / DatabaseTest.php
1 <?php
2
3 use Wikimedia\Rdbms\Database;
4 use Wikimedia\Rdbms\IDatabase;
5 use Wikimedia\Rdbms\DatabaseMysqli;
6 use Wikimedia\Rdbms\LBFactorySingle;
7 use Wikimedia\Rdbms\TransactionProfiler;
8 use Wikimedia\TestingAccessWrapper;
9 use Wikimedia\Rdbms\DatabaseSqlite;
10 use Wikimedia\Rdbms\DatabasePostgres;
11 use Wikimedia\Rdbms\DatabaseMssql;
12
13 class DatabaseTest extends PHPUnit\Framework\TestCase {
14
15 use MediaWikiCoversValidator;
16
17 protected function setUp() {
18 $this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() );
19 }
20
21 /**
22 * @dataProvider provideAddQuotes
23 * @covers Wikimedia\Rdbms\Database::factory
24 */
25 public function testFactory() {
26 $m = Database::NEW_UNCONNECTED; // no-connect mode
27 $p = [ 'host' => 'localhost', 'user' => 'me', 'password' => 'myself', 'dbname' => 'i' ];
28
29 $this->assertInstanceOf( DatabaseMysqli::class, Database::factory( 'mysqli', $p, $m ) );
30 $this->assertInstanceOf( DatabaseMysqli::class, Database::factory( 'MySqli', $p, $m ) );
31 $this->assertInstanceOf( DatabaseMysqli::class, Database::factory( 'MySQLi', $p, $m ) );
32 $this->assertInstanceOf( DatabasePostgres::class, Database::factory( 'postgres', $p, $m ) );
33 $this->assertInstanceOf( DatabasePostgres::class, Database::factory( 'Postgres', $p, $m ) );
34
35 $x = $p + [ 'port' => 10000, 'UseWindowsAuth' => false ];
36 $this->assertInstanceOf( DatabaseMssql::class, Database::factory( 'mssql', $x, $m ) );
37
38 $x = $p + [ 'dbFilePath' => 'some/file.sqlite' ];
39 $this->assertInstanceOf( DatabaseSqlite::class, Database::factory( 'sqlite', $x, $m ) );
40 $x = $p + [ 'dbDirectory' => 'some/file' ];
41 $this->assertInstanceOf( DatabaseSqlite::class, Database::factory( 'sqlite', $x, $m ) );
42 }
43
44 public static function provideAddQuotes() {
45 return [
46 [ null, 'NULL' ],
47 [ 1234, "'1234'" ],
48 [ 1234.5678, "'1234.5678'" ],
49 [ 'string', "'string'" ],
50 [ 'string\'s cause trouble', "'string\'s cause trouble'" ],
51 ];
52 }
53
54 /**
55 * @dataProvider provideAddQuotes
56 * @covers Wikimedia\Rdbms\Database::addQuotes
57 */
58 public function testAddQuotes( $input, $expected ) {
59 $this->assertEquals( $expected, $this->db->addQuotes( $input ) );
60 }
61
62 public static function provideTableName() {
63 // Formatting is mostly ignored since addIdentifierQuotes is abstract.
64 // For testing of addIdentifierQuotes, see actual Database subclas tests.
65 return [
66 'local' => [
67 'tablename',
68 'tablename',
69 'quoted',
70 ],
71 'local-raw' => [
72 'tablename',
73 'tablename',
74 'raw',
75 ],
76 'shared' => [
77 'sharedb.tablename',
78 'tablename',
79 'quoted',
80 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ],
81 ],
82 'shared-raw' => [
83 'sharedb.tablename',
84 'tablename',
85 'raw',
86 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ],
87 ],
88 'shared-prefix' => [
89 'sharedb.sh_tablename',
90 'tablename',
91 'quoted',
92 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ],
93 ],
94 'shared-prefix-raw' => [
95 'sharedb.sh_tablename',
96 'tablename',
97 'raw',
98 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ],
99 ],
100 'foreign' => [
101 'databasename.tablename',
102 'databasename.tablename',
103 'quoted',
104 ],
105 'foreign-raw' => [
106 'databasename.tablename',
107 'databasename.tablename',
108 'raw',
109 ],
110 ];
111 }
112
113 /**
114 * @dataProvider provideTableName
115 * @covers Wikimedia\Rdbms\Database::tableName
116 */
117 public function testTableName( $expected, $table, $format, array $alias = null ) {
118 if ( $alias ) {
119 $this->db->setTableAliases( [ $table => $alias ] );
120 }
121 $this->assertEquals(
122 $expected,
123 $this->db->tableName( $table, $format ?: 'quoted' )
124 );
125 }
126
127 public function provideTableNamesWithIndexClauseOrJOIN() {
128 return [
129 'one-element array' => [
130 [ 'table' ], [], 'table '
131 ],
132 'comma join' => [
133 [ 'table1', 'table2' ], [], 'table1,table2 '
134 ],
135 'real join' => [
136 [ 'table1', 'table2' ],
137 [ 'table2' => [ 'LEFT JOIN', 't1_id = t2_id' ] ],
138 'table1 LEFT JOIN table2 ON ((t1_id = t2_id))'
139 ],
140 'real join with multiple conditionals' => [
141 [ 'table1', 'table2' ],
142 [ 'table2' => [ 'LEFT JOIN', [ 't1_id = t2_id', 't2_x = \'X\'' ] ] ],
143 'table1 LEFT JOIN table2 ON ((t1_id = t2_id) AND (t2_x = \'X\'))'
144 ],
145 'join with parenthesized group' => [
146 [ 'table1', 'n' => [ 'table2', 'table3' ] ],
147 [
148 'table3' => [ 'JOIN', 't2_id = t3_id' ],
149 'n' => [ 'LEFT JOIN', 't1_id = t2_id' ],
150 ],
151 'table1 LEFT JOIN (table2 JOIN table3 ON ((t2_id = t3_id))) ON ((t1_id = t2_id))'
152 ],
153 'join with degenerate parenthesized group' => [
154 [ 'table1', 'n' => [ 't2' => 'table2' ] ],
155 [
156 'n' => [ 'LEFT JOIN', 't1_id = t2_id' ],
157 ],
158 'table1 LEFT JOIN table2 t2 ON ((t1_id = t2_id))'
159 ],
160 ];
161 }
162
163 /**
164 * @dataProvider provideTableNamesWithIndexClauseOrJOIN
165 * @covers Wikimedia\Rdbms\Database::tableNamesWithIndexClauseOrJOIN
166 */
167 public function testTableNamesWithIndexClauseOrJOIN( $tables, $join_conds, $expect ) {
168 $clause = TestingAccessWrapper::newFromObject( $this->db )
169 ->tableNamesWithIndexClauseOrJOIN( $tables, [], [], $join_conds );
170 $this->assertSame( $expect, $clause );
171 }
172
173 /**
174 * @covers Wikimedia\Rdbms\Database::onTransactionIdle
175 * @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
176 */
177 public function testTransactionIdle() {
178 $db = $this->db;
179
180 $db->clearFlag( DBO_TRX );
181 $called = false;
182 $flagSet = null;
183 $callback = function () use ( $db, &$flagSet, &$called ) {
184 $called = true;
185 $flagSet = $db->getFlag( DBO_TRX );
186 };
187
188 $db->onTransactionIdle( $callback, __METHOD__ );
189 $this->assertTrue( $called, 'Callback reached' );
190 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
191 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX still default' );
192
193 $flagSet = null;
194 $called = false;
195 $db->startAtomic( __METHOD__ );
196 $db->onTransactionIdle( $callback, __METHOD__ );
197 $this->assertFalse( $called, 'Callback not reached during TRX' );
198 $db->endAtomic( __METHOD__ );
199
200 $this->assertTrue( $called, 'Callback reached after COMMIT' );
201 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
202 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
203
204 $db->clearFlag( DBO_TRX );
205 $db->onTransactionIdle(
206 function () use ( $db ) {
207 $db->setFlag( DBO_TRX );
208 },
209 __METHOD__
210 );
211 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
212 }
213
214 /**
215 * @covers Wikimedia\Rdbms\Database::onTransactionIdle
216 * @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
217 */
218 public function testTransactionIdle_TRX() {
219 $db = $this->getMockDB( [ 'isOpen', 'ping' ] );
220 $db->method( 'isOpen' )->willReturn( true );
221 $db->method( 'ping' )->willReturn( true );
222 $db->setFlag( DBO_TRX );
223
224 $lbFactory = LBFactorySingle::newFromConnection( $db );
225 // Ask for the connection so that LB sets internal state
226 // about this connection being the master connection
227 $lb = $lbFactory->getMainLB();
228 $conn = $lb->openConnection( $lb->getWriterIndex() );
229 $this->assertSame( $db, $conn, 'Same DB instance' );
230 $this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX is set' );
231
232 $called = false;
233 $flagSet = null;
234 $callback = function () use ( $db, &$flagSet, &$called ) {
235 $called = true;
236 $flagSet = $db->getFlag( DBO_TRX );
237 };
238
239 $db->onTransactionIdle( $callback, __METHOD__ );
240 $this->assertTrue( $called, 'Called when idle if DBO_TRX is set' );
241 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
242 $this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX still default' );
243
244 $called = false;
245 $lbFactory->beginMasterChanges( __METHOD__ );
246 $db->onTransactionIdle( $callback, __METHOD__ );
247 $this->assertFalse( $called, 'Not called when lb-transaction is active' );
248
249 $lbFactory->commitMasterChanges( __METHOD__ );
250 $this->assertTrue( $called, 'Called when lb-transaction is committed' );
251
252 $called = false;
253 $lbFactory->beginMasterChanges( __METHOD__ );
254 $db->onTransactionIdle( $callback, __METHOD__ );
255 $this->assertFalse( $called, 'Not called when lb-transaction is active' );
256
257 $lbFactory->rollbackMasterChanges( __METHOD__ );
258 $this->assertFalse( $called, 'Not called when lb-transaction is rolled back' );
259
260 $lbFactory->commitMasterChanges( __METHOD__ );
261 $this->assertFalse( $called, 'Not called in next round commit' );
262 }
263
264 /**
265 * @covers Wikimedia\Rdbms\Database::onTransactionPreCommitOrIdle
266 * @covers Wikimedia\Rdbms\Database::runOnTransactionPreCommitCallbacks
267 */
268 public function testTransactionPreCommitOrIdle() {
269 $db = $this->getMockDB( [ 'isOpen' ] );
270 $db->method( 'isOpen' )->willReturn( true );
271 $db->clearFlag( DBO_TRX );
272
273 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX is not set' );
274
275 $called = false;
276 $db->onTransactionPreCommitOrIdle(
277 function () use ( &$called ) {
278 $called = true;
279 },
280 __METHOD__
281 );
282 $this->assertTrue( $called, 'Called when idle' );
283
284 $db->begin( __METHOD__ );
285 $called = false;
286 $db->onTransactionPreCommitOrIdle(
287 function () use ( &$called ) {
288 $called = true;
289 },
290 __METHOD__
291 );
292 $this->assertFalse( $called, 'Not called when transaction is active' );
293 $db->commit( __METHOD__ );
294 $this->assertTrue( $called, 'Called when transaction is committed' );
295 }
296
297 /**
298 * @covers Wikimedia\Rdbms\Database::onTransactionPreCommitOrIdle
299 * @covers Wikimedia\Rdbms\Database::runOnTransactionPreCommitCallbacks
300 */
301 public function testTransactionPreCommitOrIdle_TRX() {
302 $db = $this->getMockDB( [ 'isOpen', 'ping' ] );
303 $db->method( 'isOpen' )->willReturn( true );
304 $db->method( 'ping' )->willReturn( true );
305 $db->setFlag( DBO_TRX );
306
307 $lbFactory = LBFactorySingle::newFromConnection( $db );
308 // Ask for the connection so that LB sets internal state
309 // about this connection being the master connection
310 $lb = $lbFactory->getMainLB();
311 $conn = $lb->openConnection( $lb->getWriterIndex() );
312 $this->assertSame( $db, $conn, 'Same DB instance' );
313
314 $this->assertFalse( $lb->hasMasterChanges() );
315 $this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX is set' );
316 $called = false;
317 $callback = function () use ( &$called ) {
318 $called = true;
319 };
320 $db->onTransactionPreCommitOrIdle( $callback, __METHOD__ );
321 $this->assertTrue( $called, 'Called when idle if DBO_TRX is set' );
322 $called = false;
323 $lbFactory->commitMasterChanges();
324 $this->assertFalse( $called );
325
326 $called = false;
327 $lbFactory->beginMasterChanges( __METHOD__ );
328 $db->onTransactionPreCommitOrIdle( $callback, __METHOD__ );
329 $this->assertFalse( $called, 'Not called when lb-transaction is active' );
330 $lbFactory->commitMasterChanges( __METHOD__ );
331 $this->assertTrue( $called, 'Called when lb-transaction is committed' );
332
333 $called = false;
334 $lbFactory->beginMasterChanges( __METHOD__ );
335 $db->onTransactionPreCommitOrIdle( $callback, __METHOD__ );
336 $this->assertFalse( $called, 'Not called when lb-transaction is active' );
337
338 $lbFactory->rollbackMasterChanges( __METHOD__ );
339 $this->assertFalse( $called, 'Not called when lb-transaction is rolled back' );
340
341 $lbFactory->commitMasterChanges( __METHOD__ );
342 $this->assertFalse( $called, 'Not called in next round commit' );
343 }
344
345 /**
346 * @covers Wikimedia\Rdbms\Database::onTransactionResolution
347 * @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
348 */
349 public function testTransactionResolution() {
350 $db = $this->db;
351
352 $db->clearFlag( DBO_TRX );
353 $db->begin( __METHOD__ );
354 $called = false;
355 $db->onTransactionResolution( function () use ( $db, &$called ) {
356 $called = true;
357 $db->setFlag( DBO_TRX );
358 } );
359 $db->commit( __METHOD__ );
360 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
361 $this->assertTrue( $called, 'Callback reached' );
362
363 $db->clearFlag( DBO_TRX );
364 $db->begin( __METHOD__ );
365 $called = false;
366 $db->onTransactionResolution( function () use ( $db, &$called ) {
367 $called = true;
368 $db->setFlag( DBO_TRX );
369 } );
370 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
371 $this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
372 $this->assertTrue( $called, 'Callback reached' );
373 }
374
375 /**
376 * @covers Wikimedia\Rdbms\Database::setTransactionListener
377 */
378 public function testTransactionListener() {
379 $db = $this->db;
380
381 $db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
382 $called = true;
383 } );
384
385 $called = false;
386 $db->begin( __METHOD__ );
387 $db->commit( __METHOD__ );
388 $this->assertTrue( $called, 'Callback reached' );
389
390 $called = false;
391 $db->begin( __METHOD__ );
392 $db->commit( __METHOD__ );
393 $this->assertTrue( $called, 'Callback still reached' );
394
395 $called = false;
396 $db->begin( __METHOD__ );
397 $db->rollback( __METHOD__ );
398 $this->assertTrue( $called, 'Callback reached' );
399
400 $db->setTransactionListener( 'ping', null );
401 $called = false;
402 $db->begin( __METHOD__ );
403 $db->commit( __METHOD__ );
404 $this->assertFalse( $called, 'Callback not reached' );
405 }
406
407 /**
408 * Use this mock instead of DatabaseTestHelper for cases where
409 * DatabaseTestHelper is too inflexibile due to mocking too much
410 * or being too restrictive about fname matching (e.g. for tests
411 * that assert behaviour when the name is a mismatch, we need to
412 * catch the error here instead of there).
413 *
414 * @return Database
415 */
416 private function getMockDB( $methods = [] ) {
417 static $abstractMethods = [
418 'fetchAffectedRowCount',
419 'closeConnection',
420 'dataSeek',
421 'doQuery',
422 'fetchObject', 'fetchRow',
423 'fieldInfo', 'fieldName',
424 'getSoftwareLink', 'getServerVersion',
425 'getType',
426 'indexInfo',
427 'insertId',
428 'lastError', 'lastErrno',
429 'numFields', 'numRows',
430 'open',
431 'strencode',
432 ];
433 $db = $this->getMockBuilder( Database::class )
434 ->disableOriginalConstructor()
435 ->setMethods( array_values( array_unique( array_merge(
436 $abstractMethods,
437 $methods
438 ) ) ) )
439 ->getMock();
440 $wdb = TestingAccessWrapper::newFromObject( $db );
441 $wdb->trxProfiler = new TransactionProfiler();
442 $wdb->connLogger = new \Psr\Log\NullLogger();
443 $wdb->queryLogger = new \Psr\Log\NullLogger();
444 return $db;
445 }
446
447 /**
448 * @covers Wikimedia\Rdbms\Database::flushSnapshot
449 */
450 public function testFlushSnapshot() {
451 $db = $this->getMockDB( [ 'isOpen' ] );
452 $db->method( 'isOpen' )->willReturn( true );
453
454 $db->flushSnapshot( __METHOD__ ); // ok
455 $db->flushSnapshot( __METHOD__ ); // ok
456
457 $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
458 $db->query( 'SELECT 1', __METHOD__ );
459 $this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
460 $db->flushSnapshot( __METHOD__ ); // ok
461 $db->restoreFlags( $db::RESTORE_PRIOR );
462
463 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
464 }
465
466 /**
467 * @covers Wikimedia\Rdbms\Database::getScopedLockAndFlush
468 * @covers Wikimedia\Rdbms\Database::lock
469 * @covers Wikimedia\Rdbms\Database::unlock
470 * @covers Wikimedia\Rdbms\Database::lockIsFree
471 */
472 public function testGetScopedLock() {
473 $db = $this->getMockDB( [ 'isOpen' ] );
474 $db->method( 'isOpen' )->willReturn( true );
475
476 $this->assertEquals( 0, $db->trxLevel() );
477 $this->assertEquals( true, $db->lockIsFree( 'x', __METHOD__ ) );
478 $this->assertEquals( true, $db->lock( 'x', __METHOD__ ) );
479 $this->assertEquals( false, $db->lockIsFree( 'x', __METHOD__ ) );
480 $this->assertEquals( true, $db->unlock( 'x', __METHOD__ ) );
481 $this->assertEquals( true, $db->lockIsFree( 'x', __METHOD__ ) );
482 $this->assertEquals( 0, $db->trxLevel() );
483
484 $db->setFlag( DBO_TRX );
485 $this->assertEquals( true, $db->lockIsFree( 'x', __METHOD__ ) );
486 $this->assertEquals( true, $db->lock( 'x', __METHOD__ ) );
487 $this->assertEquals( false, $db->lockIsFree( 'x', __METHOD__ ) );
488 $this->assertEquals( true, $db->unlock( 'x', __METHOD__ ) );
489 $this->assertEquals( true, $db->lockIsFree( 'x', __METHOD__ ) );
490 $db->clearFlag( DBO_TRX );
491
492 $this->assertEquals( 0, $db->trxLevel() );
493
494 $db->setFlag( DBO_TRX );
495 try {
496 $this->badLockingMethodImplicit( $db );
497 } catch ( RunTimeException $e ) {
498 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
499 }
500 $db->clearFlag( DBO_TRX );
501 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
502 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
503
504 try {
505 $this->badLockingMethodExplicit( $db );
506 } catch ( RunTimeException $e ) {
507 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
508 }
509 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
510 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
511 }
512
513 private function badLockingMethodImplicit( IDatabase $db ) {
514 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 );
515 $db->query( "SELECT 1" ); // trigger DBO_TRX
516 throw new RunTimeException( "Uh oh!" );
517 }
518
519 private function badLockingMethodExplicit( IDatabase $db ) {
520 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 );
521 $db->begin( __METHOD__ );
522 throw new RunTimeException( "Uh oh!" );
523 }
524
525 /**
526 * @covers Wikimedia\Rdbms\Database::getFlag
527 * @covers Wikimedia\Rdbms\Database::setFlag
528 * @covers Wikimedia\Rdbms\Database::restoreFlags
529 */
530 public function testFlagSetting() {
531 $db = $this->db;
532 $origTrx = $db->getFlag( DBO_TRX );
533 $origSsl = $db->getFlag( DBO_SSL );
534
535 $origTrx
536 ? $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR )
537 : $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
538 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
539
540 $origSsl
541 ? $db->clearFlag( DBO_SSL, $db::REMEMBER_PRIOR )
542 : $db->setFlag( DBO_SSL, $db::REMEMBER_PRIOR );
543 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL ) );
544
545 $db->restoreFlags( $db::RESTORE_INITIAL );
546 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) );
547 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
548
549 $origTrx
550 ? $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR )
551 : $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
552 $origSsl
553 ? $db->clearFlag( DBO_SSL, $db::REMEMBER_PRIOR )
554 : $db->setFlag( DBO_SSL, $db::REMEMBER_PRIOR );
555
556 $db->restoreFlags();
557 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
558 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
559
560 $db->restoreFlags();
561 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
562 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) );
563 }
564
565 /**
566 * @expectedException UnexpectedValueException
567 * @covers Wikimedia\Rdbms\Database::setFlag
568 */
569 public function testDBOIgnoreSet() {
570 $db = $this->getMockBuilder( DatabaseMysqli::class )
571 ->disableOriginalConstructor()
572 ->setMethods( null )
573 ->getMock();
574
575 $db->setFlag( Database::DBO_IGNORE );
576 }
577
578 /**
579 * @expectedException UnexpectedValueException
580 * @covers Wikimedia\Rdbms\Database::clearFlag
581 */
582 public function testDBOIgnoreClear() {
583 $db = $this->getMockBuilder( DatabaseMysqli::class )
584 ->disableOriginalConstructor()
585 ->setMethods( null )
586 ->getMock();
587
588 $db->clearFlag( Database::DBO_IGNORE );
589 }
590
591 /**
592 * @covers Wikimedia\Rdbms\Database::tablePrefix
593 * @covers Wikimedia\Rdbms\Database::dbSchema
594 */
595 public function testMutators() {
596 $old = $this->db->tablePrefix();
597 $this->assertInternalType( 'string', $old, 'Prefix is string' );
598 $this->assertEquals( $old, $this->db->tablePrefix(), "Prefix unchanged" );
599 $this->assertEquals( $old, $this->db->tablePrefix( 'xxx' ) );
600 $this->assertEquals( 'xxx', $this->db->tablePrefix(), "Prefix set" );
601 $this->db->tablePrefix( $old );
602 $this->assertNotEquals( 'xxx', $this->db->tablePrefix() );
603
604 $old = $this->db->dbSchema();
605 $this->assertInternalType( 'string', $old, 'Schema is string' );
606 $this->assertEquals( $old, $this->db->dbSchema(), "Schema unchanged" );
607 $this->assertEquals( $old, $this->db->dbSchema( 'xxx' ) );
608 $this->assertEquals( 'xxx', $this->db->dbSchema(), "Schema set" );
609 $this->db->dbSchema( $old );
610 $this->assertNotEquals( 'xxx', $this->db->dbSchema() );
611 }
612
613 }