3 use Wikimedia\Rdbms\TransactionProfiler
;
4 use Wikimedia\Rdbms\DatabaseDomain
;
5 use Wikimedia\Rdbms\Database
;
8 * Helper for testing the methods from the Database class
11 class DatabaseTestHelper
extends Database
{
14 * __CLASS__ of the test suite,
15 * used to determine, if the function name is passed every time to query()
17 protected $testName = [];
20 * Array of lastSqls passed to query(),
21 * This is an array since some methods in Database can do more than one
22 * query. Cleared when calling getLastSqls().
24 protected $lastSqls = [];
26 /** @var array List of row arrays */
27 protected $nextResult = [];
29 /** @var array|null */
30 protected $nextError = null;
31 /** @var array|null */
32 protected $lastError = null;
35 * Array of tables to be considered as existing by tableExist()
36 * Use setExistingTables() to alter.
38 protected $tablesExists;
41 * Value to return from unionSupportsOrderAndLimit()
43 protected $unionSupportsOrderAndLimit = true;
45 public function __construct( $testName, array $opts = [] ) {
46 $this->testName
= $testName;
48 $this->profiler
= new ProfilerStub( [] );
49 $this->trxProfiler
= new TransactionProfiler();
50 $this->cliMode
= $opts['cliMode'] ??
true;
51 $this->connLogger
= new \Psr\Log\
NullLogger();
52 $this->queryLogger
= new \Psr\Log\
NullLogger();
53 $this->errorLogger
= function ( Exception
$e ) {
54 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
56 $this->deprecationLogger
= function ( $msg ) {
59 $this->currentDomain
= DatabaseDomain
::newUnspecified();
60 $this->open( 'localhost', 'testuser', 'password', 'testdb' );
64 * Returns SQL queries grouped by '; '
65 * Clear the list of queries that have been done so far.
68 public function getLastSqls() {
69 $lastSqls = implode( '; ', $this->lastSqls
);
75 public function setExistingTables( $tablesExists ) {
76 $this->tablesExists
= (array)$tablesExists;
80 * @param mixed $res Use an array of row arrays to set row result
82 public function forceNextResult( $res ) {
83 $this->nextResult
= $res;
87 * @param int $errno Error number
88 * @param string $error Error text
89 * @param array $options
90 * - wasKnownStatementRollbackError: Return value for wasKnownStatementRollbackError()
92 public function forceNextQueryError( $errno, $error, $options = [] ) {
93 $this->nextError
= [ 'errno' => $errno, 'error' => $error ] +
$options;
96 protected function addSql( $sql ) {
97 // clean up spaces before and after some words and the whole string
98 $this->lastSqls
[] = trim( preg_replace(
99 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
104 protected function checkFunctionName( $fname ) {
105 if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
106 return; // no $fname parameter
109 // Handle some internal calls from the Database class
111 if ( preg_match( '/^Wikimedia\\\\Rdbms\\\\Database::query \((.+)\)$/', $fname, $m ) ) {
115 if ( substr( $check, 0, strlen( $this->testName
) ) !== $this->testName
) {
116 throw new MWException( 'function name does not start with test class. ' .
117 $fname . ' vs. ' . $this->testName
. '. ' .
118 'Please provide __METHOD__ to database methods.' );
122 function strencode( $s ) {
123 // Choose apos to avoid handling of escaping double quotes in quoted text
124 return str_replace( "'", "\'", $s );
127 public function addIdentifierQuotes( $s ) {
128 // no escaping to avoid handling of double quotes in quoted text
132 public function query( $sql, $fname = '', $tempIgnore = false ) {
133 $this->checkFunctionName( $fname );
135 return parent
::query( $sql, $fname, $tempIgnore );
138 public function tableExists( $table, $fname = __METHOD__
) {
139 $tableRaw = $this->tableName( $table, 'raw' );
140 if ( isset( $this->sessionTempTables
[$tableRaw] ) ) {
141 return true; // already known to exist
144 $this->checkFunctionName( $fname );
146 return in_array( $table, (array)$this->tablesExists
);
149 // Redeclare parent method to make it public
150 public function nativeReplace( $table, $rows, $fname ) {
151 return parent
::nativeReplace( $table, $rows, $fname );
158 function open( $server, $user, $password, $dbName ) {
159 $this->conn
= (object)[ 'test' ];
164 function fetchObject( $res ) {
168 function fetchRow( $res ) {
172 function numRows( $res ) {
176 function numFields( $res ) {
180 function fieldName( $res, $n ) {
184 function insertId() {
188 function dataSeek( $res, $row ) {
192 function lastErrno() {
193 return $this->lastError ?
$this->lastError
['errno'] : -1;
196 function lastError() {
197 return $this->lastError ?
$this->lastError
['error'] : 'test';
200 protected function wasKnownStatementRollbackError() {
201 return $this->lastError
['wasKnownStatementRollbackError'] ??
false;
204 function fieldInfo( $table, $field ) {
208 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
212 function fetchAffectedRowCount() {
216 function getSoftwareLink() {
220 function getServerVersion() {
224 function getServerInfo() {
229 return $this->conn ?
true : false;
232 function ping( &$rtt = null ) {
237 protected function closeConnection() {
241 protected function doQuery( $sql ) {
242 $sql = preg_replace( '< /\* .+? \*/>', '', $sql );
243 $this->addSql( $sql );
245 if ( $this->nextError
) {
246 $this->lastError
= $this->nextError
;
247 $this->nextError
= null;
251 $res = $this->nextResult
;
252 $this->nextResult
= [];
253 $this->lastError
= null;
255 return new FakeResultWrapper( $res );
258 public function unionSupportsOrderAndLimit() {
259 return $this->unionSupportsOrderAndLimit
;
262 public function setUnionSupportsOrderAndLimit( $v ) {
263 $this->unionSupportsOrderAndLimit
= (bool)$v;