3 use Wikimedia\Rdbms\TransactionProfiler
;
4 use Wikimedia\Rdbms\DatabaseDomain
;
5 use Wikimedia\Rdbms\Database
;
8 * Helper for testing the methods from the Database class
11 class DatabaseTestHelper
extends Database
{
14 * __CLASS__ of the test suite,
15 * used to determine, if the function name is passed every time to query()
17 protected $testName = [];
20 * Array of lastSqls passed to query(),
21 * This is an array since some methods in Database can do more than one
22 * query. Cleared when calling getLastSqls().
24 protected $lastSqls = [];
26 /** @var array List of row arrays */
27 protected $nextResult = [];
30 * Array of tables to be considered as existing by tableExist()
31 * Use setExistingTables() to alter.
33 protected $tablesExists;
36 * Value to return from unionSupportsOrderAndLimit()
38 protected $unionSupportsOrderAndLimit = true;
40 public function __construct( $testName, array $opts = [] ) {
41 $this->testName
= $testName;
43 $this->profiler
= new ProfilerStub( [] );
44 $this->trxProfiler
= new TransactionProfiler();
45 $this->cliMode
= isset( $opts['cliMode'] ) ?
$opts['cliMode'] : true;
46 $this->connLogger
= new \Psr\Log\
NullLogger();
47 $this->queryLogger
= new \Psr\Log\
NullLogger();
48 $this->errorLogger
= function ( Exception
$e ) {
49 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
51 $this->currentDomain
= DatabaseDomain
::newUnspecified();
52 $this->open( 'localhost', 'testuser', 'password', 'testdb' );
56 * Returns SQL queries grouped by '; '
57 * Clear the list of queries that have been done so far.
60 public function getLastSqls() {
61 $lastSqls = implode( '; ', $this->lastSqls
);
67 public function setExistingTables( $tablesExists ) {
68 $this->tablesExists
= (array)$tablesExists;
72 * @param mixed $res Use an array of row arrays to set row result
74 public function forceNextResult( $res ) {
75 $this->nextResult
= $res;
78 protected function addSql( $sql ) {
79 // clean up spaces before and after some words and the whole string
80 $this->lastSqls
[] = trim( preg_replace(
81 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
86 protected function checkFunctionName( $fname ) {
87 if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
88 return; // no $fname parameter
91 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
92 throw new MWException( 'function name does not start with test class. ' .
93 $fname . ' vs. ' . $this->testName
. '. ' .
94 'Please provide __METHOD__ to database methods.' );
98 function strencode( $s ) {
99 // Choose apos to avoid handling of escaping double quotes in quoted text
100 return str_replace( "'", "\'", $s );
103 public function addIdentifierQuotes( $s ) {
104 // no escaping to avoid handling of double quotes in quoted text
108 public function query( $sql, $fname = '', $tempIgnore = false ) {
109 $this->checkFunctionName( $fname );
110 $this->addSql( $sql );
112 return parent
::query( $sql, $fname, $tempIgnore );
115 public function tableExists( $table, $fname = __METHOD__
) {
116 $tableRaw = $this->tableName( $table, 'raw' );
117 if ( isset( $this->sessionTempTables
[$tableRaw] ) ) {
118 return true; // already known to exist
121 $this->checkFunctionName( $fname );
123 return in_array( $table, (array)$this->tablesExists
);
126 // Redeclare parent method to make it public
127 public function nativeReplace( $table, $rows, $fname ) {
128 return parent
::nativeReplace( $table, $rows, $fname );
135 function open( $server, $user, $password, $dbName ) {
136 $this->conn
= (object)[ 'test' ];
141 function fetchObject( $res ) {
145 function fetchRow( $res ) {
149 function numRows( $res ) {
153 function numFields( $res ) {
157 function fieldName( $res, $n ) {
161 function insertId() {
165 function dataSeek( $res, $row ) {
169 function lastErrno() {
173 function lastError() {
177 function fieldInfo( $table, $field ) {
181 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
185 function fetchAffectedRowCount() {
189 function getSoftwareLink() {
193 function getServerVersion() {
197 function getServerInfo() {
202 return $this->conn ?
true : false;
205 function ping( &$rtt = null ) {
210 protected function closeConnection() {
214 protected function doQuery( $sql ) {
215 $res = $this->nextResult
;
216 $this->nextResult
= [];
218 return new FakeResultWrapper( $res );
221 public function unionSupportsOrderAndLimit() {
222 return $this->unionSupportsOrderAndLimit
;
225 public function setUnionSupportsOrderAndLimit( $v ) {
226 $this->unionSupportsOrderAndLimit
= (bool)$v;