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 versions 4 and 5                                                 |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 1998-2006 Manuel Lemos, Paul Cooper                    |
6
// | All rights reserved.                                                 |
7
// +----------------------------------------------------------------------+
8
// | MDB2 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: MDB2_bugs_testcase.php,v 1.31 2006/12/09 16:58:16 quipo Exp $
45
 
46
require_once 'MDB2_testcase.php';
47
 
48
class MDB2_Bugs_TestCase extends MDB2_TestCase {
49
    /**
50
     *
51
     */
52
    function testFetchModeBug() {
53
        $data = array();
54
 
55
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($this->fields), MDB2_PREPARE_MANIP);
56
 
57
        $data['user_name'] = 'user_=';
58
        $data['user_password'] = 'somepass';
59
        $data['subscribed'] = true;
60
        $data['user_id'] = 0;
61
        $data['quota'] = sprintf("%.2f", strval(2/100));
62
        $data['weight'] = sqrt(0);
63
        $data['access_date'] = MDB2_Date::mdbToday();
64
        $data['access_time'] = MDB2_Date::mdbTime();
65
        $data['approved'] = MDB2_Date::mdbNow();
66
 
67
        $result = $stmt->execute(array_values($data));
68
 
69
        if (PEAR::isError($result)) {
70
            $this->assertTrue(false, 'Error executing prepared query '.$result->getMessage());
71
        }
72
 
73
        $stmt->free();
74
 
75
        $query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users ORDER BY user_name';
76
        $result =& $this->db->query($query);
77
 
78
        if (PEAR::isError($result)) {
79
            $this->assertTrue(false, 'Error selecting from users: '.$result->getMessage());
80
        }
81
 
82
        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
83
 
84
        $firstRow = $result->fetchRow();
85
        $this->assertEquals($firstRow['user_name'], $data['user_name'], 'The data returned does not match that expected');
86
 
87
        $result =& $this->db->query('SELECT user_name, user_id, quota FROM users ORDER BY user_name');
88
        if (PEAR::isError($result)) {
89
            $this->assertTrue(false, 'Error selecting from users: '.$result->getMessage());
90
        }
91
        $this->db->setFetchMode(MDB2_FETCHMODE_ORDERED);
92
 
93
        $value = $result->fetchOne();
94
        $this->assertEquals($data['user_name'], $value, 'The data returned does not match that expected');
95
        $result->free();
96
    }
97
 
98
    /**
99
     * @see http://bugs.php.net/bug.php?id=22328
100
     */
101
    function testBug22328() {
102
        $result =& $this->db->query('SELECT * FROM users');
103
        $this->db->pushErrorHandling(PEAR_ERROR_RETURN);
104
        $result2 = $this->db->query('SELECT * FROM foo');
105
 
106
        $data = $result->fetchRow();
107
        $this->db->popErrorHandling();
108
        $this->assertFalse(PEAR::isError($data), 'Error messages for a query affect result reading of other queries');
109
    }
110
 
111
    /**
112
     * @see http://pear.php.net/bugs/bug.php?id=670
113
     */
114
    function testBug670() {
115
        $data['user_name'] = null;
116
        $data['user_password'] = 'somepass';
117
        $data['subscribed'] = true;
118
        $data['user_id'] = 1;
119
        $data['quota'] = sprintf("%.2f",strval(3/100));
120
        $data['weight'] = sqrt(1);
121
        $data['access_date'] = MDB2_Date::mdbToday();
122
        $data['access_time'] = MDB2_Date::mdbTime();
123
        $data['approved'] = MDB2_Date::mdbNow();
124
 
125
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($this->fields), MDB2_PREPARE_MANIP);
126
        $result = $stmt->execute(array_values($data));
127
 
128
        $result =& $this->db->query('SELECT user_name FROM users');
129
        $col = $result->fetchCol('user_name');
130
        if (PEAR::isError($col)) {
131
            $this->assertTrue(false, 'Error when fetching column first first row as NULL: '.$col->getMessage());
132
        }
133
 
134
        $data['user_name'] = "user_1";
135
        $data['user_id'] = 2;
136
 
137
        $result = $stmt->execute(array_values($data));
138
 
139
        $result =& $this->db->query('SELECT user_name FROM users');
140
        $col = $result->fetchCol('user_name');
141
        if (PEAR::isError($col)) {
142
            $this->assertTrue(false, 'Error when fetching column: '.$col->getMessage());
143
        }
144
 
145
        $data['user_name'] = null;
146
 
147
        $stmt->free();
148
    }
149
 
150
    /**
151
     * @see http://pear.php.net/bugs/bug.php?id=681
152
     */
153
    function testBug681() {
154
        $result =& $this->db->query('SELECT * FROM users WHERE 1=0');
155
 
156
        $numrows = $result->numRows();
157
        $this->assertEquals(0, $numrows, 'Numrows is not returning 0 for empty result sets');
158
 
159
        $data = $this->getSampleData(1);
160
 
161
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($this->fields), MDB2_PREPARE_MANIP);
162
        $result = $stmt->execute(array_values($data));
163
 
164
        $result =& $this->db->query('SELECT * FROM users');
165
        $numrows = $result->numRows();
166
        $this->assertEquals(1, $numrows, 'Numrows is not returning proper value');
167
 
168
        $stmt->free();
169
    }
170
 
171
    /**
172
     * @see http://pear.php.net/bugs/bug.php?id=718
173
     */
174
    function testBug718() {
175
        $data = $this->getSampleData(1);
176
 
177
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($this->fields), MDB2_PREPARE_MANIP);
178
        $result = $stmt->execute(array_values($data));
179
 
180
        $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'), MDB2_FETCHMODE_ORDERED);
181
        $this->assertEquals(2, count($row), "Columns with the same name get overwritten in ordered mode");
182
 
183
        $stmt->free();
184
    }
185
 
186
    /**
187
     * @see http://pear.php.net/bugs/bug.php?id=946
188
     */
189
    function testBug946() {
190
        $data = array();
191
        $total_rows = 5;
192
 
193
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($this->fields), MDB2_PREPARE_MANIP);
194
 
195
        for ($row = 0; $row < $total_rows; $row++) {
196
            $data[$row] = $this->getSampleData($row);
197
 
198
            $result = $stmt->execute(array_values($data[$row]));
199
 
200
            if (PEAR::isError($result)) {
201
                $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
202
            }
203
        }
204
        $stmt->free();
205
 
206
        $query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users';
207
 
208
        $this->db->setLimit(3, 1);
209
        $result =& $this->db->query($query);
210
        $numrows = $result->numRows();
211
        while ($row = $result->fetchRow()) {
212
            if (PEAR::isError($row)) {
213
                $this->assertTrue(false, 'Error fetching a row: '.$row->getMessage());
214
            }
215
        }
216
        $result->free();
217
 
218
        $result =& $this->db->query($query);
219
        $numrows = $result->numRows();
220
        while ($row = $result->fetchRow()) {
221
            if (PEAR::isError($row)) {
222
                $this->assertTrue(false, 'Error fetching a row: '.$row->getMessage());
223
            }
224
        }
225
        $result->free();
226
    }
227
 
228
    /**
229
     * @see http://pear.php.net/bugs/bug.php?id=3146
230
     */
231
    function testBug3146() {
232
        $data = array();
233
        $total_rows = 5;
234
 
235
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
236
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
237
 
238
        for ($row = 0; $row < $total_rows; $row++) {
239
            $data[$row] = $this->getSampleData($row);
240
 
241
            $result = $stmt->execute(array_values($data[$row]));
242
            if (PEAR::isError($result)) {
243
                $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
244
            }
245
        }
246
        $stmt->free();
247
 
248
        $query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users ORDER BY user_id';
249
        $result =& $this->db->query($query, $this->fields);
250
 
251
        $numrows = $result->numRows($result);
252
 
253
        $this->verifyFetchedValues($result, 0, $data[0]);
254
        $this->verifyFetchedValues($result, 2, $data[2]);
255
        $this->verifyFetchedValues($result, null, $data[3]);
256
        $this->verifyFetchedValues($result, 1, $data[1]);
257
 
258
        $result->free();
259
    }
260
 
261
    /**
262
     * Strong typing query result misbehaves when $n_columns > $n_types
263
     * @see http://pear.php.net/bugs/bug.php?id=9502
264
     */
265
    function testBug9502() {
266
        $row = 5;
267
        $data = $this->getSampleData($row);
268
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($this->fields), MDB2_PREPARE_MANIP);
269
        $result = $stmt->execute(array_values($data));
270
        $stmt->free();
271
 
272
        //provide an incomplete and scrambled types array
273
        $types = array();
274
        $types['subscribed'] = $this->fields['subscribed'];
275
        $types['user_name']  = $this->fields['user_name'];
276
        $types['weight']     = $this->fields['weight'];
277
 
278
        $query = 'SELECT weight, user_name, user_id, quota, subscribed FROM users WHERE user_id = '.$row;
279
        $result =& $this->db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
280
        if (PEAR::isError($result)) {
281
            $this->assertTrue(false, 'Error executing query: '.$result->getMessage() .' - '. $result->getUserInfo());
282
        } else {
283
            $this->assertTrue(is_bool($result['subscribed']));
284
            $this->assertTrue(is_numeric($result['user_id']));
285
            $this->assertTrue(is_float($result['weight']));
286
            $this->assertFalse(is_bool($result['user_name']));
287
        }
288
    }
289
}
290
 
291
?>