Merge "filebackend: avoid use of wfWikiId() in FileBackendGroup"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / database / DatabaseMysqlBaseTest.php
1 <?php
2 /**
3 * Holds tests for DatabaseMysqlBase class.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation and contributors
24 */
25
26 use Wikimedia\Rdbms\MySQLMasterPos;
27 use Wikimedia\TestingAccessWrapper;
28
29 class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
30
31 use MediaWikiCoversValidator;
32 use PHPUnit4And6Compat;
33
34 /**
35 * @dataProvider provideDiapers
36 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
37 */
38 public function testAddIdentifierQuotes( $expected, $in ) {
39 $db = $this->getMockBuilder( DatabaseMysqli::class )
40 ->disableOriginalConstructor()
41 ->setMethods( null )
42 ->getMock();
43
44 $quoted = $db->addIdentifierQuotes( $in );
45 $this->assertEquals( $expected, $quoted );
46 }
47
48 /**
49 * Feeds testAddIdentifierQuotes
50 *
51 * Named per T22281 convention.
52 */
53 public static function provideDiapers() {
54 return [
55 // Format: expected, input
56 [ '``', '' ],
57
58 // Yeah I really hate loosely typed PHP idiocies nowadays
59 [ '``', null ],
60
61 // Dear codereviewer, guess what addIdentifierQuotes()
62 // will return with thoses:
63 [ '``', false ],
64 [ '`1`', true ],
65
66 // We never know what could happen
67 [ '`0`', 0 ],
68 [ '`1`', 1 ],
69
70 // Whatchout! Should probably use something more meaningful
71 [ "`'`", "'" ], # single quote
72 [ '`"`', '"' ], # double quote
73 [ '````', '`' ], # backtick
74 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
75
76 // sneaky NUL bytes are lurking everywhere
77 [ '``', "\0" ],
78 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
79
80 // unicode chars
81 [
82 "`\u{0001}a\u{FFFF}b`",
83 "\u{0001}a\u{FFFF}b"
84 ],
85 [
86 "`\u{0001}\u{FFFF}`",
87 "\u{0001}\u{0000}\u{FFFF}\u{0000}"
88 ],
89 [ '`☃`', '☃' ],
90 [ '`メインページ`', 'メインページ' ],
91 [ '`Басты_бет`', 'Басты_бет' ],
92
93 // Real world:
94 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
95 [ '`Backtick: ```', 'Backtick: `' ],
96 [ '`This is a test`', 'This is a test' ],
97 ];
98 }
99
100 private function getMockForViews() {
101 $db = $this->getMockBuilder( DatabaseMysqli::class )
102 ->disableOriginalConstructor()
103 ->setMethods( [ 'fetchRow', 'query', 'getDBname' ] )
104 ->getMock();
105
106 $db->method( 'query' )
107 ->with( $this->anything() )
108 ->willReturn( new FakeResultWrapper( [
109 (object)[ 'Tables_in_' => 'view1' ],
110 (object)[ 'Tables_in_' => 'view2' ],
111 (object)[ 'Tables_in_' => 'myview' ]
112 ] ) );
113 $db->method( 'getDBname' )->willReturn( '' );
114
115 return $db;
116 }
117
118 /**
119 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
120 */
121 public function testListviews() {
122 $db = $this->getMockForViews();
123
124 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
125 $db->listViews() );
126
127 // Prefix filtering
128 $this->assertEquals( [ 'view1', 'view2' ],
129 $db->listViews( 'view' ) );
130 $this->assertEquals( [ 'myview' ],
131 $db->listViews( 'my' ) );
132 $this->assertEquals( [],
133 $db->listViews( 'UNUSED_PREFIX' ) );
134 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
135 $db->listViews( '' ) );
136 }
137
138 /**
139 * @covers Wikimedia\Rdbms\MySQLMasterPos
140 */
141 public function testBinLogName() {
142 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
143
144 $this->assertEquals( "db1052", $pos->getLogName() );
145 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
146 $this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
147 }
148
149 /**
150 * @dataProvider provideComparePositions
151 * @covers Wikimedia\Rdbms\MySQLMasterPos
152 */
153 public function testHasReached(
154 MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match, $hetero
155 ) {
156 if ( $match ) {
157 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
158
159 if ( $hetero ) {
160 // Each position is has one channel higher than the other
161 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
162 } else {
163 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
164 }
165 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
166 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
167 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
168 } else { // channels don't match
169 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
170
171 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
172 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
173 }
174 }
175
176 public static function provideComparePositions() {
177 $now = microtime( true );
178
179 return [
180 // Binlog style
181 [
182 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
183 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
184 true,
185 false
186 ],
187 [
188 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
189 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
190 true,
191 false
192 ],
193 [
194 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
195 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
196 false,
197 false
198 ],
199 // MySQL GTID style
200 [
201 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ),
202 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
203 true,
204 false
205 ],
206 [
207 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
208 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
209 true,
210 false
211 ],
212 [
213 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
214 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
215 false,
216 false
217 ],
218 // MariaDB GTID style
219 [
220 new MySQLMasterPos( '255-11-23', $now ),
221 new MySQLMasterPos( '255-11-24', $now ),
222 true,
223 false
224 ],
225 [
226 new MySQLMasterPos( '255-11-99', $now ),
227 new MySQLMasterPos( '255-11-100', $now ),
228 true,
229 false
230 ],
231 [
232 new MySQLMasterPos( '255-11-999', $now ),
233 new MySQLMasterPos( '254-11-1000', $now ),
234 false,
235 false
236 ],
237 [
238 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
239 new MySQLMasterPos( '255-11-24', $now ),
240 true,
241 false
242 ],
243 [
244 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
245 new MySQLMasterPos( '255-11-1000', $now ),
246 true,
247 false
248 ],
249 [
250 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
251 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
252 true,
253 false
254 ],
255 [
256 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
257 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
258 true,
259 false
260 ],
261 [
262 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
263 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
264 true,
265 true
266 ],
267 [
268 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
269 new MySQLMasterPos( '254-11-1000', $now ),
270 false,
271 false
272 ],
273 ];
274 }
275
276 /**
277 * @dataProvider provideChannelPositions
278 * @covers Wikimedia\Rdbms\MySQLMasterPos
279 */
280 public function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
281 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
282 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
283
284 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
285 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
286 }
287
288 public static function provideChannelPositions() {
289 $now = microtime( true );
290
291 return [
292 [
293 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
294 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
295 true
296 ],
297 [
298 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
299 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
300 true
301 ],
302 [
303 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
304 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
305 false
306 ],
307 [
308 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
309 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
310 false
311 ],
312 ];
313 }
314
315 /**
316 * @dataProvider provideCommonDomainGTIDs
317 * @covers Wikimedia\Rdbms\MySQLMasterPos
318 */
319 public function testGetRelevantActiveGTIDs( MySQLMasterPos $pos, MySQLMasterPos $ref, $gtids ) {
320 $this->assertEquals( $gtids, MySQLMasterPos::getRelevantActiveGTIDs( $pos, $ref ) );
321 }
322
323 public static function provideCommonDomainGTIDs() {
324 return [
325 [
326 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
327 new MySQLMasterPos( '255-11-1000', 1 ),
328 [ '255-13-99' ]
329 ],
330 [
331 ( new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ) )
332 ->setActiveDomain( 257 ),
333 new MySQLMasterPos( '255-11-1000,257-14-30', 1 ),
334 [ '257-14-50' ]
335 ],
336 [
337 new MySQLMasterPos(
338 '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' .
339 '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
340 '7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
341 1
342 ),
343 new MySQLMasterPos(
344 '1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
345 '3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
346 1
347 ),
348 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ]
349 ]
350 ];
351 }
352
353 /**
354 * @dataProvider provideLagAmounts
355 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
356 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
357 */
358 public function testPtHeartbeat( $lag ) {
359 $db = $this->getMockBuilder( DatabaseMysqli::class )
360 ->disableOriginalConstructor()
361 ->setMethods( [
362 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
363 ->getMock();
364
365 $db->method( 'getLagDetectionMethod' )
366 ->willReturn( 'pt-heartbeat' );
367
368 $db->method( 'getMasterServerInfo' )
369 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
370
371 // Fake the current time.
372 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
373 $now = (float)$nowSec + (float)$nowSecFrac;
374 // Fake the heartbeat time.
375 // Work arounds for weak DataTime microseconds support.
376 $ptTime = $now - $lag;
377 $ptSec = (int)$ptTime;
378 $ptSecFrac = ( $ptTime - $ptSec );
379 $ptDateTime = new DateTime( "@$ptSec" );
380 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
381 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
382
383 $db->method( 'getHeartbeatData' )
384 ->with( [ 'server_id' => 172 ] )
385 ->willReturn( [ $ptTimeISO, $now ] );
386
387 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
388 $lagEst = $db->getLag();
389
390 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
391 $this->assertLessThan( $lag + 0.010, $lagEst, "Correct heatbeat lag" );
392 }
393
394 public static function provideLagAmounts() {
395 return [
396 [ 0 ],
397 [ 0.3 ],
398 [ 6.5 ],
399 [ 10.1 ],
400 [ 200.2 ],
401 [ 400.7 ],
402 [ 600.22 ],
403 [ 1000.77 ],
404 ];
405 }
406
407 /**
408 * @dataProvider provideGtidData
409 * @covers Wikimedia\Rdbms\MySQLMasterPos
410 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getReplicaPos
411 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getMasterPos
412 */
413 public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) {
414 $db = $this->getMockBuilder( DatabaseMysqli::class )
415 ->disableOriginalConstructor()
416 ->setMethods( [
417 'useGTIDs',
418 'getServerGTIDs',
419 'getServerRoleStatus',
420 'getServerId',
421 'getServerUUID'
422 ] )
423 ->getMock();
424
425 $db->method( 'useGTIDs' )->willReturn( true );
426 $db->method( 'getServerGTIDs' )->willReturn( $gtable );
427 $db->method( 'getServerRoleStatus' )->willReturnCallback(
428 function ( $role ) use ( $rBLtable, $mBLtable ) {
429 if ( $role === 'SLAVE' ) {
430 return $rBLtable;
431 } elseif ( $role === 'MASTER' ) {
432 return $mBLtable;
433 }
434
435 return null;
436 }
437 );
438 $db->method( 'getServerId' )->willReturn( 1 );
439 $db->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' );
440
441 if ( is_array( $rGTIDs ) ) {
442 $this->assertEquals( $rGTIDs, $db->getReplicaPos()->getGTIDs() );
443 } else {
444 $this->assertEquals( false, $db->getReplicaPos() );
445 }
446 if ( is_array( $mGTIDs ) ) {
447 $this->assertEquals( $mGTIDs, $db->getMasterPos()->getGTIDs() );
448 } else {
449 $this->assertEquals( false, $db->getMasterPos() );
450 }
451 }
452
453 public static function provideGtidData() {
454 return [
455 // MariaDB
456 [
457 [
458 'gtid_domain_id' => 100,
459 'gtid_current_pos' => '100-13-77',
460 'gtid_binlog_pos' => '100-13-77',
461 'gtid_slave_pos' => null // master
462 ],
463 [
464 'Relay_Master_Log_File' => 'host.1600',
465 'Exec_Master_Log_Pos' => '77'
466 ],
467 [
468 'File' => 'host.1600',
469 'Position' => '77'
470 ],
471 [],
472 [ '100' => '100-13-77' ]
473 ],
474 [
475 [
476 'gtid_domain_id' => 100,
477 'gtid_current_pos' => '100-13-77',
478 'gtid_binlog_pos' => '100-13-77',
479 'gtid_slave_pos' => '100-13-77' // replica
480 ],
481 [
482 'Relay_Master_Log_File' => 'host.1600',
483 'Exec_Master_Log_Pos' => '77'
484 ],
485 [],
486 [ '100' => '100-13-77' ],
487 [ '100' => '100-13-77' ]
488 ],
489 [
490 [
491 'gtid_current_pos' => '100-13-77',
492 'gtid_binlog_pos' => '100-13-77',
493 'gtid_slave_pos' => '100-13-77' // replica
494 ],
495 [
496 'Relay_Master_Log_File' => 'host.1600',
497 'Exec_Master_Log_Pos' => '77'
498 ],
499 [],
500 [ '100' => '100-13-77' ],
501 [ '100' => '100-13-77' ]
502 ],
503 // MySQL
504 [
505 [
506 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77'
507 ],
508 [
509 'Relay_Master_Log_File' => 'host.1600',
510 'Exec_Master_Log_Pos' => '77'
511 ],
512 [], // only a replica
513 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
514 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
515 // replica/master use same var
516 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
517 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
518 ],
519 [
520 [
521 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-49,' .
522 '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77'
523 ],
524 [
525 'Relay_Master_Log_File' => 'host.1600',
526 'Exec_Master_Log_Pos' => '77'
527 ],
528 [], // only a replica
529 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
530 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
531 // replica/master use same var
532 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
533 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
534 ],
535 [
536 [
537 'gtid_executed' => null, // not enabled?
538 'gtid_binlog_pos' => null
539 ],
540 [
541 'Relay_Master_Log_File' => 'host.1600',
542 'Exec_Master_Log_Pos' => '77'
543 ],
544 [], // only a replica
545 [], // binlog fallback
546 false
547 ],
548 [
549 [
550 'gtid_executed' => null, // not enabled?
551 'gtid_binlog_pos' => null
552 ],
553 [], // no replication
554 [], // no replication
555 false,
556 false
557 ]
558 ];
559 }
560
561 /**
562 * @covers Wikimedia\Rdbms\MySQLMasterPos
563 */
564 public function testSerialize() {
565 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
566 $roundtripPos = unserialize( serialize( $pos ) );
567
568 $this->assertEquals( $pos, $roundtripPos );
569
570 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
571 $roundtripPos = unserialize( serialize( $pos ) );
572
573 $this->assertEquals( $pos, $roundtripPos );
574 }
575
576 /**
577 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
578 * @dataProvider provideInsertSelectCases
579 */
580 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
581 $db = $this->getMockBuilder( DatabaseMysqli::class )
582 ->disableOriginalConstructor()
583 ->setMethods( [ 'getReplicationSafetyInfo' ] )
584 ->getMock();
585 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
586 $dbw = TestingAccessWrapper::newFromObject( $db );
587
588 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
589 }
590
591 public function provideInsertSelectCases() {
592 return [
593 [
594 [],
595 [],
596 [
597 'innodb_autoinc_lock_mode' => '2',
598 'binlog_format' => 'ROW',
599 ],
600 true
601 ],
602 [
603 [],
604 [ 'LIMIT' => 100 ],
605 [
606 'innodb_autoinc_lock_mode' => '2',
607 'binlog_format' => 'ROW',
608 ],
609 true
610 ],
611 [
612 [],
613 [ 'LIMIT' => 100 ],
614 [
615 'innodb_autoinc_lock_mode' => '0',
616 'binlog_format' => 'STATEMENT',
617 ],
618 false
619 ],
620 [
621 [],
622 [],
623 [
624 'innodb_autoinc_lock_mode' => '2',
625 'binlog_format' => 'STATEMENT',
626 ],
627 false
628 ],
629 [
630 [ 'NO_AUTO_COLUMNS' ],
631 [ 'LIMIT' => 100 ],
632 [
633 'innodb_autoinc_lock_mode' => '0',
634 'binlog_format' => 'STATEMENT',
635 ],
636 false
637 ],
638 [
639 [],
640 [],
641 [
642 'innodb_autoinc_lock_mode' => 0,
643 'binlog_format' => 'STATEMENT',
644 ],
645 true
646 ],
647 [
648 [ 'NO_AUTO_COLUMNS' ],
649 [],
650 [
651 'innodb_autoinc_lock_mode' => 2,
652 'binlog_format' => 'STATEMENT',
653 ],
654 true
655 ],
656 [
657 [ 'NO_AUTO_COLUMNS' ],
658 [],
659 [
660 'innodb_autoinc_lock_mode' => 0,
661 'binlog_format' => 'STATEMENT',
662 ],
663 true
664 ],
665
666 ];
667 }
668
669 /**
670 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
671 */
672 public function testBuildIntegerCast() {
673 $db = $this->getMockBuilder( DatabaseMysqli::class )
674 ->disableOriginalConstructor()
675 ->setMethods( null )
676 ->getMock();
677 $output = $db->buildIntegerCast( 'fieldName' );
678 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
679 }
680
681 /**
682 * @covers Wikimedia\Rdbms\Database::setIndexAliases
683 */
684 public function testIndexAliases() {
685 $db = $this->getMockBuilder( DatabaseMysqli::class )
686 ->disableOriginalConstructor()
687 ->setMethods( [ 'mysqlRealEscapeString', 'dbSchema', 'tablePrefix' ] )
688 ->getMock();
689 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
690 function ( $s ) {
691 return str_replace( "'", "\\'", $s );
692 }
693 );
694
695 $db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
696 $sql = $db->selectSQLText(
697 'zend', 'field', [ 'a' => 'x' ], __METHOD__, [ 'USE INDEX' => 'a_b_idx' ] );
698
699 $this->assertEquals(
700 "SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
701 $sql
702 );
703
704 $db->setIndexAliases( [] );
705 $sql = $db->selectSQLText(
706 'zend', 'field', [ 'a' => 'x' ], __METHOD__, [ 'USE INDEX' => 'a_b_idx' ] );
707
708 $this->assertEquals(
709 "SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
710 $sql
711 );
712 }
713
714 /**
715 * @covers Wikimedia\Rdbms\Database::setTableAliases
716 */
717 public function testTableAliases() {
718 $db = $this->getMockBuilder( DatabaseMysqli::class )
719 ->disableOriginalConstructor()
720 ->setMethods( [ 'mysqlRealEscapeString', 'dbSchema', 'tablePrefix' ] )
721 ->getMock();
722 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
723 function ( $s ) {
724 return str_replace( "'", "\\'", $s );
725 }
726 );
727
728 $db->setTableAliases( [
729 'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
730 ] );
731 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__ );
732
733 $this->assertEquals(
734 "SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
735 $sql
736 );
737
738 $db->setTableAliases( [] );
739 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__ );
740
741 $this->assertEquals(
742 "SELECT field FROM `meow` WHERE a = 'x' ",
743 $sql
744 );
745 }
746 }