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
|
<?php
/*
* Basic testing for Idiorm
*
* Checks that the generated SQL is correct
*
*/
require_once dirname(__FILE__) . "/../idiorm.php";
require_once dirname(__FILE__) . "/test_classes.php";
// Enable logging
ORM::configure('logging', true);
// Set up the mock database connection
$db = new MockPDO('sqlite::memory:');
ORM::set_db($db);
ORM::for_table('widget')->find_many();
$expected = "SELECT * FROM `widget`";
Tester::check_equal_query("Basic unfiltered find_many query", $expected);
ORM::for_table('widget')->find_one();
$expected = "SELECT * FROM `widget` LIMIT 1";
Tester::check_equal_query("Basic unfiltered find_one query", $expected);
ORM::for_table('widget')->where_id_is(5)->find_one();
$expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1";
Tester::check_equal_query("where_id_is method", $expected);
ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1";
Tester::check_equal_query("Filtering on ID passed into find_one method", $expected);
ORM::for_table('widget')->count();
$expected = "SELECT COUNT(*) AS `count` FROM `widget` LIMIT 1";
Tester::check_equal_query("COUNT query", $expected);
ORM::for_table('person')->max('height');
$expected = "SELECT MAX(`height`) AS `max` FROM `person` LIMIT 1";
Tester::check_equal_query("MAX query", $expected);
ORM::for_table('person')->min('height');
$expected = "SELECT MIN(`height`) AS `min` FROM `person` LIMIT 1";
Tester::check_equal_query("MIN query", $expected);
ORM::for_table('person')->avg('height');
$expected = "SELECT AVG(`height`) AS `avg` FROM `person` LIMIT 1";
Tester::check_equal_query("AVG query", $expected);
ORM::for_table('person')->sum('height');
$expected = "SELECT SUM(`height`) AS `sum` FROM `person` LIMIT 1";
Tester::check_equal_query("SUM query", $expected);
ORM::for_table('widget')->where('name', 'Fred')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' LIMIT 1";
Tester::check_equal_query("Single where clause", $expected);
ORM::for_table('widget')->where('name', 'Fred')->where('age', 10)->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND `age` = '10' LIMIT 1";
Tester::check_equal_query("Multiple WHERE clauses", $expected);
ORM::for_table('widget')->where_not_equal('name', 'Fred')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` != 'Fred'";
Tester::check_equal_query("where_not_equal method", $expected);
ORM::for_table('widget')->where_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` LIKE '%Fred%' LIMIT 1";
Tester::check_equal_query("where_like method", $expected);
ORM::for_table('widget')->where_not_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` NOT LIKE '%Fred%' LIMIT 1";
Tester::check_equal_query("where_not_like method", $expected);
ORM::for_table('widget')->where_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` IN ('Fred', 'Joe')";
Tester::check_equal_query("where_in method", $expected);
ORM::for_table('widget')->where_not_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` NOT IN ('Fred', 'Joe')";
Tester::check_equal_query("where_not_in method", $expected);
ORM::for_table('widget')->limit(5)->find_many();
$expected = "SELECT * FROM `widget` LIMIT 5";
Tester::check_equal_query("LIMIT clause", $expected);
ORM::for_table('widget')->limit(5)->offset(5)->find_many();
$expected = "SELECT * FROM `widget` LIMIT 5 OFFSET 5";
Tester::check_equal_query("LIMIT and OFFSET clause", $expected);
ORM::for_table('widget')->order_by_desc('name')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` DESC LIMIT 1";
Tester::check_equal_query("ORDER BY DESC", $expected);
ORM::for_table('widget')->order_by_asc('name')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` ASC LIMIT 1";
Tester::check_equal_query("ORDER BY ASC", $expected);
ORM::for_table('widget')->order_by_expr('SOUNDEX(`name`)')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY SOUNDEX(`name`) LIMIT 1";
Tester::check_equal_query("ORDER BY expression", $expected);
ORM::for_table('widget')->order_by_asc('name')->order_by_desc('age')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` ASC, `age` DESC LIMIT 1";
Tester::check_equal_query("Multiple ORDER BY", $expected);
ORM::for_table('widget')->group_by('name')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name`";
Tester::check_equal_query("GROUP BY", $expected);
ORM::for_table('widget')->group_by('name')->group_by('age')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name`, `age`";
Tester::check_equal_query("Multiple GROUP BY", $expected);
ORM::for_table('widget')->group_by_expr("FROM_UNIXTIME(`time`, '%Y-%m')")->find_many();
$expected = "SELECT * FROM `widget` GROUP BY FROM_UNIXTIME(`time`, '%Y-%m')";
Tester::check_equal_query("GROUP BY expression", $expected);
ORM::for_table('widget')->group_by('name')->having('name', 'Fred')->find_one();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' LIMIT 1";
Tester::check_equal_query("Single having clause", $expected);
ORM::for_table('widget')->group_by('name')->having('name', 'Fred')->having('age', 10)->find_one();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' AND `age` = '10' LIMIT 1";
Tester::check_equal_query("Multiple HAVING clauses", $expected);
ORM::for_table('widget')->group_by('name')->having_not_equal('name', 'Fred')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` != 'Fred'";
Tester::check_equal_query("having_not_equal method", $expected);
ORM::for_table('widget')->group_by('name')->having_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` LIKE '%Fred%' LIMIT 1";
Tester::check_equal_query("having_like method", $expected);
ORM::for_table('widget')->group_by('name')->having_not_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` NOT LIKE '%Fred%' LIMIT 1";
Tester::check_equal_query("having_not_like method", $expected);
ORM::for_table('widget')->group_by('name')->having_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IN ('Fred', 'Joe')";
Tester::check_equal_query("having_in method", $expected);
ORM::for_table('widget')->group_by('name')->having_not_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` NOT IN ('Fred', 'Joe')";
Tester::check_equal_query("having_not_in method", $expected);
ORM::for_table('widget')->group_by('name')->having_lt('age', 10)->having_gt('age', 5)->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `age` < '10' AND `age` > '5'";
Tester::check_equal_query("HAVING less than and greater than", $expected);
ORM::for_table('widget')->group_by('name')->having_lte('age', 10)->having_gte('age', 5)->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `age` <= '10' AND `age` >= '5'";
Tester::check_equal_query("HAVING less than or equal and greater than or equal", $expected);
ORM::for_table('widget')->group_by('name')->having_null('name')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IS NULL";
Tester::check_equal_query("having_null method", $expected);
ORM::for_table('widget')->group_by('name')->having_not_null('name')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IS NOT NULL";
Tester::check_equal_query("having_not_null method", $expected);
ORM::for_table('widget')->group_by('name')->having_raw('`name` = ? AND (`age` = ? OR `age` = ?)', array('Fred', 5, 10))->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' AND (`age` = '5' OR `age` = '10')";
Tester::check_equal_query("Raw HAVING clause", $expected);
ORM::for_table('widget')->where('name', 'Fred')->limit(5)->offset(5)->order_by_asc('name')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' ORDER BY `name` ASC LIMIT 5 OFFSET 5";
Tester::check_equal_query("Complex query", $expected);
ORM::for_table('widget')->where_lt('age', 10)->where_gt('age', 5)->find_many();
$expected = "SELECT * FROM `widget` WHERE `age` < '10' AND `age` > '5'";
Tester::check_equal_query("Less than and greater than", $expected);
ORM::for_table('widget')->where_lte('age', 10)->where_gte('age', 5)->find_many();
$expected = "SELECT * FROM `widget` WHERE `age` <= '10' AND `age` >= '5'";
Tester::check_equal_query("Less than or equal and greater than or equal", $expected);
///////////////////////////////////////////
ORM::for_table('widget')->where_null('name')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` IS NULL";
Tester::check_equal_query("where_null method", $expected);
ORM::for_table('widget')->where_not_null('name')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` IS NOT NULL";
Tester::check_equal_query("where_not_null method", $expected);
ORM::for_table('widget')->where_raw('`name` = ? AND (`age` = ? OR `age` = ?)', array('Fred', 5, 10))->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND (`age` = '5' OR `age` = '10')";
Tester::check_equal_query("Raw WHERE clause", $expected);
ORM::for_table('widget')->where_raw('STRFTIME("%Y", "now") = ?', array(2012))->find_many();
$expected = "SELECT * FROM `widget` WHERE STRFTIME(\"%Y\", \"now\") = '2012'";
Tester::check_equal_query("Raw WHERE clause with '%'", $expected);
ORM::for_table('widget')->where_raw('`name` = "Fred"')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` = \"Fred\"";
Tester::check_equal_query("Raw WHERE clause with no parameters", $expected);
ORM::for_table('widget')->where('age', 18)->where_raw('(`name` = ? OR `name` = ?)', array('Fred', 'Bob'))->where('size', 'large')->find_many();
$expected = "SELECT * FROM `widget` WHERE `age` = '18' AND (`name` = 'Fred' OR `name` = 'Bob') AND `size` = 'large'";
Tester::check_equal_query("Raw WHERE clause in method chain", $expected);
ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w')->find_many();
$expected = "SELECT `w`.* FROM `widget` w";
Tester::check_equal_query("Raw query", $expected);
ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w WHERE `name` = ? AND `age` = ?', array('Fred', 5))->find_many();
$expected = "SELECT `w`.* FROM `widget` w WHERE `name` = 'Fred' AND `age` = '5'";
Tester::check_equal_query("Raw query with parameters", $expected);
ORM::for_table('widget')->select('name')->find_many();
$expected = "SELECT `name` FROM `widget`";
Tester::check_equal_query("Simple result column", $expected);
ORM::for_table('widget')->select('name')->select('age')->find_many();
$expected = "SELECT `name`, `age` FROM `widget`";
Tester::check_equal_query("Multiple simple result columns", $expected);
ORM::for_table('widget')->select('widget.name')->find_many();
$expected = "SELECT `widget`.`name` FROM `widget`";
Tester::check_equal_query("Specify table name and column in result columns", $expected);
ORM::for_table('widget')->select('widget.name', 'widget_name')->find_many();
$expected = "SELECT `widget`.`name` AS `widget_name` FROM `widget`";
Tester::check_equal_query("Aliases in result columns", $expected);
/////@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ORM::for_table('widget')->select_expr('COUNT(*)', 'count')->find_many();
$expected = "SELECT COUNT(*) AS `count` FROM `widget`";
Tester::check_equal_query("Literal expression in result columns", $expected);
ORM::for_table('widget')->select_many(array('widget_name' => 'widget.name'), 'widget_handle')->find_many();
$expected = "SELECT `widget`.`name` AS `widget_name`, `widget_handle` FROM `widget`";
Tester::check_equal_query("Aliases in select many result columns", $expected);
ORM::for_table('widget')->select_many_expr(array('count' => 'COUNT(*)'), 'SUM(widget_order)')->find_many();
$expected = "SELECT COUNT(*) AS `count`, SUM(widget_order) FROM `widget`";
Tester::check_equal_query("Literal expression in select many result columns", $expected);
ORM::for_table('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal_query("Simple join", $expected);
ORM::for_table('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_one(5);
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` WHERE `widget`.`id` = '5' LIMIT 1";
Tester::check_equal_query("Simple join with where_id_is method", $expected);
ORM::for_table('widget')->inner_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` INNER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal_query("Inner join", $expected);
ORM::for_table('widget')->left_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` LEFT OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal_query("Left outer join", $expected);
ORM::for_table('widget')->right_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` RIGHT OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal_query("Right outer join", $expected);
ORM::for_table('widget')->full_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` FULL OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal_query("Full outer join", $expected);
ORM::for_table('widget')
->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))
->join('widget_nozzle', array('widget_nozzle.widget_id', '=', 'widget.id'))
->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` JOIN `widget_nozzle` ON `widget_nozzle`.`widget_id` = `widget`.`id`";
Tester::check_equal_query("Multiple join sources", $expected);
ORM::for_table('widget')->table_alias('w')->find_many();
$expected = "SELECT * FROM `widget` `w`";
Tester::check_equal_query("Main table alias", $expected);
ORM::for_table('widget')->join('widget_handle', array('wh.widget_id', '=', 'widget.id'), 'wh')->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` `wh` ON `wh`.`widget_id` = `widget`.`id`";
Tester::check_equal_query("Join with alias", $expected);
ORM::for_table('widget')->join('widget_handle', "widget_handle.widget_id = widget.id")->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON widget_handle.widget_id = widget.id";
Tester::check_equal_query("Join with string constraint", $expected);
ORM::for_table('widget')->distinct()->select('name')->find_many();
$expected = "SELECT DISTINCT `name` FROM `widget`";
Tester::check_equal_query("Select with DISTINCT", $expected);
$widget = ORM::for_table('widget')->create();
$widget->name = "Fred";
$widget->age = 10;
$widget->save();
$expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')";
Tester::check_equal_query("Insert data", $expected);
$widget = ORM::for_table('widget')->create();
$widget->name = "Fred";
$widget->age = 10;
$widget->set_expr('added', 'NOW()');
$widget->save();
$expected = "INSERT INTO `widget` (`name`, `age`, `added`) VALUES ('Fred', '10', NOW())";
Tester::check_equal_query("Insert data containing an expression", $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->name = "Fred";
$widget->age = 10;
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'";
Tester::check_equal_query("Update data", $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->name = "Fred";
$widget->age = 10;
$widget->set_expr('added', 'NOW()');
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW() WHERE `id` = '1'";
Tester::check_equal_query("Update data containing an expression", $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->set(array("name" => "Fred", "age" => 10));
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'";
Tester::check_equal_query("Update multiple fields", $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->set(array("name" => "Fred", "age" => 10));
$widget->set_expr(array("added" => "NOW()", "lat_long" => "GeomFromText('POINT(1.2347 2.3436)')"));
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW(), `lat_long` = GeomFromText('POINT(1.2347 2.3436)') WHERE `id` = '1'";
Tester::check_equal_query("Update multiple fields containing an expression", $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->set(array("name" => "Fred", "age" => 10));
$widget->set_expr(array("added" => "NOW()", "lat_long" => "GeomFromText('POINT(1.2347 2.3436)')"));
$widget->lat_long = 'unknown';
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW(), `lat_long` = 'unknown' WHERE `id` = '1'";
Tester::check_equal_query("Update multiple fields containing an expression (override previously set expression with plain value)", $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->delete();
$expected = "DELETE FROM `widget` WHERE `id` = '1'";
Tester::check_equal_query("Delete data", $expected);
$widget = ORM::for_table('widget')->where_equal('age', 10)->delete_many();
$expected = "DELETE FROM `widget` WHERE `age` = '10'";
Tester::check_equal_query("Delete many", $expected);
ORM::raw_execute("INSERT OR IGNORE INTO `widget` (`id`, `name`) VALUES (?, ?)", array(1, 'Tolstoy'));
$expected = "INSERT OR IGNORE INTO `widget` (`id`, `name`) VALUES ('1', 'Tolstoy')";
Tester::check_equal_query("Raw execute", $expected); // A bit of a silly test, as query is passed through
// Tests of muliple connections
define('ALTERNATE', 'alternate');
ORM::set_db(new MockDifferentPDO('sqlite::memory:'), 'alternate');
ORM::configure('logging', true, ALTERNATE);
$person1 = ORM::for_table('person')->find_one();
$person2 = ORM::for_table('person', ALTERNATE)->find_one();
$expected = "SELECT * FROM `person` LIMIT 1";
Tester::check_equal_string("Multiple connection (1)", $person1->name, 'Fred');
Tester::check_equal_string("Multiple connection (2)", $person2->name, 'Steve');
$expectedToo = "SELECT * FROM `widget`";
ORM::raw_execute("SELECT * FROM `widget`", array(), ALTERNATE);
Tester::check_equal_string(
"Multiple connection log (1)",
ORM::get_last_query(ORM::DEFAULT_CONNECTION),
$expected
);
Tester::check_equal_string(
"Multiple connection query log (2)",
ORM::get_last_query(),
$expectedToo
);
Tester::check_equal_string(
"Multiple connection query log (3)",
ORM::get_last_query(ALTERNATE),
$expectedToo
);
$widget = ORM::for_table('widget')->create();
$widget['name'] = "Fred";
$widget['age'] = 10;
$widget->save();
$expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')";
Tester::check_equal_query("Insert data using ArrayAccess", $expected);
ORM::for_table('widget')->where('name', 'Fred')->find_one();
$statement = ORM::get_last_statement();
$test_name = 'get_last_statement() returned MockPDOStatement';
if($statement instanceOf MockPDOStatement) {
Tester::report_pass($test_name);
} else {
$actual = gettype($statement);
if('object' == $actual) {
$actual = get_class($statement);
}
Tester::report_failure($test_name, 'MockPDOStatement', $actual);
}
// Regression tests
$widget = ORM::for_table('widget')->select('widget.*')->find_one();
$expected = "SELECT `widget`.* FROM `widget` LIMIT 1";
Tester::check_equal_query("Issue #12 - incorrect quoting of column wildcard", $expected);
$widget = ORM::for_table('widget')->where_raw('username LIKE "ben%"')->find_many();
$expected = 'SELECT * FROM `widget` WHERE username LIKE "ben%"';
Tester::check_equal_query('Issue #57 - _log_query method raises a warning when query contains "%"', $expected);
$widget = ORM::for_table('widget')->where_raw('comments LIKE "has been released?%"')->find_many();
$expected = 'SELECT * FROM `widget` WHERE comments LIKE "has been released?%"';
Tester::check_equal_query('Issue #57 - _log_query method raises a warning when query contains "?"', $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->set('ad`ded', '2013-01-04');
$widget->save();
$expected = "UPDATE `widget` SET `ad``ded` = '2013-01-04' WHERE `id` = '1'";
Tester::check_equal_query('Issue #74 - escaping quote symbols in "_quote_identifier_part"', $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->set_expr('added', 'NOW()');
$widget->save();
$expected = "UPDATE `widget` SET `added` = NOW() WHERE `id` = '1'";
Tester::check_equal_query("Issue #90 - When using set_expr alone it doesn't trigger query creation", $expected);
// Tests that alter Idiorm's config are done last
ORM::configure('id_column', 'primary_key');
ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `primary_key` = '5' LIMIT 1";
Tester::check_equal_query("Setting: id_column", $expected);
ORM::configure('id_column_overrides', array(
'widget' => 'widget_id',
'widget_handle' => 'widget_handle_id',
));
ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `widget_id` = '5' LIMIT 1";
Tester::check_equal_query("Setting: id_column_overrides, first test", $expected);
ORM::for_table('widget_handle')->find_one(5);
$expected = "SELECT * FROM `widget_handle` WHERE `widget_handle_id` = '5' LIMIT 1";
Tester::check_equal_query("Setting: id_column_overrides, second test", $expected);
ORM::for_table('widget_nozzle')->find_one(5);
$expected = "SELECT * FROM `widget_nozzle` WHERE `primary_key` = '5' LIMIT 1";
Tester::check_equal_query("Setting: id_column_overrides, third test", $expected);
ORM::for_table('widget')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `new_id` = '5' LIMIT 1";
Tester::check_equal_query("Instance ID column, first test", $expected);
ORM::for_table('widget_handle')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget_handle` WHERE `new_id` = '5' LIMIT 1";
Tester::check_equal_query("Instance ID column, second test", $expected);
ORM::for_table('widget_nozzle')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget_nozzle` WHERE `new_id` = '5' LIMIT 1";
Tester::check_equal_query("Instance ID column, third test", $expected);
// Test caching. This is a bit of a hack.
ORM::configure('caching', true);
ORM::for_table('widget')->where('name', 'Fred')->where('age', 17)->find_one();
ORM::for_table('widget')->where('name', 'Bob')->where('age', 42)->find_one();
$expected = ORM::get_last_query();
ORM::for_table('widget')->where('name', 'Fred')->where('age', 17)->find_one(); // this shouldn't run a query!
Tester::check_equal_query("Caching, same query not run twice", $expected);
// Test caching with multiple connections (also a bit of a hack)
ORM::configure('caching', true, ALTERNATE);
ORM::for_table('widget', ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one();
ORM::for_table('widget', ALTERNATE)->where('name', 'Tom')->where('age', 120)->find_one();
$expectedToo = ORM::get_last_query();
ORM::for_table('widget', ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one(); // this shouldn't run a query!
Tester::check_equal_query(
"Multi-connection caching, same query not run twice, on alternate connection",
$expectedToo
);
ORM::for_table('widget')->where('name', 'Fred')->where('age', 17)->find_one(); // this still shouldn't run a query!
Tester::check_equal_string(
"Multi-conneciton caching, same query not run twice across connections",
ORM::get_last_query(ORM::DEFAULT_CONNECTION),
$expected
);
Tester::report();
|