DatabaseTest.php 13.3 KB
Newer Older
1
2
3
4
5
6
7
<?php
/**
 * @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
 */

require_once(__DIR__ . '/AbstractDatabaseTest.php');
require_once(__DIR__ . '/../../qfq/Database.php');
8
require_once(__DIR__ . '/../../qfq/exceptions/DbException.php');
9
10
11

class DatabaseTest extends AbstractDatabaseTest {

12
    /**
13
     * @throws \qfq\DbException
14
     */
15
    public function testFetchAll() {
16
        $allRows = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 2');
17
18
19
20
21
22

        $this->assertCount(2, $allRows);
        $this->assertEquals(1, $allRows[0]['id']);
        $this->assertEquals(2, $allRows[1]['id']);
    }

23
    /**
24
     * @throws \qfq\DbException
25
     */
26
    public function testFetchAllEmpty() {
27
        $allRows = $this->db->sql('SELECT * FROM Person WHERE id=0 ORDER BY id');
28

29
        $this->assertEquals(array(), $allRows);
30
31
    }

32
    /**
33
     * @throws \qfq\DbException
34
     */
35
36
37
38
39
40
41
    public function testQuerySimple() {

        $expected = [
            [
                'id' => '1',
                'name' => 'Doe',
                'firstname' => 'John',
42
43
                'gender' => 'male',
                'groups' => 'c'
44
45
46
47
48
            ],
            [
                'id' => '2',
                'name' => 'Smith',
                'firstname' => 'Jane',
49
50
                'gender' => 'female',
                'groups' => 'a,c'
51
52
53
            ],
        ];

54
55
56
57
58
59

        // Check read rows
        $dataArray = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 1');
        // Check count
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));

60
        // Check read rows
61
        $dataArray = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 2');
62

63
64
65
        // Check count
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
        // Compare rows
66
        $this->assertEquals($expected, $dataArray);
67
68
69
        // Check rows

        // Same as above, but specify 'ROW_REGULAR'
70
        $dataArray = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 2', ROW_REGULAR);
71
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
72
        // Check rows
73
        $this->assertEquals($expected, $dataArray);
74

75
76
        // ROW_EXPECT_0
        $dataArray = $this->db->sql('SELECT * FROM Person WHERE id=0', ROW_EXPECT_0);
77
        $this->assertEquals(0, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
78
        // Check rows
79
        $this->assertEquals(array(), $dataArray);
80
81
82

        // ROW_EXPECT_1
        $dataArray = $this->db->sql('SELECT * FROM Person WHERE id=1', ROW_EXPECT_1);
83
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
84
        // Check rows
85
        $this->assertEquals($expected[0], $dataArray);
86

87
88
        // ROW_EXPECT_0_1: 1
        $dataArray = $this->db->sql('SELECT * FROM Person WHERE id=1', ROW_EXPECT_0_1);
89
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
90
        // Check rows
91
        $this->assertEquals($expected[0], $dataArray);
92

93
94
        // ROW_EXPECT_0_1: 0
        $dataArray = $this->db->sql('SELECT * FROM Person WHERE id=0', ROW_EXPECT_0_1);
95
        $this->assertEquals(0, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
96
        // Check rows
97
        $this->assertEquals(array(), $dataArray);
98

99
100
        //ROW_EXPECT_GE_1 - one record
        $dataArray = $this->db->sql('SELECT * FROM Person WHERE id=1', ROW_EXPECT_GE_1);
101
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
102
        // Check rows
103
        $this->assertEquals([$expected[0]], $dataArray);
104
105
106

        // ROW_EXPECT_GE_1 - two records
        $dataArray = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 2', ROW_EXPECT_GE_1);
107
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
108
        // Check rows
109
        $this->assertEquals($expected, $dataArray);
110

111
        // Check Implode: 2 records
112
        $data = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 2', ROW_IMPLODE_ALL);
113
114
115
        $this->assertEquals(implode($expected[0]) . implode($expected[1]), $data);

        // Check Implode: 1 record
116
        $data = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 1', ROW_IMPLODE_ALL);
117
118
119
        $this->assertEquals(implode($expected[0]), $data);

        // Check Implode 0 record
120
        $data = $this->db->sql('SELECT * FROM Person WHERE id=0 ORDER BY id LIMIT 2', ROW_IMPLODE_ALL);
121
        $this->assertEquals('', $data);
122
123
124
125
126
127
128
129
130
131
132
133

        $data = $this->db->sql('SELECT "a", "b", "c"', ROW_IMPLODE_ALL);
        $this->assertEquals('abc', $data);

        // Multiple columns with same columnname
        $data = $this->db->sql('SELECT "a", "b", "a"', ROW_IMPLODE_ALL);
        $this->assertEquals('aba', $data);

        // Multiple columns with same columnname
        $data = $this->db->sql('SELECT id, name, firstname, name, id FROM Person', ROW_IMPLODE_ALL);
        $this->assertEquals('1DoeJohnDoe12SmithJaneSmith2', $data);

134
135
    }

136
    /**
137
     * @throws \qfq\DbException
138
     */
Carsten  Rose's avatar
Carsten Rose committed
139
    public function testQuerySimpleParameter() {
140
141
142
        $stat = array();
        $dummy = array();

Carsten  Rose's avatar
Carsten Rose committed
143
        // Parameter Susbstitution by '?'
144
        $dataArray = $this->db->sql('SELECT * FROM Person WHERE name LIKE ? ORDER BY id', ROW_REGULAR, ['Smith']);
Carsten  Rose's avatar
Carsten Rose committed
145
        // Check count
146
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
Carsten  Rose's avatar
Carsten Rose committed
147

148
149
        $dataArray = $this->db->sql('UPDATE Person SET groups = ?', ROW_REGULAR, ['a,b,c']);
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
Carsten  Rose's avatar
Carsten Rose committed
150

151
152
153
        $dataArray = $this->db->sql('INSERT INTO Person (`name`, `firstname`, `groups`) VALUES ( ?, ? ,? )', ROW_REGULAR, ['Meier', 'John', 'a'], 'fake', $dummy, $stat);
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
        $this->assertEquals(3, $stat[DB_INSERT_ID]);
Carsten  Rose's avatar
Carsten Rose committed
154

155
156
157
        $dataArray = $this->db->sql('INSERT INTO Person (`name`, `firstname`, `groups`) VALUES ( ?, ? ,? )', ROW_REGULAR, ['Meier', 'Jan', 'b'], 'fake', $dummy, $stat);
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
        $this->assertEquals(4, $stat[DB_INSERT_ID]);
158

159
160
        $dataArray = $this->db->sql('DELETE FROM Person WHERE name = ?', ROW_REGULAR, ['Meier']);
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
161

162
    }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

    /**
     * @expectedException \qfq\DbException
     */
    public function testSqlExceptionExpect0() {
        $data = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 1', ROW_EXPECT_0);
    }

    /**
     * @expectedException \qfq\DbException
     */
    public function testSqlExceptionExpect1_0() {
        $data = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 0', ROW_EXPECT_1);
    }

    /**
     * @expectedException \qfq\DbException
     */
    public function testSqlExceptionExpect1_2() {
        $data = $this->db->sql('SELECT * FROM Person ORDER BY id ', ROW_EXPECT_1);
    }

    /**
     * @expectedException \qfq\DbException
     */
    public function testSqlExceptionExpect01() {
        $data = $this->db->sql('SELECT * FROM Person ORDER BY id ', ROW_EXPECT_0_1);
    }

    /**
     * @expectedException \qfq\DbException
     */
    public function testSqlExceptionExpectGE1() {
        $data = $this->db->sql('SELECT * FROM Person ORDER BY id LIMIT 0', ROW_EXPECT_GE_1);
    }

199
    /**
200
     * @expectedException \qfq\DbException
201
     */
Carsten  Rose's avatar
Carsten Rose committed
202
    public function testSanitizeException() {
203
        $this->db->sql('some garbage');
204
205
    }

206
    /**
207
     * @throws \qfq\DbException
208
     */
209
    public function testParameters() {
210
211
212
        //TODO: ParameterName
//        $this->db->prepareExecute('SELECT * FROM Person WHERE id = :valueOfA', [':valueOfA' => 2]);
//        $this->assertEquals(1, $this->db->getRowCount());
213
214
    }

215
216
217
    /**
     *
     */
218
    public function testGetSetValueList() {
219
        $valueList = $this->db->getEnumSetValueList('Person', 'gender');
220
221
222
223
        $expected = [0 => '', 1 => 'male', 2 => 'female'];
        $this->assertEquals($expected, $valueList);

        $expected = [0 => '', 1 => 'a', 2 => 'b', 3 => 'c'];
224
        $valueList = $this->db->getEnumSetValueList('Person', 'groups');
225
226
227
        $this->assertEquals($expected, $valueList);
    }

228
    /**
229
     * @throws \qfq\DbException
230
     */
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    public function testSelectReturn() {
        $dummy = array();
        $stat = array();

        $sql = "SELECT name FROM Person ORDER BY id";

        $rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
        $this->assertEquals([0 => ['name' => 'Doe'], 1 => ['name' => 'Smith']], $rc);

        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
        $this->assertEquals(2, $stat[DB_NUM_ROWS]);
    }

    /**
     * @throws \qfq\DbException
     */
    public function testShowReturn() {
        $dummy = array();
        $stat = array();

        $sql = "SHOW TABLES";

        $rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);

        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
Carsten  Rose's avatar
Carsten Rose committed
256
        $this->assertEquals(6, $stat[DB_NUM_ROWS]);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    }

    /**
     * @throws \qfq\DbException
     */
    public function testExplainReturn() {
        $dummy = array();
        $stat = array();

        $sql = "EXPLAIN Person";

        $rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);

        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
        $this->assertEquals(5, $stat[DB_NUM_ROWS]);
    }

    /**
     * @throws \qfq\DbException
     */
    public function testInsertReturn() {
278
279
        $dummy = array();
        $stat = array();
280

281
        $sql = "INSERT INTO Person (id, name, firstname, gender, groups) VALUES (NULL, 'Doe', 'Jonni', 'male','')";
282

283
284
        $rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
285
        $this->assertEquals(3, $stat[DB_INSERT_ID]);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        $this->assertEquals(1, $stat[DB_AFFECTED_ROWS]);

        $this->assertEquals(3, $rc);
    }

    /**
     * @throws \qfq\DbException
     */
    public function testUpdateReturn() {
        $dummy = array();
        $stat = array();

        $sql = "UPDATE Person SET groups = 'a'";

        $rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);

        $this->assertEquals(2, $rc);

        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
        $this->assertEquals(2, $stat[DB_AFFECTED_ROWS]);
    }

    /**
     * @throws \qfq\DbException
     */
    public function testDeleteReturn() {
        $dummy = array();
        $stat = array();

        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);

        $sql = "DELETE FROM Person WHERE id=2";

        $rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);

        $this->assertEquals(1, $rc);

        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
        $this->assertEquals(1, $stat[DB_AFFECTED_ROWS]);

    }


    /**
     * @throws \qfq\DbException
     */
    public function testReplaceReturn() {
        $dummy = array();
        $stat = array();

        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);

        $sql = "REPLACE INTO Person (id, name, firstname, gender, groups) VALUES (1, 'Doe', 'John', 'male','')";

        $rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
        $this->assertEquals(1, $stat[DB_INSERT_ID]);
        $this->assertEquals(2, $stat[DB_AFFECTED_ROWS]);

        $this->assertEquals(1, $rc);


        $sql = "REPLACE INTO Person (id, name, firstname, gender, groups) VALUES (100, 'Lincoln', 'Abraham', 'male','')";

        $rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
        $this->assertEquals(100, $stat[DB_INSERT_ID]);
        $this->assertEquals(1, $stat[DB_AFFECTED_ROWS]);

        $this->assertEquals(100, $rc);
356
357
    }

358
359
360
361
362
363
364
365
    /**
     *
     */
    public function testGetTableDefinition() {
        $expected = [
            ['Field' => 'id', 'Type' => 'bigint(20)', 'Null' => 'NO', 'Key' => 'PRI', 'Default' => '', 'Extra' => 'auto_increment'],
            ['Field' => 'name', 'Type' => 'varchar(128)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => ''],
            ['Field' => 'firstname', 'Type' => 'varchar(128)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => ''],
366
            ['Field' => 'gender', 'Type' => "enum('','male','female')", 'Null' => 'NO', 'Key' => '', 'Default' => 'male', 'Extra' => ''],
367
368
369
            ['Field' => 'groups', 'Type' => "set('','a','b','c')", 'Null' => 'NO', 'Key' => '', 'Default' => '', 'Extra' => ''],
        ];

370
        $this->assertEquals($expected, $this->db->getTableDefinition('Person'));
371
372
    }

373

374
375
376
377
378
379
380
381
    public function testSqlKeys() {
        $keys = array();

        $expected = [
            ['0' => '1', '1' => 'Doe', '2' => '0'],
            ['0' => '2', '1' => 'Smith', '2' => '0'],
        ];
        // Same as above, but specify 'ROW_REGULAR'
382
383
        $stat = array();
        $dataArray = $this->db->sqlKeys('SELECT id AS "id", name, "0" AS "id" FROM Person ORDER BY id LIMIT 3', $keys, $stat);
384
385
386
387
388
389
390

        // Check rows
        $this->assertEquals($expected, $dataArray);

        // Check keys
        $this->assertEquals(['id', 'name', 'id'], $keys);
    }
391

392
393
394
    /**
     * @throws Exception
     */
395
396
397
398
399
400
    protected function setUp() {
        parent::setUp();

        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
    }
}