Convert all array() syntax to []
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
1 <?php
2
3 /**
4 * Helper for testing the methods from the DatabaseBase class
5 * @since 1.22
6 */
7 class DatabaseTestHelper extends DatabaseBase {
8
9 /**
10 * __CLASS__ of the test suite,
11 * used to determine, if the function name is passed every time to query()
12 */
13 protected $testName = [];
14
15 /**
16 * Array of lastSqls passed to query(),
17 * This is an array since some methods in DatabaseBase can do more than one
18 * query. Cleared when calling getLastSqls().
19 */
20 protected $lastSqls = [];
21
22 /**
23 * Array of tables to be considered as existing by tableExist()
24 * Use setExistingTables() to alter.
25 */
26 protected $tablesExists;
27
28 public function __construct( $testName ) {
29 $this->testName = $testName;
30 }
31
32 /**
33 * Returns SQL queries grouped by '; '
34 * Clear the list of queries that have been done so far.
35 */
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 }