3 use Wikimedia\Rdbms\TransactionProfiler
;
4 use Wikimedia\Rdbms\DatabaseDomain
;
7 * Helper for testing the methods from the Database class
10 class DatabaseTestHelper
extends Database
{
13 * __CLASS__ of the test suite,
14 * used to determine, if the function name is passed every time to query()
16 protected $testName = [];
19 * Array of lastSqls passed to query(),
20 * This is an array since some methods in Database can do more than one
21 * query. Cleared when calling getLastSqls().
23 protected $lastSqls = [];
25 /** @var array List of row arrays */
26 protected $nextResult = [];
29 * Array of tables to be considered as existing by tableExist()
30 * Use setExistingTables() to alter.
32 protected $tablesExists;
34 public function __construct( $testName, array $opts = [] ) {
35 $this->testName
= $testName;
37 $this->profiler
= new ProfilerStub( [] );
38 $this->trxProfiler
= new TransactionProfiler();
39 $this->cliMode
= isset( $opts['cliMode'] ) ?
$opts['cliMode'] : true;
40 $this->connLogger
= new \Psr\Log\
NullLogger();
41 $this->queryLogger
= new \Psr\Log\
NullLogger();
42 $this->errorLogger
= function ( Exception
$e ) {
43 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
45 $this->currentDomain
= DatabaseDomain
::newUnspecified();
49 * Returns SQL queries grouped by '; '
50 * Clear the list of queries that have been done so far.
52 public function getLastSqls() {
53 $lastSqls = implode( '; ', $this->lastSqls
);
59 public function setExistingTables( $tablesExists ) {
60 $this->tablesExists
= (array)$tablesExists;
64 * @param mixed $res Use an array of row arrays to set row result
66 public function forceNextResult( $res ) {
67 $this->nextResult
= $res;
70 protected function addSql( $sql ) {
71 // clean up spaces before and after some words and the whole string
72 $this->lastSqls
[] = trim( preg_replace(
73 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
78 protected function checkFunctionName( $fname ) {
79 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
80 throw new MWException( 'function name does not start with test class. ' .
81 $fname . ' vs. ' . $this->testName
. '. ' .
82 'Please provide __METHOD__ to database methods.' );
86 function strencode( $s ) {
87 // Choose apos to avoid handling of escaping double quotes in quoted text
88 return str_replace( "'", "\'", $s );
91 public function addIdentifierQuotes( $s ) {
92 // no escaping to avoid handling of double quotes in quoted text
96 public function query( $sql, $fname = '', $tempIgnore = false ) {
97 $this->checkFunctionName( $fname );
98 $this->addSql( $sql );
100 return parent
::query( $sql, $fname, $tempIgnore );
103 public function tableExists( $table, $fname = __METHOD__
) {
104 $tableRaw = $this->tableName( $table, 'raw' );
105 if ( isset( $this->mSessionTempTables
[$tableRaw] ) ) {
106 return true; // already known to exist
109 $this->checkFunctionName( $fname );
111 return in_array( $table, (array)$this->tablesExists
);
114 // Redeclare parent method to make it public
115 public function nativeReplace( $table, $rows, $fname ) {
116 return parent
::nativeReplace( $table, $rows, $fname );
123 function open( $server, $user, $password, $dbName ) {
127 function fetchObject( $res ) {
131 function fetchRow( $res ) {
135 function numRows( $res ) {
139 function numFields( $res ) {
143 function fieldName( $res, $n ) {
147 function insertId() {
151 function dataSeek( $res, $row ) {
155 function lastErrno() {
159 function lastError() {
163 function fieldInfo( $table, $field ) {
167 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
171 function affectedRows() {
175 function getSoftwareLink() {
179 function getServerVersion() {
183 function getServerInfo() {
191 function ping( &$rtt = null ) {
196 protected function closeConnection() {
200 protected function doQuery( $sql ) {
201 $res = $this->nextResult
;
202 $this->nextResult
= [];
204 return new FakeResultWrapper( $res );