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