forked from catfan/Medoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
555 lines (533 loc) · 17.8 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
require_once('medoo.php');
class TestProtected extends medoo {
public function select_ctx($table, $join, $columns = null, $where = null, $column_fn = null) {
return parent::select_context($table, $join, $columns, $where, $column_fn);
}
public function column_quote($string) {
return parent::column_quote($string);
}
public function where_clause($where) {
return parent::where_clause($where);
}
public function setDatabaseType($type) {
$this->database_type = $type;
}
public function getProp($name) {
return $this->$name;
}
}
/**
* Created by IntelliJ IDEA.
* User: rek
* Date: 2015/11/17
* Time: 15:42
*/
class MedooTest extends PHPUnit_Framework_TestCase {
/**
* @var TestProtected
*/
var $db;
public function setUp() {
$this->db = new TestProtected(array(
'database_type' => 'SQLite',
'database_file' => '/tmp/_medoo_test.db',
'charset' => 'utf-8',
));
}
/**
* @param $column
* @param $expect
* @dataProvider columnQuoteProvider
*/
public function testColumnQuote($column, $expect) {
$this->assertEquals($expect, $this->db->column_quote($column));
}
public function columnQuoteProvider() {
return array(
array('abc', '"abc"'),
array('table.col', '"table"."col"'),
array('t1.*', '"t1".*'),
array('*', '*'),
);
}
/**
* @param $expect
* @param $where
* @dataProvider whereDataProvider
*/
public function testWhere($expect, $where, $skip = false) {
if ($skip) {
$this->markTestSkipped('Skipped test ' . $expect);
return;
}
$this->assertEquals($expect, $this->db->where_clause($where));
}
public function whereDataProvider() {
return array(
// basic condition
array(
' WHERE "email" = \'[email protected]\'',
array('email' => '[email protected]'),
),
array(
' WHERE "user_id" = 200',
array('user_id' => 200),
),
array(
' WHERE "user_id" > 200',
array('user_id[>]' => 200),
),
array(
' WHERE "user_id" >= 200',
array('user_id[>=]' => 200),
),
array(
' WHERE "user_id" < 200',
array('user_id[<]' => 200),
),
array(
' WHERE "user_id" <= 200',
array('user_id[<=]' => 200),
),
array(
' WHERE "user_id" != 200',
array('user_id[!]' => 200),
),
// BETWEEN condition
array(
' WHERE ("age" BETWEEN 200 AND 500)',
array('age[<>]' => array(200, 500)),
),
array(
' WHERE ("age" NOT BETWEEN 200 AND 500)',
array('age[><]' => array(200, 500)),
),
array(
' WHERE ("birthday" NOT BETWEEN \'2015-01-01\' AND \'2015-05-01\')',
array('birthday[><]' => array(
date('Y-m-d', mktime(0, 0, 0, 1, 1, 2015)),
date('Y-m-d', mktime(0, 0, 0, 5, 1, 2015)),
))
),
// IN condition
array(
' WHERE "user_id" IN (2,123,234,54)',
array('user_id' => array(2, 123, 234, 54)),
),
array(
),
// IS NULL
array(
' WHERE "city" IS NULL',
array('city' => null),
),
// Negative condition
array(
' WHERE "user_name" != \'foo\'',
array('user_name[!]' => 'foo'),
),
array(
' WHERE "user_id" != 1024',
array('user_id[!]' => 1024),
),
array(
' WHERE "city" IS NOT NULL',
array('city[!]' => null),
),
array(
' WHERE "promoted" != 1',
array('promoted[!]' => true),
),
//Relativity Condition
array(
' WHERE "user_id" > 200 AND ("age" BETWEEN 18 AND 25) AND "gender" = \'female\'',
array('AND' => array(
'user_id[>]' => 200,
'age[<>]' => array(18, 25),
'gender' => 'female',
)),
),
array(
' WHERE "user_id" > 200 OR ("age" BETWEEN 18 AND 25) OR "gender" = \'female\'',
array('OR' => array(
'user_id[>]' => 200,
'age[<>]' => array(18, 25),
'gender' => 'female',
)),
),
array(
' WHERE ("user_name" = \'foo\' OR "email" = \'[email protected]\') AND "password" = \'12345\'',
array('AND' => array(
'OR' => array(
'user_name' => 'foo',
'email' => '[email protected]',
),
'password' => '12345',
)),
),
// Combine with comments
array(
' WHERE ("user_name" = \'foo\' OR "email" = \'[email protected]\') AND ("user_name" = \'bar\' OR "email" = \'[email protected]\')',
array('AND' => array(
'OR #the first condition' => array(
'user_name' => 'foo',
'email' => '[email protected]',
),
'OR #the second condition' => array(
'user_name' => 'bar',
'email' => '[email protected]',
),
)),
),
// LIKE condition
array(
' WHERE "city" LIKE \'%lon%\'',
array('city[~]' => 'lon'),
),
array(
' WHERE "city" LIKE \'%lon%\' OR "city" LIKE \'%foo%\' OR "city" LIKE \'%bar%\'',
array('city[~]' => array(
'lon',
'foo',
'bar',
)),
),
array(
' WHERE "city" NOT LIKE \'%lon%\'',
array('city[!~]' => 'lon'),
),
array(
' WHERE "city" LIKE \'%stan\'',
array('city[~]' => 'stan%'), // Kazakhstan, Uzbekistan, Türkmenistan...
),
array(
' WHERE "city" LIKE \'Londo%\'',
array('city[~]' => 'Londo_'), // London, Londox, Londos...
),
array(
' WHERE "name" LIKE \'[BCR]at\'',
array('name[~]' => '[BCR]at'), // Bat, Cat, Rat
true, // skipped
),
array(
' WHERE "name" LIKE \'[^BCR]at\'',
array('name[~]' => '[!BCR]at'),// Eat, Fat, Hat
true, // skipped
),
//Order Condition
array(
' ORDER BY "age"',
array('ORDER' => 'age'),
),
array(
' ORDER BY "user_name" DESC,"user_id" ASC',
array('ORDER' => array('user_name DESC', 'user_id ASC')),
),
// Order by field
array(
' WHERE "user_id" IN (1,12,43,57,98,144) ORDER BY FIELD("user_id", 43,12,57,98,144,1)',
array(
'user_id' => array(1, 12, 43, 57, 98, 144),
'ORDER' => array("user_id", array(43, 12, 57, 98, 144, 1)),
)
),
// Full Text Searching
array(
' WHERE MATCH ("content", "title") AGAINST (\'foo\')',
array('MATCH' => array(
'columns' => array('content', 'title'),
'keyword' => 'foo',
))
),
//Using SQL Functions
array(
' WHERE "datetime" = NOW()',
array('#datetime' => 'NOW()'),
),
array(
' WHERE "datetime1" = \'now()\' AND "datetime2" = \'NOW()\' AND "datetime3" = \'NOW\'',
array('AND' => array(
'#datetime1' => 'now()',
'datetime2' => 'NOW()',
'#datetime3' => 'NOW',
)), // checking bad case for compatibility
),
// LIMIT
array(
' LIMIT 10',
array('LIMIT' => 10),
),
array(
' LIMIT 10,10',
array('LIMIT' => array(10, 10)),
),
array(
' LIMIT 10',
array('LIMIT' => '10'),
),
// Additional condition
array(
' GROUP BY "type" HAVING "user_id" > 500 LIMIT 20,100',
array(
'GROUP' => 'type',
'HAVING' => array('user_id[>]' => 500),
'LIMIT' => array(20, 100),
)
),
// Multi-Cols GROUP BY
array(
' GROUP BY "type", "name"',
array(
'GROUP' => array('type', 'name')
)
),
// Pass SQL partial directly
array(
' WHERE name = \'foo\' AND email = \'[email protected]\' ORDER BY id ASC',
'WHERE name = \'foo\' AND email = \'[email protected]\' ORDER BY id ASC',
)
);
}
public function testPgLimit() {
$this->db->setDatabaseType('pgsql');
$this->assertEquals(
' LIMIT 10',
$this->db->where_clause(array(
'LIMIT' => 10
)
));
$this->assertEquals(
' OFFSET 10 LIMIT 10',
$this->db->where_clause(array(
'LIMIT' => array(10, 10)
)
));
$this->db->setDatabaseType('sqlite');
}
/**
* @param $expected
* @param $args
* @dataProvider selectDataProvider
*/
public function testSelect($expected, $args) {
$this->assertEquals(
$expected, call_user_func_array(
array($this->db, 'select_ctx'),
$args
));
}
public function selectDataProvider() {
return array(
// Table column
array(
'SELECT * FROM "account"',
array('account', '*'),
),
array(
'SELECT "user_name","email" FROM "account"',
array('account', array(
'user_name',
'email',
)),
),
array(
'SELECT "user_name","email" FROM "account" ORDER BY "user_name" ASC LIMIT 10',
array(
'account',
array('user_name', 'email'),
array(
'ORDER' => 'user_name ASC',
'LIMIT' => 10
)
)
),
// Table join
array(
'SELECT "post".*,"account"."user_id" FROM "post" LEFT JOIN "account" ON "post"."author_id" = "account"."user_id"',
array(
'post',
array('[>]account' => array('author_id' => 'user_id')),
array('post.*', 'account.user_id'),
),
),
array(
'SELECT * FROM "account" INNER JOIN "album" USING ("user_id")',
array(
'account',
array('[><]album' => 'user_id'),
'*',
)
),
array(
'SELECT * FROM "account" FULL JOIN "photo" USING ("user_id", "avatar_id")',
array(
'account',
array('[<>]photo' => array('user_id', 'avatar_id')),
'*',
)
),
array(
'SELECT * FROM "account" RIGHT JOIN "account" AS "b" ON "account"."user_id" = "b"."promotor_id"',
array(
'account',
array(
'[<]account(b)' => array('user_id' => 'promotor_id'),
),
'*'
)
),
array(
'SELECT * FROM "post" INNER JOIN "account" ON "post"."author_id" = "account"."user_id" INNER JOIN "album" ON "account"."user_id" = "album"."user_id"',
array(
'post',
array(
'[><]account' => array('author_id' => 'user_id'),
'[><]album' => array('account.user_id' => 'user_id'),
),
'*'
)
),
array(
'SELECT * FROM "album" LEFT JOIN "account" ON "album"."user_id" = "account"."user_id" AND "album"."user_type" = "account"."type"',
array(
'album',
array(
'[>]account' => array(
'user_id' => 'user_id',
'user_type' => 'type',
)
),
'*'
)
),
//Column Alias
array(
'SELECT "user_id","nickname" AS "my_nickname" FROM "account" LIMIT 20',
array(
'account',
array(
'user_id',
'nickname(my_nickname)'
),
array('LIMIT' => 20),
)
),
// one string column
array(
'SELECT "user_id" FROM "account"',
array(
'account',
'user_id',
)
),
// Column Function
array(
'SELECT COUNT(*) FROM "account"',
array(
'account',
null,
'*',
null,
'COUNT'
)
),
array(
'SELECT MAX("age") FROM "account" LEFT JOIN "photo" USING ("user_id") WHERE "gender" = \'female\'',
array(
'account',
array('[>]photo' => array('user_id')),
'age',
array('gender' => 'female'),
'MAX'
)
),
array(
'SELECT COUNT(*) FROM "account" WHERE "gender" = \'female\'',
array(
'account',
array(
'gender' => 'female'
),
null,
null,
'COUNT'
)
),
array(
'SELECT COUNT(*) FROM "account" WHERE "gender" = \'female\'',
array(
'account',
array(
'gender' => 'female'
),
null,
array(),
'COUNT'
)
),
// for has()
array(
'SELECT 1 FROM "account"',
array(
'account',
null,
null,
null,
'1'
)
)
);
}
public function testFetchClass() {
$r = $this->db->fetch_class('abc', array('const'));
$this->assertInstanceOf('medoo', $r);
$fc = $r->getProp('fetch_class');
$this->assertTrue(is_array($fc));
$this->assertEquals('abc', $fc['name']);
$this->assertEquals(1, count($fc['ctorargs']));
$this->assertTrue($fc['once']);
}
public function testDisableFetchClass() {
$r = $this->db->fetch_class(false);
$this->assertInstanceOf('medoo', $r);
$fc = $r->getProp('fetch_class');
$this->assertNull($fc);
}
public function testDistinct() {
$sql = $this->db->distinct()->select_ctx('account', 'age', array('gender' => 'female'));
$this->assertEquals(
'SELECT DISTINCT "age" FROM "account" WHERE "gender" = \'female\'',
$sql
);
// make sure distinct auto reset
$sql = $this->db->select_ctx('account', 'age', array('gender' => 'female'));
$this->assertEquals(
'SELECT "age" FROM "account" WHERE "gender" = \'female\'',
$sql,
'DISTINCT status havn\'t reset automatically'
);
}
public function testDistinctCount() {
$this->db->debug()->distinct()->count('account', 'age');
$this->expectOutputString('SELECT COUNT(DISTINCT "age") FROM "account"');
}
public function testOracleLimit() {
$this->db->setDatabaseType('oracle');
$this->assertEquals(
' FETCH FIRST 10 ROWS ONLY',
$this->db->where_clause(array(
'LIMIT' => 10
)
));
$this->assertEquals(
' OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY',
$this->db->where_clause(array(
'LIMIT' => array(10, 10)
)
));
$this->db->setDatabaseType('sqlite');
}
}