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 = [];
30 * Array of tables to be considered as existing by tableExist()
31 * Use setExistingTables() to alter.
33 protected $tablesExists;
36 * Value to return from unionSupportsOrderAndLimit()
38 protected $unionSupportsOrderAndLimit = true;
40 public function __construct( $testName, array $opts = [] ) {
41 $this->testName
= $testName;
43 $this->profiler
= new ProfilerStub( [] );
44 $this->trxProfiler
= new TransactionProfiler();
45 $this->cliMode
= isset( $opts['cliMode'] ) ?
$opts['cliMode'] : true;
46 $this->connLogger
= new \Psr\Log\
NullLogger();
47 $this->queryLogger
= new \Psr\Log\
NullLogger();
48 $this->errorLogger
= function ( Exception
$e ) {
49 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
51 $this->currentDomain
= DatabaseDomain
::newUnspecified();
55 * Returns SQL queries grouped by '; '
56 * Clear the list of queries that have been done so far.
59 public function getLastSqls() {
60 $lastSqls = implode( '; ', $this->lastSqls
);
66 public function setExistingTables( $tablesExists ) {
67 $this->tablesExists
= (array)$tablesExists;
71 * @param mixed $res Use an array of row arrays to set row result
73 public function forceNextResult( $res ) {
74 $this->nextResult
= $res;
77 protected function addSql( $sql ) {
78 // clean up spaces before and after some words and the whole string
79 $this->lastSqls
[] = trim( preg_replace(
80 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
85 protected function checkFunctionName( $fname ) {
86 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
87 throw new MWException( 'function name does not start with test class. ' .
88 $fname . ' vs. ' . $this->testName
. '. ' .
89 'Please provide __METHOD__ to database methods.' );
93 function strencode( $s ) {
94 // Choose apos to avoid handling of escaping double quotes in quoted text
95 return str_replace( "'", "\'", $s );
98 public function addIdentifierQuotes( $s ) {
99 // no escaping to avoid handling of double quotes in quoted text
103 public function query( $sql, $fname = '', $tempIgnore = false ) {
104 $this->checkFunctionName( $fname );
105 $this->addSql( $sql );
107 return parent
::query( $sql, $fname, $tempIgnore );
110 public function tableExists( $table, $fname = __METHOD__
) {
111 $tableRaw = $this->tableName( $table, 'raw' );
112 if ( isset( $this->sessionTempTables
[$tableRaw] ) ) {
113 return true; // already known to exist
116 $this->checkFunctionName( $fname );
118 return in_array( $table, (array)$this->tablesExists
);
121 // Redeclare parent method to make it public
122 public function nativeReplace( $table, $rows, $fname ) {
123 return parent
::nativeReplace( $table, $rows, $fname );
130 function open( $server, $user, $password, $dbName ) {
134 function fetchObject( $res ) {
138 function fetchRow( $res ) {
142 function numRows( $res ) {
146 function numFields( $res ) {
150 function fieldName( $res, $n ) {
154 function insertId() {
158 function dataSeek( $res, $row ) {
162 function lastErrno() {
166 function lastError() {
170 function fieldInfo( $table, $field ) {
174 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
178 function fetchAffectedRowCount() {
182 function getSoftwareLink() {
186 function getServerVersion() {
190 function getServerInfo() {
198 function ping( &$rtt = null ) {
203 protected function closeConnection() {
207 protected function doQuery( $sql ) {
208 $res = $this->nextResult
;
209 $this->nextResult
= [];
211 return new FakeResultWrapper( $res );
214 public function unionSupportsOrderAndLimit() {
215 return $this->unionSupportsOrderAndLimit
;
218 public function setUnionSupportsOrderAndLimit( $v ) {
219 $this->unionSupportsOrderAndLimit
= (bool)$v;