Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
blobce05e33a7cdce51765587fa6e81c3a4d34a6643f
1 <?php
3 use Wikimedia\Rdbms\TransactionProfiler;
4 use Wikimedia\Rdbms\DatabaseDomain;
6 /**
7 * Helper for testing the methods from the Database class
8 * @since 1.22
9 */
10 class DatabaseTestHelper extends Database {
12 /**
13 * __CLASS__ of the test suite,
14 * used to determine, if the function name is passed every time to query()
16 protected $testName = [];
18 /**
19 * Array of lastSqls passed to query(),
20 * This is an array since some methods in Database can do more than one
21 * query. Cleared when calling getLastSqls().
23 protected $lastSqls = [];
25 /** @var array List of row arrays */
26 protected $nextResult = [];
28 /**
29 * Array of tables to be considered as existing by tableExist()
30 * Use setExistingTables() to alter.
32 protected $tablesExists;
34 public function __construct( $testName, array $opts = [] ) {
35 $this->testName = $testName;
37 $this->profiler = new ProfilerStub( [] );
38 $this->trxProfiler = new TransactionProfiler();
39 $this->cliMode = isset( $opts['cliMode'] ) ? $opts['cliMode'] : true;
40 $this->connLogger = new \Psr\Log\NullLogger();
41 $this->queryLogger = new \Psr\Log\NullLogger();
42 $this->errorLogger = function ( Exception $e ) {
43 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
45 $this->currentDomain = DatabaseDomain::newUnspecified();
48 /**
49 * Returns SQL queries grouped by '; '
50 * Clear the list of queries that have been done so far.
52 public function getLastSqls() {
53 $lastSqls = implode( '; ', $this->lastSqls );
54 $this->lastSqls = [];
56 return $lastSqls;
59 public function setExistingTables( $tablesExists ) {
60 $this->tablesExists = (array)$tablesExists;
63 /**
64 * @param mixed $res Use an array of row arrays to set row result
66 public function forceNextResult( $res ) {
67 $this->nextResult = $res;
70 protected function addSql( $sql ) {
71 // clean up spaces before and after some words and the whole string
72 $this->lastSqls[] = trim( preg_replace(
73 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
74 ' ', $sql
75 ) );
78 protected function checkFunctionName( $fname ) {
79 if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
80 throw new MWException( 'function name does not start with test class. ' .
81 $fname . ' vs. ' . $this->testName . '. ' .
82 'Please provide __METHOD__ to database methods.' );
86 function strencode( $s ) {
87 // Choose apos to avoid handling of escaping double quotes in quoted text
88 return str_replace( "'", "\'", $s );
91 public function addIdentifierQuotes( $s ) {
92 // no escaping to avoid handling of double quotes in quoted text
93 return $s;
96 public function query( $sql, $fname = '', $tempIgnore = false ) {
97 $this->checkFunctionName( $fname );
98 $this->addSql( $sql );
100 return parent::query( $sql, $fname, $tempIgnore );
103 public function tableExists( $table, $fname = __METHOD__ ) {
104 $tableRaw = $this->tableName( $table, 'raw' );
105 if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
106 return true; // already known to exist
109 $this->checkFunctionName( $fname );
111 return in_array( $table, (array)$this->tablesExists );
114 // Redeclare parent method to make it public
115 public function nativeReplace( $table, $rows, $fname ) {
116 return parent::nativeReplace( $table, $rows, $fname );
119 function getType() {
120 return 'test';
123 function open( $server, $user, $password, $dbName ) {
124 return false;
127 function fetchObject( $res ) {
128 return false;
131 function fetchRow( $res ) {
132 return false;
135 function numRows( $res ) {
136 return -1;
139 function numFields( $res ) {
140 return -1;
143 function fieldName( $res, $n ) {
144 return 'test';
147 function insertId() {
148 return -1;
151 function dataSeek( $res, $row ) {
152 /* nop */
155 function lastErrno() {
156 return -1;
159 function lastError() {
160 return 'test';
163 function fieldInfo( $table, $field ) {
164 return false;
167 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
168 return false;
171 function affectedRows() {
172 return -1;
175 function getSoftwareLink() {
176 return 'test';
179 function getServerVersion() {
180 return 'test';
183 function getServerInfo() {
184 return 'test';
187 function isOpen() {
188 return true;
191 function ping( &$rtt = null ) {
192 $rtt = 0.0;
193 return true;
196 protected function closeConnection() {
197 return false;
200 protected function doQuery( $sql ) {
201 $res = $this->nextResult;
202 $this->nextResult = [];
204 return new FakeResultWrapper( $res );