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