3 use Psr\Log\NullLogger
;
4 use Wikimedia\Rdbms\TransactionProfiler
;
5 use Wikimedia\Rdbms\DatabaseDomain
;
6 use Wikimedia\Rdbms\Database
;
9 * Helper for testing the methods from the Database class
12 class DatabaseTestHelper
extends Database
{
15 * __CLASS__ of the test suite,
16 * used to determine, if the function name is passed every time to query()
18 protected $testName = [];
21 * Array of lastSqls passed to query(),
22 * This is an array since some methods in Database can do more than one
23 * query. Cleared when calling getLastSqls().
25 protected $lastSqls = [];
27 /** @var array List of row arrays */
28 protected $nextResult = [];
30 /** @var array|null */
31 protected $nextError = null;
32 /** @var array|null */
33 protected $lastError = null;
36 * Array of tables to be considered as existing by tableExist()
37 * Use setExistingTables() to alter.
39 protected $tablesExists;
42 * Value to return from unionSupportsOrderAndLimit()
44 protected $unionSupportsOrderAndLimit = true;
46 public function __construct( $testName, array $opts = [] ) {
47 parent
::__construct( $opts +
[
55 'cliMode' => $opts['cliMode'] ??
true,
57 'srvCache' => new HashBagOStuff(),
59 'trxProfiler' => new TransactionProfiler(),
60 'connLogger' => new NullLogger(),
61 'queryLogger' => new NullLogger(),
62 'errorLogger' => function ( Exception
$e ) {
63 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
65 'deprecationLogger' => function ( $msg ) {
70 $this->testName
= $testName;
72 $this->currentDomain
= DatabaseDomain
::newUnspecified();
73 $this->open( 'localhost', 'testuser', 'password', 'testdb', null, '' );
77 * Returns SQL queries grouped by '; '
78 * Clear the list of queries that have been done so far.
81 public function getLastSqls() {
82 $lastSqls = implode( '; ', $this->lastSqls
);
88 public function setExistingTables( $tablesExists ) {
89 $this->tablesExists
= (array)$tablesExists;
93 * @param mixed $res Use an array of row arrays to set row result
95 public function forceNextResult( $res ) {
96 $this->nextResult
= $res;
100 * @param int $errno Error number
101 * @param string $error Error text
102 * @param array $options
103 * - wasKnownStatementRollbackError: Return value for wasKnownStatementRollbackError()
105 public function forceNextQueryError( $errno, $error, $options = [] ) {
106 $this->nextError
= [ 'errno' => $errno, 'error' => $error ] +
$options;
109 protected function addSql( $sql ) {
110 // clean up spaces before and after some words and the whole string
111 $this->lastSqls
[] = trim( preg_replace(
112 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
117 protected function checkFunctionName( $fname ) {
118 if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
119 return; // no $fname parameter
122 // Handle some internal calls from the Database class
125 '/^Wikimedia\\\\Rdbms\\\\Database::(?:query|beginIfImplied) \((.+)\)$/',
132 if ( substr( $check, 0, strlen( $this->testName
) ) !== $this->testName
) {
133 throw new MWException( 'function name does not start with test class. ' .
134 $fname . ' vs. ' . $this->testName
. '. ' .
135 'Please provide __METHOD__ to database methods.' );
139 function strencode( $s ) {
140 // Choose apos to avoid handling of escaping double quotes in quoted text
141 return str_replace( "'", "\'", $s );
144 public function addIdentifierQuotes( $s ) {
145 // no escaping to avoid handling of double quotes in quoted text
149 public function query( $sql, $fname = '', $flags = 0 ) {
150 $this->checkFunctionName( $fname );
152 return parent
::query( $sql, $fname, $flags );
155 public function tableExists( $table, $fname = __METHOD__
) {
156 $tableRaw = $this->tableName( $table, 'raw' );
157 if ( isset( $this->sessionTempTables
[$tableRaw] ) ) {
158 return true; // already known to exist
161 $this->checkFunctionName( $fname );
163 return in_array( $table, (array)$this->tablesExists
);
166 // Redeclare parent method to make it public
167 public function nativeReplace( $table, $rows, $fname ) {
168 parent
::nativeReplace( $table, $rows, $fname );
175 function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
176 $this->conn
= (object)[ 'test' ];
181 function fetchObject( $res ) {
185 function fetchRow( $res ) {
189 function numRows( $res ) {
193 function numFields( $res ) {
197 function fieldName( $res, $n ) {
201 function insertId() {
205 function dataSeek( $res, $row ) {
209 function lastErrno() {
210 return $this->lastError ?
$this->lastError
['errno'] : -1;
213 function lastError() {
214 return $this->lastError ?
$this->lastError
['error'] : 'test';
217 protected function wasKnownStatementRollbackError() {
218 return $this->lastError
['wasKnownStatementRollbackError'] ??
false;
221 function fieldInfo( $table, $field ) {
225 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
229 function fetchAffectedRowCount() {
233 function getSoftwareLink() {
237 function getServerVersion() {
241 function getServerInfo() {
245 function ping( &$rtt = null ) {
250 protected function closeConnection() {
254 protected function doQuery( $sql ) {
255 $sql = preg_replace( '< /\* .+? \*/>', '', $sql );
256 $this->addSql( $sql );
258 if ( $this->nextError
) {
259 $this->lastError
= $this->nextError
;
260 $this->nextError
= null;
264 $res = $this->nextResult
;
265 $this->nextResult
= [];
266 $this->lastError
= null;
268 return new FakeResultWrapper( $res );
271 public function unionSupportsOrderAndLimit() {
272 return $this->unionSupportsOrderAndLimit
;
275 public function setUnionSupportsOrderAndLimit( $v ) {
276 $this->unionSupportsOrderAndLimit
= (bool)$v;