Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
blobc5603c40932a8e4306f09040f998a93b899c1453
1 <?php
3 /**
4 * Helper for testing the methods from the Database class
5 * @since 1.22
6 */
7 class DatabaseTestHelper extends Database {
9 /**
10 * __CLASS__ of the test suite,
11 * used to determine, if the function name is passed every time to query()
13 protected $testName = [];
15 /**
16 * Array of lastSqls passed to query(),
17 * This is an array since some methods in Database can do more than one
18 * query. Cleared when calling getLastSqls().
20 protected $lastSqls = [];
22 /** @var array List of row arrays */
23 protected $nextResult = [];
25 /**
26 * Array of tables to be considered as existing by tableExist()
27 * Use setExistingTables() to alter.
29 protected $tablesExists;
31 public function __construct( $testName, array $opts = [] ) {
32 $this->testName = $testName;
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()}" );
42 $this->currentDomain = DatabaseDomain::newUnspecified();
45 /**
46 * Returns SQL queries grouped by '; '
47 * Clear the list of queries that have been done so far.
49 public function getLastSqls() {
50 $lastSqls = implode( '; ', $this->lastSqls );
51 $this->lastSqls = [];
53 return $lastSqls;
56 public function setExistingTables( $tablesExists ) {
57 $this->tablesExists = (array)$tablesExists;
60 /**
61 * @param mixed $res Use an array of row arrays to set row result
63 public function forceNextResult( $res ) {
64 $this->nextResult = $res;
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 ) );
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.' );
83 function strencode( $s ) {
84 // Choose apos to avoid handling of escaping double quotes in quoted text
85 return str_replace( "'", "\'", $s );
88 public function addIdentifierQuotes( $s ) {
89 // no escaping to avoid handling of double quotes in quoted text
90 return $s;
93 public function query( $sql, $fname = '', $tempIgnore = false ) {
94 $this->checkFunctionName( $fname );
95 $this->addSql( $sql );
97 return parent::query( $sql, $fname, $tempIgnore );
100 public function tableExists( $table, $fname = __METHOD__ ) {
101 $tableRaw = $this->tableName( $table, 'raw' );
102 if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
103 return true; // already known to exist
106 $this->checkFunctionName( $fname );
108 return in_array( $table, (array)$this->tablesExists );
111 // Redeclare parent method to make it public
112 public function nativeReplace( $table, $rows, $fname ) {
113 return parent::nativeReplace( $table, $rows, $fname );
116 function getType() {
117 return 'test';
120 function open( $server, $user, $password, $dbName ) {
121 return false;
124 function fetchObject( $res ) {
125 return false;
128 function fetchRow( $res ) {
129 return false;
132 function numRows( $res ) {
133 return -1;
136 function numFields( $res ) {
137 return -1;
140 function fieldName( $res, $n ) {
141 return 'test';
144 function insertId() {
145 return -1;
148 function dataSeek( $res, $row ) {
149 /* nop */
152 function lastErrno() {
153 return -1;
156 function lastError() {
157 return 'test';
160 function fieldInfo( $table, $field ) {
161 return false;
164 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
165 return false;
168 function affectedRows() {
169 return -1;
172 function getSoftwareLink() {
173 return 'test';
176 function getServerVersion() {
177 return 'test';
180 function getServerInfo() {
181 return 'test';
184 function isOpen() {
185 return true;
188 function ping( &$rtt = null ) {
189 $rtt = 0.0;
190 return true;
193 protected function closeConnection() {
194 return false;
197 protected function doQuery( $sql ) {
198 $res = $this->nextResult;
199 $this->nextResult = [];
201 return new FakeResultWrapper( $res );