3 * Holds tests for DatabaseMysqlBase class.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation and contributors
26 use Wikimedia\Rdbms\TransactionProfiler
;
27 use Wikimedia\Rdbms\DatabaseDomain
;
28 use Wikimedia\Rdbms\MySQLMasterPos
;
29 use Wikimedia\Rdbms\DatabaseMysqlBase
;
30 use Wikimedia\Rdbms\Database
;
33 * Fake class around abstract class so we can call concrete methods.
35 class FakeDatabaseMysqlBase
extends DatabaseMysqlBase
{
37 function __construct() {
38 $this->profiler
= new ProfilerStub( [] );
39 $this->trxProfiler
= new TransactionProfiler();
40 $this->cliMode
= true;
41 $this->connLogger
= new \Psr\Log\
NullLogger();
42 $this->queryLogger
= new \Psr\Log\
NullLogger();
43 $this->errorLogger
= function ( Exception
$e ) {
44 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
46 $this->currentDomain
= DatabaseDomain
::newUnspecified();
49 protected function closeConnection() {
52 protected function doQuery( $sql ) {
55 // From DatabaseMysqli
56 protected function mysqlConnect( $realServer ) {
59 protected function mysqlSetCharset( $charset ) {
62 protected function mysqlFreeResult( $res ) {
65 protected function mysqlFetchObject( $res ) {
68 protected function mysqlFetchArray( $res ) {
71 protected function mysqlNumRows( $res ) {
74 protected function mysqlNumFields( $res ) {
77 protected function mysqlFieldName( $res, $n ) {
80 protected function mysqlFieldType( $res, $n ) {
83 protected function mysqlDataSeek( $res, $row ) {
86 protected function mysqlError( $conn = null ) {
89 protected function mysqlFetchField( $res, $n ) {
92 protected function mysqlRealEscapeString( $s ) {
98 function lastErrno() {
101 function affectedRows() {
104 function getServerVersion() {
108 class DatabaseMysqlBaseTest
extends PHPUnit_Framework_TestCase
{
110 use MediaWikiCoversValidator
;
113 * @dataProvider provideDiapers
114 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
116 public function testAddIdentifierQuotes( $expected, $in ) {
117 $db = new FakeDatabaseMysqlBase();
118 $quoted = $db->addIdentifierQuotes( $in );
119 $this->assertEquals( $expected, $quoted );
123 * Feeds testAddIdentifierQuotes
125 * Named per T22281 convention.
127 public static function provideDiapers() {
129 // Format: expected, input
132 // Yeah I really hate loosely typed PHP idiocies nowadays
135 // Dear codereviewer, guess what addIdentifierQuotes()
136 // will return with thoses:
140 // We never know what could happen
144 // Whatchout! Should probably use something more meaningful
145 [ "`'`", "'" ], # single quote
146 [ '`"`', '"' ], # double quote
147 [ '````', '`' ], # backtick
148 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
150 // sneaky NUL bytes are lurking everywhere
152 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
156 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
157 self
::createUnicodeString( '\u0001a\uFFFFb' )
160 self
::createUnicodeString( '`\u0001\uFFFF`' ),
161 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
164 [ '`メインページ`', 'メインページ' ],
165 [ '`Басты_бет`', 'Басты_бет' ],
168 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
169 [ '`Backtick: ```', 'Backtick: `' ],
170 [ '`This is a test`', 'This is a test' ],
174 private static function createUnicodeString( $str ) {
175 return json_decode( '"' . $str . '"' );
178 private function getMockForViews() {
179 $db = $this->getMockBuilder( 'DatabaseMysqli' )
180 ->disableOriginalConstructor()
181 ->setMethods( [ 'fetchRow', 'query' ] )
184 $db->method( 'query' )
185 ->with( $this->anything() )
186 ->willReturn( new FakeResultWrapper( [
187 (object)[ 'Tables_in_' => 'view1' ],
188 (object)[ 'Tables_in_' => 'view2' ],
189 (object)[ 'Tables_in_' => 'myview' ]
196 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
198 public function testListviews() {
199 $db = $this->getMockForViews();
201 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
205 $this->assertEquals( [ 'view1', 'view2' ],
206 $db->listViews( 'view' ) );
207 $this->assertEquals( [ 'myview' ],
208 $db->listViews( 'my' ) );
209 $this->assertEquals( [],
210 $db->listViews( 'UNUSED_PREFIX' ) );
211 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
212 $db->listViews( '' ) );
216 * @dataProvider provideComparePositions
217 * @covers Wikimedia\Rdbms\MySQLMasterPos
219 public function testHasReached( MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match ) {
221 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
223 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
224 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
225 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
226 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
227 } else { // channels don't match
228 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
230 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
231 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
235 public static function provideComparePositions() {
239 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
240 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
244 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
245 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
249 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
250 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
255 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
256 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
260 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
261 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
265 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
266 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
269 // MariaDB GTID style
271 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
272 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
276 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
277 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
281 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
282 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
289 * @dataProvider provideChannelPositions
290 * @covers Wikimedia\Rdbms\MySQLMasterPos
292 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
293 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
294 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
297 public static function provideChannelPositions() {
300 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
301 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
305 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
306 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
310 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
311 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
315 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
316 new MySQLMasterPos( 'trump2016.000976', '10000' ),
323 * @dataProvider provideLagAmounts
324 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
325 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
327 public function testPtHeartbeat( $lag ) {
328 $db = $this->getMockBuilder( 'DatabaseMysqli' )
329 ->disableOriginalConstructor()
331 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
334 $db->method( 'getLagDetectionMethod' )
335 ->willReturn( 'pt-heartbeat' );
337 $db->method( 'getMasterServerInfo' )
338 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
340 // Fake the current time.
341 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
342 $now = (float)$nowSec +
(float)$nowSecFrac;
343 // Fake the heartbeat time.
344 // Work arounds for weak DataTime microseconds support.
345 $ptTime = $now - $lag;
346 $ptSec = (int)$ptTime;
347 $ptSecFrac = ( $ptTime - $ptSec );
348 $ptDateTime = new DateTime( "@$ptSec" );
349 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
350 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
352 $db->method( 'getHeartbeatData' )
353 ->with( [ 'server_id' => 172 ] )
354 ->willReturn( [ $ptTimeISO, $now ] );
356 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
357 $lagEst = $db->getLag();
359 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
360 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
363 public static function provideLagAmounts() {
377 * @expectedException UnexpectedValueException
378 * @covers Wikimedia\Rdbms\Database::setFlag
380 public function testDBOIgnoreSet() {
381 $db = new FakeDatabaseMysqlBase();
383 $db->setFlag( Database
::DBO_IGNORE
);
387 * @expectedException UnexpectedValueException
388 * @covers Wikimedia\Rdbms\Database::clearFlag
390 public function testDBOIgnoreClear() {
391 $db = new FakeDatabaseMysqlBase();
393 $db->clearFlag( Database
::DBO_IGNORE
);