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