| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Rdbms; |
| 4 |
|
| 5 |
use DatabaseTestHelper; |
| 6 |
use MediaWiki\Tests\MockDatabase; |
| 7 |
use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock; |
| 8 |
use MediaWiki\Tests\Unit\Libs\Rdbms\SQLPlatformTestHelper; |
| 9 |
use MediaWikiCoversValidator; |
| 10 |
use PHPUnit\Framework\TestCase; |
| 11 |
use Psr\Log\NullLogger; |
| 12 |
use RuntimeException; |
| 13 |
use Throwable; |
| 14 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 15 |
use Wikimedia\Rdbms\AndExpressionGroup; |
| 16 |
use Wikimedia\Rdbms\Database; |
| 17 |
use Wikimedia\Rdbms\Database\DatabaseFlags; |
| 18 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 19 |
use Wikimedia\Rdbms\DBLanguageError; |
| 20 |
use Wikimedia\Rdbms\DBReadOnlyRoleError; |
| 21 |
use Wikimedia\Rdbms\DBTransactionStateError; |
| 22 |
use Wikimedia\Rdbms\DBUnexpectedError; |
| 23 |
use Wikimedia\Rdbms\Expression; |
| 24 |
use Wikimedia\Rdbms\IDatabase; |
| 25 |
use Wikimedia\Rdbms\IResultWrapper; |
| 26 |
use Wikimedia\Rdbms\LBFactorySingle; |
| 27 |
use Wikimedia\Rdbms\OrExpressionGroup; |
| 28 |
use Wikimedia\Rdbms\Platform\SQLPlatform; |
| 29 |
use Wikimedia\Rdbms\QueryStatus; |
| 30 |
use Wikimedia\Rdbms\Replication\ReplicationReporter; |
| 31 |
use Wikimedia\Rdbms\ServerInfo; |
| 32 |
use Wikimedia\Rdbms\TransactionManager; |
| 33 |
use Wikimedia\RequestTimeout\CriticalSectionScope; |
| 34 |
use Wikimedia\TestingAccessWrapper; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class DatabaseTest extends TestCase { |
| 43 |
|
| 44 |
use MediaWikiCoversValidator; |
| 45 |
|
| 46 |
|
| 47 |
private $db; |
| 48 |
|
| 49 |
protected function setUp(): void { |
| 50 |
$this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() ); |
| 51 |
} |
| 52 |
|
| 53 |
public static function provideAddQuotes() { |
| 54 |
return [ |
| 55 |
[ null, 'NULL' ], |
| 56 |
[ 1234, "1234" ], |
| 57 |
[ 1234.5678, "'1234.5678'" ], |
| 58 |
[ 'string', "'string'" ], |
| 59 |
[ 'string\'s cause trouble', "'string\'s cause trouble'" ], |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function testAddQuotes( $input, $expected ) { |
| 67 |
$this->assertEquals( $expected, $this->db->addQuotes( $input ) ); |
| 68 |
} |
| 69 |
|
| 70 |
public static function provideTableName() { |
| 71 |
return [ |
| 72 |
'local' => [ |
| 73 |
'"tablename"', |
| 74 |
'tablename', |
| 75 |
'quoted', |
| 76 |
], |
| 77 |
'local-raw' => [ |
| 78 |
'tablename', |
| 79 |
'tablename', |
| 80 |
'raw', |
| 81 |
], |
| 82 |
'shared' => [ |
| 83 |
'"sharedb"."tablename"', |
| 84 |
'tablename', |
| 85 |
'quoted', |
| 86 |
[ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ], |
| 87 |
], |
| 88 |
'shared-raw' => [ |
| 89 |
'sharedb.tablename', |
| 90 |
'tablename', |
| 91 |
'raw', |
| 92 |
[ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ], |
| 93 |
], |
| 94 |
'shared-prefix' => [ |
| 95 |
'"sharedb"."sh_tablename"', |
| 96 |
'tablename', |
| 97 |
'quoted', |
| 98 |
[ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ], |
| 99 |
], |
| 100 |
'shared-prefix-raw' => [ |
| 101 |
'sharedb.sh_tablename', |
| 102 |
'tablename', |
| 103 |
'raw', |
| 104 |
[ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ], |
| 105 |
], |
| 106 |
'foreign' => [ |
| 107 |
'"databasename"."tablename"', |
| 108 |
'databasename.tablename', |
| 109 |
'quoted', |
| 110 |
], |
| 111 |
'foreign-raw' => [ |
| 112 |
'databasename.tablename', |
| 113 |
'databasename.tablename', |
| 114 |
'raw', |
| 115 |
], |
| 116 |
'foreign only DB quoted' => [ |
| 117 |
'"databasename"."tablename"', |
| 118 |
'"databasename".tablename', |
| 119 |
'quoted', |
| 120 |
], |
| 121 |
'foreign only table quoted' => [ |
| 122 |
'"databasename"."tablename"', |
| 123 |
'databasename."tablename"', |
| 124 |
'quoted', |
| 125 |
], |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function testTableName( $expected, $table, $format, ?array $alias = null ) { |
| 133 |
|
| 134 |
$db = new MockDatabase(); |
| 135 |
if ( $alias ) { |
| 136 |
$db->setTableAliases( [ $table => $alias ] ); |
| 137 |
} |
| 138 |
$this->assertEquals( |
| 139 |
$expected, |
| 140 |
$db->tableName( $table, $format ?: 'quoted' ) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
public static function provideYagniTableName() { |
| 145 |
$names = [ |
| 146 |
'"', |
| 147 |
'a.b.c.d', |
| 148 |
'"a.b".c', |
| 149 |
'"my_""wiki"."mw_page"', |
| 150 |
'"my_""wiki"."mw_page"', |
| 151 |
'"""my_""wiki"."mw_page"', |
| 152 |
'"my_""wiki"""."mw_page"', |
| 153 |
]; |
| 154 |
foreach ( $names as $name ) { |
| 155 |
yield [ $name ]; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function testYagniTableName( $table ) { |
| 166 |
$this->expectException( DBLanguageError::class ); |
| 167 |
$db = new MockDatabase(); |
| 168 |
$db->tableName( $table ); |
| 169 |
} |
| 170 |
|
| 171 |
public static function provideTableNamesWithIndexClauseOrJOIN() { |
| 172 |
return [ |
| 173 |
'one-element array' => [ |
| 174 |
[ 'table' ], [], 'table ' |
| 175 |
], |
| 176 |
'comma join' => [ |
| 177 |
[ 'table1', 'table2' ], [], 'table1,table2 ' |
| 178 |
], |
| 179 |
'real join' => [ |
| 180 |
[ 'table1', 'table2' ], |
| 181 |
[ 'table2' => [ 'LEFT JOIN', 't1_id = t2_id' ] ], |
| 182 |
'table1 LEFT JOIN table2 ON ((t1_id = t2_id))' |
| 183 |
], |
| 184 |
'real join with multiple conditionals' => [ |
| 185 |
[ 'table1', 'table2' ], |
| 186 |
[ 'table2' => [ 'LEFT JOIN', [ 't1_id = t2_id', 't2_x = \'X\'' ] ] ], |
| 187 |
'table1 LEFT JOIN table2 ON ((t1_id = t2_id) AND (t2_x = \'X\'))' |
| 188 |
], |
| 189 |
'join with parenthesized group' => [ |
| 190 |
[ 'table1', 'n' => [ 'table2', 'table3' ] ], |
| 191 |
[ |
| 192 |
'table3' => [ 'JOIN', 't2_id = t3_id' ], |
| 193 |
'n' => [ 'LEFT JOIN', 't1_id = t2_id' ], |
| 194 |
], |
| 195 |
'table1 LEFT JOIN (table2 JOIN table3 ON ((t2_id = t3_id))) ON ((t1_id = t2_id))' |
| 196 |
], |
| 197 |
'join with degenerate parenthesized group' => [ |
| 198 |
[ 'table1', 'n' => [ 't2' => 'table2' ] ], |
| 199 |
[ |
| 200 |
'n' => [ 'LEFT JOIN', 't1_id = t2_id' ], |
| 201 |
], |
| 202 |
'table1 LEFT JOIN table2 t2 ON ((t1_id = t2_id))' |
| 203 |
], |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function testTableNamesWithIndexClauseOrJOIN( $tables, $join_conds, $expect ) { |
| 211 |
$clause = TestingAccessWrapper::newFromObject( ( new SQLPlatformTestHelper( new AddQuoterMock() ) ) ) |
| 212 |
->tableNamesWithIndexClauseOrJOIN( $tables, [], [], $join_conds ); |
| 213 |
$this->assertSame( $expect, $clause ); |
| 214 |
} |
| 215 |
|
| 216 |
public function testTransactionIdle() { |
| 217 |
$db = $this->db; |
| 218 |
|
| 219 |
$db->clearFlag( DBO_TRX ); |
| 220 |
$called = false; |
| 221 |
$flagSet = null; |
| 222 |
$callback = static function ( $trigger, IDatabase $db ) use ( &$flagSet, &$called ) { |
| 223 |
$called = true; |
| 224 |
$flagSet = $db->getFlag( DBO_TRX ); |
| 225 |
}; |
| 226 |
|
| 227 |
$db->onTransactionCommitOrIdle( $callback, __METHOD__ ); |
| 228 |
$this->assertTrue( $called, 'Callback reached' ); |
| 229 |
$this->assertFalse( $flagSet, 'DBO_TRX off in callback' ); |
| 230 |
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX still default' ); |
| 231 |
|
| 232 |
$flagSet = null; |
| 233 |
$called = false; |
| 234 |
$db->startAtomic( __METHOD__ ); |
| 235 |
$db->onTransactionCommitOrIdle( $callback, __METHOD__ ); |
| 236 |
$this->assertFalse( $called, 'Callback not reached during TRX' ); |
| 237 |
$db->endAtomic( __METHOD__ ); |
| 238 |
|
| 239 |
$this->assertTrue( $called, 'Callback reached after COMMIT' ); |
| 240 |
$this->assertFalse( $flagSet, 'DBO_TRX off in callback' ); |
| 241 |
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' ); |
| 242 |
|
| 243 |
$db->clearFlag( DBO_TRX ); |
| 244 |
$db->onTransactionCommitOrIdle( |
| 245 |
static function ( $trigger, IDatabase $db ) { |
| 246 |
$db->setFlag( DBO_TRX ); |
| 247 |
}, |
| 248 |
__METHOD__ |
| 249 |
); |
| 250 |
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' ); |
| 251 |
} |
| 252 |
|
| 253 |
public function testTransactionIdle_TRX() { |
| 254 |
$db = $this->getMockDB( [ 'isOpen', 'ping', 'getDBname' ] ); |
| 255 |
$db->method( 'isOpen' )->willReturn( true ); |
| 256 |
$db->method( 'ping' )->willReturn( true ); |
| 257 |
$db->method( 'getDBname' )->willReturn( '' ); |
| 258 |
$db->setFlag( DBO_TRX ); |
| 259 |
|
| 260 |
$lbFactory = LBFactorySingle::newFromConnection( $db ); |
| 261 |
|
| 262 |
|
| 263 |
$lb = $lbFactory->getMainLB(); |
| 264 |
$conn = $lb->getConnectionInternal( ServerInfo::WRITER_INDEX ); |
| 265 |
$this->assertSame( $db, $conn, 'Same DB instance' ); |
| 266 |
$this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX is set' ); |
| 267 |
|
| 268 |
$called = false; |
| 269 |
$flagSet = null; |
| 270 |
$callback = static function () use ( $db, &$flagSet, &$called ) { |
| 271 |
$called = true; |
| 272 |
$flagSet = $db->getFlag( DBO_TRX ); |
| 273 |
}; |
| 274 |
|
| 275 |
$db->onTransactionCommitOrIdle( $callback, __METHOD__ ); |
| 276 |
$this->assertTrue( $called, 'Called when idle if DBO_TRX is set' ); |
| 277 |
$this->assertFalse( $flagSet, 'DBO_TRX off in callback' ); |
| 278 |
$this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX still default' ); |
| 279 |
|
| 280 |
$called = false; |
| 281 |
$lbFactory->beginPrimaryChanges( __METHOD__ ); |
| 282 |
$db->onTransactionCommitOrIdle( $callback, __METHOD__ ); |
| 283 |
$this->assertFalse( $called, 'Not called when lb-transaction is active' ); |
| 284 |
|
| 285 |
$lbFactory->commitPrimaryChanges( __METHOD__ ); |
| 286 |
$this->assertTrue( $called, 'Called when lb-transaction is committed' ); |
| 287 |
|
| 288 |
$called = false; |
| 289 |
$lbFactory->beginPrimaryChanges( __METHOD__ ); |
| 290 |
$db->onTransactionCommitOrIdle( $callback, __METHOD__ ); |
| 291 |
$this->assertFalse( $called, 'Not called when lb-transaction is active' ); |
| 292 |
|
| 293 |
$lbFactory->rollbackPrimaryChanges( __METHOD__ ); |
| 294 |
$this->assertFalse( $called, 'Not called when lb-transaction is rolled back' ); |
| 295 |
|
| 296 |
$lbFactory->commitPrimaryChanges( __METHOD__ ); |
| 297 |
$this->assertFalse( $called, 'Not called in next round commit' ); |
| 298 |
|
| 299 |
$db->setFlag( DBO_TRX ); |
| 300 |
try { |
| 301 |
$db->onTransactionCommitOrIdle( static function () { |
| 302 |
throw new RuntimeException( 'test' ); |
| 303 |
} ); |
| 304 |
$this->fail( "Exception not thrown" ); |
| 305 |
} catch ( RuntimeException $e ) { |
| 306 |
$this->assertTrue( $db->getFlag( DBO_TRX ) ); |
| 307 |
} |
| 308 |
|
| 309 |
$lbFactory->rollbackPrimaryChanges( __METHOD__ ); |
| 310 |
$lbFactory->flushPrimarySessions( __METHOD__ ); |
| 311 |
} |
| 312 |
|
| 313 |
public function testTransactionPreCommitOrIdle() { |
| 314 |
$db = $this->getMockDB( [ 'isOpen' ] ); |
| 315 |
$db->method( 'isOpen' )->willReturn( true ); |
| 316 |
$db->clearFlag( DBO_TRX ); |
| 317 |
|
| 318 |
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX is not set' ); |
| 319 |
|
| 320 |
$called = false; |
| 321 |
$db->onTransactionPreCommitOrIdle( |
| 322 |
static function ( IDatabase $db ) use ( &$called ) { |
| 323 |
$called = true; |
| 324 |
}, |
| 325 |
__METHOD__ |
| 326 |
); |
| 327 |
$this->assertTrue( $called, 'Called when idle' ); |
| 328 |
|
| 329 |
$db->begin( __METHOD__ ); |
| 330 |
$called = false; |
| 331 |
$db->onTransactionPreCommitOrIdle( |
| 332 |
static function ( IDatabase $db ) use ( &$called ) { |
| 333 |
$called = true; |
| 334 |
}, |
| 335 |
__METHOD__ |
| 336 |
); |
| 337 |
$this->assertFalse( $called, 'Not called when transaction is active' ); |
| 338 |
$db->commit( __METHOD__ ); |
| 339 |
$this->assertTrue( $called, 'Called when transaction is committed' ); |
| 340 |
} |
| 341 |
|
| 342 |
public function testTransactionPreCommitOrIdle_TRX() { |
| 343 |
$db = $this->getMockDB( [ 'isOpen', 'ping', 'getDBname' ] ); |
| 344 |
$db->method( 'isOpen' )->willReturn( true ); |
| 345 |
$db->method( 'ping' )->willReturn( true ); |
| 346 |
$db->method( 'getDBname' )->willReturn( 'unittest' ); |
| 347 |
$db->setFlag( DBO_TRX ); |
| 348 |
|
| 349 |
$lbFactory = LBFactorySingle::newFromConnection( $db ); |
| 350 |
|
| 351 |
|
| 352 |
$lb = $lbFactory->getMainLB(); |
| 353 |
$conn = $lb->getConnectionInternal( ServerInfo::WRITER_INDEX ); |
| 354 |
$this->assertSame( $db, $conn, 'Same DB instance' ); |
| 355 |
|
| 356 |
$this->assertFalse( $lb->hasPrimaryChanges() ); |
| 357 |
$this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX is set' ); |
| 358 |
$called = false; |
| 359 |
$callback = static function ( IDatabase $db ) use ( &$called ) { |
| 360 |
$called = true; |
| 361 |
}; |
| 362 |
$db->onTransactionPreCommitOrIdle( $callback, __METHOD__ ); |
| 363 |
$this->assertTrue( $called, 'Called when idle if DBO_TRX is set' ); |
| 364 |
$called = false; |
| 365 |
$lbFactory->commitPrimaryChanges(); |
| 366 |
$this->assertFalse( $called ); |
| 367 |
|
| 368 |
$called = false; |
| 369 |
$lbFactory->beginPrimaryChanges( __METHOD__ ); |
| 370 |
$db->onTransactionPreCommitOrIdle( $callback, __METHOD__ ); |
| 371 |
$this->assertFalse( $called, 'Not called when lb-transaction is active' ); |
| 372 |
$lbFactory->commitPrimaryChanges( __METHOD__ ); |
| 373 |
$this->assertTrue( $called, 'Called when lb-transaction is committed' ); |
| 374 |
|
| 375 |
$called = false; |
| 376 |
$lbFactory->beginPrimaryChanges( __METHOD__ ); |
| 377 |
$db->onTransactionPreCommitOrIdle( $callback, __METHOD__ ); |
| 378 |
$this->assertFalse( $called, 'Not called when lb-transaction is active' ); |
| 379 |
|
| 380 |
$lbFactory->rollbackPrimaryChanges( __METHOD__ ); |
| 381 |
$this->assertFalse( $called, 'Not called when lb-transaction is rolled back' ); |
| 382 |
|
| 383 |
$lbFactory->commitPrimaryChanges( __METHOD__ ); |
| 384 |
$this->assertFalse( $called, 'Not called in next round commit' ); |
| 385 |
|
| 386 |
$lbFactory->flushPrimarySessions( __METHOD__ ); |
| 387 |
} |
| 388 |
|
| 389 |
public function testTransactionResolution() { |
| 390 |
$db = $this->db; |
| 391 |
|
| 392 |
$db->clearFlag( DBO_TRX ); |
| 393 |
$db->begin( __METHOD__ ); |
| 394 |
$called = false; |
| 395 |
$db->onTransactionResolution( static function ( $trigger, IDatabase $db ) use ( &$called ) { |
| 396 |
$called = true; |
| 397 |
$db->setFlag( DBO_TRX ); |
| 398 |
} ); |
| 399 |
$db->commit( __METHOD__ ); |
| 400 |
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' ); |
| 401 |
$this->assertTrue( $called, 'Callback reached' ); |
| 402 |
|
| 403 |
$db->clearFlag( DBO_TRX ); |
| 404 |
$db->begin( __METHOD__ ); |
| 405 |
$called = false; |
| 406 |
$db->onTransactionResolution( static function ( $trigger, IDatabase $db ) use ( &$called ) { |
| 407 |
$called = true; |
| 408 |
$db->setFlag( DBO_TRX ); |
| 409 |
} ); |
| 410 |
$db->rollback( __METHOD__ ); |
| 411 |
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' ); |
| 412 |
$this->assertTrue( $called, 'Callback reached' ); |
| 413 |
} |
| 414 |
|
| 415 |
public function testTransactionListener() { |
| 416 |
$db = $this->db; |
| 417 |
|
| 418 |
$db->setTransactionListener( 'ping', static function () use ( &$called ) { |
| 419 |
$called = true; |
| 420 |
} ); |
| 421 |
|
| 422 |
$called = false; |
| 423 |
$db->begin( __METHOD__ ); |
| 424 |
$db->commit( __METHOD__ ); |
| 425 |
$this->assertTrue( $called, 'Callback reached' ); |
| 426 |
|
| 427 |
$called = false; |
| 428 |
$db->begin( __METHOD__ ); |
| 429 |
$db->commit( __METHOD__ ); |
| 430 |
$this->assertTrue( $called, 'Callback still reached' ); |
| 431 |
|
| 432 |
$called = false; |
| 433 |
$db->begin( __METHOD__ ); |
| 434 |
$db->rollback( __METHOD__ ); |
| 435 |
$this->assertTrue( $called, 'Callback reached' ); |
| 436 |
|
| 437 |
$db->setTransactionListener( 'ping', null ); |
| 438 |
$called = false; |
| 439 |
$db->begin( __METHOD__ ); |
| 440 |
$db->commit( __METHOD__ ); |
| 441 |
$this->assertFalse( $called, 'Callback not reached' ); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
private function getMockDB( $methods = [] ) { |
| 455 |
static $abstractMethods = [ |
| 456 |
'lastInsertId', |
| 457 |
'closeConnection', |
| 458 |
'doSingleStatementQuery', |
| 459 |
'fieldInfo', |
| 460 |
'getSoftwareLink', |
| 461 |
'getServerVersion', |
| 462 |
'getType', |
| 463 |
'indexInfo', |
| 464 |
'insertId', |
| 465 |
'lastError', |
| 466 |
'lastErrno', |
| 467 |
'open', |
| 468 |
'strencode', |
| 469 |
'tableExists', |
| 470 |
'getServer' |
| 471 |
]; |
| 472 |
$db = $this->getMockBuilder( Database::class ) |
| 473 |
->disableOriginalConstructor() |
| 474 |
->onlyMethods( array_values( array_unique( array_merge( |
| 475 |
$abstractMethods, |
| 476 |
$methods |
| 477 |
) ) ) ) |
| 478 |
->getMock(); |
| 479 |
$wdb = TestingAccessWrapper::newFromObject( $db ); |
| 480 |
$wdb->logger = new NullLogger(); |
| 481 |
$wdb->errorLogger = static function ( Throwable $e ) { |
| 482 |
}; |
| 483 |
$wdb->deprecationLogger = static function ( $msg ) { |
| 484 |
}; |
| 485 |
$wdb->currentDomain = DatabaseDomain::newUnspecified(); |
| 486 |
$wdb->platform = new SQLPlatform( new AddQuoterMock() ); |
| 487 |
$wdb->flagsHolder = new DatabaseFlags( 0 ); |
| 488 |
|
| 489 |
$wdb->connectionParams = [ |
| 490 |
'host' => 'localhost', |
| 491 |
'user' => 'testuser' |
| 492 |
]; |
| 493 |
$wdb->replicationReporter = new ReplicationReporter( IDatabase::ROLE_STREAMING_MASTER, new NullLogger(), new HashBagOStuff() ); |
| 494 |
|
| 495 |
$db->method( 'getServer' )->willReturn( '*dummy*' ); |
| 496 |
$db->setTransactionManager( new TransactionManager() ); |
| 497 |
|
| 498 |
$qs = new QueryStatus( false, 0, '', 0 ); |
| 499 |
$qs->res = true; |
| 500 |
$db->method( 'doSingleStatementQuery' )->willReturn( $qs ); |
| 501 |
|
| 502 |
return $db; |
| 503 |
} |
| 504 |
|
| 505 |
public function testFlushSnapshot() { |
| 506 |
$db = $this->getMockDB( [ 'isOpen' ] ); |
| 507 |
$db->method( 'isOpen' )->willReturn( true ); |
| 508 |
|
| 509 |
$db->flushSnapshot( __METHOD__ ); |
| 510 |
$db->flushSnapshot( __METHOD__ ); |
| 511 |
|
| 512 |
$db->setFlag( DBO_TRX, IDatabase::REMEMBER_PRIOR ); |
| 513 |
$db->query( 'SELECT 1', __METHOD__ ); |
| 514 |
$this->assertTrue( (bool)$db->trxLevel(), "Transaction started." ); |
| 515 |
$db->flushSnapshot( __METHOD__ ); |
| 516 |
$db->restoreFlags( $db::RESTORE_PRIOR ); |
| 517 |
|
| 518 |
$this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." ); |
| 519 |
} |
| 520 |
|
| 521 |
public function testGetScopedLock() { |
| 522 |
$db = $this->getMockDB( [ 'isOpen', 'getDBname' ] ); |
| 523 |
$db->method( 'isOpen' )->willReturn( true ); |
| 524 |
$db->method( 'getDBname' )->willReturn( 'unittest' ); |
| 525 |
|
| 526 |
$this->assertSame( 0, $db->trxLevel() ); |
| 527 |
$this->assertTrue( $db->lockIsFree( 'x', __METHOD__ ) ); |
| 528 |
$this->assertTrue( $db->lock( 'x', __METHOD__ ) ); |
| 529 |
$this->assertFalse( $db->lockIsFree( 'x', __METHOD__ ) ); |
| 530 |
$this->assertTrue( $db->unlock( 'x', __METHOD__ ) ); |
| 531 |
$this->assertTrue( $db->lockIsFree( 'x', __METHOD__ ) ); |
| 532 |
$this->assertSame( 0, $db->trxLevel() ); |
| 533 |
|
| 534 |
$db->setFlag( DBO_TRX ); |
| 535 |
$this->assertTrue( $db->lockIsFree( 'x', __METHOD__ ) ); |
| 536 |
$this->assertTrue( $db->lock( 'x', __METHOD__ ) ); |
| 537 |
$this->assertFalse( $db->lockIsFree( 'x', __METHOD__ ) ); |
| 538 |
$this->assertTrue( $db->unlock( 'x', __METHOD__ ) ); |
| 539 |
$this->assertTrue( $db->lockIsFree( 'x', __METHOD__ ) ); |
| 540 |
$db->clearFlag( DBO_TRX ); |
| 541 |
|
| 542 |
|
| 543 |
$this->assertSame( 0, $db->trxLevel() ); |
| 544 |
$this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) ); |
| 545 |
$db->setFlag( DBO_TRX ); |
| 546 |
$db->query( "DELETE FROM test WHERE t = 1" ); |
| 547 |
try { |
| 548 |
$lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 ); |
| 549 |
$this->fail( "Exception not reached" ); |
| 550 |
} catch ( DBUnexpectedError $e ) { |
| 551 |
$this->assertSame( 1, $db->trxLevel(), "Transaction not committed." ); |
| 552 |
$this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ), 'Lock not acquired' ); |
| 553 |
} |
| 554 |
$db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS ); |
| 555 |
|
| 556 |
$db->clearFlag( DBO_TRX ); |
| 557 |
$this->assertSame( 0, $db->trxLevel() ); |
| 558 |
$this->assertTrue( $db->lockIsFree( 'meow2', __METHOD__ ) ); |
| 559 |
$db->begin( __METHOD__ ); |
| 560 |
$db->query( "DELETE FROM test WHERE t = 1" ); |
| 561 |
try { |
| 562 |
$lock = $db->getScopedLockAndFlush( 'meow2', __METHOD__, 1 ); |
| 563 |
$this->fail( "Exception not reached" ); |
| 564 |
} catch ( DBUnexpectedError $e ) { |
| 565 |
$this->assertSame( 1, $db->trxLevel(), "Transaction not committed." ); |
| 566 |
$this->assertTrue( $db->lockIsFree( 'meow2', __METHOD__ ), 'Lock not acquired' ); |
| 567 |
} |
| 568 |
$db->rollback( __METHOD__ ); |
| 569 |
|
| 570 |
$db->setFlag( DBO_TRX ); |
| 571 |
$this->assertSame( 0, $db->trxLevel() ); |
| 572 |
$this->assertTrue( $db->lockIsFree( 'wuff', __METHOD__ ) ); |
| 573 |
$db->query( "SELECT 1", __METHOD__ ); |
| 574 |
$this->assertSame( 1, $db->trxLevel() ); |
| 575 |
$lock = $db->getScopedLockAndFlush( 'wuff', __METHOD__, 1 ); |
| 576 |
$this->assertSame( 0, $db->trxLevel() ); |
| 577 |
$this->assertFalse( $db->lockIsFree( 'wuff', __METHOD__ ), 'Lock already acquired' ); |
| 578 |
$db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS ); |
| 579 |
|
| 580 |
$db->clearFlag( DBO_TRX ); |
| 581 |
$this->assertSame( 0, $db->trxLevel() ); |
| 582 |
$this->assertTrue( $db->lockIsFree( 'wuff2', __METHOD__ ) ); |
| 583 |
$db->begin( __METHOD__ ); |
| 584 |
try { |
| 585 |
$lock = $db->getScopedLockAndFlush( 'wuff2', __METHOD__, 1 ); |
| 586 |
$this->fail( "Exception not reached" ); |
| 587 |
} catch ( DBUnexpectedError $e ) { |
| 588 |
$this->assertSame( 1, $db->trxLevel(), "Transaction not committed." ); |
| 589 |
$this->assertFalse( $db->lockIsFree( 'wuff2', __METHOD__ ), 'Lock not acquired' ); |
| 590 |
} |
| 591 |
$db->rollback( __METHOD__ ); |
| 592 |
} |
| 593 |
|
| 594 |
public function testFlagSetting() { |
| 595 |
$db = $this->db; |
| 596 |
$origTrx = $db->getFlag( DBO_TRX ); |
| 597 |
$origNoBuffer = $db->getFlag( DBO_NOBUFFER ); |
| 598 |
|
| 599 |
$origTrx |
| 600 |
? $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR ) |
| 601 |
: $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR ); |
| 602 |
$this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) ); |
| 603 |
|
| 604 |
$origNoBuffer |
| 605 |
? $db->clearFlag( DBO_NOBUFFER, $db::REMEMBER_PRIOR ) |
| 606 |
: $db->setFlag( DBO_NOBUFFER, $db::REMEMBER_PRIOR ); |
| 607 |
$this->assertEquals( !$origNoBuffer, $db->getFlag( DBO_NOBUFFER ) ); |
| 608 |
|
| 609 |
$db->restoreFlags( $db::RESTORE_INITIAL ); |
| 610 |
$this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) ); |
| 611 |
$this->assertEquals( $origNoBuffer, $db->getFlag( DBO_NOBUFFER ) ); |
| 612 |
|
| 613 |
$origTrx |
| 614 |
? $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR ) |
| 615 |
: $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR ); |
| 616 |
$origNoBuffer |
| 617 |
? $db->clearFlag( DBO_NOBUFFER, $db::REMEMBER_PRIOR ) |
| 618 |
: $db->setFlag( DBO_NOBUFFER, $db::REMEMBER_PRIOR ); |
| 619 |
|
| 620 |
$db->restoreFlags(); |
| 621 |
$this->assertEquals( $origNoBuffer, $db->getFlag( DBO_NOBUFFER ) ); |
| 622 |
$this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) ); |
| 623 |
|
| 624 |
$db->restoreFlags(); |
| 625 |
$this->assertEquals( $origNoBuffer, $db->getFlag( DBO_NOBUFFER ) ); |
| 626 |
$this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) ); |
| 627 |
} |
| 628 |
|
| 629 |
public static function provideImmutableDBOFlags() { |
| 630 |
return [ |
| 631 |
[ Database::DBO_IGNORE ], |
| 632 |
[ Database::DBO_DEFAULT ], |
| 633 |
[ Database::DBO_PERSISTENT ] |
| 634 |
]; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
public function testDBOCannotSet( $flag ) { |
| 642 |
$flagsHolder = new DatabaseFlags( 0 ); |
| 643 |
|
| 644 |
$this->expectException( DBLanguageError::class ); |
| 645 |
$flagsHolder->setFlag( $flag ); |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
public function testDBOCannotClear( $flag ) { |
| 653 |
$flagsHolder = new DatabaseFlags( 0 ); |
| 654 |
|
| 655 |
$this->expectException( DBLanguageError::class ); |
| 656 |
$flagsHolder->clearFlag( $flag ); |
| 657 |
} |
| 658 |
|
| 659 |
public function testSchemaAndPrefixMutators() { |
| 660 |
$ud = DatabaseDomain::newUnspecified(); |
| 661 |
|
| 662 |
$this->assertEquals( $ud->getId(), $this->db->getDomainID() ); |
| 663 |
|
| 664 |
$oldDomain = $this->db->getDomainID(); |
| 665 |
$oldSchema = $this->db->dbSchema(); |
| 666 |
$oldPrefix = $this->db->tablePrefix(); |
| 667 |
$this->assertIsString( $oldDomain, 'DB domain is string' ); |
| 668 |
$this->assertIsString( $oldSchema, 'DB schema is string' ); |
| 669 |
$this->assertIsString( $oldPrefix, 'Prefix is string' ); |
| 670 |
$this->assertSame( $oldSchema, $this->db->dbSchema(), "Schema unchanged" ); |
| 671 |
$this->assertSame( $oldPrefix, $this->db->tablePrefix(), "Prefix unchanged" ); |
| 672 |
|
| 673 |
$this->assertSame( $oldPrefix, $this->db->tablePrefix( 'xxx_' ), "Prior prefix upon set" ); |
| 674 |
$this->assertSame( 'xxx_', $this->db->tablePrefix(), "Prefix set" ); |
| 675 |
$this->assertSame( $oldSchema, $this->db->dbSchema(), "Schema unchanged" ); |
| 676 |
|
| 677 |
$this->db->tablePrefix( $oldPrefix ); |
| 678 |
$this->assertNotEquals( 'xxx_', $this->db->tablePrefix(), "Prior prefix upon set" ); |
| 679 |
$this->assertSame( $oldPrefix, $this->db->tablePrefix(), "Prefix restored" ); |
| 680 |
$this->assertSame( $oldSchema, $this->db->dbSchema(), "Schema unchanged" ); |
| 681 |
$this->assertSame( $oldDomain, $this->db->getDomainID(), "DB domain restored" ); |
| 682 |
|
| 683 |
$newDbDomain = new DatabaseDomain( |
| 684 |
'y', |
| 685 |
( $oldSchema !== '' ) ? $oldSchema : null, |
| 686 |
$oldPrefix |
| 687 |
); |
| 688 |
|
| 689 |
$this->db->selectDomain( $newDbDomain ); |
| 690 |
$this->assertSame( 'y', $this->db->getDBname(), "DB name set" ); |
| 691 |
$this->assertSame( $oldSchema, $this->db->dbSchema(), "Schema unchanged" ); |
| 692 |
$this->assertSame( $oldPrefix, $this->db->tablePrefix(), "Prefix unchanged" ); |
| 693 |
|
| 694 |
$this->assertSame( $oldSchema, $this->db->dbSchema( 'xxx' ), "Prior schema upon set" ); |
| 695 |
$this->assertSame( 'xxx', $this->db->dbSchema(), "Schema set" ); |
| 696 |
$this->assertSame( 'y', $this->db->getDBname(), "DB name unchanged" ); |
| 697 |
$this->assertSame( $oldPrefix, $this->db->tablePrefix(), "Prefix unchanged" ); |
| 698 |
|
| 699 |
$this->assertSame( 'xxx', $this->db->dbSchema( $oldSchema ), "Prior schema upon set" ); |
| 700 |
$this->assertEquals( $oldSchema, $this->db->dbSchema(), 'Schema restored' ); |
| 701 |
} |
| 702 |
|
| 703 |
public function testSchemaWithNoDB() { |
| 704 |
$ud = DatabaseDomain::newUnspecified(); |
| 705 |
|
| 706 |
$this->assertEquals( $ud->getId(), $this->db->getDomainID() ); |
| 707 |
$this->assertSame( '', $this->db->dbSchema() ); |
| 708 |
|
| 709 |
$this->expectException( DBUnexpectedError::class ); |
| 710 |
$this->db->dbSchema( 'xxx' ); |
| 711 |
} |
| 712 |
|
| 713 |
public function testSelectDomain() { |
| 714 |
$oldDomain = $this->db->getDomainID(); |
| 715 |
$oldDatabase = $this->db->getDBname(); |
| 716 |
$oldSchema = $this->db->dbSchema(); |
| 717 |
$oldPrefix = $this->db->tablePrefix(); |
| 718 |
|
| 719 |
|
| 720 |
$platform = TestingAccessWrapper::newFromObject( $this->db )->platform; |
| 721 |
|
| 722 |
$this->db->selectDomain( 'testselectdb-xxx_' ); |
| 723 |
$this->assertSame( 'testselectdb', $this->db->getDBname() ); |
| 724 |
$this->assertSame( '', $this->db->dbSchema() ); |
| 725 |
$this->assertSame( 'xxx_', $this->db->tablePrefix() ); |
| 726 |
$this->assertSame( 'testselectdb', $platform->getCurrentDomain()->getDatabase() ); |
| 727 |
$this->assertSame( 'xxx_', $platform->getCurrentDomain()->getTablePrefix() ); |
| 728 |
|
| 729 |
$this->db->selectDomain( $oldDomain ); |
| 730 |
$this->assertSame( $oldDatabase, $this->db->getDBname() ); |
| 731 |
$this->assertSame( $oldSchema, $this->db->dbSchema() ); |
| 732 |
$this->assertSame( $oldPrefix, $this->db->tablePrefix() ); |
| 733 |
$this->assertSame( $oldDomain, $this->db->getDomainID() ); |
| 734 |
$this->assertSame( $oldDatabase, $platform->getCurrentDomain()->getDatabase() ); |
| 735 |
$this->assertSame( $oldPrefix, $platform->getCurrentDomain()->getTablePrefix() ); |
| 736 |
$this->assertSame( $oldDomain, $platform->getCurrentDomain()->getId() ); |
| 737 |
|
| 738 |
$this->db->selectDomain( 'testselectdb-schema-xxx_' ); |
| 739 |
$this->assertSame( 'testselectdb', $this->db->getDBname() ); |
| 740 |
$this->assertSame( 'schema', $this->db->dbSchema() ); |
| 741 |
$this->assertSame( 'xxx_', $this->db->tablePrefix() ); |
| 742 |
|
| 743 |
$this->db->selectDomain( $oldDomain ); |
| 744 |
$this->assertSame( $oldDatabase, $this->db->getDBname() ); |
| 745 |
$this->assertSame( $oldSchema, $this->db->dbSchema() ); |
| 746 |
$this->assertSame( $oldPrefix, $this->db->tablePrefix() ); |
| 747 |
$this->assertSame( $oldDomain, $this->db->getDomainID() ); |
| 748 |
} |
| 749 |
|
| 750 |
public function testGetSetLBInfo() { |
| 751 |
$db = $this->getMockDB(); |
| 752 |
|
| 753 |
$this->assertEquals( [], $db->getLBInfo() ); |
| 754 |
$this->assertNull( $db->getLBInfo( 'pringles' ) ); |
| 755 |
|
| 756 |
$db->setLBInfo( 'soda', 'water' ); |
| 757 |
$this->assertEquals( [ 'soda' => 'water' ], $db->getLBInfo() ); |
| 758 |
$this->assertNull( $db->getLBInfo( 'pringles' ) ); |
| 759 |
$this->assertEquals( 'water', $db->getLBInfo( 'soda' ) ); |
| 760 |
|
| 761 |
$db->setLBInfo( 'basketball', 'Lebron' ); |
| 762 |
$this->assertEquals( [ 'soda' => 'water', 'basketball' => 'Lebron' ], $db->getLBInfo() ); |
| 763 |
$this->assertEquals( 'water', $db->getLBInfo( 'soda' ) ); |
| 764 |
$this->assertEquals( 'Lebron', $db->getLBInfo( 'basketball' ) ); |
| 765 |
|
| 766 |
$db->setLBInfo( 'soda', null ); |
| 767 |
$this->assertEquals( [ 'basketball' => 'Lebron' ], $db->getLBInfo() ); |
| 768 |
|
| 769 |
$db->setLBInfo( [ 'King' => 'James' ] ); |
| 770 |
$this->assertNull( $db->getLBInfo( 'basketball' ) ); |
| 771 |
$this->assertEquals( [ 'King' => 'James' ], $db->getLBInfo() ); |
| 772 |
} |
| 773 |
|
| 774 |
public function testShouldRejectPersistentWriteQueryOnReplicaDatabaseConnection() { |
| 775 |
$this->expectException( DBReadOnlyRoleError::class ); |
| 776 |
$this->expectExceptionMessage( 'Server is configured as a read-only replica database.' ); |
| 777 |
|
| 778 |
$dbr = new DatabaseTestHelper( |
| 779 |
__CLASS__ . '::' . $this->getName(), |
| 780 |
[ 'topologyRole' => Database::ROLE_STREAMING_REPLICA ] |
| 781 |
); |
| 782 |
|
| 783 |
|
| 784 |
$dbr->query( "INSERT INTO test_table (a_column) VALUES ('foo');", __METHOD__ ); |
| 785 |
} |
| 786 |
|
| 787 |
public function testShouldAcceptTemporaryTableOperationsOnReplicaDatabaseConnection() { |
| 788 |
$dbr = new DatabaseTestHelper( |
| 789 |
__CLASS__ . '::' . $this->getName(), |
| 790 |
[ 'topologyRole' => Database::ROLE_STREAMING_REPLICA ] |
| 791 |
); |
| 792 |
|
| 793 |
|
| 794 |
$resCreate = $dbr->query( |
| 795 |
"CREATE TEMPORARY TABLE temp_test_table (temp_column int);", |
| 796 |
__METHOD__ |
| 797 |
); |
| 798 |
|
| 799 |
|
| 800 |
$resModify = $dbr->query( |
| 801 |
"INSERT INTO temp_test_table (temp_column) VALUES (42);", |
| 802 |
__METHOD__ |
| 803 |
); |
| 804 |
|
| 805 |
$this->assertInstanceOf( IResultWrapper::class, $resCreate ); |
| 806 |
$this->assertInstanceOf( IResultWrapper::class, $resModify ); |
| 807 |
} |
| 808 |
|
| 809 |
public function testShouldRejectPseudoPermanentTemporaryTableOperationsOnReplicaDatabaseConnection() { |
| 810 |
$dbr = new DatabaseTestHelper( |
| 811 |
__CLASS__ . '::' . $this->getName(), |
| 812 |
[ 'topologyRole' => Database::ROLE_STREAMING_REPLICA ] |
| 813 |
); |
| 814 |
|
| 815 |
|
| 816 |
$dbr->query( |
| 817 |
"CREATE TEMPORARY TABLE temp_test_table (temp_column int);", |
| 818 |
__METHOD__, |
| 819 |
Database::QUERY_PSEUDO_PERMANENT |
| 820 |
); |
| 821 |
|
| 822 |
$this->expectException( DBReadOnlyRoleError::class ); |
| 823 |
$this->expectExceptionMessage( 'Server is configured as a read-only replica database.' ); |
| 824 |
|
| 825 |
$dbr->query( |
| 826 |
"INSERT INTO temp_test_table (temp_column) VALUES (42);", |
| 827 |
__METHOD__ |
| 828 |
); |
| 829 |
} |
| 830 |
|
| 831 |
public function testShouldAcceptWriteQueryOnPrimaryDatabaseConnection() { |
| 832 |
$dbr = new DatabaseTestHelper( |
| 833 |
__CLASS__ . '::' . $this->getName(), |
| 834 |
[ 'topologyRole' => Database::ROLE_STREAMING_MASTER ] |
| 835 |
); |
| 836 |
|
| 837 |
|
| 838 |
$res = $dbr->query( "INSERT INTO test_table (a_column) VALUES ('foo');", __METHOD__ ); |
| 839 |
|
| 840 |
$this->assertInstanceOf( IResultWrapper::class, $res ); |
| 841 |
} |
| 842 |
|
| 843 |
public function testShouldRejectWriteQueryOnPrimaryDatabaseConnectionWhenReplicaQueryRoleFlagIsSet() { |
| 844 |
$this->expectException( DBReadOnlyRoleError::class ); |
| 845 |
$this->expectExceptionMessage( 'Cannot write; target role is DB_REPLICA' ); |
| 846 |
|
| 847 |
$dbr = new DatabaseTestHelper( |
| 848 |
__CLASS__ . '::' . $this->getName(), |
| 849 |
[ 'topologyRole' => Database::ROLE_STREAMING_MASTER ] |
| 850 |
); |
| 851 |
|
| 852 |
|
| 853 |
$dbr->query( |
| 854 |
"INSERT INTO test_table (a_column) VALUES ('foo');", |
| 855 |
__METHOD__, |
| 856 |
Database::QUERY_REPLICA_ROLE |
| 857 |
); |
| 858 |
} |
| 859 |
|
| 860 |
public function testCriticalSectionErrorSelect() { |
| 861 |
$this->expectException( DBTransactionStateError::class ); |
| 862 |
|
| 863 |
$db = TestingAccessWrapper::newFromObject( $this->db ); |
| 864 |
try { |
| 865 |
$this->corruptDbState( $db ); |
| 866 |
} catch ( RuntimeException $e ) { |
| 867 |
$this->assertEquals( "Unexpected error", $e->getMessage() ); |
| 868 |
} |
| 869 |
|
| 870 |
$db->query( "SELECT 1", __METHOD__ ); |
| 871 |
} |
| 872 |
|
| 873 |
public function testCriticalSectionErrorRollback() { |
| 874 |
$db = TestingAccessWrapper::newFromObject( $this->db ); |
| 875 |
try { |
| 876 |
$this->corruptDbState( $db ); |
| 877 |
} catch ( RuntimeException $e ) { |
| 878 |
$this->assertEquals( "Unexpected error", $e->getMessage() ); |
| 879 |
} |
| 880 |
|
| 881 |
$db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS ); |
| 882 |
$this->assertTrue( true, "No exception on ROLLBACK" ); |
| 883 |
|
| 884 |
$db->flushSession( __METHOD__, IDatabase::FLUSHING_ALL_PEERS ); |
| 885 |
$this->assertTrue( true, "No exception on session flush" ); |
| 886 |
|
| 887 |
$db->query( "SELECT 1", __METHOD__ ); |
| 888 |
$this->assertTrue( true, "No exception on next query" ); |
| 889 |
} |
| 890 |
|
| 891 |
public function testCriticalSectionErrorWithTrxRollback() { |
| 892 |
$hits = 0; |
| 893 |
$db = TestingAccessWrapper::newFromObject( $this->db ); |
| 894 |
$db->begin( __METHOD__, IDatabase::TRANSACTION_INTERNAL ); |
| 895 |
$db->onTransactionResolution( static function () use ( &$hits ) { |
| 896 |
++$hits; |
| 897 |
} ); |
| 898 |
|
| 899 |
try { |
| 900 |
$this->corruptDbState( $db ); |
| 901 |
} catch ( RuntimeException $e ) { |
| 902 |
$this->assertEquals( "Unexpected error", $e->getMessage() ); |
| 903 |
} |
| 904 |
|
| 905 |
$db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS ); |
| 906 |
$this->assertTrue( true, "No exception on ROLLBACK" ); |
| 907 |
|
| 908 |
$db->flushSession( __METHOD__, IDatabase::FLUSHING_ALL_PEERS ); |
| 909 |
$this->assertTrue( true, "No exception on session flush" ); |
| 910 |
|
| 911 |
$db->query( "SELECT 1", __METHOD__ ); |
| 912 |
$this->assertTrue( true, "No exception on next query" ); |
| 913 |
} |
| 914 |
|
| 915 |
public function testExpr() { |
| 916 |
$this->assertInstanceOf( Expression::class, $this->db->expr( 'key', '=', null ) ); |
| 917 |
$this->assertInstanceOf( AndExpressionGroup::class, $this->db->andExpr( [ 'key' => null, $this->db->expr( 'key', '=', null ) ] ) ); |
| 918 |
$this->assertInstanceOf( OrExpressionGroup::class, $this->db->orExpr( [ 'key' => null, $this->db->expr( 'key', '=', null ) ] ) ); |
| 919 |
} |
| 920 |
|
| 921 |
private function corruptDbState( $db ) { |
| 922 |
$cs = $db->commenceCriticalSection( __METHOD__ ); |
| 923 |
$this->assertInstanceOf( CriticalSectionScope::class, $cs ); |
| 924 |
throw new RuntimeException( "Unexpected error" ); |
| 925 |
} |
| 926 |
} |