-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilderTest.php
435 lines (377 loc) · 15.7 KB
/
BuilderTest.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
<?php
/**
* This file is part of SQLBuilder.
*
* @author Chopin Ngo <consatan@gmail.com>
* @license https://opensource.org/licenses/bsd-3-clause BSD-3-Clause
*/
declare(strict_types=1);
namespace Consatan\SQLBuilder\Test;
use PDO;
use PDOException;
use PDOStatement;
use ReflectionMethod;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Consatan\SQLBuilder\Bind;
use Consatan\SQLBuilder\Builder;
class BuilderTest extends TestCase
{
public function testFormatBind()
{
$builder = new Builder();
$formatBind = new ReflectionMethod(Builder::class, 'formatBind');
$formatBind->setAccessible(true);
$bind = $formatBind->invoke($builder, null);
$this->assertNull($bind);
$bind = $formatBind->invoke($builder, function () {
return null;
});
$this->assertNull($bind);
$bind = $formatBind->invoke($builder, 1);
$this->assertSame($bind, [[1, PDO::PARAM_STR]]);
$bind = $formatBind->invoke($builder, []);
$this->assertSame($bind, []);
$bind = $formatBind->invoke($builder, Bind::int(1, 2, 3));
$this->assertSame($bind, [
[1, PDO::PARAM_INT],
[2, PDO::PARAM_INT],
[3, PDO::PARAM_INT],
]);
$bind = $formatBind->invoke($builder, ['chopin', Bind::int(33), 'foo', Bind::null(null)]);
$this->assertSame($bind, [
['chopin', PDO::PARAM_STR],
[33, PDO::PARAM_INT],
['foo', PDO::PARAM_STR],
[null, PDO::PARAM_NULL],
]);
$bind = $formatBind->invoke($builder, Bind::int());
$this->assertSame($bind, []);
$bind = $formatBind->invoke($builder, [Bind::int()]);
$this->assertSame($bind, [[]]);
$bind = $formatBind->invoke($builder, [Bind::int([':limit' => 10, ':offset' => 20])]);
$this->assertSame($bind, [
':limit' => [10, PDO::PARAM_INT],
':offset' => [20, PDO::PARAM_INT],
]);
$bind = $formatBind->invoke($builder, ['foo', 'bar', [1, 2, 3]]);
$this->assertSame($bind, [
['foo', PDO::PARAM_STR],
['bar', PDO::PARAM_STR],
[[1, 2, 3], PDO::PARAM_STR],
]);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Where in bind value cannot empty.
*/
public function testWhereInEmpty()
{
$formatBind = new ReflectionMethod(Builder::class, 'formatBind');
$formatBind->setAccessible(true);
$formatBind->invoke(new Builder(), ['foo', 'bar', []]);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Mixed array is not allow.
*/
public function testMixedArray()
{
$formatBind = new ReflectionMethod(Builder::class, 'formatBind');
$formatBind->setAccessible(true);
$formatBind->invoke(new Builder(), ['foo', 'bar', ':limit' => 10]);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Bind value cannot be a multidimensional arrays.
*/
public function testWhereInMultiArray()
{
$formatBind = new ReflectionMethod(Builder::class, 'formatBind');
$formatBind->setAccessible(true);
$formatBind->invoke(new Builder(), [':foo' => Bind::int(1, 2)]);
}
public function testMultipAssocBind()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('\Consatan\SQLBuilder\Bind in bind values array '
. 'must be an indexed array, associative array given.');
$formatBind = new ReflectionMethod(Builder::class, 'formatBind');
$formatBind->setAccessible(true);
$formatBind->invoke(new Builder(), [Bind::int([':offset' => 20]), Bind::int([':limit' => 10])]);
}
public function testPrepare()
{
$builder = new Builder();
$sql = 'select * from user where name=:name and state=:state order by id {{limit: limit ?,?}}';
$builder->build('limit', Bind::int(10, 20));
$result = $builder->prepare($sql, [':name' => 'chopin', ':state' => 1]);
$this->assertSame($result, [
'select * from user where name=:name and state=:state order by id limit :__1__,:__2__',
[
':name' => [':name', 'chopin', PDO::PARAM_STR],
':state' => [':state', 1, PDO::PARAM_STR],
':__1__' => [':__1__', 10, PDO::PARAM_INT],
':__2__' => [':__2__', 20, PDO::PARAM_INT],
]
]);
$builder = new Builder();
$builder->build('limit', null);
$result = $builder->prepare($sql, [':name' => 'chopin', ':state' => 1]);
$this->assertSame($result, [
'select * from user where name=:name and state=:state order by id ',
[
':name' => [':name', 'chopin', PDO::PARAM_STR],
':state' => [':state', 1, PDO::PARAM_STR],
]
]);
$builder = new Builder();
$sql = 'select * from user where name=:name and state=:state {{age_in: and age in (?)}} order by id';
$builder->build('age_in', Bind::int([18, 24, 36]));
$result = $builder->prepare($sql, [':name' => 'chopin', ':state' => 1]);
$this->assertSame($result, [
'select * from user where name=:name and state=:state and age in (:__1_1__,:__1_2__,:__1_3__) order by id',
[
':name' => [':name', 'chopin', PDO::PARAM_STR],
':state' => [':state', 1, PDO::PARAM_STR],
':__1_1__' => [':__1_1__', 18, PDO::PARAM_INT],
':__1_2__' => [':__1_2__', 24, PDO::PARAM_INT],
':__1_3__' => [':__1_3__', 36, PDO::PARAM_INT],
]
]);
$builder = new Builder();
$sql = 'select * from user where name=:name and state=:state {{age_in: and age in (:age)}} order by id';
$builder->build('age_in', Bind::int([':age' => [18, 24, 36]]));
$result = $builder->prepare($sql, [':name' => 'chopin', ':state' => 1]);
$this->assertSame($result, [
'select * from user where name=:name and state=:state and age in (:age_1__,:age_2__,:age_3__) order by id',
[
':name' => [':name', 'chopin', PDO::PARAM_STR],
':state' => [':state', 1, PDO::PARAM_STR],
':age_1__' => [':age_1__', 18, PDO::PARAM_INT],
':age_2__' => [':age_2__', 24, PDO::PARAM_INT],
':age_3__' => [':age_3__', 36, PDO::PARAM_INT],
]
]);
$builder = new Builder();
$sql = 'select * from user where state=? {{search:and (name like :keyword or mobile like :keyword)}} '
. '{{age_in: and age in (:age)}}';
$builder->build('search', [':keyword' => '%13800138%']);
$result = $builder->prepare($sql, Bind::int(1));
$this->assertSame($result, [
'select * from user where state=:__1__ and (name like :keyword or mobile like :keyword) ',
[
':__1__' => [':__1__', 1, PDO::PARAM_INT],
':keyword' => [':keyword', '%13800138%', PDO::PARAM_STR],
]
]);
$builder = new Builder();
$sql = 'select * from user where name="chopin" {{state: and state=?}}';
$builder->build('state', 1);
$result = $builder->prepare($sql);
$this->assertSame($result, [
'select * from user where name="chopin" and state=:__1__',
[':__1__' => [':__1__', 1, PDO::PARAM_STR]],
]);
$builder = new Builder();
$sql = 'select * from user as u, order as o where u.id=o.user_id and o.state in (:state)'
. '{{user_state: and u.state in (:state)}} {{create_in: and o.created_at between ? and ?}}';
$state = Bind::int([':state' => [1, 2]]);
$builder->build('user_state', $state);
$builder->build('create_in', Bind::int(123, 456));
$result = $builder->prepare($sql, $state);
$this->assertSame($result, [
'select * from user as u, order as o where u.id=o.user_id and o.state in (:state_1__,:state_2__) '
. 'and u.state in (:state_1__,:state_2__) and o.created_at between :__1__ and :__2__',
[
':state_1__' => [':state_1__', 1, PDO::PARAM_INT],
':state_2__' => [':state_2__', 2, PDO::PARAM_INT],
':__1__' => [':__1__', 123, PDO::PARAM_INT],
':__2__' => [':__2__', 456, PDO::PARAM_INT],
],
]);
$builder = new Builder();
$sql = 'select * from user where name="chopin" {{mobile:and mobile like \'%13800%\'{{age: and age > ?}}}}';
$builder->build('mobile');
$builder->build('age', 18);
$result = $builder->prepare($sql);
$this->assertSame($result, [
'select * from user where name="chopin" and mobile like \'%13800%\' and age > :__1__',
[':__1__' => [':__1__', 18, PDO::PARAM_STR]],
]);
$builder = new Builder();
$sql = 'select * from user where 1=1 {{name: and name=?}}';
$builder->build('name', 'chopin');
$result = $builder->prepare($sql, null);
$this->assertSame($result, [
'select * from user where 1=1 and name=:__1__',
[':__1__' => [':__1__', 'chopin', PDO::PARAM_STR]],
]);
$builder = new Builder();
$sql = 'select * from user where name=:name {{age: and age in (?)}} order by id {{limit: limit ?,?}}';
$builder->build('age', 32, ' and age =?');
$builder->build('limit', Bind::int(10, 20));
$result = $builder->prepare($sql, [':name' => 'chopin']);
$this->assertSame($result, [
'select * from user where name=:name and age =:__1__ order by id limit :__2__,:__3__',
[
':name' => [':name', 'chopin', PDO::PARAM_STR],
':__1__' => [':__1__', 32, PDO::PARAM_STR],
':__2__' => [':__2__', 10, PDO::PARAM_INT],
':__3__' => [':__3__', 20, PDO::PARAM_INT],
]
]);
}
public function testDetectBindValueType()
{
$builder = new Builder(['detectBindValueType' => true]);
$sql = 'select * from user where name=? {{age: and age in (?)}} {{remark: and remark=?}} {{bool: and bool=?}}';
$builder->build('age', [[1, 2]]);
$builder->build('remark', [null]);
$builder->build('bool', false);
$result = $builder->prepare($sql, 'chopin');
$this->assertSame($result, [
'select * from user where name=:__1__ and age in (:__2_1__,:__2_2__) and remark=:__3__ and bool=:__4__',
[
':__1__' => [':__1__', 'chopin', PDO::PARAM_STR],
':__2_1__' => [':__2_1__', 1, PDO::PARAM_INT],
':__2_2__' => [':__2_2__', 2, PDO::PARAM_INT],
':__3__' => [':__3__', null, PDO::PARAM_NULL],
':__4__' => [':__4__', false, PDO::PARAM_BOOL],
]
]);
}
public function testDisableOverrideBuild()
{
$builder = new Builder(['allowBuildOverride' => false]);
$sql = 'select * from user where name=? {{age: and age=?}}';
$builder->build('age', 1);
$result = $builder->prepare($sql, 'chopin');
$expect = [
'select * from user where name=:__1__ and age=:__2__',
[
':__1__' => [':__1__', 'chopin', PDO::PARAM_STR],
':__2__' => [':__2__', 1, PDO::PARAM_STR],
]
];
$this->assertSame($result, $expect);
$builder->build('age', 2);
$result = $builder->prepare($sql, 'chopin');
$this->assertSame($result, $expect);
$builder = new Builder();
$builder->build('age', 1);
$result = $builder->prepare($sql, 'chopin');
$this->assertSame($result, $expect);
$expect[1][':__2__'][1] = 2;
$builder->build('age', 2);
$result = $builder->prepare($sql, 'chopin');
$this->assertSame($result, $expect);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Cannot use both question mark and named placeholders in an SQL.
*/
public function testUseTwoStylePlaceholder()
{
$builder = new Builder();
$sql = 'select * from user where name=:name and state=?';
$builder->prepare($sql, ['chopin', 1]);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Placeholders quantity (2) not match the Bind values quantity (1).
*/
public function testBindQtyNotMatchQuestionMarkPlaceholder()
{
$builder = new Builder();
$sql = 'select * from user where name=? and state=?';
$builder->prepare($sql, ['chopin']);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage No bind value on placeholder: ":state".
*/
public function testNotBindValueOnPlaceholder()
{
$builder = new Builder();
$sql = 'select * from user where name=:name and state=:state';
$builder->prepare($sql, [':name' => 'chopin']);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid SQL-markup label: "0name".
*/
public function testInvalidLabel()
{
$builder = new Builder();
$builder->build('0name');
}
protected function setupDB()
{
$ddl = <<<SQL
create table user(
id INTEGER PRIMARY KEY,
mobile TEXT,
name TEXT,
age INTEGER,
state INTEGER,
created_at INTEGER
)
SQL;
$data = <<<SQL
insert into user(mobile, name, age, state, created_at) values
('10086', 'CMCC', 20, 1, 1569343838),
('10000', 'CTCC', 21, 1, 1569343838),
('10010', 'CUCC', 22, 1, 1569343838)
SQL;
$dbh = new PDO('sqlite::memory:');
$dbh->query($ddl);
$dbh->query($data);
return $dbh;
}
public function testRun()
{
$dbh = $this->setupDB();
$sql = 'select id,name from user where state=?{{age: and age>?}} order by id {{limit: limit :limit,:offset}}';
$builder = new Builder();
$builder->build('age', 20);
$stmt = $builder->run($dbh, $sql, 1);
$this->assertTrue($stmt instanceof PDOStatement);
$this->assertSame($stmt->fetchAll(PDO::FETCH_ASSOC), [
['id' => '2', 'name' => 'CTCC'],
['id' => '3', 'name' => 'CUCC'],
]);
}
/**
* @expectedException PDOException
* @expectedExceptionMessage SQLSTATE[HY000]: General error: 1 no such column: not_exists_field
*/
public function testRunPrepareFail()
{
$dbh = $this->setupDB();
$sql = 'select not_exists_field from user where state=?{{age: and age>?}} order by id';
$builder = new Builder();
$builder->build('age', 20);
$builder->run($dbh, $sql, 1);
}
/**
* @expectedException PDOException
* @expectedExceptionMessage SQLSTATE[42S22]: General error: 1054 Unknown column 'not_exists_field' in 'field list'
*/
public function testRunExecuteFail()
{
$stmt = $this->getMockBuilder(PDOStatement::class)->setMethods(['execute', 'errorInfo'])->getMock();
$stmt->method('execute')->willReturn(false);
$stmt->method('errorInfo')->willReturn([
'42S22',
1054,
"Unknown column 'not_exists_field' in 'field list'",
]);
$dbh = $this->getMockBuilder(PDO::class)->disableOriginalConstructor()->setMethods(['prepare'])->getMock();
$dbh->method('prepare')->willReturn($stmt);
$sql = 'select not_exists_field from user where state=?{{age: and age>?}} order by id';
$builder = new Builder();
$builder->build('age', 20);
$builder->run($dbh, $sql, 1);
}
}