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