test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTestHelper.php
blob790f273c2d7740d445ee3c2fc2f40750e669c39d
1 <?php
3 /**
4 * Helper for testing the methods from the DatabaseBase class
5 * @since 1.22
6 */
7 class DatabaseTestHelper extends DatabaseBase {
9 /**
10 * __CLASS__ of the test suite,
11 * used to determine, if the function name is passed every time to query()
13 protected $testName = array();
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().
20 protected $lastSqls = array();
22 /**
23 * Array of tables to be considered as existing by tableExist()
24 * Use setExistingTables() to alter.
26 protected $tablesExists;
28 public function __construct( $testName ) {
29 $this->testName = $testName;
32 /**
33 * Returns SQL queries grouped by '; '
34 * Clear the list of queries that have been done so far.
36 public function getLastSqls() {
37 $lastSqls = implode( '; ', $this->lastSqls );
38 $this->lastSqls = array();
40 return $lastSqls;
43 public function setExistingTables( $tablesExists ) {
44 $this->tablesExists = (array)$tablesExists;
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 ) );
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.' );
63 function strencode( $s ) {
64 // Choose apos to avoid handling of escaping double quotes in quoted text
65 return str_replace( "'", "\'", $s );
68 public function addIdentifierQuotes( $s ) {
69 // no escaping to avoid handling of double quotes in quoted text
70 return $s;
73 public function query( $sql, $fname = '', $tempIgnore = false ) {
74 $this->checkFunctionName( $fname );
75 $this->addSql( $sql );
77 return parent::query( $sql, $fname, $tempIgnore );
80 public function tableExists( $table, $fname = __METHOD__ ) {
81 $this->checkFunctionName( $fname );
83 return in_array( $table, (array)$this->tablesExists );
86 // Redeclare parent method to make it public
87 public function nativeReplace( $table, $rows, $fname ) {
88 return parent::nativeReplace( $table, $rows, $fname );
91 function getType() {
92 return 'test';
95 function open( $server, $user, $password, $dbName ) {
96 return false;
99 function fetchObject( $res ) {
100 return false;
103 function fetchRow( $res ) {
104 return false;
107 function numRows( $res ) {
108 return -1;
111 function numFields( $res ) {
112 return -1;
115 function fieldName( $res, $n ) {
116 return 'test';
119 function insertId() {
120 return -1;
123 function dataSeek( $res, $row ) {
124 /* nop */
127 function lastErrno() {
128 return -1;
131 function lastError() {
132 return 'test';
135 function fieldInfo( $table, $field ) {
136 return false;
139 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
140 return false;
143 function affectedRows() {
144 return -1;
147 function getSoftwareLink() {
148 return 'test';
151 function getServerVersion() {
152 return 'test';
155 function getServerInfo() {
156 return 'test';
159 protected function closeConnection() {
160 return false;
163 protected function doQuery( $sql ) {
164 return array();