From: Timo Tijhof Date: Fri, 21 Jul 2017 00:14:52 +0000 (-0700) Subject: rdbms: Move DatabaseMysqlBaseTest to libs tests X-Git-Tag: 1.31.0-rc.0~2646^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/comptes/ajouter.php?a=commitdiff_plain;h=b857284f710c48ea5fbdbe69981471383faaecf2;p=lhc%2Fweb%2Fwiklou.git rdbms: Move DatabaseMysqlBaseTest to libs tests * Remove MediaWikiTestCase dependency. * Add missing @covers tags. * Add missing visibility/static declarations. Change-Id: I2d81a9265016174e7b4ff810f140f7d031ce9f14 --- diff --git a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php deleted file mode 100644 index 3dc810c811..0000000000 --- a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php +++ /dev/null @@ -1,366 +0,0 @@ -profiler = new ProfilerStub( [] ); - $this->trxProfiler = new TransactionProfiler(); - $this->cliMode = true; - $this->connLogger = new \Psr\Log\NullLogger(); - $this->queryLogger = new \Psr\Log\NullLogger(); - $this->errorLogger = function ( Exception $e ) { - wfWarn( get_class( $e ) . ": {$e->getMessage()}" ); - }; - $this->currentDomain = DatabaseDomain::newUnspecified(); - } - - protected function closeConnection() { - } - - protected function doQuery( $sql ) { - } - - // From DatabaseMysql - protected function mysqlConnect( $realServer ) { - } - - protected function mysqlSetCharset( $charset ) { - } - - protected function mysqlFreeResult( $res ) { - } - - protected function mysqlFetchObject( $res ) { - } - - protected function mysqlFetchArray( $res ) { - } - - protected function mysqlNumRows( $res ) { - } - - protected function mysqlNumFields( $res ) { - } - - protected function mysqlFieldName( $res, $n ) { - } - - protected function mysqlFieldType( $res, $n ) { - } - - protected function mysqlDataSeek( $res, $row ) { - } - - protected function mysqlError( $conn = null ) { - } - - protected function mysqlFetchField( $res, $n ) { - } - - protected function mysqlRealEscapeString( $s ) { - } - - function insertId() { - } - - function lastErrno() { - } - - function affectedRows() { - } - - function getServerVersion() { - } -} - -class DatabaseMysqlBaseTest extends MediaWikiTestCase { - /** - * @dataProvider provideDiapers - * @covers DatabaseMysqlBase::addIdentifierQuotes - */ - public function testAddIdentifierQuotes( $expected, $in ) { - $db = new FakeDatabaseMysqlBase(); - $quoted = $db->addIdentifierQuotes( $in ); - $this->assertEquals( $expected, $quoted ); - } - - /** - * Feeds testAddIdentifierQuotes - * - * Named per T22281 convention. - */ - function provideDiapers() { - return [ - // Format: expected, input - [ '``', '' ], - - // Yeah I really hate loosely typed PHP idiocies nowadays - [ '``', null ], - - // Dear codereviewer, guess what addIdentifierQuotes() - // will return with thoses: - [ '``', false ], - [ '`1`', true ], - - // We never know what could happen - [ '`0`', 0 ], - [ '`1`', 1 ], - - // Whatchout! Should probably use something more meaningful - [ "`'`", "'" ], # single quote - [ '`"`', '"' ], # double quote - [ '````', '`' ], # backtick - [ '`’`', '’' ], # apostrophe (look at your encyclopedia) - - // sneaky NUL bytes are lurking everywhere - [ '``', "\0" ], - [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ], - - // unicode chars - [ - self::createUnicodeString( '`\u0001a\uFFFFb`' ), - self::createUnicodeString( '\u0001a\uFFFFb' ) - ], - [ - self::createUnicodeString( '`\u0001\uFFFF`' ), - self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' ) - ], - [ '`☃`', '☃' ], - [ '`メインページ`', 'メインページ' ], - [ '`Басты_бет`', 'Басты_бет' ], - - // Real world: - [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); } - [ '`Backtick: ```', 'Backtick: `' ], - [ '`This is a test`', 'This is a test' ], - ]; - } - - private static function createUnicodeString( $str ) { - return json_decode( '"' . $str . '"' ); - } - - function getMockForViews() { - $db = $this->getMockBuilder( 'DatabaseMysqli' ) - ->disableOriginalConstructor() - ->setMethods( [ 'fetchRow', 'query' ] ) - ->getMock(); - - $db->method( 'query' ) - ->with( $this->anything() ) - ->willReturn( new FakeResultWrapper( [ - (object)[ 'Tables_in_' => 'view1' ], - (object)[ 'Tables_in_' => 'view2' ], - (object)[ 'Tables_in_' => 'myview' ] - ] ) ); - - return $db; - } - /** - * @covers DatabaseMysqlBase::listViews - */ - function testListviews() { - $db = $this->getMockForViews(); - - $this->assertEquals( [ 'view1', 'view2', 'myview' ], - $db->listViews() ); - - // Prefix filtering - $this->assertEquals( [ 'view1', 'view2' ], - $db->listViews( 'view' ) ); - $this->assertEquals( [ 'myview' ], - $db->listViews( 'my' ) ); - $this->assertEquals( [], - $db->listViews( 'UNUSED_PREFIX' ) ); - $this->assertEquals( [ 'view1', 'view2', 'myview' ], - $db->listViews( '' ) ); - } - - /** - * @dataProvider provideComparePositions - */ - function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) { - if ( $match ) { - $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) ); - - $this->assertTrue( $higherPos->hasReached( $lowerPos ) ); - $this->assertTrue( $higherPos->hasReached( $higherPos ) ); - $this->assertTrue( $lowerPos->hasReached( $lowerPos ) ); - $this->assertFalse( $lowerPos->hasReached( $higherPos ) ); - } else { // channels don't match - $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) ); - - $this->assertFalse( $higherPos->hasReached( $lowerPos ) ); - $this->assertFalse( $lowerPos->hasReached( $higherPos ) ); - } - } - - function provideComparePositions() { - return [ - // Binlog style - [ - new MySQLMasterPos( 'db1034-bin.000976', '843431247' ), - new MySQLMasterPos( 'db1034-bin.000976', '843431248' ), - true - ], - [ - new MySQLMasterPos( 'db1034-bin.000976', '999' ), - new MySQLMasterPos( 'db1034-bin.000976', '1000' ), - true - ], - [ - new MySQLMasterPos( 'db1034-bin.000976', '999' ), - new MySQLMasterPos( 'db1035-bin.000976', '1000' ), - false - ], - // MySQL GTID style - [ - new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ), - new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ), - true - ], - [ - new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ), - new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ), - true - ], - [ - new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ), - new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ), - false - ], - // MariaDB GTID style - [ - new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ), - new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ), - true - ], - [ - new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ), - new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ), - true - ], - [ - new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ), - new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ), - false - ], - ]; - } - - /** - * @dataProvider provideChannelPositions - */ - function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) { - $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) ); - $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) ); - } - - function provideChannelPositions() { - return [ - [ - new MySQLMasterPos( 'db1034-bin.000876', '44' ), - new MySQLMasterPos( 'db1034-bin.000976', '74' ), - true - ], - [ - new MySQLMasterPos( 'db1052-bin.000976', '999' ), - new MySQLMasterPos( 'db1052-bin.000976', '1000' ), - true - ], - [ - new MySQLMasterPos( 'db1066-bin.000976', '9999' ), - new MySQLMasterPos( 'db1035-bin.000976', '10000' ), - false - ], - [ - new MySQLMasterPos( 'db1066-bin.000976', '9999' ), - new MySQLMasterPos( 'trump2016.000976', '10000' ), - false - ], - ]; - } - - /** - * @dataProvider provideLagAmounts - */ - function testPtHeartbeat( $lag ) { - $db = $this->getMockBuilder( 'DatabaseMysqli' ) - ->disableOriginalConstructor() - ->setMethods( [ - 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] ) - ->getMock(); - - $db->method( 'getLagDetectionMethod' ) - ->willReturn( 'pt-heartbeat' ); - - $db->method( 'getMasterServerInfo' ) - ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] ); - - // Fake the current time. - list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() ); - $now = (float)$nowSec + (float)$nowSecFrac; - // Fake the heartbeat time. - // Work arounds for weak DataTime microseconds support. - $ptTime = $now - $lag; - $ptSec = (int)$ptTime; - $ptSecFrac = ( $ptTime - $ptSec ); - $ptDateTime = new DateTime( "@$ptSec" ); - $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' ); - $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' ); - - $db->method( 'getHeartbeatData' ) - ->with( [ 'server_id' => 172 ] ) - ->willReturn( [ $ptTimeISO, $now ] ); - - $db->setLBInfo( 'clusterMasterHost', 'db1052' ); - $lagEst = $db->getLag(); - - $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" ); - $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" ); - } - - function provideLagAmounts() { - return [ - [ 0 ], - [ 0.3 ], - [ 6.5 ], - [ 10.1 ], - [ 200.2 ], - [ 400.7 ], - [ 600.22 ], - [ 1000.77 ], - ]; - } -} diff --git a/tests/phpunit/includes/libs/rdbms/database/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/libs/rdbms/database/DatabaseMysqlBaseTest.php new file mode 100644 index 0000000000..0d817fa50e --- /dev/null +++ b/tests/phpunit/includes/libs/rdbms/database/DatabaseMysqlBaseTest.php @@ -0,0 +1,371 @@ +profiler = new ProfilerStub( [] ); + $this->trxProfiler = new TransactionProfiler(); + $this->cliMode = true; + $this->connLogger = new \Psr\Log\NullLogger(); + $this->queryLogger = new \Psr\Log\NullLogger(); + $this->errorLogger = function ( Exception $e ) { + wfWarn( get_class( $e ) . ": {$e->getMessage()}" ); + }; + $this->currentDomain = DatabaseDomain::newUnspecified(); + } + + protected function closeConnection() { + } + + protected function doQuery( $sql ) { + } + + // From DatabaseMysql + protected function mysqlConnect( $realServer ) { + } + + protected function mysqlSetCharset( $charset ) { + } + + protected function mysqlFreeResult( $res ) { + } + + protected function mysqlFetchObject( $res ) { + } + + protected function mysqlFetchArray( $res ) { + } + + protected function mysqlNumRows( $res ) { + } + + protected function mysqlNumFields( $res ) { + } + + protected function mysqlFieldName( $res, $n ) { + } + + protected function mysqlFieldType( $res, $n ) { + } + + protected function mysqlDataSeek( $res, $row ) { + } + + protected function mysqlError( $conn = null ) { + } + + protected function mysqlFetchField( $res, $n ) { + } + + protected function mysqlRealEscapeString( $s ) { + } + + function insertId() { + } + + function lastErrno() { + } + + function affectedRows() { + } + + function getServerVersion() { + } +} + +class DatabaseMysqlBaseTest extends PHPUnit_Framework_TestCase { + /** + * @dataProvider provideDiapers + * @covers DatabaseMysqlBase::addIdentifierQuotes + */ + public function testAddIdentifierQuotes( $expected, $in ) { + $db = new FakeDatabaseMysqlBase(); + $quoted = $db->addIdentifierQuotes( $in ); + $this->assertEquals( $expected, $quoted ); + } + + /** + * Feeds testAddIdentifierQuotes + * + * Named per T22281 convention. + */ + public static function provideDiapers() { + return [ + // Format: expected, input + [ '``', '' ], + + // Yeah I really hate loosely typed PHP idiocies nowadays + [ '``', null ], + + // Dear codereviewer, guess what addIdentifierQuotes() + // will return with thoses: + [ '``', false ], + [ '`1`', true ], + + // We never know what could happen + [ '`0`', 0 ], + [ '`1`', 1 ], + + // Whatchout! Should probably use something more meaningful + [ "`'`", "'" ], # single quote + [ '`"`', '"' ], # double quote + [ '````', '`' ], # backtick + [ '`’`', '’' ], # apostrophe (look at your encyclopedia) + + // sneaky NUL bytes are lurking everywhere + [ '``', "\0" ], + [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ], + + // unicode chars + [ + self::createUnicodeString( '`\u0001a\uFFFFb`' ), + self::createUnicodeString( '\u0001a\uFFFFb' ) + ], + [ + self::createUnicodeString( '`\u0001\uFFFF`' ), + self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' ) + ], + [ '`☃`', '☃' ], + [ '`メインページ`', 'メインページ' ], + [ '`Басты_бет`', 'Басты_бет' ], + + // Real world: + [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); } + [ '`Backtick: ```', 'Backtick: `' ], + [ '`This is a test`', 'This is a test' ], + ]; + } + + private static function createUnicodeString( $str ) { + return json_decode( '"' . $str . '"' ); + } + + private function getMockForViews() { + $db = $this->getMockBuilder( 'DatabaseMysqli' ) + ->disableOriginalConstructor() + ->setMethods( [ 'fetchRow', 'query' ] ) + ->getMock(); + + $db->method( 'query' ) + ->with( $this->anything() ) + ->willReturn( new FakeResultWrapper( [ + (object)[ 'Tables_in_' => 'view1' ], + (object)[ 'Tables_in_' => 'view2' ], + (object)[ 'Tables_in_' => 'myview' ] + ] ) ); + + return $db; + } + + /** + * @covers DatabaseMysqlBase::listViews + */ + public function testListviews() { + $db = $this->getMockForViews(); + + $this->assertEquals( [ 'view1', 'view2', 'myview' ], + $db->listViews() ); + + // Prefix filtering + $this->assertEquals( [ 'view1', 'view2' ], + $db->listViews( 'view' ) ); + $this->assertEquals( [ 'myview' ], + $db->listViews( 'my' ) ); + $this->assertEquals( [], + $db->listViews( 'UNUSED_PREFIX' ) ); + $this->assertEquals( [ 'view1', 'view2', 'myview' ], + $db->listViews( '' ) ); + } + + /** + * @dataProvider provideComparePositions + * @covers MySQLMasterPos + */ + public function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) { + if ( $match ) { + $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) ); + + $this->assertTrue( $higherPos->hasReached( $lowerPos ) ); + $this->assertTrue( $higherPos->hasReached( $higherPos ) ); + $this->assertTrue( $lowerPos->hasReached( $lowerPos ) ); + $this->assertFalse( $lowerPos->hasReached( $higherPos ) ); + } else { // channels don't match + $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) ); + + $this->assertFalse( $higherPos->hasReached( $lowerPos ) ); + $this->assertFalse( $lowerPos->hasReached( $higherPos ) ); + } + } + + public static function provideComparePositions() { + return [ + // Binlog style + [ + new MySQLMasterPos( 'db1034-bin.000976', '843431247' ), + new MySQLMasterPos( 'db1034-bin.000976', '843431248' ), + true + ], + [ + new MySQLMasterPos( 'db1034-bin.000976', '999' ), + new MySQLMasterPos( 'db1034-bin.000976', '1000' ), + true + ], + [ + new MySQLMasterPos( 'db1034-bin.000976', '999' ), + new MySQLMasterPos( 'db1035-bin.000976', '1000' ), + false + ], + // MySQL GTID style + [ + new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ), + new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ), + true + ], + [ + new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ), + new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ), + true + ], + [ + new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ), + new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ), + false + ], + // MariaDB GTID style + [ + new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ), + new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ), + true + ], + [ + new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ), + new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ), + true + ], + [ + new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ), + new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ), + false + ], + ]; + } + + /** + * @dataProvider provideChannelPositions + * @covers MySQLMasterPos + */ + public function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) { + $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) ); + $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) ); + } + + public static function provideChannelPositions() { + return [ + [ + new MySQLMasterPos( 'db1034-bin.000876', '44' ), + new MySQLMasterPos( 'db1034-bin.000976', '74' ), + true + ], + [ + new MySQLMasterPos( 'db1052-bin.000976', '999' ), + new MySQLMasterPos( 'db1052-bin.000976', '1000' ), + true + ], + [ + new MySQLMasterPos( 'db1066-bin.000976', '9999' ), + new MySQLMasterPos( 'db1035-bin.000976', '10000' ), + false + ], + [ + new MySQLMasterPos( 'db1066-bin.000976', '9999' ), + new MySQLMasterPos( 'trump2016.000976', '10000' ), + false + ], + ]; + } + + /** + * @dataProvider provideLagAmounts + * @covers DatabaseMysqlBase::getLag + * @covers DatabaseMysqlBase::getLagFromPtHeartbeat + */ + public function testPtHeartbeat( $lag ) { + $db = $this->getMockBuilder( 'DatabaseMysqli' ) + ->disableOriginalConstructor() + ->setMethods( [ + 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] ) + ->getMock(); + + $db->method( 'getLagDetectionMethod' ) + ->willReturn( 'pt-heartbeat' ); + + $db->method( 'getMasterServerInfo' ) + ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] ); + + // Fake the current time. + list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() ); + $now = (float)$nowSec + (float)$nowSecFrac; + // Fake the heartbeat time. + // Work arounds for weak DataTime microseconds support. + $ptTime = $now - $lag; + $ptSec = (int)$ptTime; + $ptSecFrac = ( $ptTime - $ptSec ); + $ptDateTime = new DateTime( "@$ptSec" ); + $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' ); + $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' ); + + $db->method( 'getHeartbeatData' ) + ->with( [ 'server_id' => 172 ] ) + ->willReturn( [ $ptTimeISO, $now ] ); + + $db->setLBInfo( 'clusterMasterHost', 'db1052' ); + $lagEst = $db->getLag(); + + $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" ); + $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" ); + } + + public static function provideLagAmounts() { + return [ + [ 0 ], + [ 0.3 ], + [ 6.5 ], + [ 10.1 ], + [ 200.2 ], + [ 400.7 ], + [ 600.22 ], + [ 1000.77 ], + ]; + } +}