MediaWiki  REL1_22
DatabaseTestHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class DatabaseTestHelper extends DatabaseBase {
00008 
00013     protected $testName = array();
00014 
00020     protected $lastSqls = array();
00021 
00026     protected $tablesExists;
00027 
00028     public function __construct( $testName ) {
00029         $this->testName = $testName;
00030     }
00031 
00036     public function getLastSqls() {
00037         $lastSqls = implode( '; ', $this->lastSqls );
00038         $this->lastSqls = array();
00039 
00040         return $lastSqls;
00041     }
00042 
00043     public function setExistingTables( $tablesExists ) {
00044         $this->tablesExists = (array)$tablesExists;
00045     }
00046 
00047     protected function addSql( $sql ) {
00048         // clean up spaces before and after some words and the whole string
00049         $this->lastSqls[] = trim( preg_replace(
00050             '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
00051             ' ', $sql
00052         ) );
00053     }
00054 
00055     protected function checkFunctionName( $fname ) {
00056         if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
00057             throw new MWException( 'function name does not start with test class. ' .
00058                 $fname . ' vs. ' . $this->testName . '. ' .
00059                 'Please provide __METHOD__ to database methods.' );
00060         }
00061     }
00062 
00063     function strencode( $s ) {
00064         // Choose apos to avoid handling of escaping double quotes in quoted text
00065         return str_replace( "'", "\'", $s );
00066     }
00067 
00068     public function addIdentifierQuotes( $s ) {
00069         // no escaping to avoid handling of double quotes in quoted text
00070         return $s;
00071     }
00072 
00073     public function query( $sql, $fname = '', $tempIgnore = false ) {
00074         $this->checkFunctionName( $fname );
00075         $this->addSql( $sql );
00076 
00077         return parent::query( $sql, $fname, $tempIgnore );
00078     }
00079 
00080     public function tableExists( $table, $fname = __METHOD__ ) {
00081         $this->checkFunctionName( $fname );
00082 
00083         return in_array( $table, (array)$this->tablesExists );
00084     }
00085 
00086     // Redeclare parent method to make it public
00087     public function nativeReplace( $table, $rows, $fname ) {
00088         return parent::nativeReplace( $table, $rows, $fname );
00089     }
00090 
00091     function getType() {
00092         return 'test';
00093     }
00094 
00095     function open( $server, $user, $password, $dbName ) {
00096         return false;
00097     }
00098 
00099     function fetchObject( $res ) {
00100         return false;
00101     }
00102 
00103     function fetchRow( $res ) {
00104         return false;
00105     }
00106 
00107     function numRows( $res ) {
00108         return -1;
00109     }
00110 
00111     function numFields( $res ) {
00112         return -1;
00113     }
00114 
00115     function fieldName( $res, $n ) {
00116         return 'test';
00117     }
00118 
00119     function insertId() {
00120         return -1;
00121     }
00122 
00123     function dataSeek( $res, $row ) {
00124         /* nop */
00125     }
00126 
00127     function lastErrno() {
00128         return -1;
00129     }
00130 
00131     function lastError() {
00132         return 'test';
00133     }
00134 
00135     function fieldInfo( $table, $field ) {
00136         return false;
00137     }
00138 
00139     function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
00140         return false;
00141     }
00142 
00143     function affectedRows() {
00144         return -1;
00145     }
00146 
00147     function getSoftwareLink() {
00148         return 'test';
00149     }
00150 
00151     function getServerVersion() {
00152         return 'test';
00153     }
00154 
00155     function getServerInfo() {
00156         return 'test';
00157     }
00158 
00159     protected function closeConnection() {
00160         return false;
00161     }
00162 
00163     protected function doQuery( $sql ) {
00164         return array();
00165     }
00166 }