phpunit: Simplify mock object syntax in includes/db/ tests
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseMysqlBaseTest.php
1 <?php
2 /**
3 * Holds tests for DatabaseMysqlBase MediaWiki 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 * @author Bryan Davis
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Bryan Davis
25 * @copyright © 2013 Wikimedia Foundation Inc.
26 */
27
28 /**
29 * Fake class around abstract class so we can call concrete methods.
30 */
31 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
32 // From DatabaseBase
33 function __construct() {
34 $this->profiler = new ProfilerStub( [] );
35 $this->trxProfiler = new TransactionProfiler();
36 }
37
38 protected function closeConnection() {
39 }
40
41 protected function doQuery( $sql ) {
42 }
43
44 // From DatabaseMysql
45 protected function mysqlConnect( $realServer ) {
46 }
47
48 protected function mysqlSetCharset( $charset ) {
49 }
50
51 protected function mysqlFreeResult( $res ) {
52 }
53
54 protected function mysqlFetchObject( $res ) {
55 }
56
57 protected function mysqlFetchArray( $res ) {
58 }
59
60 protected function mysqlNumRows( $res ) {
61 }
62
63 protected function mysqlNumFields( $res ) {
64 }
65
66 protected function mysqlFieldName( $res, $n ) {
67 }
68
69 protected function mysqlFieldType( $res, $n ) {
70 }
71
72 protected function mysqlDataSeek( $res, $row ) {
73 }
74
75 protected function mysqlError( $conn = null ) {
76 }
77
78 protected function mysqlFetchField( $res, $n ) {
79 }
80
81 protected function mysqlRealEscapeString( $s ) {
82
83 }
84
85 // From interface DatabaseType
86 function insertId() {
87 }
88
89 function lastErrno() {
90 }
91
92 function affectedRows() {
93 }
94
95 function getServerVersion() {
96 }
97 }
98
99 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
100 /**
101 * @dataProvider provideDiapers
102 * @covers DatabaseMysqlBase::addIdentifierQuotes
103 */
104 public function testAddIdentifierQuotes( $expected, $in ) {
105 $db = new FakeDatabaseMysqlBase();
106 $quoted = $db->addIdentifierQuotes( $in );
107 $this->assertEquals( $expected, $quoted );
108 }
109
110 /**
111 * Feeds testAddIdentifierQuotes
112 *
113 * Named per bug 20281 convention.
114 */
115 function provideDiapers() {
116 return [
117 // Format: expected, input
118 [ '``', '' ],
119
120 // Yeah I really hate loosely typed PHP idiocies nowadays
121 [ '``', null ],
122
123 // Dear codereviewer, guess what addIdentifierQuotes()
124 // will return with thoses:
125 [ '``', false ],
126 [ '`1`', true ],
127
128 // We never know what could happen
129 [ '`0`', 0 ],
130 [ '`1`', 1 ],
131
132 // Whatchout! Should probably use something more meaningful
133 [ "`'`", "'" ], # single quote
134 [ '`"`', '"' ], # double quote
135 [ '````', '`' ], # backtick
136 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
137
138 // sneaky NUL bytes are lurking everywhere
139 [ '``', "\0" ],
140 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
141
142 // unicode chars
143 [
144 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
145 self::createUnicodeString( '\u0001a\uFFFFb' )
146 ],
147 [
148 self::createUnicodeString( '`\u0001\uFFFF`' ),
149 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
150 ],
151 [ '`☃`', '☃' ],
152 [ '`メインページ`', 'メインページ' ],
153 [ '`Басты_бет`', 'Басты_бет' ],
154
155 // Real world:
156 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
157 [ '`Backtick: ```', 'Backtick: `' ],
158 [ '`This is a test`', 'This is a test' ],
159 ];
160 }
161
162 private static function createUnicodeString( $str ) {
163 return json_decode( '"' . $str . '"' );
164 }
165
166 function getMockForViews() {
167 $db = $this->getMockBuilder( 'DatabaseMysql' )
168 ->disableOriginalConstructor()
169 ->setMethods( [ 'fetchRow', 'query' ] )
170 ->getMock();
171
172 $db->method( 'query' )
173 ->with( $this->anything() )
174 ->willReturn( null );
175
176 $db->method( 'fetchRow' )
177 ->with( $this->anything() )
178 ->will( $this->onConsecutiveCalls(
179 [ 'Tables_in_' => 'view1' ],
180 [ 'Tables_in_' => 'view2' ],
181 [ 'Tables_in_' => 'myview' ],
182 false # no more rows
183 ) );
184 return $db;
185 }
186 /**
187 * @covers DatabaseMysqlBase::listViews
188 */
189 function testListviews() {
190 $db = $this->getMockForViews();
191
192 // The first call populate an internal cache of views
193 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
194 $db->listViews() );
195 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
196 $db->listViews() );
197
198 // Prefix filtering
199 $this->assertEquals( [ 'view1', 'view2' ],
200 $db->listViews( 'view' ) );
201 $this->assertEquals( [ 'myview' ],
202 $db->listViews( 'my' ) );
203 $this->assertEquals( [],
204 $db->listViews( 'UNUSED_PREFIX' ) );
205 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
206 $db->listViews( '' ) );
207 }
208
209 /**
210 * @covers DatabaseMysqlBase::isView
211 * @dataProvider provideViewExistanceChecks
212 */
213 function testIsView( $isView, $viewName ) {
214 $db = $this->getMockForViews();
215
216 switch ( $isView ) {
217 case true:
218 $this->assertTrue( $db->isView( $viewName ),
219 "$viewName should be considered a view" );
220 break;
221
222 case false:
223 $this->assertFalse( $db->isView( $viewName ),
224 "$viewName has not been defined as a view" );
225 break;
226 }
227
228 }
229
230 function provideViewExistanceChecks() {
231 return [
232 // format: whether it is a view, view name
233 [ true, 'view1' ],
234 [ true, 'view2' ],
235 [ true, 'myview' ],
236
237 [ false, 'user' ],
238
239 [ false, 'view10' ],
240 [ false, 'my' ],
241 [ false, 'OH_MY_GOD' ], # they killed kenny!
242 ];
243 }
244
245 /**
246 * @dataProvider provideComparePositions
247 */
248 function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
249 if ( $match ) {
250 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
251
252 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
253 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
254 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
255 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
256 } else { // channels don't match
257 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
258
259 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
260 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
261 }
262 }
263
264 function provideComparePositions() {
265 return [
266 // Binlog style
267 [
268 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
269 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
270 true
271 ],
272 [
273 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
274 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
275 true
276 ],
277 [
278 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
279 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
280 false
281 ],
282 // MySQL GTID style
283 [
284 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
285 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
286 true
287 ],
288 [
289 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
290 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
291 true
292 ],
293 [
294 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
295 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
296 false
297 ],
298 // MariaDB GTID style
299 [
300 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
301 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
302 true
303 ],
304 [
305 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
306 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
307 true
308 ],
309 [
310 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
311 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
312 false
313 ],
314 ];
315 }
316
317 /**
318 * @dataProvider provideChannelPositions
319 */
320 function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
321 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
322 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
323 }
324
325 function provideChannelPositions() {
326 return [
327 [
328 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
329 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
330 true
331 ],
332 [
333 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
334 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
335 true
336 ],
337 [
338 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
339 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
340 false
341 ],
342 [
343 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
344 new MySQLMasterPos( 'trump2016.000976', '10000' ),
345 false
346 ],
347 ];
348 }
349
350 /**
351 * @dataProvider provideLagAmounts
352 */
353 function testPtHeartbeat( $lag ) {
354 $db = $this->getMockBuilder( 'DatabaseMysql' )
355 ->disableOriginalConstructor()
356 ->setMethods( [
357 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
358 ->getMock();
359
360 $db->method( 'getLagDetectionMethod' )
361 ->willReturn( 'pt-heartbeat' );
362
363 $db->method( 'getMasterServerInfo' )
364 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
365
366 // Fake the current time.
367 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
368 $now = (float)$nowSec + (float)$nowSecFrac;
369 // Fake the heartbeat time.
370 // Work arounds for weak DataTime microseconds support.
371 $ptTime = $now - $lag;
372 $ptSec = (int)$ptTime;
373 $ptSecFrac = ( $ptTime - $ptSec );
374 $ptDateTime = new DateTime( "@$ptSec" );
375 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
376 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
377
378 $db->method( 'getHeartbeatData' )
379 ->with( [ 'server_id' => 172 ] )
380 ->willReturn( [ $ptTimeISO, $now ] );
381
382 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
383 $lagEst = $db->getLag();
384
385 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
386 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
387 }
388
389 function provideLagAmounts() {
390 return [
391 [ 0 ],
392 [ 0.3 ],
393 [ 6.5 ],
394 [ 10.1 ],
395 [ 200.2 ],
396 [ 400.7 ],
397 [ 600.22 ],
398 [ 1000.77 ],
399 ];
400 }
401 }