2013-04-13 14:40:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-01-26 17:42:38 +00:00
|
|
|
use Wikimedia\Rdbms\TransactionProfiler;
|
2017-02-06 22:32:49 +00:00
|
|
|
use Wikimedia\Rdbms\DatabaseDomain;
|
2017-01-26 17:42:38 +00:00
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
/**
|
2016-09-26 22:40:07 +00:00
|
|
|
* Helper for testing the methods from the Database class
|
2013-04-13 14:40:04 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
2016-09-26 22:40:07 +00:00
|
|
|
class DatabaseTestHelper extends Database {
|
2013-04-13 14:40:04 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* __CLASS__ of the test suite,
|
|
|
|
|
* used to determine, if the function name is passed every time to query()
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $testName = [];
|
2013-04-13 14:40:04 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Array of lastSqls passed to query(),
|
2016-09-26 22:40:07 +00:00
|
|
|
* This is an array since some methods in Database can do more than one
|
2013-04-13 14:40:04 +00:00
|
|
|
* query. Cleared when calling getLastSqls().
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $lastSqls = [];
|
2013-04-13 14:40:04 +00:00
|
|
|
|
2016-08-27 17:10:46 +00:00
|
|
|
/** @var array List of row arrays */
|
|
|
|
|
protected $nextResult = [];
|
|
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
/**
|
|
|
|
|
* Array of tables to be considered as existing by tableExist()
|
|
|
|
|
* Use setExistingTables() to alter.
|
|
|
|
|
*/
|
|
|
|
|
protected $tablesExists;
|
|
|
|
|
|
2017-06-16 17:32:03 +00:00
|
|
|
/**
|
|
|
|
|
* Value to return from unionSupportsOrderAndLimit()
|
|
|
|
|
*/
|
|
|
|
|
protected $unionSupportsOrderAndLimit = true;
|
|
|
|
|
|
2016-08-27 17:10:46 +00:00
|
|
|
public function __construct( $testName, array $opts = [] ) {
|
2013-04-13 14:40:04 +00:00
|
|
|
$this->testName = $testName;
|
2016-08-29 17:41:59 +00:00
|
|
|
|
|
|
|
|
$this->profiler = new ProfilerStub( [] );
|
|
|
|
|
$this->trxProfiler = new TransactionProfiler();
|
2016-08-27 17:10:46 +00:00
|
|
|
$this->cliMode = isset( $opts['cliMode'] ) ? $opts['cliMode'] : true;
|
2016-09-16 20:57:56 +00:00
|
|
|
$this->connLogger = new \Psr\Log\NullLogger();
|
|
|
|
|
$this->queryLogger = new \Psr\Log\NullLogger();
|
2016-09-21 18:56:22 +00:00
|
|
|
$this->errorLogger = function ( Exception $e ) {
|
|
|
|
|
wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
|
|
|
|
|
};
|
2016-09-21 10:00:48 +00:00
|
|
|
$this->currentDomain = DatabaseDomain::newUnspecified();
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns SQL queries grouped by '; '
|
|
|
|
|
* Clear the list of queries that have been done so far.
|
2017-09-09 20:47:04 +00:00
|
|
|
* @return string
|
2013-04-13 14:40:04 +00:00
|
|
|
*/
|
|
|
|
|
public function getLastSqls() {
|
|
|
|
|
$lastSqls = implode( '; ', $this->lastSqls );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->lastSqls = [];
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
return $lastSqls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setExistingTables( $tablesExists ) {
|
|
|
|
|
$this->tablesExists = (array)$tablesExists;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-27 17:10:46 +00:00
|
|
|
/**
|
|
|
|
|
* @param mixed $res Use an array of row arrays to set row result
|
|
|
|
|
*/
|
|
|
|
|
public function forceNextResult( $res ) {
|
|
|
|
|
$this->nextResult = $res;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
protected function addSql( $sql ) {
|
|
|
|
|
// clean up spaces before and after some words and the whole string
|
|
|
|
|
$this->lastSqls[] = trim( preg_replace(
|
|
|
|
|
'/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
|
|
|
|
|
' ', $sql
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function checkFunctionName( $fname ) {
|
|
|
|
|
if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
|
|
|
|
|
throw new MWException( 'function name does not start with test class. ' .
|
|
|
|
|
$fname . ' vs. ' . $this->testName . '. ' .
|
|
|
|
|
'Please provide __METHOD__ to database methods.' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function strencode( $s ) {
|
|
|
|
|
// Choose apos to avoid handling of escaping double quotes in quoted text
|
|
|
|
|
return str_replace( "'", "\'", $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function addIdentifierQuotes( $s ) {
|
|
|
|
|
// no escaping to avoid handling of double quotes in quoted text
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function query( $sql, $fname = '', $tempIgnore = false ) {
|
|
|
|
|
$this->checkFunctionName( $fname );
|
|
|
|
|
$this->addSql( $sql );
|
|
|
|
|
|
|
|
|
|
return parent::query( $sql, $fname, $tempIgnore );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function tableExists( $table, $fname = __METHOD__ ) {
|
2016-09-21 17:44:15 +00:00
|
|
|
$tableRaw = $this->tableName( $table, 'raw' );
|
|
|
|
|
if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
|
|
|
|
|
return true; // already known to exist
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
$this->checkFunctionName( $fname );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
return in_array( $table, (array)$this->tablesExists );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Redeclare parent method to make it public
|
|
|
|
|
public function nativeReplace( $table, $rows, $fname ) {
|
|
|
|
|
return parent::nativeReplace( $table, $rows, $fname );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getType() {
|
|
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function open( $server, $user, $password, $dbName ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fetchObject( $res ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fetchRow( $res ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function numRows( $res ) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function numFields( $res ) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fieldName( $res, $n ) {
|
|
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function insertId() {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dataSeek( $res, $row ) {
|
|
|
|
|
/* nop */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function lastErrno() {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function lastError() {
|
|
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fieldInfo( $table, $field ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-26 22:40:07 +00:00
|
|
|
function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
|
2013-04-13 14:40:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 14:10:39 +00:00
|
|
|
function fetchAffectedRowCount() {
|
2013-04-13 14:40:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 04:13:02 +00:00
|
|
|
function getSoftwareLink() {
|
2013-04-13 14:40:04 +00:00
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getServerVersion() {
|
|
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getServerInfo() {
|
|
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 20:37:49 +00:00
|
|
|
function isOpen() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 22:51:15 +00:00
|
|
|
function ping( &$rtt = null ) {
|
|
|
|
|
$rtt = 0.0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
protected function closeConnection() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doQuery( $sql ) {
|
2016-08-27 17:10:46 +00:00
|
|
|
$res = $this->nextResult;
|
|
|
|
|
$this->nextResult = [];
|
|
|
|
|
|
|
|
|
|
return new FakeResultWrapper( $res );
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
2017-06-16 17:32:03 +00:00
|
|
|
|
|
|
|
|
public function unionSupportsOrderAndLimit() {
|
|
|
|
|
return $this->unionSupportsOrderAndLimit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setUnionSupportsOrderAndLimit( $v ) {
|
|
|
|
|
$this->unionSupportsOrderAndLimit = (bool)$v;
|
|
|
|
|
}
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|