6use Wikimedia\TestingAccessWrapper;
18 [ 1234.5678,
"'1234.5678'" ],
19 [
'string',
"'string'" ],
20 [
'string\'s cause trouble',
"'string\'s cause trouble'" ],
29 $this->assertEquals( $expected, $this->db->addQuotes(
$input ) );
50 [
'dbname' =>
'sharedb',
'schema' =>
null,
'prefix' =>
'' ],
56 [
'dbname' =>
'sharedb',
'schema' =>
null,
'prefix' =>
'' ],
59 'sharedb.sh_tablename',
62 [
'dbname' =>
'sharedb',
'schema' =>
null,
'prefix' =>
'sh_' ],
64 'shared-prefix-raw' => [
65 'sharedb.sh_tablename',
68 [
'dbname' =>
'sharedb',
'schema' =>
null,
'prefix' =>
'sh_' ],
71 'databasename.tablename',
72 'databasename.tablename',
76 'databasename.tablename',
77 'databasename.tablename',
87 public function testTableName( $expected, $table, $format, array $alias =
null ) {
89 $this->db->setTableAliases( [ $table => $alias ] );
93 $this->db->tableName( $table, $format ?:
'quoted' )
107 $db->onTransactionIdle(
108 function () use ( $db, &$flagSet, &$called ) {
110 $flagSet = $db->getFlag(
DBO_TRX );
114 $this->assertFalse( $flagSet,
'DBO_TRX off in callback' );
115 $this->assertTrue( $db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
116 $this->assertTrue( $called,
'Callback reached' );
120 $db->onTransactionIdle(
121 function () use ( $db, &$flagSet ) {
122 $flagSet = $db->getFlag(
DBO_TRX );
126 $this->assertFalse( $flagSet,
'DBO_TRX off in callback' );
127 $this->assertFalse( $db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
130 $db->onTransactionIdle(
131 function () use ( $db ) {
136 $this->assertFalse( $db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
145 $db->method(
'isOpen' )->willReturn(
true );
148 $this->assertFalse( $db->getFlag(
DBO_TRX ),
'DBO_TRX is not set' );
151 $db->onTransactionPreCommitOrIdle(
152 function () use ( &$called ) {
157 $this->assertTrue( $called,
'Called when idle' );
159 $db->begin( __METHOD__ );
161 $db->onTransactionPreCommitOrIdle(
162 function () use ( &$called ) {
167 $this->assertFalse( $called,
'Not called when transaction is active' );
168 $db->commit( __METHOD__ );
169 $this->assertTrue( $called,
'Called when transaction is committed' );
178 $db->method(
'isOpen' )->willReturn(
true );
181 $lbFactory = LBFactorySingle::newFromConnection( $db );
184 $lb = $lbFactory->getMainLB();
185 $conn = $lb->openConnection( $lb->getWriterIndex() );
186 $this->assertSame( $db, $conn,
'Same DB instance' );
187 $this->assertTrue( $db->getFlag(
DBO_TRX ),
'DBO_TRX is set' );
190 $db->onTransactionPreCommitOrIdle(
191 function () use ( &$called ) {
195 $this->assertFalse( $called,
'Not called when idle if DBO_TRX is set' );
197 $lbFactory->beginMasterChanges( __METHOD__ );
198 $this->assertFalse( $called,
'Not called when lb-transaction is active' );
200 $lbFactory->commitMasterChanges( __METHOD__ );
201 $this->assertTrue( $called,
'Called when lb-transaction is committed' );
212 $db->begin( __METHOD__ );
214 $db->onTransactionResolution(
function () use ( $db, &$called ) {
218 $db->commit( __METHOD__ );
219 $this->assertFalse( $db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
220 $this->assertTrue( $called,
'Callback reached' );
223 $db->begin( __METHOD__ );
225 $db->onTransactionResolution(
function () use ( $db, &$called ) {
229 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
230 $this->assertFalse( $db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
231 $this->assertTrue( $called,
'Callback reached' );
240 $db->setTransactionListener(
'ping',
function () use ( $db, &$called ) {
245 $db->begin( __METHOD__ );
246 $db->commit( __METHOD__ );
247 $this->assertTrue( $called,
'Callback reached' );
250 $db->begin( __METHOD__ );
251 $db->commit( __METHOD__ );
252 $this->assertTrue( $called,
'Callback still reached' );
255 $db->begin( __METHOD__ );
256 $db->rollback( __METHOD__ );
257 $this->assertTrue( $called,
'Callback reached' );
259 $db->setTransactionListener(
'ping',
null );
261 $db->begin( __METHOD__ );
262 $db->commit( __METHOD__ );
263 $this->assertFalse( $called,
'Callback not reached' );
276 static $abstractMethods = [
281 'fetchObject',
'fetchRow',
282 'fieldInfo',
'fieldName',
283 'getSoftwareLink',
'getServerVersion',
287 'lastError',
'lastErrno',
288 'numFields',
'numRows',
292 $db = $this->getMockBuilder( Database::class )
293 ->disableOriginalConstructor()
294 ->setMethods( array_values( array_unique( array_merge(
299 $wdb = TestingAccessWrapper::newFromObject( $db );
301 $wdb->connLogger = new \Psr\Log\NullLogger();
302 $wdb->queryLogger = new \Psr\Log\NullLogger();
311 $db->method(
'isOpen' )->willReturn(
true );
313 $db->flushSnapshot( __METHOD__ );
314 $db->flushSnapshot( __METHOD__ );
316 $db->setFlag(
DBO_TRX, $db::REMEMBER_PRIOR );
317 $db->query(
'SELECT 1', __METHOD__ );
318 $this->assertTrue( (
bool)$db->trxLevel(),
"Transaction started." );
319 $db->flushSnapshot( __METHOD__ );
320 $db->restoreFlags( $db::RESTORE_PRIOR );
322 $this->assertFalse( (
bool)$db->trxLevel(),
"Transaction cleared." );
327 $db->method(
'isOpen' )->willReturn(
true );
332 }
catch ( RunTimeException
$e ) {
333 $this->assertTrue( $db->trxLevel() > 0,
"Transaction not committed." );
336 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
337 $this->assertTrue( $db->lockIsFree(
'meow', __METHOD__ ) );
341 }
catch ( RunTimeException
$e ) {
342 $this->assertTrue( $db->trxLevel() > 0,
"Transaction not committed." );
344 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
345 $this->assertTrue( $db->lockIsFree(
'meow', __METHOD__ ) );
350 $db->
query(
"SELECT 1" );
351 throw new RunTimeException(
"Uh oh!" );
356 $db->
begin( __METHOD__ );
357 throw new RunTimeException(
"Uh oh!" );
367 $origTrx = $db->getFlag(
DBO_TRX );
368 $origSsl = $db->getFlag(
DBO_SSL );
371 ? $db->clearFlag(
DBO_TRX, $db::REMEMBER_PRIOR )
372 : $db->setFlag(
DBO_TRX, $db::REMEMBER_PRIOR );
373 $this->assertEquals( !$origTrx, $db->getFlag(
DBO_TRX ) );
376 ? $db->clearFlag(
DBO_SSL, $db::REMEMBER_PRIOR )
377 : $db->setFlag(
DBO_SSL, $db::REMEMBER_PRIOR );
378 $this->assertEquals( !$origSsl, $db->getFlag(
DBO_SSL ) );
380 $db->restoreFlags( $db::RESTORE_INITIAL );
381 $this->assertEquals( $origTrx, $db->getFlag(
DBO_TRX ) );
382 $this->assertEquals( $origSsl, $db->getFlag(
DBO_SSL ) );
385 ? $db->clearFlag(
DBO_TRX, $db::REMEMBER_PRIOR )
386 : $db->setFlag(
DBO_TRX, $db::REMEMBER_PRIOR );
388 ? $db->clearFlag(
DBO_SSL, $db::REMEMBER_PRIOR )
389 : $db->setFlag(
DBO_SSL, $db::REMEMBER_PRIOR );
392 $this->assertEquals( $origSsl, $db->getFlag(
DBO_SSL ) );
393 $this->assertEquals( !$origTrx, $db->getFlag(
DBO_TRX ) );
396 $this->assertEquals( $origSsl, $db->getFlag(
DBO_SSL ) );
397 $this->assertEquals( $origTrx, $db->getFlag(
DBO_TRX ) );
405 $old = $this->db->tablePrefix();
406 $this->assertInternalType(
'string', $old,
'Prefix is string' );
407 $this->assertEquals( $old, $this->db->tablePrefix(),
"Prefix unchanged" );
408 $this->assertEquals( $old, $this->db->tablePrefix(
'xxx' ) );
409 $this->assertEquals(
'xxx', $this->db->tablePrefix(),
"Prefix set" );
410 $this->db->tablePrefix( $old );
411 $this->assertNotEquals(
'xxx', $this->db->tablePrefix() );
413 $old = $this->db->dbSchema();
414 $this->assertInternalType(
'string', $old,
'Schema is string' );
415 $this->assertEquals( $old, $this->db->dbSchema(),
"Schema unchanged" );
416 $this->assertEquals( $old, $this->db->dbSchema(
'xxx' ) );
417 $this->assertEquals(
'xxx', $this->db->dbSchema(),
"Schema set" );
418 $this->db->dbSchema( $old );
419 $this->assertNotEquals(
'xxx', $this->db->dbSchema() );
Helper for testing the methods from the Database class.
testTransactionPreCommitOrIdle_TRX()
Wikimedia\Rdbms\Database::onTransactionPreCommitOrIdle Wikimedia\Rdbms\Database::runOnTransactionPreC...
badLockingMethodImplicit(IDatabase $db)
testTransactionIdle()
Wikimedia\Rdbms\Database::onTransactionIdle Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks.
badLockingMethodExplicit(IDatabase $db)
testTableName( $expected, $table, $format, array $alias=null)
provideTableName Wikimedia\Rdbms\Database::tableName
testTransactionListener()
Wikimedia\Rdbms\Database::setTransactionListener.
testMutators()
Wikimedia\Rdbms\Database::tablePrefix Wikimedia\Rdbms\Database::dbSchema.
testFlagSetting()
Wikimedia\Rdbms\Database::getFlag Wikimedia\Rdbms\Database::setFlag Wikimedia\Rdbms\Database::restore...
static provideAddQuotes()
testFlushSnapshot()
Wikimedia\Rdbms\Database::flushSnapshot.
testTransactionResolution()
Wikimedia\Rdbms\Database::onTransactionResolution Wikimedia\Rdbms\Database::runOnTransactionIdleCallb...
static provideTableName()
testTransactionPreCommitOrIdle()
Wikimedia\Rdbms\Database::onTransactionPreCommitOrIdle Wikimedia\Rdbms\Database::runOnTransactionPreC...
testAddQuotes( $input, $expected)
provideAddQuotes Wikimedia\Rdbms\Database::addQuotes
getMockDB( $methods=[])
Use this mock instead of DatabaseTestHelper for cases where DatabaseTestHelper is too inflexibile due...
returning false will NOT prevent logging $e
if(is_array($mode)) switch( $mode) $input