MediaWiki  1.29.2
DatabaseTestHelper.php
Go to the documentation of this file.
1 <?php
2 
5 
10 class DatabaseTestHelper extends Database {
11 
16  protected $testName = [];
17 
23  protected $lastSqls = [];
24 
26  protected $nextResult = [];
27 
32  protected $tablesExists;
33 
34  public function __construct( $testName, array $opts = [] ) {
35  $this->testName = $testName;
36 
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()}" );
44  };
45  $this->currentDomain = DatabaseDomain::newUnspecified();
46  }
47 
52  public function getLastSqls() {
53  $lastSqls = implode( '; ', $this->lastSqls );
54  $this->lastSqls = [];
55 
56  return $lastSqls;
57  }
58 
59  public function setExistingTables( $tablesExists ) {
60  $this->tablesExists = (array)$tablesExists;
61  }
62 
66  public function forceNextResult( $res ) {
67  $this->nextResult = $res;
68  }
69 
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  ) );
76  }
77 
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.' );
83  }
84  }
85 
86  function strencode( $s ) {
87  // Choose apos to avoid handling of escaping double quotes in quoted text
88  return str_replace( "'", "\'", $s );
89  }
90 
91  public function addIdentifierQuotes( $s ) {
92  // no escaping to avoid handling of double quotes in quoted text
93  return $s;
94  }
95 
96  public function query( $sql, $fname = '', $tempIgnore = false ) {
97  $this->checkFunctionName( $fname );
98  $this->addSql( $sql );
99 
100  return parent::query( $sql, $fname, $tempIgnore );
101  }
102 
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
107  }
108 
109  $this->checkFunctionName( $fname );
110 
111  return in_array( $table, (array)$this->tablesExists );
112  }
113 
114  // Redeclare parent method to make it public
115  public function nativeReplace( $table, $rows, $fname ) {
116  return parent::nativeReplace( $table, $rows, $fname );
117  }
118 
119  function getType() {
120  return 'test';
121  }
122 
123  function open( $server, $user, $password, $dbName ) {
124  return false;
125  }
126 
127  function fetchObject( $res ) {
128  return false;
129  }
130 
131  function fetchRow( $res ) {
132  return false;
133  }
134 
135  function numRows( $res ) {
136  return -1;
137  }
138 
139  function numFields( $res ) {
140  return -1;
141  }
142 
143  function fieldName( $res, $n ) {
144  return 'test';
145  }
146 
147  function insertId() {
148  return -1;
149  }
150 
151  function dataSeek( $res, $row ) {
152  /* nop */
153  }
154 
155  function lastErrno() {
156  return -1;
157  }
158 
159  function lastError() {
160  return 'test';
161  }
162 
163  function fieldInfo( $table, $field ) {
164  return false;
165  }
166 
167  function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
168  return false;
169  }
170 
171  function affectedRows() {
172  return -1;
173  }
174 
175  function getSoftwareLink() {
176  return 'test';
177  }
178 
179  function getServerVersion() {
180  return 'test';
181  }
182 
183  function getServerInfo() {
184  return 'test';
185  }
186 
187  function isOpen() {
188  return true;
189  }
190 
191  function ping( &$rtt = null ) {
192  $rtt = 0.0;
193  return true;
194  }
195 
196  protected function closeConnection() {
197  return false;
198  }
199 
200  protected function doQuery( $sql ) {
202  $this->nextResult = [];
203 
204  return new FakeResultWrapper( $res );
205  }
206 }
DatabaseTestHelper\$testName
$testName
CLASS of the test suite, used to determine, if the function name is passed every time to query()
Definition: DatabaseTestHelper.php:16
DatabaseTestHelper\getType
getType()
Definition: DatabaseTestHelper.php:119
query
For a write query
Definition: database.txt:26
DatabaseTestHelper\isOpen
isOpen()
Definition: DatabaseTestHelper.php:187
DatabaseTestHelper\numFields
numFields( $res)
Definition: DatabaseTestHelper.php:139
DatabaseTestHelper\fieldInfo
fieldInfo( $table, $field)
Definition: DatabaseTestHelper.php:163
DatabaseTestHelper\getLastSqls
getLastSqls()
Returns SQL queries grouped by '; ' Clear the list of queries that have been done so far.
Definition: DatabaseTestHelper.php:52
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$user
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 account $user
Definition: hooks.txt:246
ProfilerStub
Stub profiler that does nothing.
Definition: ProfilerStub.php:29
$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
$s
$s
Definition: mergeMessageFileList.php:188
$res
$res
Definition: database.txt:21
DatabaseTestHelper\doQuery
doQuery( $sql)
Definition: DatabaseTestHelper.php:200
php
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
DatabaseTestHelper\nativeReplace
nativeReplace( $table, $rows, $fname)
Definition: DatabaseTestHelper.php:115
DatabaseTestHelper\$nextResult
array $nextResult
List of row arrays.
Definition: DatabaseTestHelper.php:26
DatabaseTestHelper\fetchRow
fetchRow( $res)
Definition: DatabaseTestHelper.php:131
DatabaseTestHelper\tableExists
tableExists( $table, $fname=__METHOD__)
Definition: DatabaseTestHelper.php:103
DatabaseTestHelper\getSoftwareLink
getSoftwareLink()
Definition: DatabaseTestHelper.php:175
MWException
MediaWiki exception.
Definition: MWException.php:26
DatabaseTestHelper\strencode
strencode( $s)
Definition: DatabaseTestHelper.php:86
tableName
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like please read the documentation for tableName() and addQuotes(). You will need both of them. ------------------------------------------------------------------------ Basic query optimisation ------------------------------------------------------------------------ MediaWiki developers who need to write DB queries should have some understanding of databases and the performance issues associated with them. Patches containing unacceptably slow features will not be accepted. Unindexed queries are generally not welcome in MediaWiki
DatabaseTestHelper\lastError
lastError()
Definition: DatabaseTestHelper.php:159
DatabaseTestHelper\$tablesExists
$tablesExists
Array of tables to be considered as existing by tableExist() Use setExistingTables() to alter.
Definition: DatabaseTestHelper.php:32
DatabaseTestHelper\setExistingTables
setExistingTables( $tablesExists)
Definition: DatabaseTestHelper.php:59
DatabaseTestHelper\query
query( $sql, $fname='', $tempIgnore=false)
Definition: DatabaseTestHelper.php:96
DatabaseTestHelper\ping
ping(&$rtt=null)
Definition: DatabaseTestHelper.php:191
DatabaseTestHelper\numRows
numRows( $res)
Definition: DatabaseTestHelper.php:135
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
DatabaseTestHelper\getServerInfo
getServerInfo()
Definition: DatabaseTestHelper.php:183
DatabaseTestHelper\getServerVersion
getServerVersion()
Definition: DatabaseTestHelper.php:179
DatabaseTestHelper\addIdentifierQuotes
addIdentifierQuotes( $s)
Definition: DatabaseTestHelper.php:91
DatabaseTestHelper\insertId
insertId()
Definition: DatabaseTestHelper.php:147
DatabaseTestHelper\checkFunctionName
checkFunctionName( $fname)
Definition: DatabaseTestHelper.php:78
DatabaseTestHelper\dataSeek
dataSeek( $res, $row)
Definition: DatabaseTestHelper.php:151
DatabaseTestHelper
Helper for testing the methods from the Database class.
Definition: DatabaseTestHelper.php:10
DatabaseTestHelper\fieldName
fieldName( $res, $n)
Definition: DatabaseTestHelper.php:143
DatabaseTestHelper\addSql
addSql( $sql)
Definition: DatabaseTestHelper.php:70
DatabaseTestHelper\indexInfo
indexInfo( $table, $index, $fname='Database::indexInfo')
Definition: DatabaseTestHelper.php:167
DatabaseTestHelper\$lastSqls
$lastSqls
Array of lastSqls passed to query(), This is an array since some methods in Database can do more than...
Definition: DatabaseTestHelper.php:23
DatabaseTestHelper\forceNextResult
forceNextResult( $res)
Definition: DatabaseTestHelper.php:66
DatabaseTestHelper\open
open( $server, $user, $password, $dbName)
Definition: DatabaseTestHelper.php:123
DatabaseTestHelper\closeConnection
closeConnection()
Definition: DatabaseTestHelper.php:196
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1142
DatabaseTestHelper\affectedRows
affectedRows()
Definition: DatabaseTestHelper.php:171
DatabaseTestHelper\fetchObject
fetchObject( $res)
Definition: DatabaseTestHelper.php:127
DatabaseTestHelper\__construct
__construct( $testName, array $opts=[])
Definition: DatabaseTestHelper.php:34
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
Wikimedia\Rdbms\TransactionProfiler
Helper class that detects high-contention DB queries via profiling calls.
Definition: TransactionProfiler.php:39
DatabaseTestHelper\lastErrno
lastErrno()
Definition: DatabaseTestHelper.php:155
array
the array() calling protocol came about after MediaWiki 1.4rc1.