Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
1 <?php
2
3 use Wikimedia\Rdbms\TransactionProfiler;
4 use Wikimedia\Rdbms\DatabaseDomain;
5 use Wikimedia\Rdbms\Database;
6
7 /**
8 * Helper for testing the methods from the Database class
9 * @since 1.22
10 */
11 class DatabaseTestHelper extends Database {
12
13 /**
14 * __CLASS__ of the test suite,
15 * used to determine, if the function name is passed every time to query()
16 */
17 protected $testName = [];
18
19 /**
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().
23 */
24 protected $lastSqls = [];
25
26 /** @var array List of row arrays */
27 protected $nextResult = [];
28
29 /** @var array|null */
30 protected $nextError = null;
31 /** @var array|null */
32 protected $lastError = null;
33
34 /**
35 * Array of tables to be considered as existing by tableExist()
36 * Use setExistingTables() to alter.
37 */
38 protected $tablesExists;
39
40 /**
41 * Value to return from unionSupportsOrderAndLimit()
42 */
43 protected $unionSupportsOrderAndLimit = true;
44
45 public function __construct( $testName, array $opts = [] ) {
46 $this->testName = $testName;
47
48 $this->profiler = null;
49 $this->trxProfiler = new TransactionProfiler();
50 $this->cliMode = $opts['cliMode'] ?? true;
51 $this->connLogger = new \Psr\Log\NullLogger();
52 $this->queryLogger = new \Psr\Log\NullLogger();
53 $this->errorLogger = function ( Exception $e ) {
54 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
55 };
56 $this->deprecationLogger = function ( $msg ) {
57 wfWarn( $msg );
58 };
59 $this->currentDomain = DatabaseDomain::newUnspecified();
60 $this->open( 'localhost', 'testuser', 'password', 'testdb', null, '' );
61 }
62
63 /**
64 * Returns SQL queries grouped by '; '
65 * Clear the list of queries that have been done so far.
66 * @return string
67 */
68 public function getLastSqls() {
69 $lastSqls = implode( '; ', $this->lastSqls );
70 $this->lastSqls = [];
71
72 return $lastSqls;
73 }
74
75 public function setExistingTables( $tablesExists ) {
76 $this->tablesExists = (array)$tablesExists;
77 }
78
79 /**
80 * @param mixed $res Use an array of row arrays to set row result
81 */
82 public function forceNextResult( $res ) {
83 $this->nextResult = $res;
84 }
85
86 /**
87 * @param int $errno Error number
88 * @param string $error Error text
89 * @param array $options
90 * - wasKnownStatementRollbackError: Return value for wasKnownStatementRollbackError()
91 */
92 public function forceNextQueryError( $errno, $error, $options = [] ) {
93 $this->nextError = [ 'errno' => $errno, 'error' => $error ] + $options;
94 }
95
96 protected function addSql( $sql ) {
97 // clean up spaces before and after some words and the whole string
98 $this->lastSqls[] = trim( preg_replace(
99 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
100 ' ', $sql
101 ) );
102 }
103
104 protected function checkFunctionName( $fname ) {
105 if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
106 return; // no $fname parameter
107 }
108
109 // Handle some internal calls from the Database class
110 $check = $fname;
111 if ( preg_match(
112 '/^Wikimedia\\\\Rdbms\\\\Database::(?:query|beginIfImplied) \((.+)\)$/',
113 $fname,
114 $m
115 ) ) {
116 $check = $m[1];
117 }
118
119 if ( substr( $check, 0, strlen( $this->testName ) ) !== $this->testName ) {
120 throw new MWException( 'function name does not start with test class. ' .
121 $fname . ' vs. ' . $this->testName . '. ' .
122 'Please provide __METHOD__ to database methods.' );
123 }
124 }
125
126 function strencode( $s ) {
127 // Choose apos to avoid handling of escaping double quotes in quoted text
128 return str_replace( "'", "\'", $s );
129 }
130
131 public function addIdentifierQuotes( $s ) {
132 // no escaping to avoid handling of double quotes in quoted text
133 return $s;
134 }
135
136 public function query( $sql, $fname = '', $flags = 0 ) {
137 $this->checkFunctionName( $fname );
138
139 return parent::query( $sql, $fname, $flags );
140 }
141
142 public function tableExists( $table, $fname = __METHOD__ ) {
143 $tableRaw = $this->tableName( $table, 'raw' );
144 if ( isset( $this->sessionTempTables[$tableRaw] ) ) {
145 return true; // already known to exist
146 }
147
148 $this->checkFunctionName( $fname );
149
150 return in_array( $table, (array)$this->tablesExists );
151 }
152
153 // Redeclare parent method to make it public
154 public function nativeReplace( $table, $rows, $fname ) {
155 parent::nativeReplace( $table, $rows, $fname );
156 }
157
158 function getType() {
159 return 'test';
160 }
161
162 function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
163 $this->conn = (object)[ 'test' ];
164
165 return true;
166 }
167
168 function fetchObject( $res ) {
169 return false;
170 }
171
172 function fetchRow( $res ) {
173 return false;
174 }
175
176 function numRows( $res ) {
177 return -1;
178 }
179
180 function numFields( $res ) {
181 return -1;
182 }
183
184 function fieldName( $res, $n ) {
185 return 'test';
186 }
187
188 function insertId() {
189 return -1;
190 }
191
192 function dataSeek( $res, $row ) {
193 /* nop */
194 }
195
196 function lastErrno() {
197 return $this->lastError ? $this->lastError['errno'] : -1;
198 }
199
200 function lastError() {
201 return $this->lastError ? $this->lastError['error'] : 'test';
202 }
203
204 protected function wasKnownStatementRollbackError() {
205 return $this->lastError['wasKnownStatementRollbackError'] ?? false;
206 }
207
208 function fieldInfo( $table, $field ) {
209 return false;
210 }
211
212 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
213 return false;
214 }
215
216 function fetchAffectedRowCount() {
217 return -1;
218 }
219
220 function getSoftwareLink() {
221 return 'test';
222 }
223
224 function getServerVersion() {
225 return 'test';
226 }
227
228 function getServerInfo() {
229 return 'test';
230 }
231
232 function ping( &$rtt = null ) {
233 $rtt = 0.0;
234 return true;
235 }
236
237 protected function closeConnection() {
238 return true;
239 }
240
241 protected function doQuery( $sql ) {
242 $sql = preg_replace( '< /\* .+? \*/>', '', $sql );
243 $this->addSql( $sql );
244
245 if ( $this->nextError ) {
246 $this->lastError = $this->nextError;
247 $this->nextError = null;
248 return false;
249 }
250
251 $res = $this->nextResult;
252 $this->nextResult = [];
253 $this->lastError = null;
254
255 return new FakeResultWrapper( $res );
256 }
257
258 public function unionSupportsOrderAndLimit() {
259 return $this->unionSupportsOrderAndLimit;
260 }
261
262 public function setUnionSupportsOrderAndLimit( $v ) {
263 $this->unionSupportsOrderAndLimit = (bool)$v;
264 }
265 }