MediaWiki  1.27.1
DatabaseTestHelper.php
Go to the documentation of this file.
1 <?php
2 
8 
13  protected $testName = [];
14 
20  protected $lastSqls = [];
21 
26  protected $tablesExists;
27 
28  public function __construct( $testName ) {
29  $this->testName = $testName;
30  }
31 
36  public function getLastSqls() {
37  $lastSqls = implode( '; ', $this->lastSqls );
38  $this->lastSqls = [];
39 
40  return $lastSqls;
41  }
42 
43  public function setExistingTables( $tablesExists ) {
44  $this->tablesExists = (array)$tablesExists;
45  }
46 
47  protected function addSql( $sql ) {
48  // clean up spaces before and after some words and the whole string
49  $this->lastSqls[] = trim( preg_replace(
50  '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
51  ' ', $sql
52  ) );
53  }
54 
55  protected function checkFunctionName( $fname ) {
56  if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
57  throw new MWException( 'function name does not start with test class. ' .
58  $fname . ' vs. ' . $this->testName . '. ' .
59  'Please provide __METHOD__ to database methods.' );
60  }
61  }
62 
63  function strencode( $s ) {
64  // Choose apos to avoid handling of escaping double quotes in quoted text
65  return str_replace( "'", "\'", $s );
66  }
67 
68  public function addIdentifierQuotes( $s ) {
69  // no escaping to avoid handling of double quotes in quoted text
70  return $s;
71  }
72 
73  public function query( $sql, $fname = '', $tempIgnore = false ) {
74  $this->checkFunctionName( $fname );
75  $this->addSql( $sql );
76 
77  return parent::query( $sql, $fname, $tempIgnore );
78  }
79 
80  public function tableExists( $table, $fname = __METHOD__ ) {
81  $this->checkFunctionName( $fname );
82 
83  return in_array( $table, (array)$this->tablesExists );
84  }
85 
86  // Redeclare parent method to make it public
87  public function nativeReplace( $table, $rows, $fname ) {
88  return parent::nativeReplace( $table, $rows, $fname );
89  }
90 
91  function getType() {
92  return 'test';
93  }
94 
95  function open( $server, $user, $password, $dbName ) {
96  return false;
97  }
98 
99  function fetchObject( $res ) {
100  return false;
101  }
102 
103  function fetchRow( $res ) {
104  return false;
105  }
106 
107  function numRows( $res ) {
108  return -1;
109  }
110 
111  function numFields( $res ) {
112  return -1;
113  }
114 
115  function fieldName( $res, $n ) {
116  return 'test';
117  }
118 
119  function insertId() {
120  return -1;
121  }
122 
123  function dataSeek( $res, $row ) {
124  /* nop */
125  }
126 
127  function lastErrno() {
128  return -1;
129  }
130 
131  function lastError() {
132  return 'test';
133  }
134 
135  function fieldInfo( $table, $field ) {
136  return false;
137  }
138 
139  function indexInfo( $table, $index, $fname = 'DatabaseBase::indexInfo' ) {
140  return false;
141  }
142 
143  function affectedRows() {
144  return -1;
145  }
146 
147  function getSoftwareLink() {
148  return 'test';
149  }
150 
151  function getServerVersion() {
152  return 'test';
153  }
154 
155  function getServerInfo() {
156  return 'test';
157  }
158 
159  function isOpen() {
160  return true;
161  }
162 
163  protected function closeConnection() {
164  return false;
165  }
166 
167  protected function doQuery( $sql ) {
168  return [];
169  }
170 }
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.
fetchObject($res)
Fetch the next row from the given result object, in object form.
insertId()
Get the inserted value of an auto-increment row.
indexInfo($table, $index, $fname= 'DatabaseBase::indexInfo')
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 DatabaseBase can do more ...
open($server, $user, $password, $dbName)
Open a connection to the database.
isOpen()
Is a connection to the database open?
affectedRows()
Get the number of rows affected by the last write query.
$res
Definition: database.txt:21
Helper for testing the methods from the DatabaseBase class.
numRows($res)
Get the number of rows in a result object.
setExistingTables($tablesExists)
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
Database abstraction object.
Definition: Database.php:32
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:35
getServerInfo()
A string describing the current software version, and possibly other details in a user-friendly way...
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.