-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueryGeneratorTest.php
More file actions
404 lines (342 loc) · 9.59 KB
/
Copy pathQueryGeneratorTest.php
File metadata and controls
404 lines (342 loc) · 9.59 KB
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
<?php
require "./QueryGenerator.php";
/**
* Designed to work with PHPUnit
*/
class QueryGeneratorTest extends PHPUnit\Framework\TestCase {
public function testIncompleteQuery(): void {
$incompleteQueryClauseSets = [
'select' => [
[],
['select'],
['from'],
],
'insert' => [
[],
['insert'],
['set'],
['insert', 'columns'],
['insert', 'values'],
['columns', 'values'],
],
'replace' => [
[],
['replace'],
['set'],
['replace', 'columns'],
['replace', 'values'],
['columns', 'values'],
],
'update' => [
[],
['update'],
['set'],
],
'delete' => [
[],
['delete'],
['from'],
],
];
foreach ($incompleteQueryClauseSets as $clauseSets) {
foreach ($clauseSets as $clauses) {
$qGen = new QueryGenerator();
foreach ($clauses as $clause) {
$qGen->$clause('clause');
}
$build = function() use ($qGen): array {
return $qGen->build();
};
$buildIncomplete = function() use ($qGen): array {
return $qGen->skipValidation()->build();
};
$this->assertTrue((bool)$this->didThrowException($build));
$this->assertFalse($this->didThrowException($buildIncomplete));
}
}
}
public function testCompleteQuery(): void {
$completeQueryClauseSets = [
'select' => [['select', 'from']],
'insert' => [
['insert', 'set'],
['insert', 'columns', 'values'],
],
'replace' => [
['replace', 'set'],
['replace', 'columns', 'values'],
],
'update' => [['update', 'set']],
'delete' => [['delete', 'from']],
];
foreach ($completeQueryClauseSets as $clauseSets) {
foreach ($clauseSets as $clauses) {
$qGen = new QueryGenerator();
foreach ($clauses as $clause) {
$qGen->$clause('clause');
}
$build = function() use ($qGen): array {
return $qGen->build();
};
$buildIncomplete = function() use ($qGen): array {
return $qGen->skipValidation()->build();
};
$this->assertFalse($this->didThrowException($build));
$this->assertFalse($this->didThrowException($buildIncomplete));
}
}
}
public function testSmallQuery(): void {
$qGen = new QueryGenerator();
$qGen->select(['field']);
$qGen->from(['table']);
$expectedQuery = <<<EOT
SELECT field
FROM table
EOT;
$expectedParams = [];
$this->assertQuery($qGen, $expectedQuery, $expectedParams);
}
public function testBigQuery(): void {
$qGen = new QueryGenerator();
$qGen->select(['field1', 'field2']);
$qGen->from(['table1', 'table2']);
$qGen->join([
'INNER JOIN table3 USING (asdf)',
'OUTER JOIN table4 ON foo = bar',
]);
$qGen->where(['where1', 'where2 OR where3']);
$qGen->group(['group1', 'group2']);
$qGen->having(['having1 > ?', 'having2 < ?'], [0, 0]);
$qGen->order(['order1', 'order2']);
$qGen->limit([3]);
$qGen->offset(['?'], [5]);
$expectedQuery = <<<EOT
SELECT field1, field2
FROM table1, table2
INNER JOIN table3 USING (asdf)
OUTER JOIN table4 ON foo = bar
WHERE (where1) AND (where2 OR where3)
GROUP BY group1, group2
HAVING (having1 > ?) AND (having2 < ?)
ORDER BY order1, order2
LIMIT 3
OFFSET ?
EOT;
$expectedParams = [0, 0, 5];
$this->assertQuery($qGen, $expectedQuery, $expectedParams);
}
public function testUseOr(): void {
$qGen = new QueryGenerator();
$qGen->select('field')
->from('table')
->where('1')
->where('2')
->useOr();
$expectedQuery = <<<EOT
SELECT field
FROM table
WHERE (1) OR (2)
EOT;
$expectedParams = [];
$this->assertQuery($qGen, $expectedQuery, $expectedParams);
}
public function testNestedGenerators(): void {
$where = new QueryGenerator();
$where->where('abcd', [1]);
$qGen = new QueryGenerator();
$qGen->select('field');
$qGen->from('table');
$qGen->where($where);
$expectedQuery = <<<EOT
SELECT field
FROM table
WHERE ((abcd))
EOT;
$expectedParams = [1];
$this->assertQuery($qGen, $expectedQuery, $expectedParams);
}
public function testSingleArguments(): void {
$qGen = new QueryGenerator();
$qGen->select('field');
$qGen->from('table');
$expectedQuery = <<<EOT
SELECT field
FROM table
EOT;
$expectedParams = [];
$this->assertQuery($qGen, $expectedQuery, $expectedParams);
}
public function testModifiers(): void {
$qGen = new QueryGenerator();
$qGen->insert('table');
$qGen->set('field = ?', 0);
$qGen->modify('IGNORE');
$expectedQuery = <<<EOT
INSERT IGNORE INTO table
SET field = ?
EOT;
$expectedParams = [0];
$this->assertQuery($qGen, $expectedQuery, $expectedParams);
}
public function testForUpdate(): void {
$qGen = new QueryGenerator();
$qGen->select('a');
$qGen->from('b');
$qGen->forUpdate();
$expectedQuery = <<<EOT
SELECT a
FROM b
FOR UPDATE
EOT;
$this->assertQuery($qGen, $expectedQuery, []);
}
public function testOnDuplicate(): void {
$qGen = new QueryGenerator();
$qGen->insert('a');
$qGen->as('row');
$qGen->set('b = 1');
$qGen->set('c = 1');
$qGen->duplicate('b = 2');
$expectedQuery = <<<EOT
INSERT INTO a
SET b = 1, c = 1
AS row
ON DUPLICATE KEY UPDATE b = 2
EOT;
$this->assertQuery($qGen, $expectedQuery, []);
}
public function testUnion(): void {
$other = new QueryGenerator();
$other->select('field');
$other->from('table2');
$qGen = new QueryGenerator();
$qGen->select('field');
$qGen->from('table');
$qGen->union($other);
$expectedQuery = <<<EOT
SELECT field
FROM table
UNION
SELECT field
FROM table2
EOT;
$this->assertQuery($qGen, $expectedQuery, []);
}
public function testUnionAll(): void {
$other = new QueryGenerator();
$other->select('field');
$other->from('table2');
$qGen = new QueryGenerator();
$qGen->select('field');
$qGen->from('table');
$qGen = $qGen->unionAll($other);
$expectedQuery = <<<EOT
SELECT field
FROM table
UNION ALL
SELECT field
FROM table2
EOT;
$this->assertQuery($qGen, $expectedQuery, []);
}
public function testMixedUnions(): void {
$qGen = new QueryGenerator();
$qGen->select('a');
$qGen->from('b');
$qGen->union('SELECT a FROM c');
$qGen->unionAll('SELECT a FROM d');
$expectedQuery = <<<EOT
SELECT a
FROM b
UNION
SELECT a FROM c
UNION ALL
SELECT a FROM d
EOT;
$this->assertQuery($qGen, $expectedQuery, []);
}
public function testUnionStringOperand(): void {
$qGen = new QueryGenerator();
$qGen->select('id');
$qGen->from('live');
$qGen->union('SELECT id FROM archive WHERE x = ?', 5);
$expectedQuery = <<<EOT
SELECT id
FROM live
UNION
SELECT id FROM archive WHERE x = ?
EOT;
$this->assertQuery($qGen, $expectedQuery, [5]);
}
public function testUnionParamOrdering(): void {
$other = new QueryGenerator();
$other->select('a');
$other->from('c');
$other->where('y = ?', 2);
$qGen = new QueryGenerator();
$qGen->select('a');
$qGen->from('b');
$qGen->where('x = ?', 1);
$qGen->union($other);
$qGen->order('a');
$qGen->limit(10);
$qGen->offset('?', 5);
$expectedQuery = <<<EOT
SELECT a
FROM b
WHERE (x = ?)
UNION
SELECT a
FROM c
WHERE (y = ?)
ORDER BY a
LIMIT 10
OFFSET ?
EOT;
$this->assertQuery($qGen, $expectedQuery, [1, 2, 5]);
}
public function testUnionGeneratorOperandWithParamsThrows(): void {
$other = new QueryGenerator();
$other->select('a');
$other->from('c');
$qGen = new QueryGenerator();
$qGen->select('a');
$qGen->from('b');
$this->expectException(Exception::class);
$qGen->union($other, [1]);
}
public function testUnionOperandValidation(): void {
$incomplete = new QueryGenerator();
$incomplete->select('a');
$qGen = new QueryGenerator();
$qGen->select('a');
$qGen->from('b');
$union = function() use ($qGen, $incomplete): QueryGenerator {
return $qGen->union($incomplete);
};
$this->assertTrue((bool)$this->didThrowException($union));
$incomplete->skipValidation();
$this->assertFalse($this->didThrowException($union));
}
public function assertQuery(QueryGenerator $qGen, string $expectedQuery, array $expectedParams): void {
[$actualQuery, $actualParams] = $qGen->build();
$this->assertEquals($expectedQuery, $actualQuery);
$this->assertEquals($expectedParams, $actualParams);
[$actualQuery, $actualParams] = $qGen->skipValidation()->build();
$this->assertEquals($expectedQuery, $actualQuery);
$this->assertEquals($expectedParams, $actualParams);
}
/**
* @param callable $callback
* @return non-falsy-string|bool
*/
public function didThrowException($callback): string|bool {
try {
$callback();
return false;
} catch (MissingClauseException $e) {
return $e->getMessage() ?: true;
}
}
}