Reset table sequences and skip some test assertions for sqlite
[lhc/web/wiklou.git] / tests / phpunit / includes / db / LBFactoryTest.php
index fa1cfc9..ed4c977 100644 (file)
@@ -194,12 +194,12 @@ class LBFactoryTest extends MediaWikiTestCase {
         * @covers \Wikimedia\Rdbms\ChronologyProtector
         */
        public function testChronologyProtector() {
-               // (a) First HTTP request
-               $m1Pos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
-               $m2Pos = new MySQLMasterPos( 'db1064-bin.002400', '794074907' );
-
                $now = microtime( true );
 
+               // (a) First HTTP request
+               $m1Pos = new MySQLMasterPos( 'db1034-bin.000976/843431247', $now );
+               $m2Pos = new MySQLMasterPos( 'db1064-bin.002400/794074907', $now );
+
                // Master DB 1
                $mockDB1 = $this->getMockBuilder( DatabaseMysqli::class )
                        ->disableOriginalConstructor()
@@ -345,45 +345,38 @@ class LBFactoryTest extends MediaWikiTestCase {
        }
 
        public function testNiceDomains() {
-               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;
+               global $wgDBname;
+
+               if ( wfGetDB( DB_MASTER )->databasesAreIndependent() ) {
+                       self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
+                       return;
                }
 
                $factory = $this->newLBFactoryMulti(
                        [],
-                       [ 'dbFilePath' => $dbPath ]
+                       []
                );
                $lb = $factory->getMainLB();
 
-               if ( $wgDBtype !== 'sqlite' ) {
-                       $db = $lb->getConnectionRef( DB_MASTER );
-                       $this->assertEquals(
-                               wfWikiID(),
-                               $db->getDomainID()
-                       );
-                       unset( $db );
-               }
+               $db = $lb->getConnectionRef( DB_MASTER );
+               $this->assertEquals(
+                       wfWikiID(),
+                       $db->getDomainID()
+               );
+               unset( $db );
 
                /** @var Database $db */
                $db = $lb->getConnection( DB_MASTER, [], '' );
 
                $this->assertEquals(
-                       $wgDBname,
+                       '',
                        $db->getDomainId(),
-                       'Main domain ID handle used; same DB name'
+                       'Null domain ID handle used'
                );
                $this->assertEquals(
-                       $wgDBname,
+                       '',
                        $db->getDBname(),
-                       'Main domain ID handle used; same DB name'
+                       'Null domain ID handle used'
                );
                $this->assertEquals(
                        '',
@@ -411,6 +404,7 @@ class LBFactoryTest extends MediaWikiTestCase {
                $db = $lb->getConnection( DB_MASTER ); // local domain connection
                $factory->setDomainPrefix( 'my_' );
 
+               $this->assertEquals( $wgDBname, $db->getDBname() );
                $this->assertEquals(
                        "$wgDBname-my_",
                        $db->getDomainID()
@@ -431,31 +425,25 @@ class LBFactoryTest extends MediaWikiTestCase {
        }
 
        public function testTrickyDomain() {
-               global $wgDBtype, $wgDBname;
-
-               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;
+               global $wgDBname;
+
+               if ( wfGetDB( DB_MASTER )->databasesAreIndependent() ) {
+                       self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
+                       return;
                }
 
                $dbname = 'unittest-domain'; // explodes if DB is selected
                $factory = $this->newLBFactoryMulti(
                        [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
-                       [ 'dbFilePath' => $dbPath ]
+                       [
+                               'dbName' => 'do_not_select_me' // explodes if DB is selected
+                       ]
                );
                $lb = $factory->getMainLB();
                /** @var Database $db */
                $db = $lb->getConnection( DB_MASTER, [], '' );
 
-               $this->assertEquals(
-                       $wgDBname,
-                       $db->getDomainID()
-               );
+               $this->assertEquals( '', $db->getDomainID(), "Null domain used" );
 
                $this->assertEquals(
                        $this->quoteTable( $db, 'page' ),
@@ -496,7 +484,27 @@ class LBFactoryTest extends MediaWikiTestCase {
                        "Correct full table name"
                );
 
-               if ( $db->databasesAreIndependent() ) {
+               $lb->reuseConnection( $db ); // don't care
+
+               $factory->closeAll();
+               $factory->destroy();
+       }
+
+       public function testInvalidSelectDB() {
+               $dbname = 'unittest-domain'; // explodes if DB is selected
+               $factory = $this->newLBFactoryMulti(
+                       [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
+                       [
+                               'dbName' => 'do_not_select_me' // explodes if DB is selected
+                       ]
+               );
+               $lb = $factory->getMainLB();
+               /** @var Database $db */
+               $db = $lb->getConnection( DB_MASTER, [], '' );
+
+               if ( $db->getType() === 'sqlite' ) {
+                       $this->assertFalse( $db->selectDB( 'garbage-db' ) );
+               } elseif ( $db->databasesAreIndependent() ) {
                        try {
                                $e = null;
                                $db->selectDB( 'garbage-db' );
@@ -506,15 +514,10 @@ class LBFactoryTest extends MediaWikiTestCase {
                        $this->assertInstanceOf( \Wikimedia\Rdbms\DBConnectionError::class, $e );
                        $this->assertFalse( $db->isOpen() );
                } else {
-                       \MediaWiki\suppressWarnings();
+                       \Wikimedia\suppressWarnings();
                        $this->assertFalse( $db->selectDB( 'garbage-db' ) );
-                       \MediaWiki\restoreWarnings();
+                       \Wikimedia\restoreWarnings();
                }
-
-               $lb->reuseConnection( $db ); // don't care
-
-               $factory->closeAll();
-               $factory->destroy();
        }
 
        private function quoteTable( Database $db, $table ) {