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 = array();
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 = array();
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;
33 * Returns SQL queries grouped by '; '
34 * Clear the list of queries that have been done so far.
36 public function getLastSqls() {
37 $lastSqls = implode( '; ', $this->lastSqls
);
38 $this->lastSqls
= array();
43 public function setExistingTables( $tablesExists ) {
44 $this->tablesExists
= (array)$tablesExists;
47 protected function addSql( $sql ) {
48 // clean up spaces before and after some words and the whole string
49 $this->lastSqls
[] = trim( preg_replace(
50 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
55 protected function checkFunctionName( $fname ) {
56 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
57 throw new MWException( 'function name does not start with test class. ' .
58 $fname . ' vs. ' . $this->testName
. '. ' .
59 'Please provide __METHOD__ to database methods.' );
63 function strencode( $s ) {
64 // Choose apos to avoid handling of escaping double quotes in quoted text
65 return str_replace( "'", "\'", $s );
68 public function addIdentifierQuotes( $s ) {
69 // no escaping to avoid handling of double quotes in quoted text
73 public function query( $sql, $fname = '', $tempIgnore = false ) {
74 $this->checkFunctionName( $fname );
75 $this->addSql( $sql );
77 return parent
::query( $sql, $fname, $tempIgnore );
80 public function tableExists( $table, $fname = __METHOD__
) {
81 $this->checkFunctionName( $fname );
83 return in_array( $table, (array)$this->tablesExists
);
86 // Redeclare parent method to make it public
87 public function nativeReplace( $table, $rows, $fname ) {
88 return parent
::nativeReplace( $table, $rows, $fname );
95 function open( $server, $user, $password, $dbName ) {
99 function fetchObject( $res ) {
103 function fetchRow( $res ) {
107 function numRows( $res ) {
111 function numFields( $res ) {
115 function fieldName( $res, $n ) {
119 function insertId() {
123 function dataSeek( $res, $row ) {
127 function lastErrno() {
131 function lastError() {
135 function fieldInfo( $table, $field ) {
139 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
143 function affectedRows() {
147 function getSoftwareLink() {
151 function getServerVersion() {
155 function getServerInfo() {
163 protected function closeConnection() {
167 protected function doQuery( $sql ) {