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 = [];
29 /** @var array|null */
30 protected $nextError = null;
31 /** @var array|null */
32 protected $lastError = null;
35 * Array of tables to be considered as existing by tableExist()
36 * Use setExistingTables() to alter.
38 protected $tablesExists;
41 * Value to return from unionSupportsOrderAndLimit()
43 protected $unionSupportsOrderAndLimit = true;
45 public function __construct( $testName, array $opts = [] ) {
46 $this->testName
= $testName;
48 $this->profiler
= new ProfilerStub( [] );
49 $this->trxProfiler
= new TransactionProfiler();
50 $this->cliMode
= isset( $opts['cliMode'] ) ?
$opts['cliMode'] : true;
51 $this->connLogger
= new \Psr\Log\
NullLogger();
52 $this->queryLogger
= new \Psr\Log\
NullLogger();
53 $this->errorLogger
= function ( Exception
$e ) {
54 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
56 $this->currentDomain
= DatabaseDomain
::newUnspecified();
57 $this->open( 'localhost', 'testuser', 'password', 'testdb' );
61 * Returns SQL queries grouped by '; '
62 * Clear the list of queries that have been done so far.
65 public function getLastSqls() {
66 $lastSqls = implode( '; ', $this->lastSqls
);
72 public function setExistingTables( $tablesExists ) {
73 $this->tablesExists
= (array)$tablesExists;
77 * @param mixed $res Use an array of row arrays to set row result
79 public function forceNextResult( $res ) {
80 $this->nextResult
= $res;
84 * @param int $errno Error number
85 * @param string $error Error text
86 * @param array $options
87 * - wasKnownStatementRollbackError: Return value for wasKnownStatementRollbackError()
89 public function forceNextQueryError( $errno, $error, $options = [] ) {
90 $this->nextError
= [ 'errno' => $errno, 'error' => $error ] +
$options;
93 protected function addSql( $sql ) {
94 // clean up spaces before and after some words and the whole string
95 $this->lastSqls
[] = trim( preg_replace(
96 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
101 protected function checkFunctionName( $fname ) {
102 if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
103 return; // no $fname parameter
106 // Handle some internal calls from the Database class
108 if ( preg_match( '/^Wikimedia\\\\Rdbms\\\\Database::query \((.+)\)$/', $fname, $m ) ) {
112 if ( substr( $check, 0, strlen( $this->testName
) ) !== $this->testName
) {
113 throw new MWException( 'function name does not start with test class. ' .
114 $fname . ' vs. ' . $this->testName
. '. ' .
115 'Please provide __METHOD__ to database methods.' );
119 function strencode( $s ) {
120 // Choose apos to avoid handling of escaping double quotes in quoted text
121 return str_replace( "'", "\'", $s );
124 public function addIdentifierQuotes( $s ) {
125 // no escaping to avoid handling of double quotes in quoted text
129 public function query( $sql, $fname = '', $tempIgnore = false ) {
130 $this->checkFunctionName( $fname );
132 return parent
::query( $sql, $fname, $tempIgnore );
135 public function tableExists( $table, $fname = __METHOD__
) {
136 $tableRaw = $this->tableName( $table, 'raw' );
137 if ( isset( $this->sessionTempTables
[$tableRaw] ) ) {
138 return true; // already known to exist
141 $this->checkFunctionName( $fname );
143 return in_array( $table, (array)$this->tablesExists
);
146 // Redeclare parent method to make it public
147 public function nativeReplace( $table, $rows, $fname ) {
148 return parent
::nativeReplace( $table, $rows, $fname );
155 function open( $server, $user, $password, $dbName ) {
156 $this->conn
= (object)[ 'test' ];
161 function fetchObject( $res ) {
165 function fetchRow( $res ) {
169 function numRows( $res ) {
173 function numFields( $res ) {
177 function fieldName( $res, $n ) {
181 function insertId() {
185 function dataSeek( $res, $row ) {
189 function lastErrno() {
190 return $this->lastError ?
$this->lastError
['errno'] : -1;
193 function lastError() {
194 return $this->lastError ?
$this->lastError
['error'] : 'test';
197 protected function wasKnownStatementRollbackError() {
198 return isset( $this->lastError
['wasKnownStatementRollbackError'] )
199 ?
$this->lastError
['wasKnownStatementRollbackError']
203 function fieldInfo( $table, $field ) {
207 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
211 function fetchAffectedRowCount() {
215 function getSoftwareLink() {
219 function getServerVersion() {
223 function getServerInfo() {
228 return $this->conn ?
true : false;
231 function ping( &$rtt = null ) {
236 protected function closeConnection() {
240 protected function doQuery( $sql ) {
241 $sql = preg_replace( '< /\* .+? \*/>', '', $sql );
242 $this->addSql( $sql );
244 if ( $this->nextError
) {
245 $this->lastError
= $this->nextError
;
246 $this->nextError
= null;
250 $res = $this->nextResult
;
251 $this->nextResult
= [];
252 $this->lastError
= null;
254 return new FakeResultWrapper( $res );
257 public function unionSupportsOrderAndLimit() {
258 return $this->unionSupportsOrderAndLimit
;
261 public function setUnionSupportsOrderAndLimit( $v ) {
262 $this->unionSupportsOrderAndLimit
= (bool)$v;