4 * Helper for testing the methods from the DatabaseBase class
7 class DatabaseTestHelper
extends DatabaseBase
{
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 DatabaseBase 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->currentDomain
= DatabaseDomain
::newUnspecified();
43 * Returns SQL queries grouped by '; '
44 * Clear the list of queries that have been done so far.
46 public function getLastSqls() {
47 $lastSqls = implode( '; ', $this->lastSqls
);
53 public function setExistingTables( $tablesExists ) {
54 $this->tablesExists
= (array)$tablesExists;
58 * @param mixed $res Use an array of row arrays to set row result
60 public function forceNextResult( $res ) {
61 $this->nextResult
= $res;
64 protected function addSql( $sql ) {
65 // clean up spaces before and after some words and the whole string
66 $this->lastSqls
[] = trim( preg_replace(
67 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
72 protected function checkFunctionName( $fname ) {
73 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
74 throw new MWException( 'function name does not start with test class. ' .
75 $fname . ' vs. ' . $this->testName
. '. ' .
76 'Please provide __METHOD__ to database methods.' );
80 function strencode( $s ) {
81 // Choose apos to avoid handling of escaping double quotes in quoted text
82 return str_replace( "'", "\'", $s );
85 public function addIdentifierQuotes( $s ) {
86 // no escaping to avoid handling of double quotes in quoted text
90 public function query( $sql, $fname = '', $tempIgnore = false ) {
91 $this->checkFunctionName( $fname );
92 $this->addSql( $sql );
94 return parent
::query( $sql, $fname, $tempIgnore );
97 public function tableExists( $table, $fname = __METHOD__
) {
98 $this->checkFunctionName( $fname );
100 return in_array( $table, (array)$this->tablesExists
);
103 // Redeclare parent method to make it public
104 public function nativeReplace( $table, $rows, $fname ) {
105 return parent
::nativeReplace( $table, $rows, $fname );
112 function open( $server, $user, $password, $dbName ) {
116 function fetchObject( $res ) {
120 function fetchRow( $res ) {
124 function numRows( $res ) {
128 function numFields( $res ) {
132 function fieldName( $res, $n ) {
136 function insertId() {
140 function dataSeek( $res, $row ) {
144 function lastErrno() {
148 function lastError() {
152 function fieldInfo( $table, $field ) {
156 function indexInfo( $table, $index, $fname = 'DatabaseBase::indexInfo' ) {
160 function affectedRows() {
164 function getSoftwareLink() {
168 function getServerVersion() {
172 function getServerInfo() {
180 function ping( &$rtt = null ) {
185 protected function closeConnection() {
189 protected function doQuery( $sql ) {
190 $res = $this->nextResult
;
191 $this->nextResult
= [];
193 return new FakeResultWrapper( $res );