MediaWiki  1.28.0
DatabaseTestHelper.php
Go to the documentation of this file.
1 <?php
2 
7 class DatabaseTestHelper extends Database {
8 
13  protected $testName = [];
14 
20  protected $lastSqls = [];
21 
23  protected $nextResult = [];
24 
29  protected $tablesExists;
30 
31  public function __construct( $testName, array $opts = [] ) {
32  $this->testName = $testName;
33 
34  $this->profiler = new ProfilerStub( [] );
35  $this->trxProfiler = new TransactionProfiler();
36  $this->cliMode = isset( $opts['cliMode'] ) ? $opts['cliMode'] : true;
37  $this->connLogger = new \Psr\Log\NullLogger();
38  $this->queryLogger = new \Psr\Log\NullLogger();
39  $this->errorLogger = function ( Exception $e ) {
40  wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
41  };
42  $this->currentDomain = DatabaseDomain::newUnspecified();
43  }
44 
49  public function getLastSqls() {
50  $lastSqls = implode( '; ', $this->lastSqls );
51  $this->lastSqls = [];
52 
53  return $lastSqls;
54  }
55 
56  public function setExistingTables( $tablesExists ) {
57  $this->tablesExists = (array)$tablesExists;
58  }
59 
63  public function forceNextResult( $res ) {
64  $this->nextResult = $res;
65  }
66 
67  protected function addSql( $sql ) {
68  // clean up spaces before and after some words and the whole string
69  $this->lastSqls[] = trim( preg_replace(
70  '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
71  ' ', $sql
72  ) );
73  }
74 
75  protected function checkFunctionName( $fname ) {
76  if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
77  throw new MWException( 'function name does not start with test class. ' .
78  $fname . ' vs. ' . $this->testName . '. ' .
79  'Please provide __METHOD__ to database methods.' );
80  }
81  }
82 
83  function strencode( $s ) {
84  // Choose apos to avoid handling of escaping double quotes in quoted text
85  return str_replace( "'", "\'", $s );
86  }
87 
88  public function addIdentifierQuotes( $s ) {
89  // no escaping to avoid handling of double quotes in quoted text
90  return $s;
91  }
92 
93  public function query( $sql, $fname = '', $tempIgnore = false ) {
94  $this->checkFunctionName( $fname );
95  $this->addSql( $sql );
96 
97  return parent::query( $sql, $fname, $tempIgnore );
98  }
99 
100  public function tableExists( $table, $fname = __METHOD__ ) {
101  $tableRaw = $this->tableName( $table, 'raw' );
102  if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
103  return true; // already known to exist
104  }
105 
106  $this->checkFunctionName( $fname );
107 
108  return in_array( $table, (array)$this->tablesExists );
109  }
110 
111  // Redeclare parent method to make it public
112  public function nativeReplace( $table, $rows, $fname ) {
113  return parent::nativeReplace( $table, $rows, $fname );
114  }
115 
116  function getType() {
117  return 'test';
118  }
119 
120  function open( $server, $user, $password, $dbName ) {
121  return false;
122  }
123 
124  function fetchObject( $res ) {
125  return false;
126  }
127 
128  function fetchRow( $res ) {
129  return false;
130  }
131 
132  function numRows( $res ) {
133  return -1;
134  }
135 
136  function numFields( $res ) {
137  return -1;
138  }
139 
140  function fieldName( $res, $n ) {
141  return 'test';
142  }
143 
144  function insertId() {
145  return -1;
146  }
147 
148  function dataSeek( $res, $row ) {
149  /* nop */
150  }
151 
152  function lastErrno() {
153  return -1;
154  }
155 
156  function lastError() {
157  return 'test';
158  }
159 
160  function fieldInfo( $table, $field ) {
161  return false;
162  }
163 
164  function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
165  return false;
166  }
167 
168  function affectedRows() {
169  return -1;
170  }
171 
172  function getSoftwareLink() {
173  return 'test';
174  }
175 
176  function getServerVersion() {
177  return 'test';
178  }
179 
180  function getServerInfo() {
181  return 'test';
182  }
183 
184  function isOpen() {
185  return true;
186  }
187 
188  function ping( &$rtt = null ) {
189  $rtt = 0.0;
190  return true;
191  }
192 
193  protected function closeConnection() {
194  return false;
195  }
196 
197  protected function doQuery( $sql ) {
199  $this->nextResult = [];
200 
201  return new FakeResultWrapper( $res );
202  }
203 }
dataSeek($res, $row)
Change the position of the cursor in a result object.
fetchRow($res)
Fetch the next row from the given result object, in associative array form.
the array() calling protocol came about after MediaWiki 1.4rc1.
ping(&$rtt=null)
Ping the server and try to reconnect if it there is no connection.
static newUnspecified()
fetchObject($res)
Fetch the next row from the given result object, in object form.
insertId()
Get the inserted value of an auto-increment row.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
getServerVersion()
A string describing the current software version, like from mysql_get_server_info().
$testName
CLASS of the test suite, used to determine, if the function name is passed every time to query() ...
fieldInfo($table, $field)
mysql_fetch_field() wrapper Returns false if the field doesn't exist
getType()
Get the type of the DBMS, as it appears in $wgDBtype.
$lastSqls
Array of lastSqls passed to query(), This is an array since some methods in Database can do more than...
Stub profiler that does nothing.
open($server, $user, $password, $dbName)
Open a connection to the database.
tableName($name, $format= 'quoted')
Format a table name ready for use in constructing an SQL query.
Definition: Database.php:1693
isOpen()
Is a connection to the database open?
__construct($testName, array $opts=[])
wfWarn($msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
affectedRows()
Get the number of rows affected by the last write query.
Helper class that detects high-contention DB queries via profiling calls.
$res
Definition: database.txt:21
Helper for testing the methods from the Database class.
numRows($res)
Get the number of rows in a result object.
setExistingTables($tablesExists)
array $nextResult
List of row arrays.
tableExists($table, $fname=__METHOD__)
Query whether a given table exists.
lastErrno()
Get the last error number.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
nativeReplace($table, $rows, $fname)
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined...
Definition: Setup.php:36
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
getServerInfo()
A string describing the current software version, and possibly other details in a user-friendly way...
indexInfo($table, $index, $fname= 'Database::indexInfo')
numFields($res)
Get the number of fields in a result object.
getSoftwareLink()
Returns a wikitext link to the DB's website, e.g., return "[http://www.mysql.com/ MySQL]"; Should at ...
query($sql, $fname= '', $tempIgnore=false)
Run an SQL query and return the result.
$tablesExists
Array of tables to be considered as existing by tableExist() Use setExistingTables() to alter...
For a write query
Definition: database.txt:26
getLastSqls()
Returns SQL queries grouped by '; ' Clear the list of queries that have been done so far...
lastError()
Get a description of the last error.
fieldName($res, $n)
Get a field name in a result object.