From 8d61cf2793abd24166f2cc5da10b517a2802d6cb Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 26 Oct 2016 22:44:08 -0700 Subject: [PATCH] Make LBFactoryTest.php pass for sqlite Change-Id: I4fe929e82218231f6c8afa64da8c0ccb42d2c362 --- .../libs/rdbms/database/DatabaseSqlite.php | 4 + tests/phpunit/includes/db/LBFactoryTest.php | 140 ++++++++++++------ 2 files changed, 97 insertions(+), 47 deletions(-) diff --git a/includes/libs/rdbms/database/DatabaseSqlite.php b/includes/libs/rdbms/database/DatabaseSqlite.php index 46f7a5bf01..8236abfc54 100644 --- a/includes/libs/rdbms/database/DatabaseSqlite.php +++ b/includes/libs/rdbms/database/DatabaseSqlite.php @@ -196,6 +196,10 @@ class DatabaseSqlite extends Database { return false; } + public function selectDB( $db ) { + return false; // doesn't make sense + } + /** * @return string SQLite DB file path * @since 1.25 diff --git a/tests/phpunit/includes/db/LBFactoryTest.php b/tests/phpunit/includes/db/LBFactoryTest.php index aed2d83a7d..d8773f8ad2 100644 --- a/tests/phpunit/includes/db/LBFactoryTest.php +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -58,17 +58,18 @@ class LBFactoryTest extends MediaWikiTestCase { } public function testLBFactorySimpleServer() { - global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype; + global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir; $servers = [ [ - 'host' => $wgDBserver, - 'dbname' => $wgDBname, - 'user' => $wgDBuser, - 'password' => $wgDBpassword, - 'type' => $wgDBtype, - 'load' => 0, - 'flags' => DBO_TRX // REPEATABLE-READ for consistency + 'host' => $wgDBserver, + 'dbname' => $wgDBname, + 'user' => $wgDBuser, + 'password' => $wgDBpassword, + 'type' => $wgDBtype, + 'dbDirectory' => $wgSQLiteDataDir, + 'load' => 0, + 'flags' => DBO_TRX // REPEATABLE-READ for consistency ], ]; @@ -86,26 +87,28 @@ class LBFactoryTest extends MediaWikiTestCase { } public function testLBFactorySimpleServers() { - global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype; + global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir; $servers = [ [ // master - 'host' => $wgDBserver, - 'dbname' => $wgDBname, - 'user' => $wgDBuser, - 'password' => $wgDBpassword, - 'type' => $wgDBtype, - 'load' => 0, - 'flags' => DBO_TRX // REPEATABLE-READ for consistency + 'host' => $wgDBserver, + 'dbname' => $wgDBname, + 'user' => $wgDBuser, + 'password' => $wgDBpassword, + 'type' => $wgDBtype, + 'dbDirectory' => $wgSQLiteDataDir, + 'load' => 0, + 'flags' => DBO_TRX // REPEATABLE-READ for consistency ], [ // emulated slave - 'host' => $wgDBserver, - 'dbname' => $wgDBname, - 'user' => $wgDBuser, - 'password' => $wgDBpassword, - 'type' => $wgDBtype, - 'load' => 100, - 'flags' => DBO_TRX // REPEATABLE-READ for consistency + 'host' => $wgDBserver, + 'dbname' => $wgDBname, + 'user' => $wgDBuser, + 'password' => $wgDBpassword, + 'type' => $wgDBtype, + 'dbDirectory' => $wgSQLiteDataDir, + 'load' => 100, + 'flags' => DBO_TRX // REPEATABLE-READ for consistency ] ]; @@ -118,19 +121,23 @@ class LBFactoryTest extends MediaWikiTestCase { $dbw = $lb->getConnection( DB_MASTER ); $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' ); $this->assertEquals( - $wgDBserver, $dbw->getLBInfo( 'clusterMasterHost' ), 'cluster master set' ); + ( $wgDBserver != '' ) ? $wgDBserver : 'localhost', + $dbw->getLBInfo( 'clusterMasterHost' ), + 'cluster master set' ); $dbr = $lb->getConnection( DB_SLAVE ); $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' ); $this->assertEquals( - $wgDBserver, $dbr->getLBInfo( 'clusterMasterHost' ), 'cluster master set' ); + ( $wgDBserver != '' ) ? $wgDBserver : 'localhost', + $dbr->getLBInfo( 'clusterMasterHost' ), + 'cluster master set' ); $factory->shutdown(); $lb->closeAll(); } public function testLBFactoryMulti() { - global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype; + global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir; $factory = new LBFactoryMulti( [ 'sectionsByDB' => [], @@ -145,6 +152,7 @@ class LBFactoryTest extends MediaWikiTestCase { 'user' => $wgDBuser, 'password' => $wgDBpassword, 'type' => $wgDBtype, + 'dbDirectory' => $wgSQLiteDataDir, 'flags' => DBO_DEFAULT ], 'hostsByName' => [ @@ -233,7 +241,7 @@ class LBFactoryTest extends MediaWikiTestCase { } private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) { - global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype; + global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgSQLiteDataDir; return new LBFactoryMulti( $baseOverride + [ 'sectionsByDB' => [], @@ -247,6 +255,7 @@ class LBFactoryTest extends MediaWikiTestCase { 'user' => $wgDBuser, 'password' => $wgDBpassword, 'type' => $wgDBtype, + 'dbDirectory' => $wgSQLiteDataDir, 'flags' => DBO_DEFAULT ], 'hostsByName' => [ @@ -258,17 +267,32 @@ class LBFactoryTest extends MediaWikiTestCase { } public function testNiceDomains() { - global $wgDBname; + global $wgDBname, $wgDBtype; + + if ( $wgDBtype === 'sqlite' ) { + $tmpDir = $this->getNewTempDirectory(); + $dbPath = "$tmpDir/unit_test_db.sqlite"; + file_put_contents( $dbPath, '' ); + $tempFsFile = new TempFSFile( $dbPath ); + $tempFsFile->autocollect(); + } else { + $dbPath = null; + } - $factory = $this->newLBFactoryMulti(); + $factory = $this->newLBFactoryMulti( + [], + [ 'dbFilePath' => $dbPath ] + ); $lb = $factory->getMainLB(); - $db = $lb->getConnectionRef( DB_MASTER ); - $this->assertEquals( - $wgDBname, - $db->getDomainID() - ); - unset( $db ); + if ( $wgDBtype !== 'sqlite' ) { + $db = $lb->getConnectionRef( DB_MASTER ); + $this->assertEquals( + $wgDBname, + $db->getDomainID() + ); + unset( $db ); + } /** @var Database $db */ $db = $lb->getConnection( DB_MASTER, [], '' ); @@ -280,19 +304,19 @@ class LBFactoryTest extends MediaWikiTestCase { ); $this->assertEquals( - $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, 'page' ), $db->tableName( 'page' ), "Correct full table name" ); $this->assertEquals( - $db->addIdentifierQuotes( $wgDBname ) . '.' . $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( "$wgDBname.page" ), "Correct full table name" ); $this->assertEquals( - $db->addIdentifierQuotes( 'nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( 'nice_db.page' ), "Correct full table name" ); @@ -303,12 +327,12 @@ class LBFactoryTest extends MediaWikiTestCase { $db->getDomainID() ); $this->assertEquals( - $db->addIdentifierQuotes( 'my_page' ), + $this->quoteTable( $db, 'my_page' ), $db->tableName( 'page' ), "Correct full table name" ); $this->assertEquals( - $db->addIdentifierQuotes( 'other_nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( 'other_nice_db.page' ), "Correct full table name" ); @@ -318,9 +342,23 @@ class LBFactoryTest extends MediaWikiTestCase { } public function testTrickyDomain() { + global $wgDBtype; + + if ( $wgDBtype === 'sqlite' ) { + $tmpDir = $this->getNewTempDirectory(); + $dbPath = "$tmpDir/unit_test_db.sqlite"; + file_put_contents( $dbPath, '' ); + $tempFsFile = new TempFSFile( $dbPath ); + $tempFsFile->autocollect(); + } else { + $dbPath = null; + } + $dbname = 'unittest-domain'; $factory = $this->newLBFactoryMulti( - [ 'localDomain' => $dbname ], [ 'dbname' => $dbname ] ); + [ 'localDomain' => $dbname ], + [ 'dbname' => $dbname, 'dbFilePath' => $dbPath ] + ); $lb = $factory->getMainLB(); /** @var Database $db */ $db = $lb->getConnection( DB_MASTER, [], '' ); @@ -332,19 +370,19 @@ class LBFactoryTest extends MediaWikiTestCase { ); $this->assertEquals( - $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, 'page' ), $db->tableName( 'page' ), "Correct full table name" ); $this->assertEquals( - $db->addIdentifierQuotes( $dbname ) . '.' . $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( "$dbname.page" ), "Correct full table name" ); $this->assertEquals( - $db->addIdentifierQuotes( 'nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( 'nice_db.page' ), "Correct full table name" ); @@ -352,12 +390,12 @@ class LBFactoryTest extends MediaWikiTestCase { $factory->setDomainPrefix( 'my_' ); $this->assertEquals( - $db->addIdentifierQuotes( 'my_page' ), + $this->quoteTable( $db, 'my_page' ), $db->tableName( 'page' ), "Correct full table name" ); $this->assertEquals( - $db->addIdentifierQuotes( 'other_nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( 'other_nice_db.page' ), "Correct full table name" ); @@ -367,7 +405,7 @@ class LBFactoryTest extends MediaWikiTestCase { \MediaWiki\restoreWarnings(); $this->assertEquals( - $db->addIdentifierQuotes( 'garbage-db' ) . '.' . $db->addIdentifierQuotes( 'page' ), + $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( 'garbage-db.page' ), "Correct full table name" ); @@ -375,4 +413,12 @@ class LBFactoryTest extends MediaWikiTestCase { $factory->closeAll(); $factory->destroy(); } + + private function quoteTable( Database $db, $table ) { + if ( $db->getType() === 'sqlite' ) { + return $table; + } else { + return $db->addIdentifierQuotes( $table ); + } + } } -- 2.20.1