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