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 Paul Cooper, Lukas Smith, Lorenzo Alberton   |
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
// | Authors: Paul Cooper <pgc@ucecom.com>                                |
42
// |          Lorenzo Alberton <l dot alberton at quipo dot it>           |
43
// +----------------------------------------------------------------------+
44
//
45
// $Id: MDB2_extended_testcase.php,v 1.13 2007/02/28 12:15:26 quipo Exp $
46
 
47
require_once 'MDB2_testcase.php';
48
 
49
class MDB2_Extended_TestCase extends MDB2_TestCase
50
{
51
    /**
52
     *
53
     */
54
    function testAutoExecute()
55
    {
56
        $data = $this->getSampleData();
57
        $select_query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users';
58
 
59
        $result = $this->db->loadModule('Extended');
60
        if (PEAR::isError($result)) {
61
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
62
        }
63
        $result = $this->db->extended->autoExecute('users', $data, MDB2_AUTOQUERY_INSERT, null, $this->fields);
64
        if (PEAR::isError($result)) {
65
            $this->assertTrue(false, 'Error auto executing insert: '.$result->getMessage());
66
        }
67
 
68
        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
69
        $result =& $this->db->query($select_query, $this->fields);
70
        if (PEAR::isError($result)) {
71
            $this->assertTrue(false, 'Error selecting from users: '.$result->getMessage());
72
        } else {
73
            $this->verifyFetchedValues($result, null, $data);
74
            $result->free();
75
        }
76
 
77
        $where = 'user_id = '.$this->db->quote($data['user_id'], 'integer');
78
        $result = $this->db->extended->autoExecute('users', null, MDB2_AUTOQUERY_SELECT, $where, null, true, $this->fields);
79
        if (PEAR::isError($result)) {
80
            $this->assertTrue(false, 'Error auto executing select: '.$result->getMessage());
81
        } else {
82
            $this->verifyFetchedValues($result, null, $data);
83
            $result->free();
84
        }
85
 
86
        $where = 'user_id = '.$this->db->quote($data['user_id'], 'integer');
87
        $result = $this->db->extended->autoExecute('users', null, MDB2_AUTOQUERY_SELECT, $where, null, true, MDB2_PREPARE_RESULT);
88
        if (PEAR::isError($result)) {
89
            $this->assertTrue(false, 'Error auto executing select: '.$result->getMessage());
90
        } else {
91
            $result->setResultTypes($this->fields);
92
            $this->verifyFetchedValues($result, null, $data);
93
            $result->free();
94
        }
95
 
96
        $update_data = array();
97
        $data['user_name'] = $update_data['user_name'] = 'foo';
98
 
99
        $where = 'user_id = '.$this->db->quote($data['user_id'], 'integer');
100
        $result = $this->db->extended->autoExecute('users', $update_data, MDB2_AUTOQUERY_UPDATE, $where, $this->fields);
101
        if (PEAR::isError($result)) {
102
            $this->assertTrue(false, 'Error auto executing insert: '.$result->getMessage());
103
        }
104
 
105
        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
106
        $result =& $this->db->query($select_query, $this->fields);
107
        if (PEAR::isError($result)) {
108
            $this->assertTrue(false, 'Error selecting from users: '.$result->getMessage());
109
        } else {
110
            $this->verifyFetchedValues($result, null, $data);
111
            $result->free();
112
        }
113
 
114
        $where = array($where, 'user_name = '.$this->db->quote($data['user_name'], 'text'));
115
        $result = $this->db->extended->autoExecute('users', null, MDB2_AUTOQUERY_DELETE, $where, null);
116
        if (PEAR::isError($result)) {
117
            $this->assertTrue(false, 'Error auto executing insert: '.$result->getMessage());
118
        }
119
 
120
        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
121
        $result =& $this->db->query($select_query, $this->fields);
122
        if (PEAR::isError($result)) {
123
            $this->assertTrue(false, 'Error selecting from users: '.$result->getMessage());
124
        } else {
125
            $this->assertEquals(0, $result->numRows(), 'No rows were expected to be returned');
126
            $result->free();
127
        }
128
    }
129
 
130
    /**
131
     * Test getAssoc()
132
     *
133
     * Test fetching two columns from a resultset. Return them as (key,value) pairs.
134
     */
135
    function testGetAssoc()
136
    {
137
        $result = $this->db->loadModule('Extended');
138
        if (PEAR::isError($result)) {
139
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
140
        }
141
 
142
        $data = $this->getSampleData(1234);
143
 
144
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
145
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
146
        $result = $stmt->execute(array_values($data));
147
        $stmt->free();
148
 
149
        if (PEAR::isError($result)) {
150
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
151
        }
152
 
153
        //test getAssoc() with query parameters
154
        $query = 'SELECT user_id, user_name FROM users WHERE user_id=?';
155
        $result = $this->db->extended->getAssoc($query, array('integer', 'text'), array(1234), array('integer'));
156
        if (PEAR::isError($result)) {
157
            $this->assertTrue(false, 'Error executing getAssoc(): '.$result->getMessage());
158
        }
159
        $this->assertTrue(array_key_exists($data['user_id'], $result), 'Unexpected returned key');
160
        $this->assertEquals($data['user_name'], $result[$data['user_id']], 'Unexpected returned value');
161
 
162
        //test getAssoc() without query parameters
163
        $query = 'SELECT user_id, user_name FROM users WHERE user_id=1234';
164
        $result = $this->db->extended->getAssoc($query, array('integer', 'text'));
165
        if (PEAR::isError($result)) {
166
            $this->assertTrue(false, 'Error executing getAssoc(): '.$result->getMessage());
167
        } else {
168
            $this->assertTrue(array_key_exists($data['user_id'], $result), 'Unexpected returned key');
169
            $this->assertEquals($data['user_name'], $result[$data['user_id']], 'Unexpected returned value');
170
        }
171
 
172
        //add another record to the db
173
        $data2 = $this->getSampleData(4321);
174
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
175
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
176
        $result = $stmt->execute(array_values($data2));
177
        $stmt->free();
178
 
179
        //test getAssoc() with $force_array=true
180
        $query = 'SELECT user_id, user_name FROM users ORDER BY user_id';
181
        $values = $this->db->extended->getAssoc($query, array('integer', 'text'), null, null, MDB2_FETCHMODE_ASSOC, true);
182
        if (PEAR::isError($values)) {
183
            $this->assertTrue(false, 'Error executing getAssoc(): '.$values->getMessage());
184
        } else {
185
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
186
            list($id, $value) = each($values);
187
            $this->assertEquals($data['user_id'],   $id,                 'Unexpected returned value');
188
            $this->assertEquals($data['user_name'], $value['user_name'], 'Unexpected returned value');
189
            list($id, $value) = each($values);
190
            $this->assertEquals($data2['user_id'],   $id,                 'Unexpected returned value');
191
            $this->assertEquals($data2['user_name'], $value['user_name'], 'Unexpected returned value');
192
        }
193
 
194
 
195
        //test getAssoc() with $force_array=false and $group=true
196
        $query = 'SELECT user_id, user_name FROM users ORDER BY user_id';
197
        $values = $this->db->extended->getAssoc($query, array('integer', 'text'), null, null, MDB2_FETCHMODE_ASSOC, false, true);
198
        if (PEAR::isError($values)) {
199
            $this->assertTrue(false, 'Error executing getAssoc(): '.$values->getMessage());
200
        } else {
201
            //@todo: check if MDB2_FETCHMODE_ASSOC is behaving correctly in this case
202
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
203
            list($id, $value) = each($values);
204
            $this->assertEquals($data['user_id'],   $id,       'Unexpected returned value');
205
            $this->assertEquals($data['user_name'], $value[0], 'Unexpected returned value');
206
            list($id, $value) = each($values);
207
            $this->assertEquals($data2['user_id'],   $id,       'Unexpected returned value');
208
            $this->assertEquals($data2['user_name'], $value[0], 'Unexpected returned value');
209
 
210
        }
211
 
212
        //test $group=true with 3 fields
213
        $query = 'SELECT user_password, user_id, user_name FROM users ORDER BY user_id';
214
        $values = $this->db->extended->getAssoc($query, array('integer', 'text', 'text'), null, null, MDB2_FETCHMODE_ASSOC, false, true);
215
        if (PEAR::isError($values)) {
216
            $this->assertTrue(false, 'Error executing getAssoc(): '.$values->getMessage());
217
        } else {
218
            //the 2 values for user_password are equals, so they are collapsed in the same array
219
            $this->assertEquals(1, count($values), 'Error: incorrect number of returned rows');
220
            $values = $values[0];
221
            //there are 2 records
222
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
223
            $value = array_shift($values);
224
            $this->assertEquals($data['user_id'],   $value['user_id'], 'Unexpected returned value');
225
            $this->assertEquals($data['user_name'], $value['user_name'], 'Unexpected returned value');
226
            $value = array_shift($values);
227
            $this->assertEquals($data2['user_id'],   $value['user_id'], 'Unexpected returned value');
228
            $this->assertEquals($data2['user_name'], $value['user_name'], 'Unexpected returned value');
229
        }
230
 
231
        //test $group=false with 3 fields
232
        $values = $this->db->extended->getAssoc($query, array('integer', 'text', 'text'), null, null, MDB2_FETCHMODE_ASSOC, false, false);
233
        if (PEAR::isError($values)) {
234
            $this->assertTrue(false, 'Error executing getAssoc(): '.$values->getMessage());
235
        } else {
236
            //the 2 values for user_password are equals, so the first record is overwritten
237
            $this->assertEquals(1, count($values), 'Error: incorrect number of returned rows');
238
            $values = $values[0];
239
            $this->assertEquals($data2['user_id'],   $value['user_id'], 'Unexpected returned value');
240
            $this->assertEquals($data2['user_name'], $value['user_name'], 'Unexpected returned value');
241
        }
242
    }
243
 
244
    /**
245
     * Test getOne()
246
     *
247
     * Test fetching a single value
248
     */
249
    function testGetOne()
250
    {
251
        $result = $this->db->loadModule('Extended');
252
        if (PEAR::isError($result)) {
253
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
254
        }
255
 
256
        $data = $this->getSampleData(1234);
257
 
258
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
259
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
260
        $result = $stmt->execute(array_values($data));
261
        $stmt->free();
262
 
263
        if (PEAR::isError($result)) {
264
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
265
        }
266
 
267
        //test getOne() with query parameters
268
        $query = 'SELECT user_name FROM users WHERE user_id=?';
269
        $result = $this->db->extended->getOne($query, 'text', array(1234), array('integer'));
270
        if (PEAR::isError($result)) {
271
            $this->assertTrue(false, 'Error executing getOne(): '.$result->getMessage());
272
        }
273
        $this->assertEquals($data['user_name'], $result, 'Unexpected returned value');
274
 
275
        //test getOne() without query parameters
276
        $query = 'SELECT user_name FROM users WHERE user_id=1234';
277
        $result = $this->db->extended->getOne($query, 'text');
278
        if (PEAR::isError($result)) {
279
            $this->assertTrue(false, 'Error executing getOne(): '.$result->getMessage());
280
        }
281
        $this->assertEquals($data['user_name'], $result, 'Unexpected returned value');
282
 
283
        //test getOne() with column number (resultset: 0-based array)
284
        $query = 'SELECT user_id, user_name, approved FROM users WHERE user_id=1234';
285
        $result = $this->db->extended->getOne($query, 'text', null, null, 1); //get the 2nd column
286
        if (PEAR::isError($result)) {
287
            $this->assertTrue(false, 'Error executing getOne(): '.$result->getMessage());
288
        }
289
        $this->assertEquals($data['user_name'], $result, 'Unexpected returned value');
290
    }
291
 
292
    /**
293
     * Test getCol()
294
     *
295
     * Test fetching a column of result data.
296
     */
297
    function testGetCol()
298
    {
299
        $result = $this->db->loadModule('Extended');
300
        if (PEAR::isError($result)) {
301
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
302
        }
303
 
304
        $data = array(
305
 
306
            1 => $this->getSampleData(4321),
307
        );
308
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
309
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
310
        $result = $stmt->execute(array_values($data[0]));
311
        if (PEAR::isError($result)) {
312
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
313
        }
314
        $result = $stmt->execute(array_values($data[1]));
315
        if (PEAR::isError($result)) {
316
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
317
        }
318
        $stmt->free();
319
 
320
        //test getCol() with query parameters
321
        $query = 'SELECT user_name FROM users WHERE user_id>?';
322
        $result = $this->db->extended->getCol($query, 'text', array(1), array('integer'));
323
        if (PEAR::isError($result)) {
324
            $this->assertTrue(false, 'Error executing getCol(): '.$result->getMessage());
325
        }
326
        $expected = array(
327
            $data[0]['user_name'],
328
            $data[1]['user_name'],
329
        );
330
        $this->assertEquals($expected, $result, 'Unexpected returned value');
331
 
332
        //test getCol() without query parameters
333
        $query = 'SELECT user_name FROM users';
334
        $result = $this->db->extended->getCol($query, 'text');
335
        if (PEAR::isError($result)) {
336
            $this->assertTrue(false, 'Error executing getCol(): '.$result->getMessage());
337
        }
338
        $this->assertEquals($expected, $result, 'Unexpected returned value');
339
 
340
        //test getCol() with column number (resultset: 0-based array)
341
        $query = 'SELECT user_id, user_name, approved FROM users';
342
        $result = $this->db->extended->getCol($query, 'text', null, null, 1); //get the 2nd column
343
        if (PEAR::isError($result)) {
344
            $this->assertTrue(false, 'Error executing getCol(): '.$result->getMessage());
345
        }
346
        $this->assertEquals($expected, $result, 'Unexpected returned value');
347
    }
348
 
349
    /**
350
     * Test getRow()
351
     *
352
     * Test fetching a row of result data.
353
     */
354
    function testGetRow()
355
    {
356
        $result = $this->db->loadModule('Extended');
357
        if (PEAR::isError($result)) {
358
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
359
        }
360
 
361
        $data = $this->getSampleData(1234);
362
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
363
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
364
        $result = $stmt->execute(array_values($data));
365
        if (PEAR::isError($result)) {
366
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
367
        }
368
        $stmt->free();
369
 
370
        //test getRow() with query parameters
371
        $query = 'SELECT user_id, user_name, user_password FROM users WHERE user_id=?';
372
        $result = $this->db->extended->getRow($query, array('integer', 'text', 'text'), array(1234), array('integer'), MDB2_FETCHMODE_ASSOC);
373
        if (PEAR::isError($result)) {
374
            $this->assertTrue(false, 'Error executing getRow(): '.$result->getMessage());
375
        }
376
        $this->assertEquals($data['user_id'],       $result['user_id'],       'Unexpected returned value');
377
        $this->assertEquals($data['user_name'],     $result['user_name'],     'Unexpected returned value');
378
        $this->assertEquals($data['user_password'], $result['user_password'], 'Unexpected returned value');
379
 
380
        //test getRow() without query parameters
381
        $query = 'SELECT user_id, user_name, user_password FROM users';
382
        $result = $this->db->extended->getRow($query, array('integer', 'text', 'text'), null, null, MDB2_FETCHMODE_ASSOC);
383
        if (PEAR::isError($result)) {
384
            $this->assertTrue(false, 'Error executing getRow(): '.$result->getMessage());
385
        }
386
        $this->assertEquals($data['user_id'],       $result['user_id'],       'Unexpected returned value');
387
        $this->assertEquals($data['user_name'],     $result['user_name'],     'Unexpected returned value');
388
        $this->assertEquals($data['user_password'], $result['user_password'], 'Unexpected returned value');
389
    }
390
 
391
    /**
392
     * Test getAll()
393
     *
394
     * Test fetching result data all at once.
395
     */
396
    function testGetAll()
397
    {
398
        $result = $this->db->loadModule('Extended');
399
        if (PEAR::isError($result)) {
400
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
401
        }
402
 
403
        $data = array();
404
        $total_rows = 5;
405
 
406
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')', array_values($this->fields), MDB2_PREPARE_MANIP);
407
 
408
        for ($row = 0; $row < $total_rows; $row++) {
409
            $data[$row] = $this->getSampleData($row);
410
            $result = $stmt->execute(array_values($data[$row]));
411
            if (PEAR::isError($result)) {
412
                $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
413
            }
414
        }
415
        $stmt->free();
416
 
417
        //test getAll() with query parameters
418
        $query = 'SELECT user_id, user_name, user_password FROM users WHERE user_id > ? ORDER BY user_id';
419
        $values = $this->db->extended->getAll($query, array('integer', 'text', 'text'), array(2), array('integer'), MDB2_FETCHMODE_ASSOC);
420
        if (PEAR::isError($values)) {
421
            $this->assertTrue(false, 'Error fetching the result set: '.$values->getMessage());
422
        } else {
423
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
424
            for ($i=0; $i<2; $i++) {
425
                $this->assertEquals($data[$i+3]['user_id'],       $values[$i]['user_id'],       'Unexpected returned value');
426
                $this->assertEquals($data[$i+3]['user_name'],     $values[$i]['user_name'],     'Unexpected returned value');
427
                $this->assertEquals($data[$i+3]['user_password'], $values[$i]['user_password'], 'Unexpected returned value');
428
            }
429
        }
430
 
431
        //test getAll() without query parameters
432
        $query = 'SELECT user_id, user_name, user_password FROM users ORDER BY user_id';
433
        $values = $this->db->extended->getAll($query, array('integer', 'text', 'text'), null, null, MDB2_FETCHMODE_ASSOC);
434
        if (PEAR::isError($values)) {
435
            $this->assertTrue(false, 'Error fetching the result set: '.$values->getMessage());
436
        } else {
437
            $this->assertEquals($total_rows, count($values), 'Error: incorrect number of returned rows');
438
            for ($i=0; $i<$total_rows; $i++) {
439
                $this->assertEquals($data[$i]['user_id'],       $values[$i]['user_id'],       'Unexpected returned value');
440
                $this->assertEquals($data[$i]['user_name'],     $values[$i]['user_name'],     'Unexpected returned value');
441
                $this->assertEquals($data[$i]['user_password'], $values[$i]['user_password'], 'Unexpected returned value');
442
            }
443
        }
444
    }
445
 
446
    /**
447
     * Test limitQuery()
448
     *
449
     * Test fetching a limited resultset.
450
     */
451
    function testLimitQuery()
452
    {
453
        $result = $this->db->loadModule('Extended');
454
        if (PEAR::isError($result)) {
455
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
456
        }
457
 
458
        $data = array();
459
        $total_rows = 5;
460
 
461
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')', array_values($this->fields), MDB2_PREPARE_MANIP);
462
 
463
        for ($row = 0; $row < $total_rows; $row++) {
464
            $data[$row] = $this->getSampleData($row);
465
            $result = $stmt->execute(array_values($data[$row]));
466
            if (PEAR::isError($result)) {
467
                $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
468
            }
469
        }
470
        $stmt->free();
471
 
472
        $query = 'SELECT user_id, user_name, user_password FROM users ORDER BY user_id';
473
        $types = array('integer', 'text', 'text');
474
 
475
        //test limitQuery() with offset = 0
476
        $result = $this->db->extended->limitQuery($query, $types, 2);
477
        if (PEAR::isError($result)) {
478
            $this->assertTrue(false, 'Error executing limitQuery(): '.$result->getMessage());
479
        }
480
        $values = $result->fetchAll(MDB2_FETCHMODE_ASSOC);
481
        if (PEAR::isError($values)) {
482
            $this->assertTrue(false, 'Error fetching the result set');
483
        } else {
484
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
485
            for ($i=0; $i<2; $i++) {
486
                $this->assertEquals($data[$i]['user_id'],       $values[$i]['user_id'],       'Unexpected returned value');
487
                $this->assertEquals($data[$i]['user_name'],     $values[$i]['user_name'],     'Unexpected returned value');
488
                $this->assertEquals($data[$i]['user_password'], $values[$i]['user_password'], 'Unexpected returned value');
489
            }
490
        }
491
        $result->free();
492
 
493
        //test limitQuery() with offset > 0
494
        $result = $this->db->extended->limitQuery($query, $types, 3, 2);
495
        if (PEAR::isError($result)) {
496
            $this->assertTrue(false, 'Error executing limitQuery(): '.$result->getMessage());
497
        }
498
        $values = $result->fetchAll(MDB2_FETCHMODE_ASSOC);
499
        if (PEAR::isError($values)) {
500
            $this->assertTrue(false, 'Error fetching the result set');
501
        } else {
502
            $this->assertEquals(3, count($values), 'Error: incorrect number of returned rows');
503
            for ($i=0; $i<3; $i++) {
504
                $this->assertEquals($data[$i+2]['user_id'],       $values[$i]['user_id'],       'Unexpected returned value');
505
                $this->assertEquals($data[$i+2]['user_name'],     $values[$i]['user_name'],     'Unexpected returned value');
506
                $this->assertEquals($data[$i+2]['user_password'], $values[$i]['user_password'], 'Unexpected returned value');
507
            }
508
        }
509
        $result->free();
510
    }
511
 
512
    /**
513
     * Test execParam()
514
     *
515
     * Test executing a query with parameters
516
     */
517
    function testExecParam()
518
    {
519
        $result = $this->db->loadModule('Extended');
520
        if (PEAR::isError($result)) {
521
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
522
        }
523
 
524
        $data = $this->getSampleData(1234);
525
 
526
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
527
 
528
        $result = $this->db->extended->execParam($query, array_values($data), $this->fields);
529
        if (PEAR::isError($result)) {
530
            $this->assertTrue(false, 'Error executing execParam(): '.$result->getMessage());
531
        }
532
 
533
        $query = 'SELECT user_id, user_name, user_password FROM users WHERE user_id=?';
534
        $result = $this->db->extended->getRow($query, array('integer', 'text', 'text'), array(1234), array('integer'), MDB2_FETCHMODE_ASSOC);
535
        if (PEAR::isError($result)) {
536
            $this->assertTrue(false, 'Error executing getRow(): '.$result->getMessage());
537
        }
538
        $this->assertEquals($data['user_id'],       $result['user_id'],       'Unexpected returned value');
539
        $this->assertEquals($data['user_name'],     $result['user_name'],     'Unexpected returned value');
540
        $this->assertEquals($data['user_password'], $result['user_password'], 'Unexpected returned value');
541
    }
542
 
543
    /**
544
     * Test executeMultiple()
545
     *
546
     * Test executing multiple prepared queries
547
     */
548
    function testExecuteMultiple()
549
    {
550
        $result = $this->db->loadModule('Extended');
551
        if (PEAR::isError($result)) {
552
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
553
        }
554
 
555
        $data = array();
556
        $total_rows = 5;
557
 
558
        for ($row = 0; $row < $total_rows; $row++) {
559
            $data[$row] = array_values($this->getSampleData($row));
560
        }
561
 
562
        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')');
563
        $result = $this->db->extended->executeMultiple($stmt, $data);
564
        if (PEAR::isError($result)) {
565
            $this->assertTrue(false, 'Error executing executeMultiple(): '.$result->getMessage());
566
        }
567
        $stmt->free();
568
 
569
        $query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users';
570
        $values = $this->db->queryAll($query, $this->fields, MDB2_FETCHMODE_ORDERED);
571
 
572
        $n_fields = count($this->fields);
573
        for ($i=0; $i<$total_rows; $i++) {
574
            for ($field=0; $field<$n_fields; $field++) {
575
                $this->assertEquals(strval($data[$i][$field]), strval($values[$i][$field]), 'Unexpected returned value');
576
            }
577
        }
578
    }
579
}
580
?>