rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
blob9ed8f1563091117789dcbc62754177b4869820e3
1 <?php
3 use Wikimedia\Rdbms\TransactionProfiler;
4 use Wikimedia\Rdbms\DatabaseDomain;
6 /**
7 * Helper for testing the methods from the Database class
8 * @since 1.22
9 */
10 class DatabaseTestHelper extends Database {
12 /**
13 * __CLASS__ of the test suite,
14 * used to determine, if the function name is passed every time to query()
16 protected $testName = [];
18 /**
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 = [];
28 /**
29 * Array of tables to be considered as existing by tableExist()
30 * Use setExistingTables() to alter.
32 protected $tablesExists;
34 /**
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();
53 /**
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 );
59 $this->lastSqls = [];
61 return $lastSqls;
64 public function setExistingTables( $tablesExists ) {
65 $this->tablesExists = (array)$tablesExists;
68 /**
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,}/',
79 ' ', $sql
80 ) );
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
98 return $s;
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 );
124 function getType() {
125 return 'test';
128 function open( $server, $user, $password, $dbName ) {
129 return false;
132 function fetchObject( $res ) {
133 return false;
136 function fetchRow( $res ) {
137 return false;
140 function numRows( $res ) {
141 return -1;
144 function numFields( $res ) {
145 return -1;
148 function fieldName( $res, $n ) {
149 return 'test';
152 function insertId() {
153 return -1;
156 function dataSeek( $res, $row ) {
157 /* nop */
160 function lastErrno() {
161 return -1;
164 function lastError() {
165 return 'test';
168 function fieldInfo( $table, $field ) {
169 return false;
172 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
173 return false;
176 function affectedRows() {
177 return -1;
180 function getSoftwareLink() {
181 return 'test';
184 function getServerVersion() {
185 return 'test';
188 function getServerInfo() {
189 return 'test';
192 function isOpen() {
193 return true;
196 function ping( &$rtt = null ) {
197 $rtt = 0.0;
198 return true;
201 protected function closeConnection() {
202 return false;
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;