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