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