3 use Wikimedia\Rdbms\IDatabase
;
4 use Wikimedia\Rdbms\LBFactorySingle
;
5 use Wikimedia\Rdbms\TransactionProfiler
;
6 use Wikimedia\TestingAccessWrapper
;
8 class DatabaseTest
extends PHPUnit_Framework_TestCase
{
10 use MediaWikiCoversValidator
;
12 protected function setUp() {
13 $this->db
= new DatabaseTestHelper( __CLASS__
. '::' . $this->getName() );
16 public static function provideAddQuotes() {
20 [ 1234.5678, "'1234.5678'" ],
21 [ 'string', "'string'" ],
22 [ 'string\'s cause trouble', "'string\'s cause trouble'" ],
27 * @dataProvider provideAddQuotes
28 * @covers Wikimedia\Rdbms\Database::addQuotes
30 public function testAddQuotes( $input, $expected ) {
31 $this->assertEquals( $expected, $this->db
->addQuotes( $input ) );
34 public static function provideTableName() {
35 // Formatting is mostly ignored since addIdentifierQuotes is abstract.
36 // For testing of addIdentifierQuotes, see actual Database subclas tests.
52 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ],
58 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ],
61 'sharedb.sh_tablename',
64 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ],
66 'shared-prefix-raw' => [
67 'sharedb.sh_tablename',
70 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ],
73 'databasename.tablename',
74 'databasename.tablename',
78 'databasename.tablename',
79 'databasename.tablename',
86 * @dataProvider provideTableName
87 * @covers Wikimedia\Rdbms\Database::tableName
89 public function testTableName( $expected, $table, $format, array $alias = null ) {
91 $this->db
->setTableAliases( [ $table => $alias ] );
95 $this->db
->tableName( $table, $format ?
: 'quoted' )
99 public function provideTableNamesWithIndexClauseOrJOIN() {
101 'one-element array' => [
102 [ 'table' ], [], 'table '
105 [ 'table1', 'table2' ], [], 'table1,table2 '
108 [ 'table1', 'table2' ],
109 [ 'table2' => [ 'LEFT JOIN', 't1_id = t2_id' ] ],
110 'table1 LEFT JOIN table2 ON ((t1_id = t2_id))'
112 'real join with multiple conditionals' => [
113 [ 'table1', 'table2' ],
114 [ 'table2' => [ 'LEFT JOIN', [ 't1_id = t2_id', 't2_x = \'X\'' ] ] ],
115 'table1 LEFT JOIN table2 ON ((t1_id = t2_id) AND (t2_x = \'X\'))'
117 'join with parenthesized group' => [
118 [ 'table1', 'n' => [ 'table2', 'table3' ] ],
120 'table3' => [ 'JOIN', 't2_id = t3_id' ],
121 'n' => [ 'LEFT JOIN', 't1_id = t2_id' ],
123 'table1 LEFT JOIN (table2 JOIN table3 ON ((t2_id = t3_id))) ON ((t1_id = t2_id))'
125 'join with degenerate parenthesized group' => [
126 [ 'table1', 'n' => [ 't2' => 'table2' ] ],
128 'n' => [ 'LEFT JOIN', 't1_id = t2_id' ],
130 'table1 LEFT JOIN table2 t2 ON ((t1_id = t2_id))'
136 * @dataProvider provideTableNamesWithIndexClauseOrJOIN
137 * @covers Wikimedia\Rdbms\Database::tableNamesWithIndexClauseOrJOIN
139 public function testTableNamesWithIndexClauseOrJOIN( $tables, $join_conds, $expect ) {
140 $clause = TestingAccessWrapper
::newFromObject( $this->db
)
141 ->tableNamesWithIndexClauseOrJOIN( $tables, [], [], $join_conds );
142 $this->assertSame( $expect, $clause );
146 * @covers Wikimedia\Rdbms\Database::onTransactionIdle
147 * @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
149 public function testTransactionIdle() {
152 $db->setFlag( DBO_TRX
);
155 $db->onTransactionIdle(
156 function () use ( $db, &$flagSet, &$called ) {
158 $flagSet = $db->getFlag( DBO_TRX
);
162 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
163 $this->assertTrue( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
164 $this->assertTrue( $called, 'Callback reached' );
166 $db->clearFlag( DBO_TRX
);
168 $db->onTransactionIdle(
169 function () use ( $db, &$flagSet ) {
170 $flagSet = $db->getFlag( DBO_TRX
);
174 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
175 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
177 $db->clearFlag( DBO_TRX
);
178 $db->onTransactionIdle(
179 function () use ( $db ) {
180 $db->setFlag( DBO_TRX
);
184 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
188 * @covers Wikimedia\Rdbms\Database::onTransactionPreCommitOrIdle
189 * @covers Wikimedia\Rdbms\Database::runOnTransactionPreCommitCallbacks
191 public function testTransactionPreCommitOrIdle() {
192 $db = $this->getMockDB( [ 'isOpen' ] );
193 $db->method( 'isOpen' )->willReturn( true );
194 $db->clearFlag( DBO_TRX
);
196 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX is not set' );
199 $db->onTransactionPreCommitOrIdle(
200 function () use ( &$called ) {
205 $this->assertTrue( $called, 'Called when idle' );
207 $db->begin( __METHOD__
);
209 $db->onTransactionPreCommitOrIdle(
210 function () use ( &$called ) {
215 $this->assertFalse( $called, 'Not called when transaction is active' );
216 $db->commit( __METHOD__
);
217 $this->assertTrue( $called, 'Called when transaction is committed' );
221 * @covers Wikimedia\Rdbms\Database::onTransactionPreCommitOrIdle
222 * @covers Wikimedia\Rdbms\Database::runOnTransactionPreCommitCallbacks
224 public function testTransactionPreCommitOrIdle_TRX() {
225 $db = $this->getMockDB( [ 'isOpen' ] );
226 $db->method( 'isOpen' )->willReturn( true );
227 $db->setFlag( DBO_TRX
);
229 $lbFactory = LBFactorySingle
::newFromConnection( $db );
230 // Ask for the connectin so that LB sets internal state
231 // about this connection being the master connection
232 $lb = $lbFactory->getMainLB();
233 $conn = $lb->openConnection( $lb->getWriterIndex() );
234 $this->assertSame( $db, $conn, 'Same DB instance' );
235 $this->assertTrue( $db->getFlag( DBO_TRX
), 'DBO_TRX is set' );
238 $db->onTransactionPreCommitOrIdle(
239 function () use ( &$called ) {
243 $this->assertFalse( $called, 'Not called when idle if DBO_TRX is set' );
245 $lbFactory->beginMasterChanges( __METHOD__
);
246 $this->assertFalse( $called, 'Not called when lb-transaction is active' );
248 $lbFactory->commitMasterChanges( __METHOD__
);
249 $this->assertTrue( $called, 'Called when lb-transaction is committed' );
253 * @covers Wikimedia\Rdbms\Database::onTransactionResolution
254 * @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
256 public function testTransactionResolution() {
259 $db->clearFlag( DBO_TRX
);
260 $db->begin( __METHOD__
);
262 $db->onTransactionResolution( function () use ( $db, &$called ) {
264 $db->setFlag( DBO_TRX
);
266 $db->commit( __METHOD__
);
267 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
268 $this->assertTrue( $called, 'Callback reached' );
270 $db->clearFlag( DBO_TRX
);
271 $db->begin( __METHOD__
);
273 $db->onTransactionResolution( function () use ( $db, &$called ) {
275 $db->setFlag( DBO_TRX
);
277 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
278 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
279 $this->assertTrue( $called, 'Callback reached' );
283 * @covers Wikimedia\Rdbms\Database::setTransactionListener
285 public function testTransactionListener() {
288 $db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
293 $db->begin( __METHOD__
);
294 $db->commit( __METHOD__
);
295 $this->assertTrue( $called, 'Callback reached' );
298 $db->begin( __METHOD__
);
299 $db->commit( __METHOD__
);
300 $this->assertTrue( $called, 'Callback still reached' );
303 $db->begin( __METHOD__
);
304 $db->rollback( __METHOD__
);
305 $this->assertTrue( $called, 'Callback reached' );
307 $db->setTransactionListener( 'ping', null );
309 $db->begin( __METHOD__
);
310 $db->commit( __METHOD__
);
311 $this->assertFalse( $called, 'Callback not reached' );
315 * Use this mock instead of DatabaseTestHelper for cases where
316 * DatabaseTestHelper is too inflexibile due to mocking too much
317 * or being too restrictive about fname matching (e.g. for tests
318 * that assert behaviour when the name is a mismatch, we need to
319 * catch the error here instead of there).
323 private function getMockDB( $methods = [] ) {
324 static $abstractMethods = [
325 'fetchAffectedRowCount',
329 'fetchObject', 'fetchRow',
330 'fieldInfo', 'fieldName',
331 'getSoftwareLink', 'getServerVersion',
335 'lastError', 'lastErrno',
336 'numFields', 'numRows',
340 $db = $this->getMockBuilder( Database
::class )
341 ->disableOriginalConstructor()
342 ->setMethods( array_values( array_unique( array_merge(
347 $wdb = TestingAccessWrapper
::newFromObject( $db );
348 $wdb->trxProfiler
= new TransactionProfiler();
349 $wdb->connLogger
= new \Psr\Log\
NullLogger();
350 $wdb->queryLogger
= new \Psr\Log\
NullLogger();
355 * @covers Wikimedia\Rdbms\Database::flushSnapshot
357 public function testFlushSnapshot() {
358 $db = $this->getMockDB( [ 'isOpen' ] );
359 $db->method( 'isOpen' )->willReturn( true );
361 $db->flushSnapshot( __METHOD__
); // ok
362 $db->flushSnapshot( __METHOD__
); // ok
364 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
365 $db->query( 'SELECT 1', __METHOD__
);
366 $this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
367 $db->flushSnapshot( __METHOD__
); // ok
368 $db->restoreFlags( $db::RESTORE_PRIOR
);
370 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
373 public function testGetScopedLock() {
374 $db = $this->getMockDB( [ 'isOpen' ] );
375 $db->method( 'isOpen' )->willReturn( true );
377 $db->setFlag( DBO_TRX
);
379 $this->badLockingMethodImplicit( $db );
380 } catch ( RunTimeException
$e ) {
381 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
383 $db->clearFlag( DBO_TRX
);
384 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
385 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
388 $this->badLockingMethodExplicit( $db );
389 } catch ( RunTimeException
$e ) {
390 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
392 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
393 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
396 private function badLockingMethodImplicit( IDatabase
$db ) {
397 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
398 $db->query( "SELECT 1" ); // trigger DBO_TRX
399 throw new RunTimeException( "Uh oh!" );
402 private function badLockingMethodExplicit( IDatabase
$db ) {
403 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
404 $db->begin( __METHOD__
);
405 throw new RunTimeException( "Uh oh!" );
409 * @covers Wikimedia\Rdbms\Database::getFlag
410 * @covers Wikimedia\Rdbms\Database::setFlag
411 * @covers Wikimedia\Rdbms\Database::restoreFlags
413 public function testFlagSetting() {
415 $origTrx = $db->getFlag( DBO_TRX
);
416 $origSsl = $db->getFlag( DBO_SSL
);
419 ?
$db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
)
420 : $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
421 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
424 ?
$db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
)
425 : $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
426 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL
) );
428 $db->restoreFlags( $db::RESTORE_INITIAL
);
429 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
430 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
433 ?
$db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
)
434 : $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
436 ?
$db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
)
437 : $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
440 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
441 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
444 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
445 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
449 * @covers Wikimedia\Rdbms\Database::tablePrefix
450 * @covers Wikimedia\Rdbms\Database::dbSchema
452 public function testMutators() {
453 $old = $this->db
->tablePrefix();
454 $this->assertInternalType( 'string', $old, 'Prefix is string' );
455 $this->assertEquals( $old, $this->db
->tablePrefix(), "Prefix unchanged" );
456 $this->assertEquals( $old, $this->db
->tablePrefix( 'xxx' ) );
457 $this->assertEquals( 'xxx', $this->db
->tablePrefix(), "Prefix set" );
458 $this->db
->tablePrefix( $old );
459 $this->assertNotEquals( 'xxx', $this->db
->tablePrefix() );
461 $old = $this->db
->dbSchema();
462 $this->assertInternalType( 'string', $old, 'Schema is string' );
463 $this->assertEquals( $old, $this->db
->dbSchema(), "Schema unchanged" );
464 $this->assertEquals( $old, $this->db
->dbSchema( 'xxx' ) );
465 $this->assertEquals( 'xxx', $this->db
->dbSchema(), "Schema set" );
466 $this->db
->dbSchema( $old );
467 $this->assertNotEquals( 'xxx', $this->db
->dbSchema() );