d6ca5964b8fc41fd19750e89e62756cd5bf290cd
[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 $this->profiler = new ProfilerStub( [] );
32 $this->trxProfiler = new TransactionProfiler();
33 }
34
35 /**
36 * Returns SQL queries grouped by '; '
37 * Clear the list of queries that have been done so far.
38 */
39 public function getLastSqls() {
40 $lastSqls = implode( '; ', $this->lastSqls );
41 $this->lastSqls = [];
42
43 return $lastSqls;
44 }
45
46 public function setExistingTables( $tablesExists ) {
47 $this->tablesExists = (array)$tablesExists;
48 }
49
50 protected function addSql( $sql ) {
51 // clean up spaces before and after some words and the whole string
52 $this->lastSqls[] = trim( preg_replace(
53 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
54 ' ', $sql
55 ) );
56 }
57
58 protected function checkFunctionName( $fname ) {
59 if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
60 throw new MWException( 'function name does not start with test class. ' .
61 $fname . ' vs. ' . $this->testName . '. ' .
62 'Please provide __METHOD__ to database methods.' );
63 }
64 }
65
66 function strencode( $s ) {
67 // Choose apos to avoid handling of escaping double quotes in quoted text
68 return str_replace( "'", "\'", $s );
69 }
70
71 public function addIdentifierQuotes( $s ) {
72 // no escaping to avoid handling of double quotes in quoted text
73 return $s;
74 }
75
76 public function query( $sql, $fname = '', $tempIgnore = false ) {
77 $this->checkFunctionName( $fname );
78 $this->addSql( $sql );
79
80 return parent::query( $sql, $fname, $tempIgnore );
81 }
82
83 public function tableExists( $table, $fname = __METHOD__ ) {
84 $this->checkFunctionName( $fname );
85
86 return in_array( $table, (array)$this->tablesExists );
87 }
88
89 // Redeclare parent method to make it public
90 public function nativeReplace( $table, $rows, $fname ) {
91 return parent::nativeReplace( $table, $rows, $fname );
92 }
93
94 function getType() {
95 return 'test';
96 }
97
98 function open( $server, $user, $password, $dbName ) {
99 return false;
100 }
101
102 function fetchObject( $res ) {
103 return false;
104 }
105
106 function fetchRow( $res ) {
107 return false;
108 }
109
110 function numRows( $res ) {
111 return -1;
112 }
113
114 function numFields( $res ) {
115 return -1;
116 }
117
118 function fieldName( $res, $n ) {
119 return 'test';
120 }
121
122 function insertId() {
123 return -1;
124 }
125
126 function dataSeek( $res, $row ) {
127 /* nop */
128 }
129
130 function lastErrno() {
131 return -1;
132 }
133
134 function lastError() {
135 return 'test';
136 }
137
138 function fieldInfo( $table, $field ) {
139 return false;
140 }
141
142 function indexInfo( $table, $index, $fname = 'DatabaseBase::indexInfo' ) {
143 return false;
144 }
145
146 function affectedRows() {
147 return -1;
148 }
149
150 function getSoftwareLink() {
151 return 'test';
152 }
153
154 function getServerVersion() {
155 return 'test';
156 }
157
158 function getServerInfo() {
159 return 'test';
160 }
161
162 function isOpen() {
163 return true;
164 }
165
166 protected function closeConnection() {
167 return false;
168 }
169
170 protected function doQuery( $sql ) {
171 return [];
172 }
173 }