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_api_testcase.php,v 1.22 2007/04/25 09:11:35 quipo Exp $
45
 
46
require_once 'MDB2_testcase.php';
47
 
48
class MDB2_Api_TestCase extends MDB2_TestCase {
49
    var $clear_tables = false;
50
 
51
    function testParseDSN() {
52
        $expected = array (
53
            'phptype'  => 'phptype',
54
            'dbsyntax' => 'phptype',
55
            'username' => 'username',
56
            'password' => 'password',
57
            'protocol' => 'protocol',
58
            'hostspec' => false,
59
            'port'     => '110',
60
            'socket'   => false,
61
            'database' => '/usr/db_file.db',
62
            'mode'     => false,
63
        );
64
        $original = 'phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644';
65
        $this->assertEquals($expected, MDB2::parseDSN($original));
66
 
67
        // ---------------------------------------------------------------------
68
 
69
        $original = 'phptype(dbsyntax)://username:password@hostspec/database_name';
70
        $expected = array (
71
            'phptype'  => 'phptype',
72
            'dbsyntax' => 'dbsyntax',
73
            'username' => 'username',
74
            'password' => 'password',
75
            'protocol' => 'tcp',
76
            'hostspec' => 'hostspec',
77
            'port'     => false,
78
            'socket'   => false,
79
            'database' => 'database_name',
80
            'mode'     => false,
81
        );
82
        $this->assertEquals($expected, MDB2::parseDSN($original));
83
 
84
        // ---------------------------------------------------------------------
85
 
86
        $original = 'phptype://username:password@hostspec/database_name';
87
        $expected['dbsyntax'] = 'phptype';
88
        $this->assertEquals($expected, MDB2::parseDSN($original));
89
 
90
        // ---------------------------------------------------------------------
91
 
92
        $original = 'phptype://username:password@hostspec';
93
        $expected['database'] = false;
94
        $this->assertEquals($expected, MDB2::parseDSN($original));
95
 
96
        // ---------------------------------------------------------------------
97
 
98
        $original = 'phptype://username@hostspec';
99
        $expected['password'] = false;
100
        $this->assertEquals($expected, MDB2::parseDSN($original));
101
 
102
        // ---------------------------------------------------------------------
103
 
104
        $original = 'phptype://hostspec/database';
105
        $expected['username'] = false;
106
        $expected['database'] = 'database';
107
        $this->assertEquals($expected, MDB2::parseDSN($original));
108
 
109
        // ---------------------------------------------------------------------
110
 
111
        $original = 'phptype(dbsyntax)';
112
        $expected['database'] = false;
113
        $expected['hostspec'] = false;
114
        $expected['protocol'] = false;
115
        $expected['dbsyntax'] = 'dbsyntax';
116
        $this->assertEquals($expected, MDB2::parseDSN($original));
117
 
118
        // ---------------------------------------------------------------------
119
 
120
        //oracle's "Easy Connect" syntax (Oracle 10g, @see Bug #4854)
121
        $original = 'oci8://scott:tiger@//localhost/XE';
122
        $expected = array (
123
            'phptype'  => 'oci8',
124
            'dbsyntax' => 'oci8',
125
            'username' => 'scott',
126
            'password' => 'tiger',
127
            'protocol' => 'tcp',
128
            'hostspec' => '//localhost/XE',
129
            'port'     => false,
130
            'socket'   => false,
131
            'database' => false,
132
            'mode'     => false,
133
        );
134
        $this->assertEquals($expected, MDB2::parseDSN($original));
135
 
136
        // ---------------------------------------------------------------------
137
 
138
        //ibase dbname+path on windows
139
        $original = 'ibase://user:pwd@localhost/C:\\PATH_TO_DB\\TEST.FDB';
140
        $expected = array (
141
            'phptype'  => 'ibase',
142
            'dbsyntax' => 'ibase',
143
            'username' => 'user',
144
            'password' => 'pwd',
145
            'protocol' => 'tcp',
146
            'hostspec' => 'localhost',
147
            'port'     => false,
148
            'socket'   => false,
149
            'database' => 'C:\\PATH_TO_DB\\TEST.FDB',
150
            'mode'     => false,
151
        );
152
        $this->assertEquals($expected, MDB2::parseDSN($original));
153
 
154
        // ---------------------------------------------------------------------
155
 
156
        //sqlite dbname+path on unix
157
        $original = 'sqlite:////full/unix/path/to/file.db?mode=0666';
158
        $expected = array (
159
            'phptype'  => 'sqlite',
160
            'dbsyntax' => 'sqlite',
161
            'username' => false,
162
            'password' => false,
163
            'protocol' => 'tcp',
164
            'hostspec' => '',
165
            'port'     => false,
166
            'socket'   => false,
167
            'database' => '/full/unix/path/to/file.db',
168
            'mode'     => false,
169
        );
170
        $this->assertEquals($expected, MDB2::parseDSN($original));
171
    }
172
 
173
    //test stuff in common.php
174
    function testConnect() {
175
        $db =& MDB2::factory($this->dsn, $this->options);
176
        if (PEAR::isError($db)) {
177
            $this->assertTrue(false, 'Connect failed bailing out - ' .$db->getMessage() . ' - ' .$db->getUserInfo());
178
        }
179
        if (PEAR::isError($this->db)) {
180
            exit;
181
        }
182
    }
183
 
184
    function testGetOption() {
185
        if (!$this->methodExists($this->db, 'getOption')) {
186
            return;
187
        }
188
        $option = $this->db->getOption('persistent');
189
        $this->assertEquals($option, $this->db->options['persistent']);
190
    }
191
 
192
    function testSetOption() {
193
        if (!$this->methodExists($this->db, 'setOption')) {
194
            return;
195
        }
196
        $option = $this->db->getOption('persistent');
197
        $this->db->setOption('persistent', !$option);
198
        $this->assertEquals(!$option, $this->db->getOption('persistent'));
199
        $this->db->setOption('persistent', $option);
200
    }
201
 
202
    function testLoadModule() {
203
        if (!$this->methodExists($this->db, 'loadModule')) {
204
            return;
205
        }
206
        $this->assertTrue(!PEAR::isError($this->db->loadModule('Manager', null, true)));
207
    }
208
 
209
    // test of the driver
210
    // helper function so that we don't have to write out a query a million times
211
    function standardQuery() {
212
        $query = 'SELECT * FROM users';
213
        // run the query and get a result handler
214
        if (!PEAR::isError($this->db)) {
215
            return $this->db->query($query);
216
        }
217
        return false;
218
    }
219
 
220
    function testQuery() {
221
        if (!$this->methodExists($this->db, 'query')) {
222
            return;
223
        }
224
        $result = $this->standardQuery();
225
 
226
        $this->assertTrue(MDB2::isResult($result), 'query: $result returned is not a resource');
227
    }
228
 
229
    function testExec() {
230
        if (!$this->methodExists($this->db, 'exec')) {
231
            return;
232
        }
233
        $result = $this->db->exec('UPDATE users SET user_name = user_name WHERE user_id = user_id');
234
        $this->assertFalse(PEAR::isError($result), 'exec: $result returned is an error');
235
        $this->assertEquals(0, $result, 'exec: incorrect number of affected rows returned');
236
    }
237
 
238
    function testFetchRow() {
239
        $result = $this->standardQuery();
240
        if (!$this->methodExists($result, 'fetchRow')) {
241
            return;
242
        }
243
        $err = $result->fetchRow();
244
        $result->free();
245
 
246
        if (PEAR::isError($err)) {
247
            $this->assertTrue(false, 'Error testFetch: '.$err->getMessage().' - '.$err->getUserInfo());
248
        }
249
    }
250
 
251
    function testNumRows() {
252
        $result = $this->standardQuery();
253
        if (!$this->methodExists($result, 'numRows')) {
254
            return;
255
        }
256
        $numrows = $result->numRows();
257
        $this->assertTrue(!PEAR::isError($numrows) && is_int($numrows));
258
        $result->free();
259
    }
260
 
261
    function testNumCols() {
262
        $result = $this->standardQuery();
263
        if (!$this->methodExists($result, 'numCols')) {
264
            return;
265
        }
266
        $numcols = $result->numCols();
267
        $this->assertTrue(!PEAR::isError($numcols) && $numcols > 0);
268
        $result->free();
269
    }
270
 
271
    function testSingleton() {
272
        $db =& MDB2::singleton();
273
        $this->assertTrue(MDB2::isConnection($db));
274
 
275
        // should have a different database name set
276
        $db =& MDB2::singleton($this->dsn, $this->options);
277
 
278
        $this->assertTrue($db->db_index != $this->db->db_index);
279
    }
280
 
281
    function testGetServerVersion() {
282
        $server_info = $this->db->getServerVersion(true);
283
        if (PEAR::isError($server_info)) {
284
            $this->assertTrue(false, 'Error: '.$server_info->getMessage().' - '.$server_info->getUserInfo());
285
        } else {
286
            $this->assertTrue(is_string($server_info), 'Error: Server info is not returned as a string: '. serialize($server_info));
287
        }
288
        $server_info = $this->db->getServerVersion();
289
        if (PEAR::isError($server_info)) {
290
            $this->assertTrue(false, 'Error: '.$server_info->getMessage().' - '.$server_info->getUserInfo());
291
        } else {
292
            $this->assertTrue(is_array($server_info), 'Error: Server info is not returned as an array: '. serialize($server_info));
293
        }
294
    }
295
}
296
 
297
?>