|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Yiisoft\Db\Expression\Expression; |
|
9
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
10
|
|
|
use Yiisoft\Db\Query\Query; |
|
11
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
|
12
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractQueryTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
use TestTrait; |
|
17
|
|
|
|
|
18
|
|
|
public function testAddGroupByExpression(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$db = $this->getConnection(); |
|
21
|
|
|
|
|
22
|
|
|
$expression = new Expression('[[id]] + 1'); |
|
23
|
|
|
$query = new Query($db); |
|
24
|
|
|
$query->addGroupBy($expression); |
|
25
|
|
|
|
|
26
|
|
|
$this->assertSame([$expression], $query->getGroupBy()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testAddOrderByEmpty(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$db = $this->getConnection(); |
|
32
|
|
|
|
|
33
|
|
|
$query = new Query($db); |
|
34
|
|
|
$query->addOrderBy([]); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertSame([], $query->getOrderBy()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testAddParamsWithNameInt(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$db = $this->getConnection(); |
|
42
|
|
|
|
|
43
|
|
|
$query = new Query($db); |
|
44
|
|
|
$query->params([1 => 'value']); |
|
45
|
|
|
$query->addParams([2 => 'test']); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertSame([1 => 'value', 2 => 'test'], $query->getParams()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @depends testFilterWhereWithHashFormat |
|
52
|
|
|
* @depends testFilterWhereWithOperatorFormat |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testAndFilterCompare(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$db = $this->getConnection(); |
|
57
|
|
|
|
|
58
|
|
|
$query = new Query($db); |
|
59
|
|
|
$result = $query->andFilterCompare('name', null); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertInstanceOf(QueryInterface::class, $result); |
|
62
|
|
|
$this->assertNull($query->getWhere()); |
|
63
|
|
|
|
|
64
|
|
|
$query->andFilterCompare('name', ''); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertNull($query->getWhere()); |
|
67
|
|
|
|
|
68
|
|
|
$query->andFilterCompare('name', 'John Doe'); |
|
69
|
|
|
$condition = ['=', 'name', 'John Doe']; |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
72
|
|
|
|
|
73
|
|
|
$condition = ['and', $condition, ['like', 'name', 'Doe']]; |
|
74
|
|
|
$query->andFilterCompare('name', 'Doe', 'like'); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
77
|
|
|
|
|
78
|
|
|
$condition[] = ['>', 'rating', '9']; |
|
79
|
|
|
$query->andFilterCompare('rating', '>9'); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
82
|
|
|
|
|
83
|
|
|
$condition[] = ['<=', 'value', '100']; |
|
84
|
|
|
$query->andFilterCompare('value', '<=100'); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testAndFilterHaving(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$db = $this->getConnection(); |
|
92
|
|
|
|
|
93
|
|
|
$query = new Query($db); |
|
94
|
|
|
$result = $query->andFilterHaving(['>', 'id', 1]); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertInstanceOf(QueryInterface::class, $result); |
|
97
|
|
|
$this->assertSame(['>', 'id', 1], $query->getHaving()); |
|
98
|
|
|
|
|
99
|
|
|
$query->andFilterHaving(['>', 'id', 2]); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertSame(['and', ['>', 'id', 1], ['>', 'id', 2]], $query->getHaving()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testAndFilterHavingWithHashFormat(): void |
|
105
|
|
|
{ |
|
106
|
|
|
$db = $this->getConnection(); |
|
107
|
|
|
|
|
108
|
|
|
$query = new Query($db); |
|
109
|
|
|
$result = $query->andFilterHaving(['status' => 1]); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertInstanceOf(QueryInterface::class, $result); |
|
112
|
|
|
$this->assertSame(['status' => 1], $query->getHaving()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testEmulateExecution(): void |
|
116
|
|
|
{ |
|
117
|
|
|
$db = $this->getConnectionWithData(); |
|
118
|
|
|
|
|
119
|
|
|
$rows = (new Query($db))->from('customer')->emulateExecution()->all(); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertSame([], $rows); |
|
122
|
|
|
|
|
123
|
|
|
$row = (new Query($db))->from('customer')->emulateExecution()->one(); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertNull($row); |
|
126
|
|
|
|
|
127
|
|
|
$exists = (new Query($db))->from('customer')->emulateExecution()->exists(); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertFalse($exists); |
|
130
|
|
|
|
|
131
|
|
|
$count = (new Query($db))->from('customer')->emulateExecution()->count(); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertSame(0, $count); |
|
134
|
|
|
|
|
135
|
|
|
$sum = (new Query($db))->from('customer')->emulateExecution()->sum('id'); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertNull($sum); |
|
138
|
|
|
|
|
139
|
|
|
$sum = (new Query($db))->from('customer')->emulateExecution()->average('id'); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertNull($sum); |
|
142
|
|
|
|
|
143
|
|
|
$max = (new Query($db))->from('customer')->emulateExecution()->max('id'); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertNull($max); |
|
146
|
|
|
|
|
147
|
|
|
$min = (new Query($db))->from('customer')->emulateExecution()->min('id'); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertNull($min); |
|
150
|
|
|
|
|
151
|
|
|
$scalar = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->scalar(); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertNull($scalar); |
|
154
|
|
|
|
|
155
|
|
|
$column = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->column(); |
|
156
|
|
|
$this->assertSame([], $column); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function testFilterHavingWithHashFormat(): void |
|
160
|
|
|
{ |
|
161
|
|
|
$db = $this->getConnection(); |
|
162
|
|
|
|
|
163
|
|
|
$query = new Query($db); |
|
164
|
|
|
$query->filterHaving(['id' => 0, 'title' => ' ', 'author_ids' => []]); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
|
167
|
|
|
|
|
168
|
|
|
$query->andFilterHaving(['status' => null]); |
|
169
|
|
|
|
|
170
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
|
171
|
|
|
|
|
172
|
|
|
$query->orFilterHaving(['name' => '']); |
|
173
|
|
|
|
|
174
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testFilterHavingWithOperatorFormat(): void |
|
178
|
|
|
{ |
|
179
|
|
|
$db = $this->getConnection(); |
|
180
|
|
|
|
|
181
|
|
|
$query = new Query($db); |
|
182
|
|
|
$condition = ['like', 'name', 'Alex']; |
|
183
|
|
|
$query->filterHaving($condition); |
|
184
|
|
|
|
|
185
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
186
|
|
|
|
|
187
|
|
|
$query->andFilterHaving(['between', 'id', null, null]); |
|
188
|
|
|
|
|
189
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
190
|
|
|
|
|
191
|
|
|
$query->orFilterHaving(['not between', 'id', null, null]); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
194
|
|
|
|
|
195
|
|
|
$query->andFilterHaving(['in', 'id', []]); |
|
196
|
|
|
|
|
197
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
198
|
|
|
|
|
199
|
|
|
$query->andFilterHaving(['not in', 'id', []]); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
202
|
|
|
|
|
203
|
|
|
$query->andFilterHaving(['like', 'id', '']); |
|
204
|
|
|
|
|
205
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
206
|
|
|
|
|
207
|
|
|
$query->andFilterHaving(['or like', 'id', '']); |
|
208
|
|
|
|
|
209
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
210
|
|
|
|
|
211
|
|
|
$query->andFilterHaving(['not like', 'id', ' ']); |
|
212
|
|
|
|
|
213
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
214
|
|
|
|
|
215
|
|
|
$query->andFilterHaving(['or not like', 'id', null]); |
|
216
|
|
|
|
|
217
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
218
|
|
|
|
|
219
|
|
|
$query->andFilterHaving(['or', ['eq', 'id', null], ['eq', 'id', []]]); |
|
220
|
|
|
|
|
221
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function testFilterRecursively(): void |
|
225
|
|
|
{ |
|
226
|
|
|
$db = $this->getConnection(); |
|
227
|
|
|
|
|
228
|
|
|
$query = new Query($db); |
|
229
|
|
|
$query->filterWhere( |
|
230
|
|
|
['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]] |
|
231
|
|
|
); |
|
232
|
|
|
|
|
233
|
|
|
$this->assertSame(['and', ['id' => 1]], $query->getWhere()); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function testFilterWhereWithHashFormat(): void |
|
237
|
|
|
{ |
|
238
|
|
|
$db = $this->getConnection(); |
|
239
|
|
|
|
|
240
|
|
|
$query = new Query($db); |
|
241
|
|
|
$query->filterWhere(['id' => 0, 'title' => ' ', 'author_ids' => []]); |
|
242
|
|
|
|
|
243
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
|
244
|
|
|
|
|
245
|
|
|
$query->andFilterWhere(['status' => null]); |
|
246
|
|
|
|
|
247
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
|
248
|
|
|
|
|
249
|
|
|
$query->orFilterWhere(['name' => '']); |
|
250
|
|
|
|
|
251
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function testFilterWhereWithOperatorFormat(): void |
|
255
|
|
|
{ |
|
256
|
|
|
$db = $this->getConnection(); |
|
257
|
|
|
|
|
258
|
|
|
$query = new Query($db); |
|
259
|
|
|
$condition = ['like', 'name', 'Alex']; |
|
260
|
|
|
$query->filterWhere($condition); |
|
261
|
|
|
|
|
262
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
263
|
|
|
|
|
264
|
|
|
$query->andFilterWhere(['between', 'id', null, null]); |
|
265
|
|
|
|
|
266
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
267
|
|
|
|
|
268
|
|
|
$query->orFilterWhere(['not between', 'id', null, null]); |
|
269
|
|
|
|
|
270
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
271
|
|
|
|
|
272
|
|
|
$query->andFilterWhere(['in', 'id', []]); |
|
273
|
|
|
|
|
274
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
275
|
|
|
|
|
276
|
|
|
$query->andFilterWhere(['not in', 'id', []]); |
|
277
|
|
|
|
|
278
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
279
|
|
|
|
|
280
|
|
|
$query->andFilterWhere(['like', 'id', '']); |
|
281
|
|
|
|
|
282
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
283
|
|
|
|
|
284
|
|
|
$query->andFilterWhere(['or like', 'id', '']); |
|
285
|
|
|
|
|
286
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
287
|
|
|
|
|
288
|
|
|
$query->andFilterWhere(['not like', 'id', ' ']); |
|
289
|
|
|
|
|
290
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
291
|
|
|
|
|
292
|
|
|
$query->andFilterWhere(['or not like', 'id', null]); |
|
293
|
|
|
|
|
294
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
295
|
|
|
|
|
296
|
|
|
$query->andFilterWhere(['or', ['eq', 'id', null], ['eq', 'id', []]]); |
|
297
|
|
|
|
|
298
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function testFrom(): void |
|
302
|
|
|
{ |
|
303
|
|
|
$db = $this->getConnection(); |
|
304
|
|
|
|
|
305
|
|
|
$query = new Query($db); |
|
306
|
|
|
$query->from('user'); |
|
307
|
|
|
|
|
308
|
|
|
$this->assertSame(['user'], $query->getFrom()); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
public function testFromTableIsArrayWithExpression(): void |
|
312
|
|
|
{ |
|
313
|
|
|
$db = $this->getConnection(); |
|
314
|
|
|
|
|
315
|
|
|
$query = new Query($db); |
|
316
|
|
|
$tables = new Expression('(SELECT id,name FROM user) u'); |
|
317
|
|
|
$query->from($tables); |
|
318
|
|
|
$from = $query->getFrom(); |
|
319
|
|
|
|
|
320
|
|
|
$this->assertIsArray($from); |
|
321
|
|
|
$this->assertInstanceOf(ExpressionInterface::class, $from[0]); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
public function testGroup(): void |
|
325
|
|
|
{ |
|
326
|
|
|
$db = $this->getConnection(); |
|
327
|
|
|
|
|
328
|
|
|
$query = new Query($db); |
|
329
|
|
|
$query->groupBy('team'); |
|
330
|
|
|
|
|
331
|
|
|
$this->assertSame(['team'], $query->getGroupBy()); |
|
332
|
|
|
|
|
333
|
|
|
$query->addGroupBy('company'); |
|
334
|
|
|
|
|
335
|
|
|
$this->assertSame(['team', 'company'], $query->getGroupBy()); |
|
336
|
|
|
|
|
337
|
|
|
$query->addGroupBy('age'); |
|
338
|
|
|
|
|
339
|
|
|
$this->assertSame(['team', 'company', 'age'], $query->getGroupBy()); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
public function testHaving(): void |
|
343
|
|
|
{ |
|
344
|
|
|
$db = $this->getConnection(); |
|
345
|
|
|
|
|
346
|
|
|
$query = new Query($db); |
|
347
|
|
|
$query->having('id = :id', [':id' => 1]); |
|
348
|
|
|
|
|
349
|
|
|
$this->assertSame('id = :id', $query->getHaving()); |
|
350
|
|
|
$this->assertSame([':id' => 1], $query->getParams()); |
|
351
|
|
|
|
|
352
|
|
|
$query->andHaving('name = :name', [':name' => 'something']); |
|
353
|
|
|
$this->assertSame(['and', 'id = :id', 'name = :name'], $query->getHaving()); |
|
354
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something'], $query->getParams()); |
|
355
|
|
|
|
|
356
|
|
|
$query->orHaving('age = :age', [':age' => '30']); |
|
357
|
|
|
$this->assertSame(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getHaving()); |
|
358
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams()); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
public function testJoin(): void |
|
362
|
|
|
{ |
|
363
|
|
|
$db = $this->getConnection(); |
|
364
|
|
|
|
|
365
|
|
|
$query = new Query($db); |
|
366
|
|
|
$query->join('INNER JOIN', 'profile', 'user.id = profile.user_id'); |
|
367
|
|
|
|
|
368
|
|
|
$this->assertSame([['INNER JOIN', 'profile', 'user.id = profile.user_id']], $query->getJoin()); |
|
369
|
|
|
|
|
370
|
|
|
$query->join('LEFT JOIN', 'order', 'user.id = order.user_id'); |
|
371
|
|
|
|
|
372
|
|
|
$this->assertSame( |
|
373
|
|
|
[['INNER JOIN', 'profile', 'user.id = profile.user_id'], ['LEFT JOIN', 'order', 'user.id = order.user_id']], |
|
374
|
|
|
$query->getJoin() |
|
375
|
|
|
); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
public function testLimitOffset(): void |
|
379
|
|
|
{ |
|
380
|
|
|
$db = $this->getConnection(); |
|
381
|
|
|
|
|
382
|
|
|
$query = new Query($db); |
|
383
|
|
|
$query->limit(10)->offset(5); |
|
384
|
|
|
|
|
385
|
|
|
$this->assertSame(10, $query->getLimit()); |
|
386
|
|
|
$this->assertSame(5, $query->getOffset()); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
public function testOrFilterHavingHashFormat(): void |
|
390
|
|
|
{ |
|
391
|
|
|
$db = $this->getConnection(); |
|
392
|
|
|
|
|
393
|
|
|
$query = new Query($db); |
|
394
|
|
|
$query->orFilterHaving(['status' => 1]); |
|
395
|
|
|
|
|
396
|
|
|
$this->assertSame(['status' => 1], $query->getHaving()); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
public function testOrFilterWhereHashFormat(): void |
|
400
|
|
|
{ |
|
401
|
|
|
$db = $this->getConnection(); |
|
402
|
|
|
|
|
403
|
|
|
$query = new Query($db); |
|
404
|
|
|
$query->orFilterWhere(['status' => 1]); |
|
405
|
|
|
|
|
406
|
|
|
$this->assertSame(['status' => 1], $query->getWhere()); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
public function testOrder(): void |
|
410
|
|
|
{ |
|
411
|
|
|
$db = $this->getConnection(); |
|
412
|
|
|
|
|
413
|
|
|
$query = new Query($db); |
|
414
|
|
|
$query->orderBy('team'); |
|
415
|
|
|
|
|
416
|
|
|
$this->assertSame(['team' => SORT_ASC], $query->getOrderBy()); |
|
417
|
|
|
|
|
418
|
|
|
$query->addOrderBy('company'); |
|
419
|
|
|
|
|
420
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC], $query->getOrderBy()); |
|
421
|
|
|
|
|
422
|
|
|
$query->addOrderBy('age'); |
|
423
|
|
|
|
|
424
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->getOrderBy()); |
|
425
|
|
|
|
|
426
|
|
|
$query->addOrderBy(['age' => SORT_DESC]); |
|
427
|
|
|
|
|
428
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->getOrderBy()); |
|
429
|
|
|
|
|
430
|
|
|
$query->addOrderBy('age ASC, company DESC'); |
|
431
|
|
|
|
|
432
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->getOrderBy()); |
|
433
|
|
|
|
|
434
|
|
|
$expression1 = new Expression('SUBSTR(name, 3, 4) DESC, x ASC'); |
|
435
|
|
|
|
|
436
|
|
|
$query->orderBy($expression1); |
|
437
|
|
|
|
|
438
|
|
|
$this->assertSame([$expression1], $query->getOrderBy()); |
|
439
|
|
|
|
|
440
|
|
|
$expression2 = new Expression('SUBSTR(name, 3, 4) DESC, x ASC'); |
|
441
|
|
|
|
|
442
|
|
|
$query->addOrderBy($expression2); |
|
443
|
|
|
|
|
444
|
|
|
$this->assertSame([$expression1, $expression2], $query->getOrderBy()); |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
public function testRightJoin(): void |
|
448
|
|
|
{ |
|
449
|
|
|
$db = $this->getConnection(); |
|
450
|
|
|
|
|
451
|
|
|
$query = new Query($db); |
|
452
|
|
|
$query->rightJoin('profile', 'user.id = profile.user_id'); |
|
453
|
|
|
|
|
454
|
|
|
$this->assertSame([['RIGHT JOIN', 'profile', 'user.id = profile.user_id']], $query->getJoin()); |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
public function testSelect(): void |
|
458
|
|
|
{ |
|
459
|
|
|
$db = $this->getConnection(); |
|
460
|
|
|
|
|
461
|
|
|
/* default */ |
|
462
|
|
|
$query = new Query($db); |
|
463
|
|
|
$query->select('*'); |
|
464
|
|
|
|
|
465
|
|
|
$this->assertSame(['*' => '*'], $query->getSelect()); |
|
466
|
|
|
$this->assertNull($query->getDistinct()); |
|
467
|
|
|
$this->assertNull($query->getSelectOption()); |
|
|
|
|
|
|
468
|
|
|
|
|
469
|
|
|
$query = new Query($db); |
|
470
|
|
|
$query->select('id, name', 'something')->distinct(true); |
|
471
|
|
|
|
|
472
|
|
|
$this->assertSame(['id' => 'id', 'name' => 'name'], $query->getSelect()); |
|
473
|
|
|
$this->assertTrue($query->getDistinct()); |
|
474
|
|
|
$this->assertSame('something', $query->getSelectOption()); |
|
475
|
|
|
|
|
476
|
|
|
$query = new Query($db); |
|
477
|
|
|
$query->addSelect('email'); |
|
478
|
|
|
|
|
479
|
|
|
$this->assertSame(['email' => 'email'], $query->getSelect()); |
|
480
|
|
|
|
|
481
|
|
|
$query = new Query($db); |
|
482
|
|
|
$query->select('id, name'); |
|
483
|
|
|
$query->addSelect('email'); |
|
484
|
|
|
|
|
485
|
|
|
$this->assertSame(['id' => 'id', 'name' => 'name', 'email' => 'email'], $query->getSelect()); |
|
486
|
|
|
|
|
487
|
|
|
$query = new Query($db); |
|
488
|
|
|
$query->select('name, lastname'); |
|
489
|
|
|
$query->addSelect('name'); |
|
490
|
|
|
|
|
491
|
|
|
$this->assertSame(['name' => 'name', 'lastname' => 'lastname'], $query->getSelect()); |
|
492
|
|
|
|
|
493
|
|
|
$query = new Query($db); |
|
494
|
|
|
$query->addSelect(['*', 'abc']); |
|
495
|
|
|
$query->addSelect(['*', 'bca']); |
|
496
|
|
|
|
|
497
|
|
|
$this->assertSame(['*' => '*', 'abc' => 'abc', 'bca' => 'bca'], $query->getSelect()); |
|
498
|
|
|
|
|
499
|
|
|
$query = new Query($db); |
|
500
|
|
|
$query->addSelect(['field1 as a', 'field 1 as b']); |
|
501
|
|
|
|
|
502
|
|
|
$this->assertSame(['a' => 'field1', 'b' => 'field 1'], $query->getSelect()); |
|
503
|
|
|
|
|
504
|
|
|
$query = new Query($db); |
|
505
|
|
|
$query->addSelect(['field1 a', 'field 1 b']); |
|
506
|
|
|
|
|
507
|
|
|
$this->assertSame(['a' => 'field1', 'b' => 'field 1'], $query->getSelect()); |
|
508
|
|
|
|
|
509
|
|
|
$query = new Query($db); |
|
510
|
|
|
$query->select(['name' => 'firstname', 'lastname']); |
|
511
|
|
|
$query->addSelect(['firstname', 'surname' => 'lastname']); |
|
512
|
|
|
$query->addSelect(['firstname', 'lastname']); |
|
513
|
|
|
|
|
514
|
|
|
$this->assertSame( |
|
515
|
|
|
['name' => 'firstname', 'lastname' => 'lastname', 'firstname' => 'firstname', 'surname' => 'lastname'], |
|
516
|
|
|
$query->getSelect(), |
|
517
|
|
|
); |
|
518
|
|
|
|
|
519
|
|
|
$query = new Query($db); |
|
520
|
|
|
$query->select('name, name, name as X, name as X'); |
|
521
|
|
|
|
|
522
|
|
|
$this->assertSame(['name' => 'name', 'X' => 'name'], $query->getSelect()); |
|
523
|
|
|
|
|
524
|
|
|
/** |
|
525
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15676} |
|
526
|
|
|
*/ |
|
527
|
|
|
$query = (new Query($db))->select('id'); |
|
528
|
|
|
|
|
529
|
|
|
$this->assertSame(['id' => 'id'], $query->getSelect()); |
|
530
|
|
|
|
|
531
|
|
|
$query->select(['id', 'brand_id']); |
|
532
|
|
|
|
|
533
|
|
|
$this->assertSame(['id' => 'id', 'brand_id' => 'brand_id'], $query->getSelect()); |
|
534
|
|
|
|
|
535
|
|
|
/** |
|
536
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15676} |
|
537
|
|
|
*/ |
|
538
|
|
|
$query = (new Query($db))->select(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)']); |
|
539
|
|
|
|
|
540
|
|
|
$this->assertSame(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)'], $query->getSelect()); |
|
541
|
|
|
|
|
542
|
|
|
$query->addSelect(['LEFT(name,7) as test']); |
|
543
|
|
|
|
|
544
|
|
|
$this->assertSame( |
|
545
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
|
546
|
|
|
$query->getSelect() |
|
547
|
|
|
); |
|
548
|
|
|
|
|
549
|
|
|
$query->addSelect(['LEFT(name,7) as test']); |
|
550
|
|
|
|
|
551
|
|
|
$this->assertSame( |
|
552
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
|
553
|
|
|
$query->getSelect() |
|
554
|
|
|
); |
|
555
|
|
|
|
|
556
|
|
|
$query->addSelect(['test' => 'LEFT(name,7)']); |
|
557
|
|
|
|
|
558
|
|
|
$this->assertSame( |
|
559
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
|
560
|
|
|
$query->getSelect() |
|
561
|
|
|
); |
|
562
|
|
|
|
|
563
|
|
|
/** |
|
564
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15731} |
|
565
|
|
|
*/ |
|
566
|
|
|
$selectedCols = [ |
|
567
|
|
|
'total_sum' => 'SUM(f.amount)', |
|
568
|
|
|
'in_sum' => 'SUM(IF(f.type = :type_in, f.amount, 0))', |
|
569
|
|
|
'out_sum' => 'SUM(IF(f.type = :type_out, f.amount, 0))', |
|
570
|
|
|
]; |
|
571
|
|
|
$query = (new Query($db)) |
|
572
|
|
|
->select($selectedCols) |
|
573
|
|
|
->addParams([':type_in' => 'in', ':type_out' => 'out', ':type_partner' => 'partner']); |
|
574
|
|
|
|
|
575
|
|
|
$this->assertSame($selectedCols, $query->getSelect()); |
|
576
|
|
|
|
|
577
|
|
|
$query->select($selectedCols); |
|
578
|
|
|
|
|
579
|
|
|
$this->assertSame($selectedCols, $query->getSelect()); |
|
580
|
|
|
|
|
581
|
|
|
/** |
|
582
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/17384} |
|
583
|
|
|
*/ |
|
584
|
|
|
$query = new Query($db); |
|
585
|
|
|
$query->select('DISTINCT ON(tour_dates.date_from) tour_dates.date_from, tour_dates.id'); |
|
586
|
|
|
|
|
587
|
|
|
$this->assertSame( |
|
588
|
|
|
['DISTINCT ON(tour_dates.date_from) tour_dates.date_from', 'tour_dates.id' => 'tour_dates.id'], |
|
589
|
|
|
$query->getSelect() |
|
590
|
|
|
); |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
public function testSetJoin(): void |
|
594
|
|
|
{ |
|
595
|
|
|
$db = $this->getConnection(); |
|
596
|
|
|
|
|
597
|
|
|
$query = new Query($db); |
|
598
|
|
|
$query->setJoin(['INNER JOIN', 'table1', 'table1.id = table2.id']); |
|
599
|
|
|
|
|
600
|
|
|
$this->assertSame(['INNER JOIN', 'table1', 'table1.id = table2.id'], $query->getJoin()); |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
public function testSetUnion(): void |
|
604
|
|
|
{ |
|
605
|
|
|
$db = $this->getConnection(); |
|
606
|
|
|
|
|
607
|
|
|
$query = new Query($db); |
|
608
|
|
|
$query->setUnion(['SELECT * FROM table1', 'SELECT * FROM table2']); |
|
609
|
|
|
|
|
610
|
|
|
$this->assertSame(['SELECT * FROM table1', 'SELECT * FROM table2'], $query->getUnion()); |
|
611
|
|
|
} |
|
612
|
|
|
|
|
613
|
|
|
public function testShouldEmulateExecution(): void |
|
614
|
|
|
{ |
|
615
|
|
|
$db = $this->getConnection(); |
|
616
|
|
|
|
|
617
|
|
|
$query = new Query($db); |
|
618
|
|
|
$this->assertFalse($query->shouldEmulateExecution()); |
|
619
|
|
|
|
|
620
|
|
|
$query = new Query($db); |
|
621
|
|
|
$query->emulateExecution(true); |
|
622
|
|
|
|
|
623
|
|
|
$this->assertTrue($query->shouldEmulateExecution()); |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
public function testToString(): void |
|
627
|
|
|
{ |
|
628
|
|
|
$db = $this->getConnection(); |
|
629
|
|
|
|
|
630
|
|
|
$query = new Query($db); |
|
631
|
|
|
$query->select('id')->from('user')->where(['id' => 1]); |
|
632
|
|
|
|
|
633
|
|
|
$this->assertSame(serialize($query), (string) $query); |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
public function testWhere(): void |
|
637
|
|
|
{ |
|
638
|
|
|
$db = $this->getConnection(); |
|
639
|
|
|
|
|
640
|
|
|
$query = new Query($db); |
|
641
|
|
|
$query->where('id = :id', [':id' => 1]); |
|
642
|
|
|
|
|
643
|
|
|
$this->assertSame('id = :id', $query->getWhere()); |
|
644
|
|
|
$this->assertSame([':id' => 1], $query->getParams()); |
|
645
|
|
|
|
|
646
|
|
|
$query->andWhere('name = :name', [':name' => 'something']); |
|
647
|
|
|
|
|
648
|
|
|
$this->assertSame(['and', 'id = :id', 'name = :name'], $query->getWhere()); |
|
649
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something'], $query->getParams()); |
|
650
|
|
|
|
|
651
|
|
|
$query->orWhere('age = :age', [':age' => '30']); |
|
652
|
|
|
|
|
653
|
|
|
$this->assertSame(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getWhere()); |
|
654
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams()); |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
public function testWithQueries(): void |
|
658
|
|
|
{ |
|
659
|
|
|
$db = $this->getConnection(); |
|
660
|
|
|
|
|
661
|
|
|
$query = new Query($db); |
|
662
|
|
|
$query->withQueries(['query1', 'query2']); |
|
663
|
|
|
|
|
664
|
|
|
$this->assertSame(['query1', 'query2'], $query->getWithQueries()); |
|
665
|
|
|
} |
|
666
|
|
|
} |
|
667
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.