36254f7d9c20352e515b0b911e8225f2d7c156c7
[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 = new ProfilerStub( [] );
49 $this->trxProfiler = new TransactionProfiler();
50 $this->cliMode = isset( $opts['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->currentDomain = DatabaseDomain::newUnspecified();
57 $this->open( 'localhost', 'testuser', 'password', 'testdb' );
58 }
59
60 /**
61 * Returns SQL queries grouped by '; '
62 * Clear the list of queries that have been done so far.
63 * @return string
64 */
65 public function getLastSqls() {
66 $lastSqls = implode( '; ', $this->lastSqls );
67 $this->lastSqls = [];
68
69 return $lastSqls;
70 }
71
72 public function setExistingTables( $tablesExists ) {
73 $this->tablesExists = (array)$tablesExists;
74 }
75
76 /**
77 * @param mixed $res Use an array of row arrays to set row result
78 */
79 public function forceNextResult( $res ) {
80 $this->nextResult = $res;
81 }
82
83 /**
84 * @param int $errno Error number
85 * @param string $error Error text
86 * @param array $options
87 * - wasKnownStatementRollbackError: Return value for wasKnownStatementRollbackError()
88 */
89 public function forceNextQueryError( $errno, $error, $options = [] ) {
90 $this->nextError = [ 'errno' => $errno, 'error' => $error ] + $options;
91 }
92
93 protected function addSql( $sql ) {
94 // clean up spaces before and after some words and the whole string
95 $this->lastSqls[] = trim( preg_replace(
96 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
97 ' ', $sql
98 ) );
99 }
100
101 protected function checkFunctionName( $fname ) {
102 if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
103 return; // no $fname parameter
104 }
105
106 // Handle some internal calls from the Database class
107 $check = $fname;
108 if ( preg_match( '/^Wikimedia\\\\Rdbms\\\\Database::query \((.+)\)$/', $fname, $m ) ) {
109 $check = $m[1];
110 }
111
112 if ( substr( $check, 0, strlen( $this->testName ) ) !== $this->testName ) {
113 throw new MWException( 'function name does not start with test class. ' .
114 $fname . ' vs. ' . $this->testName . '. ' .
115 'Please provide __METHOD__ to database methods.' );
116 }
117 }
118
119 function strencode( $s ) {
120 // Choose apos to avoid handling of escaping double quotes in quoted text
121 return str_replace( "'", "\'", $s );
122 }
123
124 public function addIdentifierQuotes( $s ) {
125 // no escaping to avoid handling of double quotes in quoted text
126 return $s;
127 }
128
129 public function query( $sql, $fname = '', $tempIgnore = false ) {
130 $this->checkFunctionName( $fname );
131
132 return parent::query( $sql, $fname, $tempIgnore );
133 }
134
135 public function tableExists( $table, $fname = __METHOD__ ) {
136 $tableRaw = $this->tableName( $table, 'raw' );
137 if ( isset( $this->sessionTempTables[$tableRaw] ) ) {
138 return true; // already known to exist
139 }
140
141 $this->checkFunctionName( $fname );
142
143 return in_array( $table, (array)$this->tablesExists );
144 }
145
146 // Redeclare parent method to make it public
147 public function nativeReplace( $table, $rows, $fname ) {
148 return parent::nativeReplace( $table, $rows, $fname );
149 }
150
151 function getType() {
152 return 'test';
153 }
154
155 function open( $server, $user, $password, $dbName ) {
156 $this->conn = (object)[ 'test' ];
157
158 return true;
159 }
160
161 function fetchObject( $res ) {
162 return false;
163 }
164
165 function fetchRow( $res ) {
166 return false;
167 }
168
169 function numRows( $res ) {
170 return -1;
171 }
172
173 function numFields( $res ) {
174 return -1;
175 }
176
177 function fieldName( $res, $n ) {
178 return 'test';
179 }
180
181 function insertId() {
182 return -1;
183 }
184
185 function dataSeek( $res, $row ) {
186 /* nop */
187 }
188
189 function lastErrno() {
190 return $this->lastError ? $this->lastError['errno'] : -1;
191 }
192
193 function lastError() {
194 return $this->lastError ? $this->lastError['error'] : 'test';
195 }
196
197 protected function wasKnownStatementRollbackError() {
198 return isset( $this->lastError['wasKnownStatementRollbackError'] )
199 ? $this->lastError['wasKnownStatementRollbackError']
200 : false;
201 }
202
203 function fieldInfo( $table, $field ) {
204 return false;
205 }
206
207 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
208 return false;
209 }
210
211 function fetchAffectedRowCount() {
212 return -1;
213 }
214
215 function getSoftwareLink() {
216 return 'test';
217 }
218
219 function getServerVersion() {
220 return 'test';
221 }
222
223 function getServerInfo() {
224 return 'test';
225 }
226
227 function isOpen() {
228 return $this->conn ? true : false;
229 }
230
231 function ping( &$rtt = null ) {
232 $rtt = 0.0;
233 return true;
234 }
235
236 protected function closeConnection() {
237 return true;
238 }
239
240 protected function doQuery( $sql ) {
241 $sql = preg_replace( '< /\* .+? \*/>', '', $sql );
242 $this->addSql( $sql );
243
244 if ( $this->nextError ) {
245 $this->lastError = $this->nextError;
246 $this->nextError = null;
247 return false;
248 }
249
250 $res = $this->nextResult;
251 $this->nextResult = [];
252 $this->lastError = null;
253
254 return new FakeResultWrapper( $res );
255 }
256
257 public function unionSupportsOrderAndLimit() {
258 return $this->unionSupportsOrderAndLimit;
259 }
260
261 public function setUnionSupportsOrderAndLimit( $v ) {
262 $this->unionSupportsOrderAndLimit = (bool)$v;
263 }
264 }