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.
57 public function getLastSqls() {
58 $lastSqls = implode( '; ', $this->lastSqls
);
64 public function setExistingTables( $tablesExists ) {
65 $this->tablesExists
= (array)$tablesExists;
69 * @param mixed $res Use an array of row arrays to set row result
71 public function forceNextResult( $res ) {
72 $this->nextResult
= $res;
75 protected function addSql( $sql ) {
76 // clean up spaces before and after some words and the whole string
77 $this->lastSqls
[] = trim( preg_replace(
78 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
83 protected function checkFunctionName( $fname ) {
84 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
85 throw new MWException( 'function name does not start with test class. ' .
86 $fname . ' vs. ' . $this->testName
. '. ' .
87 'Please provide __METHOD__ to database methods.' );
91 function strencode( $s ) {
92 // Choose apos to avoid handling of escaping double quotes in quoted text
93 return str_replace( "'", "\'", $s );
96 public function addIdentifierQuotes( $s ) {
97 // no escaping to avoid handling of double quotes in quoted text
101 public function query( $sql, $fname = '', $tempIgnore = false ) {
102 $this->checkFunctionName( $fname );
103 $this->addSql( $sql );
105 return parent
::query( $sql, $fname, $tempIgnore );
108 public function tableExists( $table, $fname = __METHOD__
) {
109 $tableRaw = $this->tableName( $table, 'raw' );
110 if ( isset( $this->mSessionTempTables
[$tableRaw] ) ) {
111 return true; // already known to exist
114 $this->checkFunctionName( $fname );
116 return in_array( $table, (array)$this->tablesExists
);
119 // Redeclare parent method to make it public
120 public function nativeReplace( $table, $rows, $fname ) {
121 return parent
::nativeReplace( $table, $rows, $fname );
128 function open( $server, $user, $password, $dbName ) {
132 function fetchObject( $res ) {
136 function fetchRow( $res ) {
140 function numRows( $res ) {
144 function numFields( $res ) {
148 function fieldName( $res, $n ) {
152 function insertId() {
156 function dataSeek( $res, $row ) {
160 function lastErrno() {
164 function lastError() {
168 function fieldInfo( $table, $field ) {
172 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
176 function affectedRows() {
180 function getSoftwareLink() {
184 function getServerVersion() {
188 function getServerInfo() {
196 function ping( &$rtt = null ) {
201 protected function closeConnection() {
205 protected function doQuery( $sql ) {
206 $res = $this->nextResult
;
207 $this->nextResult
= [];
209 return new FakeResultWrapper( $res );
212 public function unionSupportsOrderAndLimit() {
213 return $this->unionSupportsOrderAndLimit
;
216 public function setUnionSupportsOrderAndLimit( $v ) {
217 $this->unionSupportsOrderAndLimit
= (bool)$v;