DatabaseTest.php 21.2 KB
Newer Older
1
2
<?php
/**
3
 * @author Carsten Rose <carsten.rose@math.uzh.ch>
4
5
 */

Marc Egger's avatar
Marc Egger committed
6
7
8
namespace IMATHUZH\Qfq\Tests\Unit\Core\Database;

 
9

10
11
require_once(__DIR__ . '/AbstractDatabaseTest.php');

12
13
14
/**
 * Class DatabaseTest
 */
15
16
class DatabaseTest extends AbstractDatabaseTest {

17
    /**
18
19
20
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
21
     */
22
    public function testFetchAll() {
23
        $allRows = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id LIMIT 2');
24
25
26
27
28
29

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

30
    /**
31
32
33
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
34
     */
35
    public function testFetchAllEmpty() {
36
        $allRows = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person WHERE id=0 ORDER BY id');
37

38
        $this->assertEquals(array(), $allRows);
39
40
    }

41
    /**
Marc Egger's avatar
Marc Egger committed
42
     * @throws \CodeException
43
     * @throws \DbException
Marc Egger's avatar
Marc Egger committed
44
     * @throws \UserFormException
45
     */
46
47
48
49
    public function testQuerySimple() {

        $expected = [
            [
Carsten  Rose's avatar
Carsten Rose committed
50
51
                'id' => '1',
                'name' => 'Doe',
52
                'firstName' => 'John',
Carsten  Rose's avatar
Carsten Rose committed
53
                'adrId' => '0',
54
                'gender' => 'male',
Carsten  Rose's avatar
Carsten Rose committed
55
                'groups' => 'c',
56
57
            ],
            [
Carsten  Rose's avatar
Carsten Rose committed
58
59
                'id' => '2',
                'name' => 'Smith',
60
                'firstName' => 'Jane',
Carsten  Rose's avatar
Carsten Rose committed
61
                'adrId' => '0',
62
                'gender' => 'female',
Carsten  Rose's avatar
Carsten Rose committed
63
                'groups' => 'a,c',
64
65
66
            ],
        ];

67
68

        // Check read rows
69
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id LIMIT 1');
70
71
72
        // Check count
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));

73
        // Check read rows
74
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id,name,firstName,adrId,gender,`groups` FROM Person ORDER BY id LIMIT 2');
75

76
77
78
        // Check count
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
        // Compare rows
79
        $this->assertEquals($expected, $dataArray);
80
81
82
        // Check rows

        // Same as above, but specify 'ROW_REGULAR'
83
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id,name,firstName,adrId,gender,`groups` FROM Person ORDER BY id LIMIT 2', ROW_REGULAR);
84
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
85
        // Check rows
86
        $this->assertEquals($expected, $dataArray);
87

88
        // ROW_EXPECT_0
89
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person WHERE id=0', ROW_EXPECT_0);
90
        $this->assertEquals(0, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
91
        // Check rows
92
        $this->assertEquals(array(), $dataArray);
93
94

        // ROW_EXPECT_1
95
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id,name,firstName,adrId,gender,`groups` FROM Person WHERE id=1', ROW_EXPECT_1);
96
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
97
        // Check rows
98
        $this->assertEquals($expected[0], $dataArray);
99

100
        // ROW_EXPECT_0_1: 1
101
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id,name,firstName,adrId,gender,`groups` FROM Person WHERE id=1', ROW_EXPECT_0_1);
102
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
103
        // Check rows
104
        $this->assertEquals($expected[0], $dataArray);
105

106
        // ROW_EXPECT_0_1: 0
107
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person WHERE id=0', ROW_EXPECT_0_1);
108
        $this->assertEquals(0, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
109
        // Check rows
110
        $this->assertEquals(array(), $dataArray);
111

112
        //ROW_EXPECT_GE_1 - one record
113
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id,name,firstName,adrId,gender,`groups` FROM Person WHERE id=1', ROW_EXPECT_GE_1);
114
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
115
        // Check rows
116
        $this->assertEquals([$expected[0]], $dataArray);
117
118

        // ROW_EXPECT_GE_1 - two records
119
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id,name,firstName,adrId,gender,`groups` FROM Person ORDER BY id LIMIT 2', ROW_EXPECT_GE_1);
120
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
121
        // Check rows
122
        $this->assertEquals($expected, $dataArray);
123

124
        // Check Implode: 2 records
125
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id,name,firstName,adrId,gender,`groups` FROM Person ORDER BY id LIMIT 2', ROW_IMPLODE_ALL);
126
127
128
        $this->assertEquals(implode($expected[0]) . implode($expected[1]), $data);

        // Check Implode: 1 record
129
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id,name,firstName,adrId,gender,`groups` FROM Person ORDER BY id LIMIT 1', ROW_IMPLODE_ALL);
130
131
132
        $this->assertEquals(implode($expected[0]), $data);

        // Check Implode 0 record
133
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person WHERE id=0 ORDER BY id LIMIT 2', ROW_IMPLODE_ALL);
134
        $this->assertEquals('', $data);
135

136
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT "a", "b", "c"', ROW_IMPLODE_ALL);
137
138
139
        $this->assertEquals('abc', $data);

        // Multiple columns with same columnname
140
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT "a", "b", "a"', ROW_IMPLODE_ALL);
141
142
143
        $this->assertEquals('aba', $data);

        // Multiple columns with same columnname
144
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id, name, firstname, name, id FROM Person', ROW_IMPLODE_ALL);
145
146
        $this->assertEquals('1DoeJohnDoe12SmithJaneSmith2', $data);

147
148
    }

149
    /**
Marc Egger's avatar
Marc Egger committed
150
     * @throws \CodeException
151
     * @throws \DbException
Marc Egger's avatar
Marc Egger committed
152
     * @throws \UserFormException
153
     */
Carsten  Rose's avatar
Carsten Rose committed
154
    public function testQuerySimpleParameter() {
155
156
157
        $stat = array();
        $dummy = array();

Carsten  Rose's avatar
Carsten Rose committed
158
        // Parameter Susbstitution by '?'
159
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person WHERE name LIKE ? ORDER BY id', ROW_REGULAR, ['Smith']);
Carsten  Rose's avatar
Carsten Rose committed
160
        // Check count
161
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
Carsten  Rose's avatar
Carsten Rose committed
162

163
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('UPDATE Person SET `groups` = ?', ROW_REGULAR, ['a,b,c']);
164
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
Carsten  Rose's avatar
Carsten Rose committed
165

166
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('INSERT INTO Person (`name`, `firstname`, `groups`) VALUES ( ?, ? ,? )', ROW_REGULAR, ['Meier', 'John', 'a'], 'fake', $dummy, $stat);
167
168
        $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
169

170
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('INSERT INTO Person (`name`, `firstname`, `groups`) VALUES ( ?, ? ,? )', ROW_REGULAR, ['Meier', 'Jan', 'b'], 'fake', $dummy, $stat);
171
172
        $this->assertEquals(1, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
        $this->assertEquals(4, $stat[DB_INSERT_ID]);
173

174
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sql('DELETE FROM Person WHERE name = ?', ROW_REGULAR, ['Meier']);
175
        $this->assertEquals(2, $this->store->getVar(SYSTEM_SQL_COUNT, STORE_SYSTEM));
176

177
    }
178
179

    /**
Marc Egger's avatar
Marc Egger committed
180
     * @expectedException DbException
181
182
183
184
     *
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
185
186
     */
    public function testSqlExceptionExpect0() {
187
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id LIMIT 1', ROW_EXPECT_0);
188
189
190
    }

    /**
Marc Egger's avatar
Marc Egger committed
191
     * @expectedException DbException
192
193
194
195
     *
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
196
197
     */
    public function testSqlExceptionExpect1_0() {
198
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id LIMIT 0', ROW_EXPECT_1);
199
200
201
    }

    /**
Marc Egger's avatar
Marc Egger committed
202
     * @expectedException DbException
203
204
205
206
     *
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
207
208
     */
    public function testSqlExceptionExpect1_2() {
209
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id ', ROW_EXPECT_1);
210
211
212
    }

    /**
Marc Egger's avatar
Marc Egger committed
213
     * @expectedException DbException
214
215
216
217
     *
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
218
219
     */
    public function testSqlExceptionExpect01() {
220
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id ', ROW_EXPECT_0_1);
221
222
223
    }

    /**
Marc Egger's avatar
Marc Egger committed
224
     * @expectedException DbException
225
226
227
228
     *
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
229
230
     */
    public function testSqlExceptionExpectGE1() {
231
        $data = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id LIMIT 0', ROW_EXPECT_GE_1);
232
233
    }

234
    /**
Marc Egger's avatar
Marc Egger committed
235
     * @expectedException DbException
236
237
238
239
     *
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
240
     */
Carsten  Rose's avatar
Carsten Rose committed
241
    public function testSanitizeException() {
242
        $this->dbArray[DB_INDEX_DEFAULT]->sql('some garbage');
243
244
    }

Carsten  Rose's avatar
Carsten Rose committed
245
246
247
248
249
250
251
252
253
254
255
    /**
     * @expectedException DbException
     *
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     */
    public function testReportException() {
        $this->dbArray[DB_INDEX_DEFAULT]->sql("INSERT INTO Person (`id`,`name`) VALUES (1,'test')");
    }

256
    /**
257
     * Check to skip mysqli errno 1060
258
     *
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     */
    public function testSkipErrno() {
        $dummy = array();
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("ALTER TABLE  `Person` ADD  `name` CHAR(16) NOT NULL  AFTER  `id`",
            ROW_REGULAR, array(), '', $dummy, $dummy, [1060]);

        $this->assertEquals('-1', $result);
    }

    /**
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
275
     */
276
    public function testGetSetValueList() {
277
        $valueList = $this->dbArray[DB_INDEX_DEFAULT]->getEnumSetValueList('Person', 'gender');
278
279
280
281
        $expected = [0 => '', 1 => 'male', 2 => 'female'];
        $this->assertEquals($expected, $valueList);

        $expected = [0 => '', 1 => 'a', 2 => 'b', 3 => 'c'];
282
        $valueList = $this->dbArray[DB_INDEX_DEFAULT]->getEnumSetValueList('Person', 'groups');
283
284
285
        $this->assertEquals($expected, $valueList);
    }

286
    /**
287
288
289
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
290
     */
291
292
293
294
295
296
    public function testSelectReturn() {
        $dummy = array();
        $stat = array();

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

297
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
298
299
300
301
302
303
304
        $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]);
    }

    /**
305
306
307
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
308
309
310
311
312
313
314
     */
    public function testShowReturn() {
        $dummy = array();
        $stat = array();

        $sql = "SHOW TABLES";

315
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
316
317

        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
Carsten  Rose's avatar
Carsten Rose committed
318
319
320
321
322
        $all = '';
        foreach ($rc as $val) {
            $all .= '-' . implode('-', $val);
        }

Carsten  Rose's avatar
Carsten Rose committed
323
        $this->assertEquals(3, $stat[DB_NUM_ROWS]);
324
325
326
    }

    /**
327
328
329
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
330
331
332
333
334
335
336
     */
    public function testExplainReturn() {
        $dummy = array();
        $stat = array();

        $sql = "EXPLAIN Person";

337
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
338
339

        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
340
        $this->assertEquals(8, $stat[DB_NUM_ROWS]);
341
342
343
    }

    /**
344
345
346
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
347
348
     */
    public function testInsertReturn() {
349
350
        $dummy = array();
        $stat = array();
351

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

354
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
355
        // DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
356
        $this->assertEquals(3, $stat[DB_INSERT_ID]);
357
358
359
360
361
362
        $this->assertEquals(1, $stat[DB_AFFECTED_ROWS]);

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

    /**
363
364
365
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
366
367
368
369
370
     */
    public function testUpdateReturn() {
        $dummy = array();
        $stat = array();

371
        $sql = "UPDATE Person SET `groups` = 'a'";
372

373
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
374
375
376
377
378
379
380
381

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

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

    /**
382
     * @throws \Exception
383
384
385
386
387
388
389
390
391
     */
    public function testDeleteReturn() {
        $dummy = array();
        $stat = array();

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

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

392
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
393
394
395
396
397
398
399
400
401
402

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

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

    }


    /**
403
     * @throws \Exception
404
405
406
407
408
409
410
     */
    public function testReplaceReturn() {
        $dummy = array();
        $stat = array();

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

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

413
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
414
415
416
417
418
419
420
        // 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);


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

423
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
424
425
426
427
428
        // 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);
429
430
    }

431
432
433
434
435
436
437
    /**
     *
     */
    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' => ''],
438
439
            ['Field' => 'firstName', 'Type' => 'varchar(128)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => ''],
            ['Field' => 'adrId', 'Type' => 'int(11)', 'Null' => 'NO', 'Key' => '', 'Default' => '0', 'Extra' => ''],
440
            ['Field' => 'gender', 'Type' => "enum('','male','female')", 'Null' => 'NO', 'Key' => '', 'Default' => 'male', 'Extra' => ''],
441
            ['Field' => 'groups', 'Type' => "set('','a','b','c')", 'Null' => 'NO', 'Key' => '', 'Default' => '', 'Extra' => ''],
442
443
            ['Field' => 'modified', 'Type' => "timestamp", 'Null' => 'NO', 'Key' => '', 'Default' => 'CURRENT_TIMESTAMP', 'Extra' => 'on update CURRENT_TIMESTAMP'],
            ['Field' => 'created', 'Type' => "datetime", 'Null' => 'NO', 'Key' => '', 'Default' => '', 'Extra' => ''],
444
445
        ];

446
        $this->assertEquals($expected, $this->dbArray[DB_INDEX_DEFAULT]->getTableDefinition('Person'));
447
448
    }

449
    /**
450
451
452
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
453
     */
454
455
456
457
458
459
460
461
    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'
462
        $stat = array();
463
        $dataArray = $this->dbArray[DB_INDEX_DEFAULT]->sqlKeys('SELECT id AS "id", name, "0" AS "id" FROM Person ORDER BY id LIMIT 3', $keys, $stat);
464
465
466
467
468
469
470

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

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

472
473
474
475
476
    /**
     *
     */
    public function testhasLimit() {

477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
        $this->assertEquals(false, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit(''));
        $this->assertEquals(false, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT'));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1'));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1 '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT  1  '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1,1'));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1,1 '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT  1,1 '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT  1,1  '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1 ,1 '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1 , 1 '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1 ,  1 '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1  ,  1 '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT 1  ,  1  '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT  1  ,  1  '));
        $this->assertEquals(true, $this->dbArray[DB_INDEX_DEFAULT]->hasLimit('SELECT ... LIMIT   1   ,   1   '));
493
494
495
496
    }

    public function testMakeArrayDict() {

497
        $this->assertEquals(array(), $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict(array(), 'key', 'value'));
498
499
500

        $arr[] = ['red', 'green'];
        $expect[] = ['key' => 'red', 'value' => 'green'];
501
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
502
503
504

        $arr[] = ['blue', 'orange'];
        $expect[] = ['key' => 'blue', 'value' => 'orange'];
505
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
506
507

        $arr = [['key1' => 'red', 'value1' => 'green'], ['key1' => 'blue', 'value1' => 'orange']];
508
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
509
510

        $arr = [['key' => 'red', 'value1' => 'green'], ['key' => 'blue', 'value1' => 'orange']];
511
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
512
513

        $arr = [['key1' => 'red', 'value' => 'green'], ['key1' => 'blue', 'value' => 'orange']];
514
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
515
516

        $arr = [['key' => 'red', 'value' => 'green'], ['key' => 'blue', 'value' => 'orange']];
517
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
518
519

        $arr = [['value' => 'green', 'key' => 'red'], ['value' => 'orange', 'key' => 'blue']];
520
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
521
522
523

        $arr = [['green'], ['orange']];
        $expect = [['key' => 'green', 'value' => 'green'], ['key' => 'orange', 'value' => 'orange']];
524
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
525
526

        $arr = [['value1' => 'green'], ['value1' => 'orange']];
527
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
528
529

        $arr = [['key' => 'green'], ['key' => 'orange']];
530
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
531
532

        $arr = [['value' => 'green'], ['value' => 'orange']];
533
        $this->assertEquals($expect, $this->dbArray[DB_INDEX_DEFAULT]->makeArrayDict($arr, 'key', 'value'));
534
535
536

    }

537
    /**
Marc Egger's avatar
Marc Egger committed
538
539
540
541
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
542
     * @throws \Exception
543
     */
544
545
546
    protected function setUp() {
        parent::setUp();

Carsten  Rose's avatar
Carsten Rose committed
547
        // remove existing tables
548
        $allTables = $this->dbArray[DB_INDEX_DEFAULT]->sql("SHOW TABLES", ROW_REGULAR);
Carsten  Rose's avatar
Carsten Rose committed
549
550
        foreach ($allTables AS $val) {
            $table = current($val);
551
            $this->dbArray[DB_INDEX_DEFAULT]->sql("DROP TABLE $table");
Carsten  Rose's avatar
Carsten Rose committed
552
553
        }

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