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_testcase.php,v 1.18 2006/12/19 15:04:53 quipo Exp $
45
 
46
class MDB2_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 MDB2 object of the db once we have connected
54
    var $db;
55
    // contains field names from the test table
56
    var $fields;
57
    // if the tables should be cleared in the setUp() and tearDown() methods
58
    var $clear_tables = true;
59
 
60
    function MDB2_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 =& MDB2::factory($this->dsn, $this->options);
69
        $this->db->setDatabase($this->database);
70
        $this->db->expectError(MDB2_ERROR_UNSUPPORTED);
71
        $this->fields = array(
72
            'user_name' => 'text',
73
            'user_password' => 'text',
74
            'subscribed' => 'boolean',
75
            'user_id' => 'integer',
76
            'quota' => 'decimal',
77
            'weight' => 'float',
78
            'access_date' => 'date',
79
            'access_time' => 'time',
80
            'approved' => 'timestamp',
81
        );
82
        $this->clearTables();
83
    }
84
 
85
    function tearDown() {
86
        $this->clearTables();
87
        $this->db->popExpect();
88
        unset($this->dsn);
89
        if (!PEAR::isError($this->db)) {
90
            $this->db->disconnect();
91
        }
92
        unset($this->db);
93
    }
94
 
95
    function clearTables() {
96
        if (!$this->clear_tables) {
97
            return;
98
        }
99
        if (PEAR::isError($this->db->exec('DELETE FROM users'))) {
100
            $this->assertTrue(false, 'Error deleting from table users');
101
        }
102
        if (PEAR::isError($this->db->exec('DELETE FROM files'))) {
103
            $this->assertTrue(false, 'Error deleting from table users');
104
        }
105
    }
106
 
107
    function supported($feature) {
108
        if (!$this->db->supports($feature)) {
109
            $this->assertTrue(false, 'This database does not support '.$feature);
110
            return false;
111
        }
112
        return true;
113
    }
114
 
115
    function verifyFetchedValues(&$result, $rownum, $data) {
116
        //$row = $result->fetchRow(MDB2_FETCHMODE_DEFAULT, $rownum);
117
        $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC, $rownum);
118
        if (!is_array($row)) {
119
            $this->assertTrue(false, 'Error result row is not an array');
120
            return;
121
        }
122
        //reset($row);
123
        foreach ($this->fields as $field => $type) {
124
            //$value = current($row);
125
            $value = $row[$field];
126
            if ($type == 'float') {
127
                $delta = 0.0000000001;
128
            } else {
129
                $delta = 0;
130
            }
131
 
132
            $this->assertEquals($data[$field], $value, "the value retrieved for field \"$field\" doesn't match what was stored into the rownum $rownum", $delta);
133
            //next($row);
134
        }
135
    }
136
 
137
    function getSampleData($row = 1) {
138
        $data = array();
139
        $data['user_name']     = 'user_' . $row;
140
        $data['user_password'] = 'somepass';
141
        $data['subscribed']    = $row % 2 ? true : false;
142
        $data['user_id']       = $row;
143
        $data['quota']         = strval($row/100);
144
        $data['weight']        = sqrt($row);
145
        $data['access_date']   = MDB2_Date::mdbToday();
146
        $data['access_time']   = MDB2_Date::mdbTime();
147
        $data['approved']      = MDB2_Date::mdbNow();
148
        return $data;
149
    }
150
 
151
    function methodExists(&$class, $name) {
152
        if (is_object($class)
153
            && in_array(strtolower($name), array_map('strtolower', get_class_methods($class)))
154
        ) {
155
            return true;
156
        }
157
        $this->assertTrue(false, 'method '. $name.' not implemented in '.get_class($class));
158
        return false;
159
    }
160
 
161
    function tableExists($table) {
162
        $this->db->loadModule('Manager', null, true);
163
        $tables = $this->db->manager->listTables();
164
        return in_array(strtolower($table), array_map('strtolower', $tables));
165
    }
166
}
167
 
168
?>