4 * Helper for testing the methods from the Database class
7 class DatabaseTestHelper
extends Database
{
10 * __CLASS__ of the test suite,
11 * used to determine, if the function name is passed every time to query()
13 protected $testName = [];
16 * Array of lastSqls passed to query(),
17 * This is an array since some methods in Database can do more than one
18 * query. Cleared when calling getLastSqls().
20 protected $lastSqls = [];
22 /** @var array List of row arrays */
23 protected $nextResult = [];
26 * Array of tables to be considered as existing by tableExist()
27 * Use setExistingTables() to alter.
29 protected $tablesExists;
31 public function __construct( $testName, array $opts = [] ) {
32 $this->testName
= $testName;
34 $this->profiler
= new ProfilerStub( [] );
35 $this->trxProfiler
= new TransactionProfiler();
36 $this->cliMode
= isset( $opts['cliMode'] ) ?
$opts['cliMode'] : true;
37 $this->connLogger
= new \Psr\Log\
NullLogger();
38 $this->queryLogger
= new \Psr\Log\
NullLogger();
39 $this->errorLogger
= function ( Exception
$e ) {
40 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
42 $this->currentDomain
= DatabaseDomain
::newUnspecified();
46 * Returns SQL queries grouped by '; '
47 * Clear the list of queries that have been done so far.
49 public function getLastSqls() {
50 $lastSqls = implode( '; ', $this->lastSqls
);
56 public function setExistingTables( $tablesExists ) {
57 $this->tablesExists
= (array)$tablesExists;
61 * @param mixed $res Use an array of row arrays to set row result
63 public function forceNextResult( $res ) {
64 $this->nextResult
= $res;
67 protected function addSql( $sql ) {
68 // clean up spaces before and after some words and the whole string
69 $this->lastSqls
[] = trim( preg_replace(
70 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
75 protected function checkFunctionName( $fname ) {
76 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
77 throw new MWException( 'function name does not start with test class. ' .
78 $fname . ' vs. ' . $this->testName
. '. ' .
79 'Please provide __METHOD__ to database methods.' );
83 function strencode( $s ) {
84 // Choose apos to avoid handling of escaping double quotes in quoted text
85 return str_replace( "'", "\'", $s );
88 public function addIdentifierQuotes( $s ) {
89 // no escaping to avoid handling of double quotes in quoted text
93 public function query( $sql, $fname = '', $tempIgnore = false ) {
94 $this->checkFunctionName( $fname );
95 $this->addSql( $sql );
97 return parent
::query( $sql, $fname, $tempIgnore );
100 public function tableExists( $table, $fname = __METHOD__
) {
101 $tableRaw = $this->tableName( $table, 'raw' );
102 if ( isset( $this->mSessionTempTables
[$tableRaw] ) ) {
103 return true; // already known to exist
106 $this->checkFunctionName( $fname );
108 return in_array( $table, (array)$this->tablesExists
);
111 // Redeclare parent method to make it public
112 public function nativeReplace( $table, $rows, $fname ) {
113 return parent
::nativeReplace( $table, $rows, $fname );
120 function open( $server, $user, $password, $dbName ) {
124 function fetchObject( $res ) {
128 function fetchRow( $res ) {
132 function numRows( $res ) {
136 function numFields( $res ) {
140 function fieldName( $res, $n ) {
144 function insertId() {
148 function dataSeek( $res, $row ) {
152 function lastErrno() {
156 function lastError() {
160 function fieldInfo( $table, $field ) {
164 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
168 function affectedRows() {
172 function getSoftwareLink() {
176 function getServerVersion() {
180 function getServerInfo() {
188 function ping( &$rtt = null ) {
193 protected function closeConnection() {
197 protected function doQuery( $sql ) {
198 $res = $this->nextResult
;
199 $this->nextResult
= [];
201 return new FakeResultWrapper( $res );