3 use Wikimedia\Rdbms\TransactionProfiler
;
4 use Wikimedia\Rdbms\DatabaseDomain
;
7 * Helper for testing the methods from the Database class
10 class DatabaseTestHelper
extends Database
{
13 * __CLASS__ of the test suite,
14 * used to determine, if the function name is passed every time to query()
16 protected $testName = [];
19 * Array of lastSqls passed to query(),
20 * This is an array since some methods in Database can do more than one
21 * query. Cleared when calling getLastSqls().
23 protected $lastSqls = [];
25 /** @var array List of row arrays */
26 protected $nextResult = [];
29 * Array of tables to be considered as existing by tableExist()
30 * Use setExistingTables() to alter.
32 protected $tablesExists;
35 * Value to return from unionSupportsOrderAndLimit()
37 protected $unionSupportsOrderAndLimit = true;
39 public function __construct( $testName, array $opts = [] ) {
40 $this->testName
= $testName;
42 $this->profiler
= new ProfilerStub( [] );
43 $this->trxProfiler
= new TransactionProfiler();
44 $this->cliMode
= isset( $opts['cliMode'] ) ?
$opts['cliMode'] : true;
45 $this->connLogger
= new \Psr\Log\
NullLogger();
46 $this->queryLogger
= new \Psr\Log\
NullLogger();
47 $this->errorLogger
= function ( Exception
$e ) {
48 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
50 $this->currentDomain
= DatabaseDomain
::newUnspecified();
54 * Returns SQL queries grouped by '; '
55 * Clear the list of queries that have been done so far.
58 public function getLastSqls() {
59 $lastSqls = implode( '; ', $this->lastSqls
);
65 public function setExistingTables( $tablesExists ) {
66 $this->tablesExists
= (array)$tablesExists;
70 * @param mixed $res Use an array of row arrays to set row result
72 public function forceNextResult( $res ) {
73 $this->nextResult
= $res;
76 protected function addSql( $sql ) {
77 // clean up spaces before and after some words and the whole string
78 $this->lastSqls
[] = trim( preg_replace(
79 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
84 protected function checkFunctionName( $fname ) {
85 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
86 throw new MWException( 'function name does not start with test class. ' .
87 $fname . ' vs. ' . $this->testName
. '. ' .
88 'Please provide __METHOD__ to database methods.' );
92 function strencode( $s ) {
93 // Choose apos to avoid handling of escaping double quotes in quoted text
94 return str_replace( "'", "\'", $s );
97 public function addIdentifierQuotes( $s ) {
98 // no escaping to avoid handling of double quotes in quoted text
102 public function query( $sql, $fname = '', $tempIgnore = false ) {
103 $this->checkFunctionName( $fname );
104 $this->addSql( $sql );
106 return parent
::query( $sql, $fname, $tempIgnore );
109 public function tableExists( $table, $fname = __METHOD__
) {
110 $tableRaw = $this->tableName( $table, 'raw' );
111 if ( isset( $this->mSessionTempTables
[$tableRaw] ) ) {
112 return true; // already known to exist
115 $this->checkFunctionName( $fname );
117 return in_array( $table, (array)$this->tablesExists
);
120 // Redeclare parent method to make it public
121 public function nativeReplace( $table, $rows, $fname ) {
122 return parent
::nativeReplace( $table, $rows, $fname );
129 function open( $server, $user, $password, $dbName ) {
133 function fetchObject( $res ) {
137 function fetchRow( $res ) {
141 function numRows( $res ) {
145 function numFields( $res ) {
149 function fieldName( $res, $n ) {
153 function insertId() {
157 function dataSeek( $res, $row ) {
161 function lastErrno() {
165 function lastError() {
169 function fieldInfo( $table, $field ) {
173 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
177 function affectedRows() {
181 function getSoftwareLink() {
185 function getServerVersion() {
189 function getServerInfo() {
197 function ping( &$rtt = null ) {
202 protected function closeConnection() {
206 protected function doQuery( $sql ) {
207 $res = $this->nextResult
;
208 $this->nextResult
= [];
210 return new FakeResultWrapper( $res );
213 public function unionSupportsOrderAndLimit() {
214 return $this->unionSupportsOrderAndLimit
;
217 public function setUnionSupportsOrderAndLimit( $v ) {
218 $this->unionSupportsOrderAndLimit
= (bool)$v;