Move ChronologyProtector/TransactionProfiler to Rdbms namespace
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
blobd689d50a42a0a899eb0d2f818b5df465f633e856
1 <?php
3 use Wikimedia\Rdbms\TransactionProfiler;
5 /**
6 * Helper for testing the methods from the Database class
7 * @since 1.22
8 */
9 class DatabaseTestHelper extends Database {
11 /**
12 * __CLASS__ of the test suite,
13 * used to determine, if the function name is passed every time to query()
15 protected $testName = [];
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().
22 protected $lastSqls = [];
24 /** @var array List of row arrays */
25 protected $nextResult = [];
27 /**
28 * Array of tables to be considered as existing by tableExist()
29 * Use setExistingTables() to alter.
31 protected $tablesExists;
33 public function __construct( $testName, array $opts = [] ) {
34 $this->testName = $testName;
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()}" );
44 $this->currentDomain = DatabaseDomain::newUnspecified();
47 /**
48 * Returns SQL queries grouped by '; '
49 * Clear the list of queries that have been done so far.
51 public function getLastSqls() {
52 $lastSqls = implode( '; ', $this->lastSqls );
53 $this->lastSqls = [];
55 return $lastSqls;
58 public function setExistingTables( $tablesExists ) {
59 $this->tablesExists = (array)$tablesExists;
62 /**
63 * @param mixed $res Use an array of row arrays to set row result
65 public function forceNextResult( $res ) {
66 $this->nextResult = $res;
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 ) );
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.' );
85 function strencode( $s ) {
86 // Choose apos to avoid handling of escaping double quotes in quoted text
87 return str_replace( "'", "\'", $s );
90 public function addIdentifierQuotes( $s ) {
91 // no escaping to avoid handling of double quotes in quoted text
92 return $s;
95 public function query( $sql, $fname = '', $tempIgnore = false ) {
96 $this->checkFunctionName( $fname );
97 $this->addSql( $sql );
99 return parent::query( $sql, $fname, $tempIgnore );
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
108 $this->checkFunctionName( $fname );
110 return in_array( $table, (array)$this->tablesExists );
113 // Redeclare parent method to make it public
114 public function nativeReplace( $table, $rows, $fname ) {
115 return parent::nativeReplace( $table, $rows, $fname );
118 function getType() {
119 return 'test';
122 function open( $server, $user, $password, $dbName ) {
123 return false;
126 function fetchObject( $res ) {
127 return false;
130 function fetchRow( $res ) {
131 return false;
134 function numRows( $res ) {
135 return -1;
138 function numFields( $res ) {
139 return -1;
142 function fieldName( $res, $n ) {
143 return 'test';
146 function insertId() {
147 return -1;
150 function dataSeek( $res, $row ) {
151 /* nop */
154 function lastErrno() {
155 return -1;
158 function lastError() {
159 return 'test';
162 function fieldInfo( $table, $field ) {
163 return false;
166 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
167 return false;
170 function affectedRows() {
171 return -1;
174 function getSoftwareLink() {
175 return 'test';
178 function getServerVersion() {
179 return 'test';
182 function getServerInfo() {
183 return 'test';
186 function isOpen() {
187 return true;
190 function ping( &$rtt = null ) {
191 $rtt = 0.0;
192 return true;
195 protected function closeConnection() {
196 return false;
199 protected function doQuery( $sql ) {
200 $res = $this->nextResult;
201 $this->nextResult = [];
203 return new FakeResultWrapper( $res );