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