Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / db / DatabaseMysqlBaseTest.php
blobb95462abfeec7cc158a58b95adca42fa2ea238fd
1 <?php
2 /**
3 * Holds tests for DatabaseMysqlBase MediaWiki 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
20 * @file
21 * @author Antoine Musso
22 * @author Bryan Davis
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Bryan Davis
25 * @copyright © 2013 Wikimedia Foundation Inc.
28 use Wikimedia\Rdbms\TransactionProfiler;
29 use Wikimedia\Rdbms\DatabaseDomain;
30 use Wikimedia\Rdbms\MySQLMasterPos;
32 /**
33 * Fake class around abstract class so we can call concrete methods.
35 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
36 // From Database
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 DatabaseMysql
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 ) {
96 function insertId() {
99 function lastErrno() {
102 function affectedRows() {
105 function getServerVersion() {
109 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
111 * @dataProvider provideDiapers
112 * @covers DatabaseMysqlBase::addIdentifierQuotes
114 public function testAddIdentifierQuotes( $expected, $in ) {
115 $db = new FakeDatabaseMysqlBase();
116 $quoted = $db->addIdentifierQuotes( $in );
117 $this->assertEquals( $expected, $quoted );
121 * Feeds testAddIdentifierQuotes
123 * Named per T22281 convention.
125 function provideDiapers() {
126 return [
127 // Format: expected, input
128 [ '``', '' ],
130 // Yeah I really hate loosely typed PHP idiocies nowadays
131 [ '``', null ],
133 // Dear codereviewer, guess what addIdentifierQuotes()
134 // will return with thoses:
135 [ '``', false ],
136 [ '`1`', true ],
138 // We never know what could happen
139 [ '`0`', 0 ],
140 [ '`1`', 1 ],
142 // Whatchout! Should probably use something more meaningful
143 [ "`'`", "'" ], # single quote
144 [ '`"`', '"' ], # double quote
145 [ '````', '`' ], # backtick
146 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
148 // sneaky NUL bytes are lurking everywhere
149 [ '``', "\0" ],
150 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
152 // unicode chars
154 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
155 self::createUnicodeString( '\u0001a\uFFFFb' )
158 self::createUnicodeString( '`\u0001\uFFFF`' ),
159 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
161 [ '`☃`', '☃' ],
162 [ '`メインページ`', 'メインページ' ],
163 [ '`Басты_бет`', 'Басты_бет' ],
165 // Real world:
166 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
167 [ '`Backtick: ```', 'Backtick: `' ],
168 [ '`This is a test`', 'This is a test' ],
172 private static function createUnicodeString( $str ) {
173 return json_decode( '"' . $str . '"' );
176 function getMockForViews() {
177 $db = $this->getMockBuilder( 'DatabaseMysqli' )
178 ->disableOriginalConstructor()
179 ->setMethods( [ 'fetchRow', 'query' ] )
180 ->getMock();
182 $db->method( 'query' )
183 ->with( $this->anything() )
184 ->willReturn( new FakeResultWrapper( [
185 (object)[ 'Tables_in_' => 'view1' ],
186 (object)[ 'Tables_in_' => 'view2' ],
187 (object)[ 'Tables_in_' => 'myview' ]
188 ] ) );
190 return $db;
193 * @covers DatabaseMysqlBase::listViews
195 function testListviews() {
196 $db = $this->getMockForViews();
198 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
199 $db->listViews() );
201 // Prefix filtering
202 $this->assertEquals( [ 'view1', 'view2' ],
203 $db->listViews( 'view' ) );
204 $this->assertEquals( [ 'myview' ],
205 $db->listViews( 'my' ) );
206 $this->assertEquals( [],
207 $db->listViews( 'UNUSED_PREFIX' ) );
208 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
209 $db->listViews( '' ) );
213 * @dataProvider provideComparePositions
215 function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
216 if ( $match ) {
217 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
219 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
220 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
221 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
222 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
223 } else { // channels don't match
224 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
226 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
227 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
231 function provideComparePositions() {
232 return [
233 // Binlog style
235 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
236 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
237 true
240 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
241 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
242 true
245 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
246 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
247 false
249 // MySQL GTID style
251 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
252 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
253 true
256 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
257 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
258 true
261 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
262 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
263 false
265 // MariaDB GTID style
267 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
268 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
269 true
272 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
273 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
274 true
277 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
278 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
279 false
285 * @dataProvider provideChannelPositions
287 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
288 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
289 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
292 function provideChannelPositions() {
293 return [
295 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
296 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
297 true
300 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
301 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
302 true
305 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
306 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
307 false
310 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
311 new MySQLMasterPos( 'trump2016.000976', '10000' ),
312 false
318 * @dataProvider provideLagAmounts
320 function testPtHeartbeat( $lag ) {
321 $db = $this->getMockBuilder( 'DatabaseMysqli' )
322 ->disableOriginalConstructor()
323 ->setMethods( [
324 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
325 ->getMock();
327 $db->method( 'getLagDetectionMethod' )
328 ->willReturn( 'pt-heartbeat' );
330 $db->method( 'getMasterServerInfo' )
331 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
333 // Fake the current time.
334 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
335 $now = (float)$nowSec + (float)$nowSecFrac;
336 // Fake the heartbeat time.
337 // Work arounds for weak DataTime microseconds support.
338 $ptTime = $now - $lag;
339 $ptSec = (int)$ptTime;
340 $ptSecFrac = ( $ptTime - $ptSec );
341 $ptDateTime = new DateTime( "@$ptSec" );
342 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
343 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
345 $db->method( 'getHeartbeatData' )
346 ->with( [ 'server_id' => 172 ] )
347 ->willReturn( [ $ptTimeISO, $now ] );
349 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
350 $lagEst = $db->getLag();
352 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
353 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
356 function provideLagAmounts() {
357 return [
358 [ 0 ],
359 [ 0.3 ],
360 [ 6.5 ],
361 [ 10.1 ],
362 [ 200.2 ],
363 [ 400.7 ],
364 [ 600.22 ],
365 [ 1000.77 ],