d6ca5964b8fc41fd19750e89e62756cd5bf290cd
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 = [];
23 * Array of tables to be considered as existing by tableExist()
24 * Use setExistingTables() to alter.
26 protected $tablesExists;
28 public function __construct( $testName ) {
29 $this->testName
= $testName;
31 $this->profiler
= new ProfilerStub( [] );
32 $this->trxProfiler
= new TransactionProfiler();
36 * Returns SQL queries grouped by '; '
37 * Clear the list of queries that have been done so far.
39 public function getLastSqls() {
40 $lastSqls = implode( '; ', $this->lastSqls
);
46 public function setExistingTables( $tablesExists ) {
47 $this->tablesExists
= (array)$tablesExists;
50 protected function addSql( $sql ) {
51 // clean up spaces before and after some words and the whole string
52 $this->lastSqls
[] = trim( preg_replace(
53 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
58 protected function checkFunctionName( $fname ) {
59 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
60 throw new MWException( 'function name does not start with test class. ' .
61 $fname . ' vs. ' . $this->testName
. '. ' .
62 'Please provide __METHOD__ to database methods.' );
66 function strencode( $s ) {
67 // Choose apos to avoid handling of escaping double quotes in quoted text
68 return str_replace( "'", "\'", $s );
71 public function addIdentifierQuotes( $s ) {
72 // no escaping to avoid handling of double quotes in quoted text
76 public function query( $sql, $fname = '', $tempIgnore = false ) {
77 $this->checkFunctionName( $fname );
78 $this->addSql( $sql );
80 return parent
::query( $sql, $fname, $tempIgnore );
83 public function tableExists( $table, $fname = __METHOD__
) {
84 $this->checkFunctionName( $fname );
86 return in_array( $table, (array)$this->tablesExists
);
89 // Redeclare parent method to make it public
90 public function nativeReplace( $table, $rows, $fname ) {
91 return parent
::nativeReplace( $table, $rows, $fname );
98 function open( $server, $user, $password, $dbName ) {
102 function fetchObject( $res ) {
106 function fetchRow( $res ) {
110 function numRows( $res ) {
114 function numFields( $res ) {
118 function fieldName( $res, $n ) {
122 function insertId() {
126 function dataSeek( $res, $row ) {
130 function lastErrno() {
134 function lastError() {
138 function fieldInfo( $table, $field ) {
142 function indexInfo( $table, $index, $fname = 'DatabaseBase::indexInfo' ) {
146 function affectedRows() {
150 function getSoftwareLink() {
154 function getServerVersion() {
158 function getServerInfo() {
166 protected function closeConnection() {
170 protected function doQuery( $sql ) {