Merge "Improve "selfmove" message's wording"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
1 <?php
2
3 use Wikimedia\Rdbms\TransactionProfiler;
4 use Wikimedia\Rdbms\DatabaseDomain;
5
6 /**
7 * Helper for testing the methods from the Database class
8 * @since 1.22
9 */
10 class DatabaseTestHelper extends Database {
11
12 /**
13 * __CLASS__ of the test suite,
14 * used to determine, if the function name is passed every time to query()
15 */
16 protected $testName = [];
17
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().
22 */
23 protected $lastSqls = [];
24
25 /** @var array List of row arrays */
26 protected $nextResult = [];
27
28 /**
29 * Array of tables to be considered as existing by tableExist()
30 * Use setExistingTables() to alter.
31 */
32 protected $tablesExists;
33
34 /**
35 * Value to return from unionSupportsOrderAndLimit()
36 */
37 protected $unionSupportsOrderAndLimit = true;
38
39 public function __construct( $testName, array $opts = [] ) {
40 $this->testName = $testName;
41
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()}" );
49 };
50 $this->currentDomain = DatabaseDomain::newUnspecified();
51 }
52
53 /**
54 * Returns SQL queries grouped by '; '
55 * Clear the list of queries that have been done so far.
56 * @return string
57 */
58 public function getLastSqls() {
59 $lastSqls = implode( '; ', $this->lastSqls );
60 $this->lastSqls = [];
61
62 return $lastSqls;
63 }
64
65 public function setExistingTables( $tablesExists ) {
66 $this->tablesExists = (array)$tablesExists;
67 }
68
69 /**
70 * @param mixed $res Use an array of row arrays to set row result
71 */
72 public function forceNextResult( $res ) {
73 $this->nextResult = $res;
74 }
75
76 protected function addSql( $sql ) {
77 // clean up spaces before and after some words and the whole string
78 $this->lastSqls[] = trim( preg_replace(
79 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
80 ' ', $sql
81 ) );
82 }
83
84 protected function checkFunctionName( $fname ) {
85 if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
86 throw new MWException( 'function name does not start with test class. ' .
87 $fname . ' vs. ' . $this->testName . '. ' .
88 'Please provide __METHOD__ to database methods.' );
89 }
90 }
91
92 function strencode( $s ) {
93 // Choose apos to avoid handling of escaping double quotes in quoted text
94 return str_replace( "'", "\'", $s );
95 }
96
97 public function addIdentifierQuotes( $s ) {
98 // no escaping to avoid handling of double quotes in quoted text
99 return $s;
100 }
101
102 public function query( $sql, $fname = '', $tempIgnore = false ) {
103 $this->checkFunctionName( $fname );
104 $this->addSql( $sql );
105
106 return parent::query( $sql, $fname, $tempIgnore );
107 }
108
109 public function tableExists( $table, $fname = __METHOD__ ) {
110 $tableRaw = $this->tableName( $table, 'raw' );
111 if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
112 return true; // already known to exist
113 }
114
115 $this->checkFunctionName( $fname );
116
117 return in_array( $table, (array)$this->tablesExists );
118 }
119
120 // Redeclare parent method to make it public
121 public function nativeReplace( $table, $rows, $fname ) {
122 return parent::nativeReplace( $table, $rows, $fname );
123 }
124
125 function getType() {
126 return 'test';
127 }
128
129 function open( $server, $user, $password, $dbName ) {
130 return false;
131 }
132
133 function fetchObject( $res ) {
134 return false;
135 }
136
137 function fetchRow( $res ) {
138 return false;
139 }
140
141 function numRows( $res ) {
142 return -1;
143 }
144
145 function numFields( $res ) {
146 return -1;
147 }
148
149 function fieldName( $res, $n ) {
150 return 'test';
151 }
152
153 function insertId() {
154 return -1;
155 }
156
157 function dataSeek( $res, $row ) {
158 /* nop */
159 }
160
161 function lastErrno() {
162 return -1;
163 }
164
165 function lastError() {
166 return 'test';
167 }
168
169 function fieldInfo( $table, $field ) {
170 return false;
171 }
172
173 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
174 return false;
175 }
176
177 function affectedRows() {
178 return -1;
179 }
180
181 function getSoftwareLink() {
182 return 'test';
183 }
184
185 function getServerVersion() {
186 return 'test';
187 }
188
189 function getServerInfo() {
190 return 'test';
191 }
192
193 function isOpen() {
194 return true;
195 }
196
197 function ping( &$rtt = null ) {
198 $rtt = 0.0;
199 return true;
200 }
201
202 protected function closeConnection() {
203 return false;
204 }
205
206 protected function doQuery( $sql ) {
207 $res = $this->nextResult;
208 $this->nextResult = [];
209
210 return new FakeResultWrapper( $res );
211 }
212
213 public function unionSupportsOrderAndLimit() {
214 return $this->unionSupportsOrderAndLimit;
215 }
216
217 public function setUnionSupportsOrderAndLimit( $v ) {
218 $this->unionSupportsOrderAndLimit = (bool)$v;
219 }
220 }