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();
42 public function setExistingTables( $tablesExists ) {
43 $this->tablesExists
= (array)$tablesExists;
46 protected function addSql( $sql ) {
47 // clean up spaces before and after some words and the whole string
48 $this->lastSqls
[] = trim( preg_replace(
49 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
54 protected function checkFunctionName( $fname ) {
55 if ( substr( $fname, 0, strlen( $this->testName
) ) !== $this->testName
) {
56 throw new MWException( 'function name does not start with test class. ' .
57 $fname . ' vs. ' . $this->testName
. '. ' .
58 'Please provide __METHOD__ to database methods.' );
62 function strencode( $s ) {
63 // Choose apos to avoid handling of escaping double quotes in quoted text
64 return str_replace( "'", "\'", $s );
67 public function addIdentifierQuotes( $s ) {
68 // no escaping to avoid handling of double quotes in quoted text
72 public function query( $sql, $fname = '', $tempIgnore = false ) {
73 $this->checkFunctionName( $fname );
74 $this->addSql( $sql );
76 return parent
::query( $sql, $fname, $tempIgnore );
79 public function tableExists( $table, $fname = __METHOD__
) {
80 $this->checkFunctionName( $fname );
81 return in_array( $table, (array)$this->tablesExists
);
84 // Redeclare parent method to make it public
85 public function nativeReplace( $table, $rows, $fname ) {
86 return parent
::nativeReplace( $table, $rows, $fname );
93 function open( $server, $user, $password, $dbName ) {
97 function fetchObject( $res ) {
101 function fetchRow( $res ) {
105 function numRows( $res ) {
109 function numFields( $res ) {
113 function fieldName( $res, $n ) {
117 function insertId() {
121 function dataSeek( $res, $row ) {
125 function lastErrno() {
129 function lastError() {
133 function fieldInfo( $table, $field ) {
137 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
141 function affectedRows() {
145 static function getSoftwareLink() {
149 function getServerVersion() {
153 function getServerInfo() {
157 protected function closeConnection() {
161 protected function doQuery( $sql ) {