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