9ed8f1563091117789dcbc62754177b4869820e3
[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 */
57 public function getLastSqls() {
58 $lastSqls = implode( '; ', $this->lastSqls );
59 $this->lastSqls = [];
60
61 return $lastSqls;
62 }
63
64 public function setExistingTables( $tablesExists ) {
65 $this->tablesExists = (array)$tablesExists;
66 }
67
68 /**
69 * @param mixed $res Use an array of row arrays to set row result
70 */
71 public function forceNextResult( $res ) {
72 $this->nextResult = $res;
73 }
74
75 protected function addSql( $sql ) {
76 // clean up spaces before and after some words and the whole string
77 $this->lastSqls[] = trim( preg_replace(
78 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
79 ' ', $sql
80 ) );
81 }
82
83 protected function checkFunctionName( $fname ) {
84 if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
85 throw new MWException( 'function name does not start with test class. ' .
86 $fname . ' vs. ' . $this->testName . '. ' .
87 'Please provide __METHOD__ to database methods.' );
88 }
89 }
90
91 function strencode( $s ) {
92 // Choose apos to avoid handling of escaping double quotes in quoted text
93 return str_replace( "'", "\'", $s );
94 }
95
96 public function addIdentifierQuotes( $s ) {
97 // no escaping to avoid handling of double quotes in quoted text
98 return $s;
99 }
100
101 public function query( $sql, $fname = '', $tempIgnore = false ) {
102 $this->checkFunctionName( $fname );
103 $this->addSql( $sql );
104
105 return parent::query( $sql, $fname, $tempIgnore );
106 }
107
108 public function tableExists( $table, $fname = __METHOD__ ) {
109 $tableRaw = $this->tableName( $table, 'raw' );
110 if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
111 return true; // already known to exist
112 }
113
114 $this->checkFunctionName( $fname );
115
116 return in_array( $table, (array)$this->tablesExists );
117 }
118
119 // Redeclare parent method to make it public
120 public function nativeReplace( $table, $rows, $fname ) {
121 return parent::nativeReplace( $table, $rows, $fname );
122 }
123
124 function getType() {
125 return 'test';
126 }
127
128 function open( $server, $user, $password, $dbName ) {
129 return false;
130 }
131
132 function fetchObject( $res ) {
133 return false;
134 }
135
136 function fetchRow( $res ) {
137 return false;
138 }
139
140 function numRows( $res ) {
141 return -1;
142 }
143
144 function numFields( $res ) {
145 return -1;
146 }
147
148 function fieldName( $res, $n ) {
149 return 'test';
150 }
151
152 function insertId() {
153 return -1;
154 }
155
156 function dataSeek( $res, $row ) {
157 /* nop */
158 }
159
160 function lastErrno() {
161 return -1;
162 }
163
164 function lastError() {
165 return 'test';
166 }
167
168 function fieldInfo( $table, $field ) {
169 return false;
170 }
171
172 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
173 return false;
174 }
175
176 function affectedRows() {
177 return -1;
178 }
179
180 function getSoftwareLink() {
181 return 'test';
182 }
183
184 function getServerVersion() {
185 return 'test';
186 }
187
188 function getServerInfo() {
189 return 'test';
190 }
191
192 function isOpen() {
193 return true;
194 }
195
196 function ping( &$rtt = null ) {
197 $rtt = 0.0;
198 return true;
199 }
200
201 protected function closeConnection() {
202 return false;
203 }
204
205 protected function doQuery( $sql ) {
206 $res = $this->nextResult;
207 $this->nextResult = [];
208
209 return new FakeResultWrapper( $res );
210 }
211
212 public function unionSupportsOrderAndLimit() {
213 return $this->unionSupportsOrderAndLimit;
214 }
215
216 public function setUnionSupportsOrderAndLimit( $v ) {
217 $this->unionSupportsOrderAndLimit = (bool)$v;
218 }
219 }