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