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;
40 * Returns SQL queries grouped by '; '
41 * Clear the list of queries that have been done so far.
43 public function getLastSqls() {
44 $lastSqls = implode( '; ', $this->lastSqls
);
50 public function setExistingTables( $tablesExists ) {
51 $this->tablesExists
= (array)$tablesExists;
55 * @param mixed $res Use an array of row arrays to set row result
57 public function forceNextResult( $res ) {
58 $this->nextResult
= $res;
61 protected function addSql( $sql ) {
62 // clean up spaces before and after some words and the whole string
63 $this->lastSqls
[] = trim( preg_replace(
64 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
69 protected function checkFunctionName( $fname ) {
70 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
71 throw new MWException( 'function name does not start with test class. ' .
72 $fname . ' vs. ' . $this->testName
. '. ' .
73 'Please provide __METHOD__ to database methods.' );
77 function strencode( $s ) {
78 // Choose apos to avoid handling of escaping double quotes in quoted text
79 return str_replace( "'", "\'", $s );
82 public function addIdentifierQuotes( $s ) {
83 // no escaping to avoid handling of double quotes in quoted text
87 public function query( $sql, $fname = '', $tempIgnore = false ) {
88 $this->checkFunctionName( $fname );
89 $this->addSql( $sql );
91 return parent
::query( $sql, $fname, $tempIgnore );
94 public function tableExists( $table, $fname = __METHOD__
) {
95 $this->checkFunctionName( $fname );
97 return in_array( $table, (array)$this->tablesExists
);
100 // Redeclare parent method to make it public
101 public function nativeReplace( $table, $rows, $fname ) {
102 return parent
::nativeReplace( $table, $rows, $fname );
109 function open( $server, $user, $password, $dbName ) {
113 function fetchObject( $res ) {
117 function fetchRow( $res ) {
121 function numRows( $res ) {
125 function numFields( $res ) {
129 function fieldName( $res, $n ) {
133 function insertId() {
137 function dataSeek( $res, $row ) {
141 function lastErrno() {
145 function lastError() {
149 function fieldInfo( $table, $field ) {
153 function indexInfo( $table, $index, $fname = 'DatabaseBase::indexInfo' ) {
157 function affectedRows() {
161 function getSoftwareLink() {
165 function getServerVersion() {
169 function getServerInfo() {
177 function ping( &$rtt = null ) {
182 protected function closeConnection() {
186 protected function doQuery( $sql ) {
187 $res = $this->nextResult
;
188 $this->nextResult
= [];
190 return new FakeResultWrapper( $res );