| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock; |
| 4 |
use MediaWiki\Tests\Unit\Libs\Rdbms\SQLPlatformTestHelper; |
| 5 |
use Psr\Log\NullLogger; |
| 6 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 7 |
use Wikimedia\Rdbms\Database; |
| 8 |
use Wikimedia\Rdbms\Database\DatabaseFlags; |
| 9 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 10 |
use Wikimedia\Rdbms\FakeResultWrapper; |
| 11 |
use Wikimedia\Rdbms\QueryStatus; |
| 12 |
use Wikimedia\Rdbms\Replication\ReplicationReporter; |
| 13 |
use Wikimedia\Rdbms\TransactionProfiler; |
| 14 |
use Wikimedia\RequestTimeout\RequestTimeout; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class DatabaseTestHelper extends Database { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected string $testName; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected $lastSqls = []; |
| 34 |
|
| 35 |
|
| 36 |
protected $nextResMapQueue = []; |
| 37 |
|
| 38 |
|
| 39 |
protected $lastResMap = null; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected $tablesExists; |
| 46 |
|
| 47 |
|
| 48 |
protected $forcedAffectedCountQueue = []; |
| 49 |
|
| 50 |
public function __construct( string $testName, array $opts = [] ) { |
| 51 |
$params = $opts + [ |
| 52 |
'host' => null, |
| 53 |
'user' => null, |
| 54 |
'password' => null, |
| 55 |
'dbname' => null, |
| 56 |
'schema' => null, |
| 57 |
'tablePrefix' => '', |
| 58 |
'flags' => 0, |
| 59 |
'cliMode' => true, |
| 60 |
'agent' => '', |
| 61 |
'serverName' => null, |
| 62 |
'topologyRole' => null, |
| 63 |
'srvCache' => new HashBagOStuff(), |
| 64 |
'profiler' => null, |
| 65 |
'trxProfiler' => new TransactionProfiler(), |
| 66 |
'logger' => new NullLogger(), |
| 67 |
'errorLogger' => static function ( Exception $e ) { |
| 68 |
wfWarn( get_class( $e ) . ': ' . $e->getMessage() ); |
| 69 |
}, |
| 70 |
'deprecationLogger' => static function ( $msg ) { |
| 71 |
wfWarn( $msg ); |
| 72 |
}, |
| 73 |
'criticalSectionProvider' => |
| 74 |
RequestTimeout::singleton()->createCriticalSectionProvider( 120 ) |
| 75 |
]; |
| 76 |
parent::__construct( $params ); |
| 77 |
|
| 78 |
$this->testName = $testName; |
| 79 |
$this->platform = new SQLPlatformTestHelper( new AddQuoterMock() ); |
| 80 |
$this->flagsHolder = new DatabaseFlags( 0 ); |
| 81 |
$this->replicationReporter = new ReplicationReporter( |
| 82 |
$params['topologyRole'], |
| 83 |
$params['logger'], |
| 84 |
$params['srvCache'] |
| 85 |
); |
| 86 |
|
| 87 |
$this->currentDomain = DatabaseDomain::newUnspecified(); |
| 88 |
$this->open( 'localhost', 'testuser', 'password', 'testdb', null, '' ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function getLastSqls() { |
| 97 |
$lastSqls = implode( '; ', $this->lastSqls ); |
| 98 |
$this->lastSqls = []; |
| 99 |
|
| 100 |
return $lastSqls; |
| 101 |
} |
| 102 |
|
| 103 |
public function setExistingTables( $tablesExists ) { |
| 104 |
$this->tablesExists = (array)$tablesExists; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function forceNextResult( $res, $errno = 0, $error = '', $options = [] ) { |
| 115 |
$this->nextResMapQueue[] = [ |
| 116 |
'res' => $res, |
| 117 |
'errno' => $errno, |
| 118 |
'error' => $error |
| 119 |
] + $options; |
| 120 |
} |
| 121 |
|
| 122 |
protected function addSql( $sql ) { |
| 123 |
|
| 124 |
$this->lastSqls[] = trim( preg_replace( |
| 125 |
'/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/', |
| 126 |
' ', $sql |
| 127 |
) ); |
| 128 |
} |
| 129 |
|
| 130 |
protected function checkFunctionName( $fname ) { |
| 131 |
if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
$check = $fname; |
| 137 |
if ( preg_match( |
| 138 |
'/^Wikimedia\\\\Rdbms\\\\Database::(?:query|beginIfImplied) \((.+)\)$/', |
| 139 |
$fname, |
| 140 |
$m |
| 141 |
) ) { |
| 142 |
$check = $m[1]; |
| 143 |
} |
| 144 |
|
| 145 |
if ( !str_starts_with( $check, $this->testName ) ) { |
| 146 |
throw new LogicException( 'function name does not start with test class. ' . |
| 147 |
$fname . ' vs. ' . $this->testName . '. ' . |
| 148 |
'Please provide __METHOD__ to database methods.' ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public function strencode( $s ) { |
| 153 |
|
| 154 |
return str_replace( "'", "\'", $s ); |
| 155 |
} |
| 156 |
|
| 157 |
public function query( $sql, $fname = '', $flags = 0 ) { |
| 158 |
$this->checkFunctionName( $fname ); |
| 159 |
|
| 160 |
return parent::query( $sql, $fname, $flags ); |
| 161 |
} |
| 162 |
|
| 163 |
public function tableExists( $table, $fname = __METHOD__ ) { |
| 164 |
[ $db, $pt ] = $this->platform->getDatabaseAndTableIdentifier( $table ); |
| 165 |
if ( isset( $this->sessionTempTables[$db][$pt] ) ) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
$this->checkFunctionName( $fname ); |
| 170 |
|
| 171 |
return in_array( $table, (array)$this->tablesExists ); |
| 172 |
} |
| 173 |
|
| 174 |
public function getType() { |
| 175 |
return 'test'; |
| 176 |
} |
| 177 |
|
| 178 |
public function open( $server, $user, $password, $db, $schema, $tablePrefix ) { |
| 179 |
$this->conn = (object)[ 'test' ]; |
| 180 |
|
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
protected function lastInsertId() { |
| 185 |
return -1; |
| 186 |
} |
| 187 |
|
| 188 |
public function lastErrno() { |
| 189 |
return $this->lastResMap ? $this->lastResMap['errno'] : -1; |
| 190 |
} |
| 191 |
|
| 192 |
public function lastError() { |
| 193 |
return $this->lastResMap ? $this->lastResMap['error'] : 'test'; |
| 194 |
} |
| 195 |
|
| 196 |
protected function isKnownStatementRollbackError( $errno ) { |
| 197 |
return ( $this->lastResMap['errno'] ?? 0 ) === $errno |
| 198 |
? ( $this->lastResMap['isKnownStatementRollbackError'] ?? false ) |
| 199 |
: false; |
| 200 |
} |
| 201 |
|
| 202 |
public function fieldInfo( $table, $field ) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
public function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
public function getSoftwareLink() { |
| 211 |
return 'test'; |
| 212 |
} |
| 213 |
|
| 214 |
public function getServerVersion() { |
| 215 |
return 'test'; |
| 216 |
} |
| 217 |
|
| 218 |
public function getServerInfo() { |
| 219 |
return 'test'; |
| 220 |
} |
| 221 |
|
| 222 |
public function ping( &$rtt = null ) { |
| 223 |
$rtt = 0.0; |
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
protected function closeConnection() { |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
public function setNextQueryAffectedRowCounts( array $counts ) { |
| 232 |
$this->forcedAffectedCountQueue = $counts; |
| 233 |
} |
| 234 |
|
| 235 |
protected function doSingleStatementQuery( string $sql ): QueryStatus { |
| 236 |
$sql = preg_replace( '< /\* .+? \*/>', '', $sql ); |
| 237 |
$this->addSql( $sql ); |
| 238 |
|
| 239 |
if ( $this->nextResMapQueue ) { |
| 240 |
$this->lastResMap = array_shift( $this->nextResMapQueue ); |
| 241 |
if ( !$this->lastResMap['errno'] && $this->forcedAffectedCountQueue ) { |
| 242 |
$count = array_shift( $this->forcedAffectedCountQueue ); |
| 243 |
$this->lastQueryAffectedRows = $count; |
| 244 |
} |
| 245 |
} else { |
| 246 |
$this->lastResMap = [ 'res' => [], 'errno' => 0, 'error' => '' ]; |
| 247 |
} |
| 248 |
$res = $this->lastResMap['res']; |
| 249 |
|
| 250 |
return new QueryStatus( |
| 251 |
is_bool( $res ) ? $res : new FakeResultWrapper( $res ), |
| 252 |
$this->affectedRows(), |
| 253 |
$this->lastError(), |
| 254 |
$this->lastErrno() |
| 255 |
); |
| 256 |
} |
| 257 |
} |