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 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
// | Author: Lorenzo Alberton <l dot alberton at quipo dot it>            |
42
// +----------------------------------------------------------------------+
43
//
44
// $Id: MDB2_function_testcase.php,v 1.20 2006/08/07 20:15:57 lsmith Exp $
45
 
46
class MDB2_Function_TestCase extends MDB2_TestCase
47
{
48
    function setUp() {
49
        parent::setUp();
50
        $this->db->loadModule('Function', null, true);
51
    }
52
 
53
    /**
54
     * Test functionTable()
55
     */
56
    function testFunctionTable()
57
    {
58
        if (!$this->methodExists($this->db->function, 'functionTable')) {
59
            return;
60
        }
61
 
62
        $functionTable_clause = $this->db->function->functionTable();
63
        $query = 'SELECT 1 '.$functionTable_clause;
64
        $result = $this->db->queryOne($query);
65
        if (PEAR::isError($result)) {
66
            $this->assertFalse(true, 'Error fetching from function table');
67
        } else {
68
            $this->assertEquals('1', $result, 'Error fetching value from function table');
69
        }
70
    }
71
 
72
    /**
73
     * Test now()
74
     */
75
    function testNow()
76
    {
77
        if (!$this->methodExists($this->db->function, 'now')) {
78
            return;
79
        }
80
 
81
        $tests = array(
82
            'timestamp' => '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/',
83
            'date' => '/^\d{4}-\d{2}-\d{2}$/',
84
            'time' => '/^\d{2}:\d{2}:\d{2}$/',
85
        );
86
 
87
        foreach ($tests as $type => $regexp) {
88
            $functionTable_clause = $this->db->function->functionTable();
89
            $now_clause = $this->db->function->now($type);
90
            $query = 'SELECT '.$now_clause . $functionTable_clause;
91
            $result = $this->db->queryOne($query, $type);
92
            if (PEAR::isError($result)) {
93
                $this->assertFalse(true, 'Error getting '.$type);
94
            } else {
95
                $this->assertRegExp($regexp, $result, 'Error: not a proper '.$type);
96
            }
97
        }
98
    }
99
 
100
    /**
101
     * Test substring()
102
     */
103
    function testSubstring()
104
    {
105
        if (!$this->methodExists($this->db->function, 'substring')) {
106
            return;
107
        }
108
        $data = $this->getSampleData(1234);
109
 
110
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
111
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
112
 
113
        $result = $stmt->execute(array_values($data));
114
        $stmt->free();
115
 
116
        if (PEAR::isError($result)) {
117
            $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
118
        }
119
 
120
        $substring_clause = $this->db->function->substring('user_name', 1, 4);
121
        $query = 'SELECT '.$substring_clause .' FROM users';
122
        $result = $this->db->queryOne($query);
123
        if (PEAR::isError($result)) {
124
            $this->assertFalse(true, 'Error getting substring');
125
        } else {
126
            $this->assertEquals('user', $result, 'Error: substrings not equals');
127
        }
128
 
129
        $substring_clause = $this->db->function->substring('user_name', 5, 1);
130
        $query = 'SELECT '.$substring_clause .' FROM users';
131
        $result = $this->db->queryOne($query);
132
        if (PEAR::isError($result)) {
133
            $this->assertFalse(true, 'Error getting substring');
134
        } else {
135
            $this->assertEquals('_', $result, 'Error: substrings not equals');
136
        }
137
 
138
        //test NULL 2nd parameter
139
        $substring_clause = $this->db->function->substring('user_name', 6);
140
        $query = 'SELECT '.$substring_clause .' FROM users';
141
        $result = $this->db->queryOne($query);
142
        if (PEAR::isError($result)) {
143
            $this->assertFalse(true, 'Error getting substring');
144
        } else {
145
            $this->assertEquals('1234', $result, 'Error: substrings not equals');
146
        }
147
    }
148
 
149
    /**
150
     * Test concat()
151
     */
152
    function testConcat()
153
    {
154
        if (!$this->methodExists($this->db->function, 'concat')) {
155
            return;
156
        }
157
 
158
        $functionTable_clause = $this->db->function->functionTable();
159
        $concat_clause = $this->db->function->concat($this->db->quote('time', 'text'), $this->db->quote('stamp', 'text'));
160
        $query = 'SELECT '.$concat_clause . $functionTable_clause;
161
        $result = $this->db->queryOne($query);
162
        if (PEAR::isError($result)) {
163
            $this->assertFalse(true, 'Error getting concat');
164
        } else {
165
            $this->assertEquals('timestamp', $result, 'Error: could not concatenate "time+stamp"');
166
        }
167
    }
168
 
169
    /**
170
     * Test random()
171
     */
172
    function testRandom()
173
    {
174
        if (!$this->methodExists($this->db->function, 'random')) {
175
            return;
176
        }
177
 
178
        $rand_clause = $this->db->function->random();
179
        $functionTable_clause = $this->db->function->functionTable();
180
        $query = 'SELECT '.$rand_clause . $functionTable_clause;
181
        $result = $this->db->queryOne($query, 'float');
182
        if (PEAR::isError($result)) {
183
            $this->assertFalse(true, 'Error getting random value:'. $result->getMessage());
184
        } else {
185
            $this->assertTrue(($result >= 0 && $result <= 1), 'Error: could not get random value between 0 and 1: '.$result);
186
        }
187
    }
188
 
189
    /**
190
     * Test lower()
191
     */
192
    function testLower()
193
    {
194
        if (!$this->methodExists($this->db->function, 'lower')) {
195
            return;
196
        }
197
        $string = $this->db->quote('FoO');
198
        $lower_clause = $this->db->function->lower($string);
199
        $functionTable_clause = $this->db->function->functionTable();
200
        $query = 'SELECT '.$lower_clause . $functionTable_clause;
201
        $result = $this->db->queryOne($query, 'text');
202
        if (PEAR::isError($result)) {
203
            $this->assertFalse(true, 'Error getting lower case value:'. $result->getMessage());
204
        } else {
205
            $this->assertTrue(($result === 'foo'), 'Error: could not lower case "FoO": '.$result);
206
        }
207
    }
208
 
209
    /**
210
     * Test upper()
211
     */
212
    function testUpper()
213
    {
214
        if (!$this->methodExists($this->db->function, 'upper')) {
215
            return;
216
        }
217
        $string = $this->db->quote('FoO');
218
        $upper_clause = $this->db->function->upper($string);
219
        $functionTable_clause = $this->db->function->functionTable();
220
        $query = 'SELECT '.$upper_clause . $functionTable_clause;
221
        $result = $this->db->queryOne($query, 'text');
222
        if (PEAR::isError($result)) {
223
            $this->assertFalse(true, 'Error getting upper case value:'. $result->getMessage());
224
        } else {
225
            $this->assertTrue(($result === 'FOO'), 'Error: could not upper case "FoO": '.$result);
226
        }
227
    }
228
}
229
?>