Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// +----------------------------------------------------------------------+
3
// | PHP Version 4                                                        |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 1998-2004 Manuel Lemos, Paul Cooper                    |
6
// | All rights reserved.                                                 |
7
// +----------------------------------------------------------------------+
8
// | MDB is a merge of PEAR DB and Metabases that provides a unified DB   |
9
// | API as well as database abstraction for PHP applications.            |
10
// | This LICENSE is in the BSD license style.                            |
11
// |                                                                      |
12
// | Redistribution and use in source and binary forms, with or without   |
13
// | modification, are permitted provided that the following conditions   |
14
// | are met:                                                             |
15
// |                                                                      |
16
// | Redistributions of source code must retain the above copyright       |
17
// | notice, this list of conditions and the following disclaimer.        |
18
// |                                                                      |
19
// | Redistributions in binary form must reproduce the above copyright    |
20
// | notice, this list of conditions and the following disclaimer in the  |
21
// | documentation and/or other materials provided with the distribution. |
22
// |                                                                      |
23
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
24
// | Lukas Smith nor the names of his contributors may be used to endorse |
25
// | or promote products derived from this software without specific prior|
26
// | written permission.                                                  |
27
// |                                                                      |
28
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
29
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
30
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
31
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
32
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
33
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
34
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
35
// |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
36
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
37
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
38
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
39
// | POSSIBILITY OF SUCH DAMAGE.                                          |
40
// +----------------------------------------------------------------------+
41
// | Author: Paul Cooper <pgc@ucecom.com>                                 |
42
// +----------------------------------------------------------------------+
43
//
44
// $Id:
45
 
46
class MDB_Bugs_TestCase extends PHPUnit_TestCase {
47
    //contains the dsn of the database we are testing
48
    var $dsn;
49
    //contains the options that should be used during testing
50
    var $options;
51
    //contains the name of the database we are testing
52
    var $database;
53
    //contains the MDB object of the db once we have connected
54
    var $db;
55
    // contains field names from the test table
56
    var $fields;
57
    // contains the types of the fields from the test table
58
    var $types;
59
 
60
    function MDB_Bugs_TestCase($name) {
61
        $this->PHPUnit_TestCase($name);
62
    }
63
 
64
    function setUp() {
65
        $this->dsn      = $GLOBALS['dsn'];
66
        $this->options  = $GLOBALS['options'];
67
        $this->database = $GLOBALS['database'];
68
        $this->db =& MDB::connect($this->dsn, $this->options);
69
        if (MDB::isError($this->db)) {
70
            $this->assertTrue(false, 'Could not connect to database in setUp');
71
            exit;
72
        }
73
        $this->db->setDatabase($this->database);
74
        $this->fields = array(
75
                        'user_name',
76
                        'user_password',
77
                        'subscribed',
78
                        'user_id',
79
                        'quota',
80
                        'weight',
81
                        'access_date',
82
                        'access_time',
83
                        'approved'
84
                    );
85
 
86
        $this->types = array(
87
                        'text',
88
                        'text',
89
                        'boolean',
90
                        'integer',
91
                        'decimal',
92
                        'float',
93
                        'date',
94
                        'time',
95
                        'timestamp'
96
                    );
97
        $this->clearTables();
98
    }
99
 
100
    function tearDown() {
101
        $this->clearTables();
102
        unset($this->dsn);
103
        if (!MDB::isError($this->db)) {
104
            $this->db->disconnect();
105
        }
106
        unset($this->db);
107
    }
108
 
109
    function clearTables() {
110
        if (MDB::isError($this->db->query('DELETE FROM users'))) {
111
            $this->assertTrue(false, 'Error deleting from table users');
112
        }
113
        if (MDB::isError($this->db->query('DELETE FROM files'))) {
114
            $this->assertTrue(false, 'Error deleting from table users');
115
        }
116
    }
117
 
118
    function insertTestValues($prepared_query, &$data) {
119
        for ($i = 0; $i < count($this->fields); $i++) {
120
            $func = 'setParam'.$this->types[$i];
121
            $this->db->$func($prepared_query, ($i + 1), $data[$this->fields[$i]]);
122
        }
123
    }
124
 
125
    /**
126
     *
127
     */
128
    function testFetchModeBug() {
129
        $data = array();
130
        $total_rows = 3;
131
 
132
        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
133
 
134
        for ($row = 0; $row < $total_rows; $row++) {
135
            $data[$row]['user_name'] = "user_$row";
136
            $data[$row]['user_password'] = 'somepassword';
137
            $data[$row]['subscribed'] = $row % 2;
138
            $data[$row]['user_id'] = $row;
139
            $data[$row]['quota'] = sprintf("%.2f",strval(1+($row+1)/100));
140
            $data[$row]['weight'] = sqrt($row);
141
            $data[$row]['access_date'] = MDB_Date::mdbToday();
142
            $data[$row]['access_time'] = MDB_Date::mdbTime();
143
            $data[$row]['approved'] = MDB_Date::mdbNow();
144
 
145
            $this->insertTestValues($prepared_query, $data[$row]);
146
 
147
            $result = $this->db->executeQuery($prepared_query);
148
 
149
            if (MDB::isError($result)) {
150
                $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
151
            }
152
        }
153
 
154
        $this->db->freePreparedQuery($prepared_query);
155
 
156
        $result = $this->db->query('SELECT user_name, user_id, quota FROM users ORDER BY user_name');
157
        if (MDB::isError($result)) {
158
            $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
159
        }
160
        $this->db->setFetchMode(MDB_FETCHMODE_ASSOC);
161
 
162
        $firstRow = $this->db->fetchRow($result);
163
        $this->assertEquals($data[0]['user_name'], $firstRow['user_name'], "The data returned ($firstRow[user_name]) does not match that expected (".$data[0]['user_name'].")");
164
 
165
        $result = $this->db->query('SELECT user_name, user_id, quota FROM users ORDER BY user_name');
166
        if (MDB::isError($result)) {
167
            $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
168
        }
169
        $this->db->setFetchMode(MDB_FETCHMODE_ORDERED);
170
 
171
        $firstRow = $this->db->fetchRow($result);
172
        $this->assertEquals($data[0]['user_name'], $firstRow[0], "The data returned ($firstRow[0]) does not match that expected (".$data[0]['user_name'].")");
173
    }
174
 
175
    /**
176
     * http://bugs.php.net/bug.php?id=22328
177
     */
178
    function testBug22328() {
179
        $result = $this->db->query('SELECT * FROM users');
180
        $this->db->pushErrorHandling(PEAR_ERROR_RETURN);
181
        $result2 = $this->db->query('SELECT * FROM foo');
182
 
183
        $data = $this->db->fetchRow($result);
184
        $this->db->popErrorHandling();
185
        $this->assertEquals(false, MDB::isError($data), "Error messages for a query affect result reading of other queries");
186
    }
187
 
188
    /**
189
     * http://pear.php.net/bugs/bug.php?id=670
190
     */
191
    function testBug670() {
192
        $data['user_name'] = null;
193
        $data['user_password'] = 'somepassword';
194
        $data['subscribed'] = true;
195
        $data['user_id'] = 1;
196
        $data['quota'] = sprintf("%.2f",strval(3/100));
197
        $data['weight'] = sqrt(1);
198
        $data['access_date'] = MDB_Date::mdbToday();
199
        $data['access_time'] = MDB_Date::mdbTime();
200
        $data['approved'] = MDB_Date::mdbNow();
201
 
202
        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
203
        $this->insertTestValues($prepared_query, $data);
204
        $result = $this->db->executeQuery($prepared_query);
205
 
206
        $result = $this->db->query('SELECT user_name FROM users');
207
        $col = $this->db->fetchCol($result, 'user_name');
208
        if (MDB::isError($col)) {
209
            $this->assertTrue(false, "Error when fetching column first first row as NULL: ".$col->getMessage());
210
        }
211
 
212
        $data['user_name'] = "user_1";
213
        $data['user_id'] = 2;
214
 
215
        $this->insertTestValues($prepared_query, $data);
216
        $result = $this->db->executeQuery($prepared_query);
217
 
218
        $result = $this->db->query('SELECT user_name FROM users');
219
        $col = $this->db->fetchCol($result, 'user_name');
220
        if (MDB::isError($col)) {
221
            $this->assertTrue(false, "Error when fetching column: ".$col->getMessage());
222
        }
223
 
224
        $data['user_name'] = null;
225
 
226
        $this->db->freePreparedQuery($prepared_query);
227
    }
228
 
229
    /**
230
     * http://pear.php.net/bugs/bug.php?id=681
231
     */
232
    function testBug681() {
233
        $result = $this->db->query('SELECT * FROM users WHERE 1=0');
234
 
235
        $numrows = $this->db->numRows($result);
236
        $this->assertEquals(0, $numrows, "Numrows is not returning 0 for empty result sets");
237
 
238
        $data['user_name'] = "user_1";
239
        $data['user_password'] = 'somepassword';
240
        $data['subscribed'] = true;
241
        $data['user_id'] = 1;
242
        $data['quota'] = sprintf("%.2f",strval(3/100));
243
        $data['weight'] = sqrt(1);
244
        $data['access_date'] = MDB_Date::mdbToday();
245
        $data['access_time'] = MDB_Date::mdbTime();
246
        $data['approved'] = MDB_Date::mdbNow();
247
 
248
        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
249
        $this->insertTestValues($prepared_query, $data);
250
        $result = $this->db->executeQuery($prepared_query);
251
 
252
        $result = $this->db->query('SELECT * FROM users');
253
        $numrows = $this->db->numRows($result);
254
        $this->assertEquals(1, $numrows, "Numrows is not returning proper value");
255
 
256
        $this->db->freePreparedQuery($prepared_query);
257
    }
258
 
259
    /**
260
     * http://pear.php.net/bugs/bug.php?id=718
261
     */
262
    function testBug718() {
263
        $data['user_name'] = "user_1";
264
        $data['user_password'] = 'somepassword';
265
        $data['subscribed'] = true;
266
        $data['user_id'] = 1;
267
        $data['quota'] = sprintf("%.2f",strval(3/100));
268
        $data['weight'] = sqrt(1);
269
        $data['access_date'] = MDB_Date::mdbToday();
270
        $data['access_time'] = MDB_Date::mdbTime();
271
        $data['approved'] = MDB_Date::mdbNow();
272
 
273
        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
274
        $this->insertTestValues($prepared_query, $data);
275
        $result = $this->db->executeQuery($prepared_query);
276
 
277
        $row = $this->db->queryRow('SELECT a.user_id, b.user_id FROM users a, users b where a.user_id = b.user_id', array('integer', 'integer'), MDB_FETCHMODE_ORDERED);
278
        $this->assertEquals(2, count($row), "Columns with the same name get overwritten in ordered mode");
279
 
280
        $this->db->freePreparedQuery($prepared_query);
281
    }
282
 
283
    /**
284
     * http://pear.php.net/bugs/bug.php?id=946
285
     */
286
    function testBug946() {
287
        $data = array();
288
        $total_rows = 5;
289
 
290
        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
291
 
292
        for ($row = 0; $row < $total_rows; $row++) {
293
            $data[$row]['user_name'] = "user_$row";
294
            $data[$row]['user_password'] = 'somepassword';
295
            $data[$row]['subscribed'] = (boolean)($row % 2);
296
            $data[$row]['user_id'] = $row;
297
            $data[$row]['quota'] = sprintf("%.2f",strval(1+($row+1)/100));
298
            $data[$row]['weight'] = sqrt($row);
299
            $data[$row]['access_date'] = MDB_Date::mdbToday();
300
            $data[$row]['access_time'] = MDB_Date::mdbTime();
301
            $data[$row]['approved'] = MDB_Date::mdbNow();
302
 
303
            $this->insertTestValues($prepared_query, $data[$row]);
304
 
305
            $result = $this->db->executeQuery($prepared_query);
306
 
307
            if (MDB::isError($result)) {
308
                $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
309
            }
310
        }
311
        $this->db->freePreparedQuery($prepared_query);
312
 
313
        $result = $this->db->limitQuery('SELECT * FROM users', null, 1, 3);
314
        $numrows = $this->db->numRows($result);
315
        while ($row = $this->db->fetchInto($result)) {
316
            if (MDB::isError($row)) {
317
                $this->assertTrue(false, 'Error fetching a row'.$row->getMessage());
318
            }
319
        }
320
 
321
        $this->db->freeResult($result);
322
        $result = $this->db->query('SELECT * FROM users');
323
        $numrows = $this->db->numRows($result);
324
        while ($row = $this->db->fetchInto($result)) {
325
            if (MDB::isError($row)) {
326
                $this->assertTrue(false, 'Error fetching a row with limit'.$row->getMessage());
327
            }
328
        }
329
 
330
        $this->db->freeResult($result);
331
    }
332
}
333
 
334
?>