Introduce InterwikiLookupAdapter on top of SiteLookup
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
1 <?php
2
3 /**
4 * Helper for testing the methods from the DatabaseBase class
5 * @since 1.22
6 */
7 class DatabaseTestHelper extends DatabaseBase {
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 DatabaseBase 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 }
38
39 /**
40 * Returns SQL queries grouped by '; '
41 * Clear the list of queries that have been done so far.
42 */
43 public function getLastSqls() {
44 $lastSqls = implode( '; ', $this->lastSqls );
45 $this->lastSqls = [];
46
47 return $lastSqls;
48 }
49
50 public function setExistingTables( $tablesExists ) {
51 $this->tablesExists = (array)$tablesExists;
52 }
53
54 /**
55 * @param mixed $res Use an array of row arrays to set row result
56 */
57 public function forceNextResult( $res ) {
58 $this->nextResult = $res;
59 }
60
61 protected function addSql( $sql ) {
62 // clean up spaces before and after some words and the whole string
63 $this->lastSqls[] = trim( preg_replace(
64 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
65 ' ', $sql
66 ) );
67 }
68
69 protected function checkFunctionName( $fname ) {
70 if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
71 throw new MWException( 'function name does not start with test class. ' .
72 $fname . ' vs. ' . $this->testName . '. ' .
73 'Please provide __METHOD__ to database methods.' );
74 }
75 }
76
77 function strencode( $s ) {
78 // Choose apos to avoid handling of escaping double quotes in quoted text
79 return str_replace( "'", "\'", $s );
80 }
81
82 public function addIdentifierQuotes( $s ) {
83 // no escaping to avoid handling of double quotes in quoted text
84 return $s;
85 }
86
87 public function query( $sql, $fname = '', $tempIgnore = false ) {
88 $this->checkFunctionName( $fname );
89 $this->addSql( $sql );
90
91 return parent::query( $sql, $fname, $tempIgnore );
92 }
93
94 public function tableExists( $table, $fname = __METHOD__ ) {
95 $this->checkFunctionName( $fname );
96
97 return in_array( $table, (array)$this->tablesExists );
98 }
99
100 // Redeclare parent method to make it public
101 public function nativeReplace( $table, $rows, $fname ) {
102 return parent::nativeReplace( $table, $rows, $fname );
103 }
104
105 function getType() {
106 return 'test';
107 }
108
109 function open( $server, $user, $password, $dbName ) {
110 return false;
111 }
112
113 function fetchObject( $res ) {
114 return false;
115 }
116
117 function fetchRow( $res ) {
118 return false;
119 }
120
121 function numRows( $res ) {
122 return -1;
123 }
124
125 function numFields( $res ) {
126 return -1;
127 }
128
129 function fieldName( $res, $n ) {
130 return 'test';
131 }
132
133 function insertId() {
134 return -1;
135 }
136
137 function dataSeek( $res, $row ) {
138 /* nop */
139 }
140
141 function lastErrno() {
142 return -1;
143 }
144
145 function lastError() {
146 return 'test';
147 }
148
149 function fieldInfo( $table, $field ) {
150 return false;
151 }
152
153 function indexInfo( $table, $index, $fname = 'DatabaseBase::indexInfo' ) {
154 return false;
155 }
156
157 function affectedRows() {
158 return -1;
159 }
160
161 function getSoftwareLink() {
162 return 'test';
163 }
164
165 function getServerVersion() {
166 return 'test';
167 }
168
169 function getServerInfo() {
170 return 'test';
171 }
172
173 function isOpen() {
174 return true;
175 }
176
177 function ping( &$rtt = null ) {
178 $rtt = 0.0;
179 return true;
180 }
181
182 protected function closeConnection() {
183 return false;
184 }
185
186 protected function doQuery( $sql ) {
187 $res = $this->nextResult;
188 $this->nextResult = [];
189
190 return new FakeResultWrapper( $res );
191 }
192 }