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();
42 * Returns SQL queries grouped by '; '
43 * Clear the list of queries that have been done so far.
45 public function getLastSqls() {
46 $lastSqls = implode( '; ', $this->lastSqls
);
52 public function setExistingTables( $tablesExists ) {
53 $this->tablesExists
= (array)$tablesExists;
57 * @param mixed $res Use an array of row arrays to set row result
59 public function forceNextResult( $res ) {
60 $this->nextResult
= $res;
63 protected function addSql( $sql ) {
64 // clean up spaces before and after some words and the whole string
65 $this->lastSqls
[] = trim( preg_replace(
66 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
71 protected function checkFunctionName( $fname ) {
72 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
73 throw new MWException( 'function name does not start with test class. ' .
74 $fname . ' vs. ' . $this->testName
. '. ' .
75 'Please provide __METHOD__ to database methods.' );
79 function strencode( $s ) {
80 // Choose apos to avoid handling of escaping double quotes in quoted text
81 return str_replace( "'", "\'", $s );
84 public function addIdentifierQuotes( $s ) {
85 // no escaping to avoid handling of double quotes in quoted text
89 public function query( $sql, $fname = '', $tempIgnore = false ) {
90 $this->checkFunctionName( $fname );
91 $this->addSql( $sql );
93 return parent
::query( $sql, $fname, $tempIgnore );
96 public function tableExists( $table, $fname = __METHOD__
) {
97 $this->checkFunctionName( $fname );
99 return in_array( $table, (array)$this->tablesExists
);
102 // Redeclare parent method to make it public
103 public function nativeReplace( $table, $rows, $fname ) {
104 return parent
::nativeReplace( $table, $rows, $fname );
111 function open( $server, $user, $password, $dbName ) {
115 function fetchObject( $res ) {
119 function fetchRow( $res ) {
123 function numRows( $res ) {
127 function numFields( $res ) {
131 function fieldName( $res, $n ) {
135 function insertId() {
139 function dataSeek( $res, $row ) {
143 function lastErrno() {
147 function lastError() {
151 function fieldInfo( $table, $field ) {
155 function indexInfo( $table, $index, $fname = 'DatabaseBase::indexInfo' ) {
159 function affectedRows() {
163 function getSoftwareLink() {
167 function getServerVersion() {
171 function getServerInfo() {
179 function ping( &$rtt = null ) {
184 protected function closeConnection() {
188 protected function doQuery( $sql ) {
189 $res = $this->nextResult
;
190 $this->nextResult
= [];
192 return new FakeResultWrapper( $res );