3 use Wikimedia\Rdbms\IDatabase
;
4 use Wikimedia\Rdbms\TransactionProfiler
;
5 use Wikimedia\TestingAccessWrapper
;
7 class DatabaseTest
extends PHPUnit_Framework_TestCase
{
9 protected function setUp() {
10 $this->db
= new DatabaseTestHelper( __CLASS__
. '::' . $this->getName() );
13 public static function provideAddQuotes() {
17 [ 1234.5678, "'1234.5678'" ],
18 [ 'string', "'string'" ],
19 [ 'string\'s cause trouble', "'string\'s cause trouble'" ],
24 * @dataProvider provideAddQuotes
25 * @covers Wikimedia\Rdbms\Database::addQuotes
27 public function testAddQuotes( $input, $expected ) {
28 $this->assertEquals( $expected, $this->db
->addQuotes( $input ) );
31 public static function provideTableName() {
32 // Formatting is mostly ignored since addIdentifierQuotes is abstract.
33 // For testing of addIdentifierQuotes, see actual Database subclas tests.
49 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ],
55 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ],
58 'sharedb.sh_tablename',
61 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ],
63 'shared-prefix-raw' => [
64 'sharedb.sh_tablename',
67 [ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ],
70 'databasename.tablename',
71 'databasename.tablename',
75 'databasename.tablename',
76 'databasename.tablename',
83 * @dataProvider provideTableName
84 * @covers Wikimedia\Rdbms\Database::tableName
86 public function testTableName( $expected, $table, $format, array $alias = null ) {
88 $this->db
->setTableAliases( [ $table => $alias ] );
92 $this->db
->tableName( $table, $format ?
: 'quoted' )
97 * @covers Wikimedia\Rdbms\Database::onTransactionIdle
98 * @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
100 public function testTransactionIdle() {
103 $db->setFlag( DBO_TRX
);
106 $db->onTransactionIdle(
107 function () use ( $db, &$flagSet, &$called ) {
109 $flagSet = $db->getFlag( DBO_TRX
);
113 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
114 $this->assertTrue( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
115 $this->assertTrue( $called, 'Callback reached' );
117 $db->clearFlag( DBO_TRX
);
119 $db->onTransactionIdle(
120 function () use ( $db, &$flagSet ) {
121 $flagSet = $db->getFlag( DBO_TRX
);
125 $this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
126 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
128 $db->clearFlag( DBO_TRX
);
129 $db->onTransactionIdle(
130 function () use ( $db ) {
131 $db->setFlag( DBO_TRX
);
135 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
139 * @covers Wikimedia\Rdbms\Database::onTransactionResolution
140 * @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
142 public function testTransactionResolution() {
145 $db->clearFlag( DBO_TRX
);
146 $db->begin( __METHOD__
);
148 $db->onTransactionResolution( function () use ( $db, &$called ) {
150 $db->setFlag( DBO_TRX
);
152 $db->commit( __METHOD__
);
153 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
154 $this->assertTrue( $called, 'Callback reached' );
156 $db->clearFlag( DBO_TRX
);
157 $db->begin( __METHOD__
);
159 $db->onTransactionResolution( function () use ( $db, &$called ) {
161 $db->setFlag( DBO_TRX
);
163 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
164 $this->assertFalse( $db->getFlag( DBO_TRX
), 'DBO_TRX restored to default' );
165 $this->assertTrue( $called, 'Callback reached' );
169 * @covers Wikimedia\Rdbms\Database::setTransactionListener
171 public function testTransactionListener() {
174 $db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
179 $db->begin( __METHOD__
);
180 $db->commit( __METHOD__
);
181 $this->assertTrue( $called, 'Callback reached' );
184 $db->begin( __METHOD__
);
185 $db->commit( __METHOD__
);
186 $this->assertTrue( $called, 'Callback still reached' );
189 $db->begin( __METHOD__
);
190 $db->rollback( __METHOD__
);
191 $this->assertTrue( $called, 'Callback reached' );
193 $db->setTransactionListener( 'ping', null );
195 $db->begin( __METHOD__
);
196 $db->commit( __METHOD__
);
197 $this->assertFalse( $called, 'Callback not reached' );
201 * Use this mock instead of DatabaseTestHelper for cases where
202 * DatabaseTestHelper is too inflexibile due to mocking too much
203 * or being too restrictive about fname matching (e.g. for tests
204 * that assert behaviour when the name is a mismatch, we need to
205 * catch the error here instead of there).
209 private function getMockDB( $methods = [] ) {
210 static $abstractMethods = [
215 'fetchObject', 'fetchRow',
216 'fieldInfo', 'fieldName',
217 'getSoftwareLink', 'getServerVersion',
221 'lastError', 'lastErrno',
222 'numFields', 'numRows',
226 $db = $this->getMockBuilder( Database
::class )
227 ->disableOriginalConstructor()
228 ->setMethods( array_values( array_unique( array_merge(
233 $wdb = TestingAccessWrapper
::newFromObject( $db );
234 $wdb->trxProfiler
= new TransactionProfiler();
235 $wdb->connLogger
= new \Psr\Log\
NullLogger();
236 $wdb->queryLogger
= new \Psr\Log\
NullLogger();
241 * @covers Wikimedia\Rdbms\Database::flushSnapshot
243 public function testFlushSnapshot() {
244 $db = $this->getMockDB( [ 'isOpen' ] );
245 $db->method( 'isOpen' )->willReturn( true );
247 $db->flushSnapshot( __METHOD__
); // ok
248 $db->flushSnapshot( __METHOD__
); // ok
250 $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
251 $db->query( 'SELECT 1', __METHOD__
);
252 $this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
253 $db->flushSnapshot( __METHOD__
); // ok
254 $db->restoreFlags( $db::RESTORE_PRIOR
);
256 $this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
259 public function testGetScopedLock() {
260 $db = $this->getMockDB( [ 'isOpen' ] );
261 $db->method( 'isOpen' )->willReturn( true );
263 $db->setFlag( DBO_TRX
);
265 $this->badLockingMethodImplicit( $db );
266 } catch ( RunTimeException
$e ) {
267 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
269 $db->clearFlag( DBO_TRX
);
270 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
271 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
274 $this->badLockingMethodExplicit( $db );
275 } catch ( RunTimeException
$e ) {
276 $this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
278 $db->rollback( __METHOD__
, IDatabase
::FLUSHING_ALL_PEERS
);
279 $this->assertTrue( $db->lockIsFree( 'meow', __METHOD__
) );
282 private function badLockingMethodImplicit( IDatabase
$db ) {
283 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
284 $db->query( "SELECT 1" ); // trigger DBO_TRX
285 throw new RunTimeException( "Uh oh!" );
288 private function badLockingMethodExplicit( IDatabase
$db ) {
289 $lock = $db->getScopedLockAndFlush( 'meow', __METHOD__
, 1 );
290 $db->begin( __METHOD__
);
291 throw new RunTimeException( "Uh oh!" );
295 * @covers Wikimedia\Rdbms\Database::getFlag
296 * @covers Wikimedia\Rdbms\Database::setFlag
297 * @covers Wikimedia\Rdbms\Database::restoreFlags
299 public function testFlagSetting() {
301 $origTrx = $db->getFlag( DBO_TRX
);
302 $origSsl = $db->getFlag( DBO_SSL
);
305 ?
$db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
)
306 : $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
307 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
310 ?
$db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
)
311 : $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
312 $this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL
) );
314 $db->restoreFlags( $db::RESTORE_INITIAL
);
315 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
316 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
319 ?
$db->clearFlag( DBO_TRX
, $db::REMEMBER_PRIOR
)
320 : $db->setFlag( DBO_TRX
, $db::REMEMBER_PRIOR
);
322 ?
$db->clearFlag( DBO_SSL
, $db::REMEMBER_PRIOR
)
323 : $db->setFlag( DBO_SSL
, $db::REMEMBER_PRIOR
);
326 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
327 $this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX
) );
330 $this->assertEquals( $origSsl, $db->getFlag( DBO_SSL
) );
331 $this->assertEquals( $origTrx, $db->getFlag( DBO_TRX
) );
335 * @covers Wikimedia\Rdbms\Database::tablePrefix
336 * @covers Wikimedia\Rdbms\Database::dbSchema
338 public function testMutators() {
339 $old = $this->db
->tablePrefix();
340 $this->assertInternalType( 'string', $old, 'Prefix is string' );
341 $this->assertEquals( $old, $this->db
->tablePrefix(), "Prefix unchanged" );
342 $this->assertEquals( $old, $this->db
->tablePrefix( 'xxx' ) );
343 $this->assertEquals( 'xxx', $this->db
->tablePrefix(), "Prefix set" );
344 $this->db
->tablePrefix( $old );
345 $this->assertNotEquals( 'xxx', $this->db
->tablePrefix() );
347 $old = $this->db
->dbSchema();
348 $this->assertInternalType( 'string', $old, 'Schema is string' );
349 $this->assertEquals( $old, $this->db
->dbSchema(), "Schema unchanged" );
350 $this->assertEquals( $old, $this->db
->dbSchema( 'xxx' ) );
351 $this->assertEquals( 'xxx', $this->db
->dbSchema(), "Schema set" );
352 $this->db
->dbSchema( $old );
353 $this->assertNotEquals( 'xxx', $this->db
->dbSchema() );