Passed
Pull Request — master (#372)
by Wilmer
03:58
created

AbstractQueryTest::testEmulateExecution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 35
rs 9.568
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Connection\ConnectionInterface;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidArgumentException;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Exception\NotSupportedException;
13
use Yiisoft\Db\Expression\Expression;
14
use Yiisoft\Db\Query\Query;
15
use Yiisoft\Db\Query\QueryInterface;
16
use Yiisoft\Db\Schema\Schema;
17
18
abstract class AbstractQueryTest extends TestCase
19
{
20
    use GetTablesAliasTrait;
21
22
    /**
23
     * @depends testFilterWhereWithHashFormat
24
     * @depends testFilterWhereWithOperatorFormat
25
     */
26
    public function testAndFilterCompare(): void
27
    {
28
        $db = $this->getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Yiisoft\Db\Tests\AbstractQueryTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        /** @scrutinizer ignore-call */ 
29
        $db = $this->getConnection();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
30
        $query = new Query($db);
31
        $result = $query->andFilterCompare('name', null);
32
33
        $this->assertInstanceOf(QueryInterface::class, $result);
34
        $this->assertNull($query->getWhere());
35
36
        $query->andFilterCompare('name', '');
37
38
        $this->assertNull($query->getWhere());
39
40
        $query->andFilterCompare('name', 'John Doe');
41
        $condition = ['=', 'name', 'John Doe'];
42
43
        $this->assertSame($condition, $query->getWhere());
44
45
        $condition = ['and', $condition, ['like', 'name', 'Doe']];
46
        $query->andFilterCompare('name', 'Doe', 'like');
47
48
        $this->assertSame($condition, $query->getWhere());
49
50
        $condition[] = ['>', 'rating', '9'];
51
        $query->andFilterCompare('rating', '>9');
52
53
        $this->assertSame($condition, $query->getWhere());
54
55
        $condition[] = ['<=', 'value', '100'];
56
        $query->andFilterCompare('value', '<=100');
57
58
        $this->assertSame($condition, $query->getWhere());
59
    }
60
61
    public function testColumn(): void
62
    {
63
        $db = $this->getConnection();
64
65
        $result = (new Query($db))
66
            ->select('name')
67
            ->from('customer')
68
            ->orderBy(['id' => SORT_DESC])
69
            ->column();
70
71
        $this->assertSame(['user3', 'user2', 'user1'], $result);
72
73
        /**
74
         * {@see https://github.com/yiisoft/yii2/issues/7515}
75
         */
76
        $result = (new Query($db))->from('customer')
77
            ->select('name')
78
            ->orderBy(['id' => SORT_DESC])
79
            ->indexBy('id')
80
            ->column();
81
82
        $this->assertSame([3 => 'user3', 2 => 'user2', 1 => 'user1'], $result);
83
84
        /**
85
         * {@see https://github.com/yiisoft/yii2/issues/12649}
86
         */
87
        $result = (new Query($db))->from('customer')
88
            ->select(['name', 'id'])
89
            ->orderBy(['id' => SORT_DESC])
90
            ->indexBy(fn ($row) => $row['id'] * 2)
91
            ->column();
92
93
        $this->assertSame([6 => 'user3', 4 => 'user2', 2 => 'user1'], $result);
94
95
        $result = (new Query($db))->from('customer')
96
            ->select(['name'])
97
            ->indexBy('name')
98
            ->orderBy(['id' => SORT_DESC])
99
            ->column();
100
101
        $this->assertSame(['user3' => 'user3', 'user2' => 'user2', 'user1' => 'user1'], $result);
102
103
        $result = (new Query($db))->from('customer')
104
            ->select(['name'])
105
            ->where(['id' => 10])
106
            ->orderBy(['id' => SORT_DESC])
107
            ->column();
108
109
        $this->assertSame([], $result);
110
    }
111
112
    public function testCount(): void
113
    {
114
        $db = $this->getConnection();
115
116
        $count = (new Query($db))->from('customer')->count('*');
117
118
        $this->assertSame(3, $count);
119
120
        $count = (new Query($db))->from('customer')->where(['status' => 2])->count('*');
121
122
        $this->assertSame(1, $count);
123
124
        $count = (new Query($db))
125
            ->select('[[status]], COUNT([[id]]) cnt')
126
            ->from('customer')
127
            ->groupBy('status')
128
            ->count('*');
129
130
        $this->assertSame(2, $count);
131
132
        /* testing that orderBy() should be ignored here as it does not affect the count anyway. */
133
        $count = (new Query($db))->from('customer')->orderBy('status')->count('*');
134
135
        $this->assertSame(3, $count);
136
137
        $count = (new Query($db))->from('customer')->orderBy('id')->limit(1)->count('*');
138
139
        $this->assertSame(3, $count);
140
    }
141
142
    public function testEmulateExecution(): void
143
    {
144
        $db = $this->getConnection();
145
146
        $this->assertGreaterThan(0, (new Query($db))->from('customer')->count('*'));
147
148
        $rows = (new Query($db))->from('customer')->emulateExecution()->all();
149
        $this->assertSame([], $rows);
150
151
        $row = (new Query($db))->from('customer')->emulateExecution()->one();
152
        $this->assertNull($row);
153
154
        $exists = (new Query($db))->from('customer')->emulateExecution()->exists();
155
        $this->assertFalse($exists);
156
157
        $count = (new Query($db))->from('customer')->emulateExecution()->count('*');
158
        $this->assertSame(0, $count);
159
160
        $sum = (new Query($db))->from('customer')->emulateExecution()->sum('id');
161
        $this->assertNull($sum);
162
163
        $sum = (new Query($db))->from('customer')->emulateExecution()->average('id');
164
        $this->assertNull($sum);
165
166
        $max = (new Query($db))->from('customer')->emulateExecution()->max('id');
167
        $this->assertNull($max);
168
169
        $min = (new Query($db))->from('customer')->emulateExecution()->min('id');
170
        $this->assertNull($min);
171
172
        $scalar = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->scalar();
173
        $this->assertNull($scalar);
174
175
        $column = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->column();
176
        $this->assertSame([], $column);
177
    }
178
179
    public function testExists(): void
180
    {
181
        $db = $this->getConnection();
182
183
        $result = (new Query($db))->from('customer')->where(['status' => 2])->exists();
184
185
        $this->assertTrue($result);
186
187
        $result = (new Query($db))->from('customer')->where(['status' => 3])->exists();
188
189
        $this->assertFalse($result);
190
    }
191
192
    /**
193
     * {@see https://github.com/yiisoft/yii2/issues/15355}
194
     */
195
    public function testExpressionInFrom(): void
196
    {
197
        $db = $this->getConnection();
198
199
        $query = (new Query($db))
200
            ->from(
201
                new Expression(
202
                    '(SELECT [[id]], [[name]], [[email]], [[address]], [[status]] FROM {{customer}}) c'
203
                )
204
            )
205
            ->where(['status' => 2]);
206
207
        $result = $query->one();
208
209
        $this->assertSame('user3', $result['name']);
210
    }
211
212
    public function testFilterHavingWithHashFormat(): void
213
    {
214
        $db = $this->getConnection();
215
216
        $query = new Query($db);
217
        $query->filterHaving(['id' => 0, 'title' => '   ', 'author_ids' => []]);
218
219
        $this->assertSame(['id' => 0], $query->getHaving());
220
221
        $query->andFilterHaving(['status' => null]);
222
223
        $this->assertSame(['id' => 0], $query->getHaving());
224
225
        $query->orFilterHaving(['name' => '']);
226
227
        $this->assertSame(['id' => 0], $query->getHaving());
228
    }
229
230
    public function testFilterHavingWithOperatorFormat(): void
231
    {
232
        $db = $this->getConnection();
233
234
        $query = new Query($db);
235
        $condition = ['like', 'name', 'Alex'];
236
        $query->filterHaving($condition);
237
238
        $this->assertSame($condition, $query->getHaving());
239
240
        $query->andFilterHaving(['between', 'id', null, null]);
241
242
        $this->assertSame($condition, $query->getHaving());
243
244
        $query->orFilterHaving(['not between', 'id', null, null]);
245
246
        $this->assertSame($condition, $query->getHaving());
247
248
        $query->andFilterHaving(['in', 'id', []]);
249
250
        $this->assertSame($condition, $query->getHaving());
251
252
        $query->andFilterHaving(['not in', 'id', []]);
253
254
        $this->assertSame($condition, $query->getHaving());
255
256
        $query->andFilterHaving(['like', 'id', '']);
257
258
        $this->assertSame($condition, $query->getHaving());
259
260
        $query->andFilterHaving(['or like', 'id', '']);
261
262
        $this->assertSame($condition, $query->getHaving());
263
264
        $query->andFilterHaving(['not like', 'id', '   ']);
265
266
        $this->assertSame($condition, $query->getHaving());
267
268
        $query->andFilterHaving(['or not like', 'id', null]);
269
270
        $this->assertSame($condition, $query->getHaving());
271
272
        $query->andFilterHaving(['or', ['eq', 'id', null], ['eq', 'id', []]]);
273
274
        $this->assertSame($condition, $query->getHaving());
275
    }
276
277
    public function testFilterRecursively(): void
278
    {
279
        $db = $this->getConnection();
280
281
        $query = new Query($db);
282
        $query->filterWhere(
283
            [
284
                'and',
285
                ['like', 'name', ''],
286
                ['like', 'title', ''],
287
                ['id' => 1],
288
                ['not', ['like', 'name', '']],
289
            ],
290
        );
291
292
        $this->assertSame(['and', ['id' => 1]], $query->getWhere());
293
    }
294
295
    public function testFilterWhereWithHashFormat(): void
296
    {
297
        $db = $this->getConnection();
298
299
        $query = new Query($db);
300
        $query->filterWhere(['id' => 0, 'title' => '   ', 'author_ids' => []]);
301
302
        $this->assertSame(['id' => 0], $query->getWhere());
303
304
        $query->andFilterWhere(['status' => null]);
305
306
        $this->assertSame(['id' => 0], $query->getWhere());
307
308
        $query->orFilterWhere(['name' => '']);
309
310
        $this->assertSame(['id' => 0], $query->getWhere());
311
    }
312
313
    public function testFilterWhereWithOperatorFormat(): void
314
    {
315
        $db = $this->getConnection();
316
317
        $query = new Query($db);
318
        $condition = ['like', 'name', 'Alex'];
319
        $query->filterWhere($condition);
320
321
        $this->assertSame($condition, $query->getWhere());
322
323
        $query->andFilterWhere(['between', 'id', null, null]);
324
325
        $this->assertSame($condition, $query->getWhere());
326
327
        $query->orFilterWhere(['not between', 'id', null, null]);
328
329
        $this->assertSame($condition, $query->getWhere());
330
331
        $query->andFilterWhere(['in', 'id', []]);
332
333
        $this->assertSame($condition, $query->getWhere());
334
335
        $query->andFilterWhere(['not in', 'id', []]);
336
337
        $this->assertSame($condition, $query->getWhere());
338
339
        $query->andFilterWhere(['like', 'id', '']);
340
341
        $this->assertSame($condition, $query->getWhere());
342
343
        $query->andFilterWhere(['or like', 'id', '']);
344
345
        $this->assertSame($condition, $query->getWhere());
346
347
        $query->andFilterWhere(['not like', 'id', '   ']);
348
349
        $this->assertSame($condition, $query->getWhere());
350
351
        $query->andFilterWhere(['or not like', 'id', null]);
352
353
        $this->assertSame($condition, $query->getWhere());
354
355
        $query->andFilterWhere(['or', ['eq', 'id', null], ['eq', 'id', []]]);
356
357
        $this->assertSame($condition, $query->getWhere());
358
    }
359
360
    public function testFrom(): void
361
    {
362
        $db = $this->getConnection();
363
364
        $query = new Query($db);
365
        $query->from('user');
366
367
        $this->assertSame(['user'], $query->getFrom());
368
    }
369
370
    public function testFromTableIsArrayWithExpression(): void
371
    {
372
        $db = $this->getConnection();
373
374
        $query = new Query($db);
375
        $tables = new Expression('(SELECT id,name FROM user) u');
376
377
        $query->from($tables);
378
        $from = $query->getFrom();
379
380
        $this->assertIsArray($from);
381
        $this->assertInstanceOf(Expression::class, $from[0]);
382
    }
383
384
    public function testGroup(): void
385
    {
386
        $db = $this->getConnection();
387
388
        $query = new Query($db);
389
        $query->groupBy('team');
390
391
        $this->assertSame(['team'], $query->getGroupBy());
392
393
        $query->addGroupBy('company');
394
395
        $this->assertSame(['team', 'company'], $query->getGroupBy());
396
397
        $query->addGroupBy('age');
398
399
        $this->assertSame(['team', 'company', 'age'], $query->getGroupBy());
400
    }
401
402
    public function testHaving(): void
403
    {
404
        $db = $this->getConnection();
405
406
        $query = new Query($db);
407
        $query->having('id = :id', [':id' => 1]);
408
409
        $this->assertSame('id = :id', $query->getHaving());
410
        $this->assertSame([':id' => 1], $query->getParams());
411
412
        $query->andHaving('name = :name', [':name' => 'something']);
413
414
        $this->assertSame(['and', 'id = :id', 'name = :name'], $query->getHaving());
415
        $this->assertSame([':id' => 1, ':name' => 'something'], $query->getParams());
416
417
        $query->orHaving('age = :age', [':age' => '30']);
418
419
        $this->assertSame(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getHaving());
420
        $this->assertSame([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams());
421
    }
422
423
    public function testLimitOffset(): void
424
    {
425
        $db = $this->getConnection();
426
427
        $query = new Query($db);
428
        $query->limit(10)->offset(5);
429
430
        $this->assertSame(10, $query->getLimit());
431
        $this->assertSame(5, $query->getOffset());
432
    }
433
434
    public function testLimitOffsetWithExpression(): void
435
    {
436
        $db = $this->getConnection();
437
438
        $query = (new Query($db))->from('customer')->select('id')->orderBy('id');
439
        $query->limit(new Expression('1 + 1'))->offset(new Expression('1 + 0'));
440
        $result = $query->column();
441
442
        $this->assertCount(2, $result);
443
        $this->assertContains('2', $result);
444
        $this->assertContains('3', $result);
445
        $this->assertNotContains('1', $result);
446
    }
447
448
    /**
449
     * {@see https://github.com/yiisoft/yii2/issues/13745}
450
     */
451
    public function testMultipleLikeConditions(): void
452
    {
453
        $tableName = 'like_test';
454
        $columnName = 'col';
455
456
        $db = $this->getConnection();
457
458
        if ($db->getSchema()->getTableSchema($tableName) !== null) {
459
            $db->createCommand()->dropTable($tableName)->execute();
460
        }
461
462
        $db->createCommand()->createTable($tableName, [
463
            $columnName => $db->getSchema()->createColumnSchemaBuilder(Schema::TYPE_STRING, 64),
464
        ])->execute();
465
466
        $db->createCommand()->batchInsert(
467
            $tableName,
468
            ['col'],
469
            [
470
                ['test0'],
471
                ['test\1'],
472
                ['test\2'],
473
                ['foo%'],
474
                ['%bar'],
475
                ['%baz%'],
476
            ]
477
        )->execute();
478
479
        /* Basic tests */
480
        $this->assertSame(1, $this->countLikeQuery($db, $tableName, $columnName, ['test0']));
481
        $this->assertSame(2, $this->countLikeQuery($db, $tableName, $columnName, ['test\\']));
482
        $this->assertSame(0, $this->countLikeQuery($db, $tableName, $columnName, ['test%']));
483
        $this->assertSame(3, $this->countLikeQuery($db, $tableName, $columnName, ['%']));
484
485
        /* Multiple condition tests */
486
        $this->assertSame(
487
            2,
488
            $this->countLikeQuery($db, $tableName, $columnName, ['test0', 'test\1']),
489
        );
490
        $this->assertSame(
491
            3,
492
            $this->countLikeQuery($db, $tableName, $columnName, ['test0', 'test\1', 'test\2']),
493
        );
494
        $this->assertSame(
495
            3,
496
            $this->countLikeQuery($db, $tableName, $columnName, ['foo', '%ba']),
497
        );
498
    }
499
500
    public function testOne(): void
501
    {
502
        $db = $this->getConnection();
503
504
        $result = (new Query($db))->from('customer')->where(['status' => 2])->one();
505
506
        $this->assertSame('user3', $result['name']);
507
508
        $result = (new Query($db))->from('customer')->where(['status' => 3])->one();
509
510
        $this->assertNull($result);
511
    }
512
513
    public function testOrder(): void
514
    {
515
        $db = $this->getConnection();
516
517
        $query = new Query($db);
518
        $query->orderBy('team');
519
520
        $this->assertSame(['team' => SORT_ASC], $query->getOrderBy());
521
522
        $query->addOrderBy('company');
523
524
        $this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC], $query->getOrderBy());
525
526
        $query->addOrderBy('age');
527
528
        $this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->getOrderBy());
529
530
        $query->addOrderBy(['age' => SORT_DESC]);
531
532
        $this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->getOrderBy());
533
534
        $query->addOrderBy('age ASC, company DESC');
535
536
        $this->assertSame(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->getOrderBy());
537
538
        $expression = new Expression('SUBSTR(name, 3, 4) DESC, x ASC');
539
        $query->orderBy($expression);
540
541
        $this->assertEquals([$expression], $query->getOrderBy());
542
543
        $expression = new Expression('SUBSTR(name, 3, 4) DESC, x ASC');
544
        $query->addOrderBy($expression);
545
546
        $this->assertEquals([$expression, $expression], $query->getOrderBy());
547
    }
548
549
    public function testSelect(): void
550
    {
551
        $db = $this->getConnection();
552
553
        /* default */
554
        $query = new Query($db);
555
        $query->select('*');
556
557
        $this->assertSame(['*' => '*'], $query->getSelect());
558
        $this->assertNull($query->getDistinct());
559
        $this->assertNull($query->getSelectOption());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $query->getSelectOption() targeting Yiisoft\Db\Query\Query::getSelectOption() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
560
561
        $query = new Query($db);
562
        $query->select('id, name', 'something')->distinct(true);
563
564
        $this->assertSame(['id' => 'id', 'name' => 'name'], $query->getSelect());
565
        $this->assertTrue($query->getDistinct());
566
        $this->assertSame('something', $query->getSelectOption());
567
568
        $query = new Query($db);
569
        $query->addSelect('email');
570
571
        $this->assertSame(['email' => 'email'], $query->getSelect());
572
573
        $query = new Query($db);
574
        $query->select('id, name');
575
        $query->addSelect('email');
576
577
        $this->assertSame(['id' => 'id', 'name' => 'name', 'email' => 'email'], $query->getSelect());
578
579
        $query = new Query($db);
580
        $query->select('name, lastname');
581
        $query->addSelect('name');
582
583
        $this->assertSame(['name' => 'name', 'lastname' => 'lastname'], $query->getSelect());
584
585
        $query = new Query($db);
586
        $query->addSelect(['*', 'abc']);
587
        $query->addSelect(['*', 'bca']);
588
589
        $this->assertSame(['*' => '*', 'abc' => 'abc', 'bca' => 'bca'], $query->getSelect());
590
591
        $query = new Query($db);
592
        $query->addSelect(['field1 as a', 'field 1 as b']);
593
594
        $this->assertSame(['a' => 'field1', 'b' => 'field 1'], $query->getSelect());
595
596
        $query = new Query($db);
597
        $query->addSelect(['field1 a', 'field 1 b']);
598
599
        $this->assertSame(['a' => 'field1', 'b' => 'field 1'], $query->getSelect());
600
601
        $query = new Query($db);
602
        $query->select(['name' => 'firstname', 'lastname']);
603
        $query->addSelect(['firstname', 'surname' => 'lastname']);
604
        $query->addSelect(['firstname', 'lastname']);
605
606
        $this->assertSame(
607
            ['name' => 'firstname', 'lastname' => 'lastname', 'firstname' => 'firstname', 'surname' => 'lastname'],
608
            $query->getSelect()
609
        );
610
611
        $query = new Query($db);
612
        $query->select('name, name, name as X, name as X');
613
614
        $this->assertSame(['name' => 'name', 'X' => 'name'], $query->getSelect());
615
616
        /**
617
         * {@see https://github.com/yiisoft/yii2/issues/15676}
618
         */
619
        $query = (new Query($db))->select('id');
620
621
        $this->assertSame(['id' => 'id'], $query->getSelect());
622
623
        $query->select(['id', 'brand_id']);
624
625
        $this->assertSame(['id' => 'id', 'brand_id' => 'brand_id'], $query->getSelect());
626
627
        /**
628
         * {@see https://github.com/yiisoft/yii2/issues/15676}
629
         */
630
        $query = (new Query($db))->select(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)']);
631
632
        $this->assertSame(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)'], $query->getSelect());
633
634
        $query->addSelect(['LEFT(name,7) as test']);
635
636
        $this->assertSame(
637
            ['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'],
638
            $query->getSelect()
639
        );
640
641
        $query->addSelect(['LEFT(name,7) as test']);
642
643
        $this->assertSame(
644
            ['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'],
645
            $query->getSelect()
646
        );
647
648
        $query->addSelect(['test' => 'LEFT(name,7)']);
649
650
        $this->assertSame(
651
            ['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'],
652
            $query->getSelect()
653
        );
654
655
        /**
656
         * {@see https://github.com/yiisoft/yii2/issues/15731}
657
         */
658
        $selectedCols = [
659
            'total_sum' => 'SUM(f.amount)',
660
            'in_sum' => 'SUM(IF(f.type = :type_in, f.amount, 0))',
661
            'out_sum' => 'SUM(IF(f.type = :type_out, f.amount, 0))',
662
        ];
663
664
        $query = (new Query($db))
665
            ->select($selectedCols)
666
            ->addParams([':type_in' => 'in', ':type_out' => 'out', ':type_partner' => 'partner']);
667
668
        $this->assertSame($selectedCols, $query->getSelect());
669
670
        $query->select($selectedCols);
671
672
        $this->assertSame($selectedCols, $query->getSelect());
673
674
        /**
675
         * {@see https://github.com/yiisoft/yii2/issues/17384}
676
         */
677
        $query = new Query($db);
678
679
        $query->select('DISTINCT ON(tour_dates.date_from) tour_dates.date_from, tour_dates.id');
680
681
        $this->assertSame(
682
            ['DISTINCT ON(tour_dates.date_from) tour_dates.date_from', 'tour_dates.id' => 'tour_dates.id'],
683
            $query->getSelect()
684
        );
685
    }
686
687
    public function testWhere(): void
688
    {
689
        $db = $this->getConnection();
690
691
        $query = new Query($db);
692
        $query->where('id = :id', [':id' => 1]);
693
694
        $this->assertSame('id = :id', $query->getWhere());
695
        $this->assertSame([':id' => 1], $query->getParams());
696
697
        $query->andWhere('name = :name', [':name' => 'something']);
698
699
        $this->assertSame(['and', 'id = :id', 'name = :name'], $query->getWhere());
700
        $this->assertSame([':id' => 1, ':name' => 'something'], $query->getParams());
701
702
        $query->orWhere('age = :age', [':age' => '30']);
703
704
        $this->assertSame(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getWhere());
705
        $this->assertSame([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams());
706
    }
707
708
    /**
709
     * @throws Exception
710
     * @throws InvalidArgumentException
711
     * @throws InvalidConfigException
712
     * @throws NotSupportedException
713
     */
714
    protected function countLikeQuery(
715
        ConnectionInterface $db,
716
        string $tableName,
717
        string $columnName,
718
        array $condition,
719
        string $operator = 'or'
720
    ): int {
721
        $whereCondition = [$operator];
722
723
        foreach ($condition as $value) {
724
            $whereCondition[] = ['like', $columnName, $value];
725
        }
726
727
        $result = (new Query($db))->from($tableName)->where($whereCondition)->count('*');
728
729
        if (is_numeric($result)) {
730
            return (int) $result;
731
        }
732
733
        return 0;
734
    }
735
}
736