3 use Wikimedia\Rdbms\TransactionProfiler
;
6 * Helper for testing the methods from the Database class
9 class DatabaseTestHelper
extends Database
{
12 * __CLASS__ of the test suite,
13 * used to determine, if the function name is passed every time to query()
15 protected $testName = [];
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 = [];
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();
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
);
58 public function setExistingTables( $tablesExists ) {
59 $this->tablesExists
= (array)$tablesExists;
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,}/',
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
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 );
122 function open( $server, $user, $password, $dbName ) {
126 function fetchObject( $res ) {
130 function fetchRow( $res ) {
134 function numRows( $res ) {
138 function numFields( $res ) {
142 function fieldName( $res, $n ) {
146 function insertId() {
150 function dataSeek( $res, $row ) {
154 function lastErrno() {
158 function lastError() {
162 function fieldInfo( $table, $field ) {
166 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
170 function affectedRows() {
174 function getSoftwareLink() {
178 function getServerVersion() {
182 function getServerInfo() {
190 function ping( &$rtt = null ) {
195 protected function closeConnection() {
199 protected function doQuery( $sql ) {
200 $res = $this->nextResult
;
201 $this->nextResult
= [];
203 return new FakeResultWrapper( $res );