Passed
Pull Request — master (#163)
by Wilmer
19:50 queued 04:50
created

TestQueryTrait::testGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestUtility;
6
7
use Yiisoft\Db\Connection\Connection;
8
use Yiisoft\Db\Exception\Exception;
9
use Yiisoft\Db\Exception\InvalidArgumentException;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Exception\NotSupportedException;
12
use Yiisoft\Db\Expression\Expression;
13
use Yiisoft\Db\Query\Query;
14
use Yiisoft\Db\Schema\Schema;
15
16
trait TestQueryTrait
17
{
18
    use GetTablesAliasTestTrait;
19
20
    public function testSelect(): void
21
    {
22
        $db = $this->getConnection();
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

22
        /** @scrutinizer ignore-call */ 
23
        $db = $this->getConnection();
Loading history...
23
24
        /* default */
25
        $query = new Query($db);
26
27
        $query->select('*');
28
29
        $this->assertEquals(['*' => '*'], $query->getSelect());
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

29
        $this->/** @scrutinizer ignore-call */ 
30
               assertEquals(['*' => '*'], $query->getSelect());
Loading history...
30
        $this->assertNull($query->getDistinct());
0 ignored issues
show
Bug introduced by
It seems like assertNull() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

30
        $this->/** @scrutinizer ignore-call */ 
31
               assertNull($query->getDistinct());
Loading history...
31
        $this->assertNull($query->getSelectOption());
32
33
        $query = new Query($db);
34
35
        $query->select('id, name', 'something')->distinct(true);
36
37
        $this->assertEquals(['id' => 'id', 'name' => 'name'], $query->getSelect());
38
        $this->assertTrue($query->getDistinct());
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
        $this->/** @scrutinizer ignore-call */ 
39
               assertTrue($query->getDistinct());
Loading history...
39
        $this->assertEquals('something', $query->getSelectOption());
40
41
        $query = new Query($db);
42
43
        $query->addSelect('email');
44
45
        $this->assertEquals(['email' => 'email'], $query->getSelect());
46
47
        $query = new Query($db);
48
49
        $query->select('id, name');
50
        $query->addSelect('email');
51
52
        $this->assertEquals(['id' => 'id', 'name' => 'name', 'email' => 'email'], $query->getSelect());
53
54
        $query = new Query($db);
55
56
        $query->select('name, lastname');
57
        $query->addSelect('name');
58
59
        $this->assertEquals(['name' => 'name', 'lastname' => 'lastname'], $query->getSelect());
60
61
        $query = new Query($db);
62
63
        $query->addSelect(['*', 'abc']);
64
        $query->addSelect(['*', 'bca']);
65
66
        $this->assertEquals(['*' => '*', 'abc' => 'abc', 'bca' => 'bca'], $query->getSelect());
67
68
        $query = new Query($db);
69
70
        $query->addSelect(['field1 as a', 'field 1 as b']);
71
72
        $this->assertEquals(['a' => 'field1', 'b' => 'field 1'], $query->getSelect());
73
74
        $query = new Query($db);
75
76
        $query->addSelect(['field1 a', 'field 1 b']);
77
78
        $this->assertEquals(['a' => 'field1', 'b' => 'field 1'], $query->getSelect());
79
80
        $query = new Query($db);
81
82
        $query->select(['name' => 'firstname', 'lastname']);
83
        $query->addSelect(['firstname', 'surname' => 'lastname']);
84
        $query->addSelect(['firstname', 'lastname']);
85
86
        $this->assertEquals(
87
            ['name' => 'firstname', 'lastname' => 'lastname', 'firstname' => 'firstname', 'surname' => 'lastname'],
88
            $query->getSelect()
89
        );
90
91
        $query = new Query($db);
92
93
        $query->select('name, name, name as X, name as X');
94
95
        $this->assertEquals(['name' => 'name', 'X' => 'name'], $query->getSelect());
96
97
        /**
98
         * {@see https://github.com/yiisoft/yii2/issues/15676}
99
         */
100
        $query = (new Query($db))->select('id');
101
102
        $this->assertSame(['id' => 'id'], $query->getSelect());
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

102
        $this->/** @scrutinizer ignore-call */ 
103
               assertSame(['id' => 'id'], $query->getSelect());
Loading history...
103
104
        $query->select(['id', 'brand_id']);
105
106
        $this->assertSame(['id' => 'id', 'brand_id' => 'brand_id'], $query->getSelect());
107
108
        /**
109
         * {@see https://github.com/yiisoft/yii2/issues/15676}
110
         */
111
        $query = (new Query($db))
112
            ->select(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)']);
113
114
        $this->assertSame(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)'], $query->getSelect());
115
116
        $query->addSelect(['LEFT(name,7) as test']);
117
118
        $this->assertSame(
119
            ['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'],
120
            $query->getSelect()
121
        );
122
123
        $query->addSelect(['LEFT(name,7) as test']);
124
125
        $this->assertSame(
126
            ['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'],
127
            $query->getSelect()
128
        );
129
130
        $query->addSelect(['test' => 'LEFT(name,7)']);
131
132
        $this->assertSame(
133
            ['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'],
134
            $query->getSelect()
135
        );
136
137
        /**
138
         * {@see https://github.com/yiisoft/yii2/issues/15731}
139
         */
140
        $selectedCols = [
141
            'total_sum' => 'SUM(f.amount)',
142
            'in_sum'    => 'SUM(IF(f.type = :type_in, f.amount, 0))',
143
            'out_sum'   => 'SUM(IF(f.type = :type_out, f.amount, 0))',
144
        ];
145
146
        $query = (new Query($db))->select($selectedCols)->addParams([
147
            ':type_in'      => 'in',
148
            ':type_out'     => 'out',
149
            ':type_partner' => 'partner',
150
        ]);
151
152
        $this->assertSame($selectedCols, $query->getSelect());
153
154
        $query->select($selectedCols);
155
156
        $this->assertSame($selectedCols, $query->getSelect());
157
158
        /**
159
         * {@see https://github.com/yiisoft/yii2/issues/17384}
160
         */
161
        $query = new Query($db);
162
163
        $query->select('DISTINCT ON(tour_dates.date_from) tour_dates.date_from, tour_dates.id');
164
165
        $this->assertEquals(
166
            ['DISTINCT ON(tour_dates.date_from) tour_dates.date_from', 'tour_dates.id' => 'tour_dates.id'],
167
            $query->getSelect()
168
        );
169
    }
170
171
    public function testFrom(): void
172
    {
173
        $db = $this->getConnection();
174
175
        $query = new Query($db);
176
177
        $query->from('user');
178
179
        $this->assertEquals(['user'], $query->getFrom());
180
    }
181
182
    public function testFromTableIsArrayWithExpression(): void
183
    {
184
        $db = $this->getConnection();
185
186
        $query = new Query($db);
187
188
        $tables = new Expression('(SELECT id,name FROM user) u');
189
190
        $query->from($tables);
191
192
        $this->assertInstanceOf(Expression::class, $query->getFrom()[0]);
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

192
        $this->/** @scrutinizer ignore-call */ 
193
               assertInstanceOf(Expression::class, $query->getFrom()[0]);
Loading history...
193
    }
194
195
    protected function createQuery(): Query
196
    {
197
        return new Query($this->getConnection());
198
    }
199
200
    public function testWhere(): void
201
    {
202
        $db = $this->getConnection();
203
204
        $query = new Query($db);
205
206
        $query->where('id = :id', [':id' => 1]);
207
208
        $this->assertEquals('id = :id', $query->getWhere());
209
        $this->assertEquals([':id' => 1], $query->getParams());
210
211
        $query->andWhere('name = :name', [':name' => 'something']);
212
213
        $this->assertEquals(['and', 'id = :id', 'name = :name'], $query->getWhere());
214
        $this->assertEquals([':id' => 1, ':name' => 'something'], $query->getParams());
215
216
        $query->orWhere('age = :age', [':age' => '30']);
217
218
        $this->assertEquals(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getWhere());
219
        $this->assertEquals([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams());
220
    }
221
222
    public function testFilterWhereWithHashFormat(): void
223
    {
224
        $db = $this->getConnection();
225
226
        $query = new Query($db);
227
228
        $query->filterWhere([
229
            'id'         => 0,
230
            'title'      => '   ',
231
            'author_ids' => [],
232
        ]);
233
234
        $this->assertEquals(['id' => 0], $query->getWhere());
235
236
        $query->andFilterWhere(['status' => null]);
237
238
        $this->assertEquals(['id' => 0], $query->getWhere());
239
240
        $query->orFilterWhere(['name' => '']);
241
242
        $this->assertEquals(['id' => 0], $query->getWhere());
243
    }
244
245
    public function testFilterWhereWithOperatorFormat(): void
246
    {
247
        $db = $this->getConnection();
248
249
        $query = new Query($db);
250
251
        $condition = ['like', 'name', 'Alex'];
252
253
        $query->filterWhere($condition);
254
255
        $this->assertEquals($condition, $query->getWhere());
256
257
        $query->andFilterWhere(['between', 'id', null, null]);
258
259
        $this->assertEquals($condition, $query->getWhere());
260
261
        $query->orFilterWhere(['not between', 'id', null, null]);
262
263
        $this->assertEquals($condition, $query->getWhere());
264
265
        $query->andFilterWhere(['in', 'id', []]);
266
267
        $this->assertEquals($condition, $query->getWhere());
268
269
        $query->andFilterWhere(['not in', 'id', []]);
270
271
        $this->assertEquals($condition, $query->getWhere());
272
273
        $query->andFilterWhere(['like', 'id', '']);
274
275
        $this->assertEquals($condition, $query->getWhere());
276
277
        $query->andFilterWhere(['or like', 'id', '']);
278
279
        $this->assertEquals($condition, $query->getWhere());
280
281
        $query->andFilterWhere(['not like', 'id', '   ']);
282
283
        $this->assertEquals($condition, $query->getWhere());
284
285
        $query->andFilterWhere(['or not like', 'id', null]);
286
287
        $this->assertEquals($condition, $query->getWhere());
288
289
        $query->andFilterWhere(['or', ['eq', 'id', null], ['eq', 'id', []]]);
290
291
        $this->assertEquals($condition, $query->getWhere());
292
    }
293
294
    public function testFilterHavingWithHashFormat(): void
295
    {
296
        $db = $this->getConnection();
297
298
        $query = new Query($db);
299
300
        $query->filterHaving([
301
            'id'         => 0,
302
            'title'      => '   ',
303
            'author_ids' => [],
304
        ]);
305
306
        $this->assertEquals(['id' => 0], $query->getHaving());
307
308
        $query->andFilterHaving(['status' => null]);
309
310
        $this->assertEquals(['id' => 0], $query->getHaving());
311
312
        $query->orFilterHaving(['name' => '']);
313
314
        $this->assertEquals(['id' => 0], $query->getHaving());
315
    }
316
317
    public function testFilterHavingWithOperatorFormat(): void
318
    {
319
        $db = $this->getConnection();
320
321
        $query = new Query($db);
322
323
        $condition = ['like', 'name', 'Alex'];
324
325
        $query->filterHaving($condition);
326
327
        $this->assertEquals($condition, $query->getHaving());
328
329
        $query->andFilterHaving(['between', 'id', null, null]);
330
331
        $this->assertEquals($condition, $query->getHaving());
332
333
        $query->orFilterHaving(['not between', 'id', null, null]);
334
335
        $this->assertEquals($condition, $query->getHaving());
336
337
        $query->andFilterHaving(['in', 'id', []]);
338
339
        $this->assertEquals($condition, $query->getHaving());
340
341
        $query->andFilterHaving(['not in', 'id', []]);
342
343
        $this->assertEquals($condition, $query->getHaving());
344
345
        $query->andFilterHaving(['like', 'id', '']);
346
347
        $this->assertEquals($condition, $query->getHaving());
348
349
        $query->andFilterHaving(['or like', 'id', '']);
350
351
        $this->assertEquals($condition, $query->getHaving());
352
353
        $query->andFilterHaving(['not like', 'id', '   ']);
354
355
        $this->assertEquals($condition, $query->getHaving());
356
357
        $query->andFilterHaving(['or not like', 'id', null]);
358
359
        $this->assertEquals($condition, $query->getHaving());
360
361
        $query->andFilterHaving(['or', ['eq', 'id', null], ['eq', 'id', []]]);
362
363
        $this->assertEquals($condition, $query->getHaving());
364
    }
365
366
    public function testFilterRecursively(): void
367
    {
368
        $db = $this->getConnection();
369
370
        $query = new Query($db);
371
372
        $query->filterWhere(
373
            ['and', ['like', 'name', ''],
374
            ['like', 'title', ''],
375
            ['id' => 1],
376
            ['not',
377
            ['like', 'name', '']]]
378
        );
379
380
        $this->assertEquals(['and', ['id' => 1]], $query->getWhere());
381
    }
382
383
    public function testGroup(): void
384
    {
385
        $db = $this->getConnection();
386
387
        $query = new Query($db);
388
389
        $query->groupBy('team');
390
391
        $this->assertEquals(['team'], $query->getGroupBy());
392
393
        $query->addGroupBy('company');
394
395
        $this->assertEquals(['team', 'company'], $query->getGroupBy());
396
397
        $query->addGroupBy('age');
398
399
        $this->assertEquals(['team', 'company', 'age'], $query->getGroupBy());
400
    }
401
402
    public function testHaving(): void
403
    {
404
        $db = $this->getConnection();
405
406
        $query = new Query($db);
407
408
        $query->having('id = :id', [':id' => 1]);
409
410
        $this->assertEquals('id = :id', $query->getHaving());
411
        $this->assertEquals([':id' => 1], $query->getParams());
412
413
        $query->andHaving('name = :name', [':name' => 'something']);
414
        $this->assertEquals(['and', 'id = :id', 'name = :name'], $query->getHaving());
415
        $this->assertEquals([':id' => 1, ':name' => 'something'], $query->getParams());
416
417
        $query->orHaving('age = :age', [':age' => '30']);
418
        $this->assertEquals(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getHaving());
419
        $this->assertEquals([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams());
420
    }
421
422
    public function testOrder(): void
423
    {
424
        $db = $this->getConnection();
425
426
        $query = new Query($db);
427
428
        $query->orderBy('team');
429
430
        $this->assertEquals(['team' => SORT_ASC], $query->getOrderBy());
431
432
        $query->addOrderBy('company');
433
434
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->getOrderBy());
435
436
        $query->addOrderBy('age');
437
438
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->getOrderBy());
439
440
        $query->addOrderBy(['age' => SORT_DESC]);
441
442
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->getOrderBy());
443
444
        $query->addOrderBy('age ASC, company DESC');
445
446
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->getOrderBy());
447
448
        $expression = new Expression('SUBSTR(name, 3, 4) DESC, x ASC');
449
450
        $query->orderBy($expression);
451
452
        $this->assertEquals([$expression], $query->getOrderBy());
453
454
        $expression = new Expression('SUBSTR(name, 3, 4) DESC, x ASC');
455
456
        $query->addOrderBy($expression);
457
458
        $this->assertEquals([$expression, $expression], $query->getOrderBy());
459
    }
460
461
    public function testLimitOffset(): void
462
    {
463
        $db = $this->getConnection();
464
465
        $query = new Query($db);
466
467
        $query->limit(10)->offset(5);
468
469
        $this->assertEquals(10, $query->getLimit());
470
        $this->assertEquals(5, $query->getOffset());
471
    }
472
473
    public function testLimitOffsetWithExpression(): void
474
    {
475
        $db = $this->getConnection();
476
477
        $query = (new Query($db))->from('customer')->select('id')->orderBy('id');
478
479
        $query
480
            ->limit(new Expression('1 + 1'))
481
            ->offset(new Expression('1 + 0'));
482
483
        $result = $query->column();
484
485
        $this->assertCount(2, $result);
0 ignored issues
show
Bug introduced by
It seems like assertCount() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

485
        $this->/** @scrutinizer ignore-call */ 
486
               assertCount(2, $result);
Loading history...
486
        if ($db->getDriverName() !== 'sqlsrv') {
487
            $this->assertContains(2, $result);
0 ignored issues
show
Bug introduced by
It seems like assertContains() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

487
            $this->/** @scrutinizer ignore-call */ 
488
                   assertContains(2, $result);
Loading history...
488
            $this->assertContains(3, $result);
489
        } else {
490
            $this->assertContains("2", $result);
491
            $this->assertContains("3", $result);
492
        }
493
        $this->assertNotContains(1, $result);
0 ignored issues
show
Bug introduced by
It seems like assertNotContains() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

493
        $this->/** @scrutinizer ignore-call */ 
494
               assertNotContains(1, $result);
Loading history...
494
    }
495
496
    public function testOne(): void
497
    {
498
        $db = $this->getConnection();
499
500
        $result = (new Query($db))->from('customer')->where(['status' => 2])->one();
501
502
        $this->assertEquals('user3', $result['name']);
503
504
        $result = (new Query($db))->from('customer')->where(['status' => 3])->one();
505
506
        $this->assertFalse($result);
0 ignored issues
show
Bug introduced by
It seems like assertFalse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

506
        $this->/** @scrutinizer ignore-call */ 
507
               assertFalse($result);
Loading history...
507
    }
508
509
    public function testExists(): void
510
    {
511
        $db = $this->getConnection();
512
513
        $result = (new Query($db))->from('customer')->where(['status' => 2])->exists();
514
515
        $this->assertTrue($result);
516
517
        $result = (new Query($db))->from('customer')->where(['status' => 3])->exists();
518
519
        $this->assertFalse($result);
520
    }
521
522
    public function testColumn(): void
523
    {
524
        $db = $this->getConnection();
525
526
        $result = (new Query($db))
527
            ->select('name')
528
            ->from('customer')
529
            ->orderBy(['id' => SORT_DESC])
530
            ->column();
531
532
        $this->assertEquals(['user3', 'user2', 'user1'], $result);
533
534
        /**
535
         * {@see https://github.com/yiisoft/yii2/issues/7515}
536
         */
537
        $result = (new Query($db))->from('customer')
538
            ->select('name')
539
            ->orderBy(['id' => SORT_DESC])
540
            ->indexBy('id')
541
            ->column();
542
543
        $this->assertEquals([3 => 'user3', 2 => 'user2', 1 => 'user1'], $result);
544
545
        /**
546
         * {@see https://github.com/yiisoft/yii2/issues/12649}
547
         */
548
        $result = (new Query($db))->from('customer')
549
            ->select(['name', 'id'])
550
            ->orderBy(['id' => SORT_DESC])
551
            ->indexBy(function ($row) {
552
                return $row['id'] * 2;
553
            })
554
            ->column();
555
556
        $this->assertEquals([6 => 'user3', 4 => 'user2', 2 => 'user1'], $result);
557
558
        $result = (new Query($db))->from('customer')
559
            ->select(['name'])
560
            ->indexBy('name')
561
            ->orderBy(['id' => SORT_DESC])
562
            ->column($db);
563
564
        $this->assertEquals(['user3' => 'user3', 'user2' => 'user2', 'user1' => 'user1'], $result);
565
    }
566
567
    public function testCount(): void
568
    {
569
        $db = $this->getConnection();
570
571
        $count = (new Query($db))->from('customer')->count('*');
572
573
        $this->assertEquals(3, $count);
574
575
        $count = (new Query($db))->from('customer')->where(['status' => 2])->count('*');
576
577
        $this->assertEquals(1, $count);
578
579
        $count = (new Query($db))
580
            ->select('[[status]], COUNT([[id]]) cnt')
581
            ->from('customer')
582
            ->groupBy('status')
583
            ->count('*');
584
585
        $this->assertEquals(2, $count);
586
587
        /* testing that orderBy() should be ignored here as it does not affect the count anyway. */
588
        $count = (new Query($db))->from('customer')->orderBy('status')->count('*');
589
590
        $this->assertEquals(3, $count);
591
592
        $count = (new Query($db))->from('customer')->orderBy('id')->limit(1)->count('*');
593
594
        $this->assertEquals(3, $count);
595
    }
596
597
    /**
598
     * @depends testFilterWhereWithHashFormat
599
     * @depends testFilterWhereWithOperatorFormat
600
     */
601
    public function testAndFilterCompare(): void
602
    {
603
        $db = $this->getConnection();
604
605
        $query = new Query($db);
606
607
        $result = $query->andFilterCompare('name', null);
608
609
        $this->assertInstanceOf(Query::class, $result);
610
        $this->assertNull($query->getWhere());
611
612
        $query->andFilterCompare('name', '');
613
614
        $this->assertNull($query->getWhere());
615
616
        $query->andFilterCompare('name', 'John Doe');
617
        $condition = ['=', 'name', 'John Doe'];
618
619
        $this->assertEquals($condition, $query->getWhere());
620
621
        $condition = ['and', $condition, ['like', 'name', 'Doe']];
622
        $query->andFilterCompare('name', 'Doe', 'like');
623
624
        $this->assertEquals($condition, $query->getWhere());
625
626
        $condition[] = ['>', 'rating', '9'];
627
        $query->andFilterCompare('rating', '>9');
628
629
        $this->assertEquals($condition, $query->getWhere());
630
631
        $condition[] = ['<=', 'value', '100'];
632
        $query->andFilterCompare('value', '<=100');
633
634
        $this->assertEquals($condition, $query->getWhere());
635
    }
636
637
    public function testEmulateExecution(): void
638
    {
639
        $db = $this->getConnection();
640
641
        $this->assertGreaterThan(0, (new Query($db))->from('customer')->count('*'));
0 ignored issues
show
Bug introduced by
It seems like assertGreaterThan() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

641
        $this->/** @scrutinizer ignore-call */ 
642
               assertGreaterThan(0, (new Query($db))->from('customer')->count('*'));
Loading history...
642
643
        $rows = (new Query($db))->from('customer')->emulateExecution()->all();
644
645
        $this->assertSame([], $rows);
646
647
        $row = (new Query($db))->from('customer')->emulateExecution()->one();
648
649
        $this->assertFalse($row);
650
651
        $exists = (new Query($db))->from('customer')->emulateExecution()->exists($db);
652
653
        $this->assertFalse($exists);
654
655
        $count = (new Query($db))->from('customer')->emulateExecution()->count('*');
656
657
        $this->assertSame(0, $count);
658
659
        $sum = (new Query($db))->from('customer')->emulateExecution()->sum('id');
660
661
        $this->assertSame(0, $sum);
662
663
        $sum = (new Query($db))->from('customer')->emulateExecution()->average('id');
664
665
        $this->assertSame(0, $sum);
666
667
        $max = (new Query($db))->from('customer')->emulateExecution()->max('id');
668
669
        $this->assertNull($max);
670
671
        $min = (new Query($db))->from('customer')->emulateExecution()->min('id');
672
673
        $this->assertNull($min);
674
675
        $scalar = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->scalar();
676
677
        $this->assertNull($scalar);
678
679
        $column = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->column();
680
681
        $this->assertSame([], $column);
682
    }
683
684
    /**
685
     * @param Connection $db
686
     * @param string $tableName
687
     * @param string $columnName
688
     * @param array $condition
689
     * @param string $operator
690
     *
691
     * @throws Exception
692
     * @throws InvalidArgumentException
693
     * @throws InvalidConfigException
694
     * @throws NotSupportedException
695
     *
696
     * @return int
697
     */
698
    protected function countLikeQuery(
699
        Connection $db,
0 ignored issues
show
Unused Code introduced by
The parameter $db is not used and could be removed. ( Ignorable by Annotation )

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

699
        /** @scrutinizer ignore-unused */ Connection $db,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
700
        string $tableName,
701
        string $columnName,
702
        array $condition,
703
        string $operator = 'or'
704
    ): int {
705
        $db = $this->getConnection();
706
707
        $whereCondition = [$operator];
708
709
        foreach ($condition as $value) {
710
            $whereCondition[] = ['like', $columnName, $value];
711
        }
712
713
        $result = (new Query($db))->from($tableName)->where($whereCondition)->count('*');
714
715
        if (is_numeric($result)) {
716
            $result = (int) $result;
717
        }
718
719
        return $result;
720
    }
721
722
    /**
723
     * {@see https://github.com/yiisoft/yii2/issues/13745}
724
     */
725
    public function testMultipleLikeConditions(): void
726
    {
727
        $db = $this->getConnection();
728
729
        $tableName = 'like_test';
730
        $columnName = 'col';
731
732
        if ($db->getSchema()->getTableSchema($tableName) !== null) {
733
            $db->createCommand()->dropTable($tableName)->execute();
734
        }
735
736
        $db->createCommand()->createTable($tableName, [
737
            $columnName => $db->getSchema()->createColumnSchemaBuilder(Schema::TYPE_STRING, 64),
738
        ])->execute();
739
740
        $db->createCommand()->batchInsert($tableName, ['col'], [
741
            ['test0'],
742
            ['test\1'],
743
            ['test\2'],
744
            ['foo%'],
745
            ['%bar'],
746
            ['%baz%'],
747
        ])->execute();
748
749
        /* Basic tests */
750
        $this->assertSame(1, $this->countLikeQuery($db, $tableName, $columnName, ['test0']));
751
        $this->assertSame(2, $this->countLikeQuery($db, $tableName, $columnName, ['test\\']));
752
        $this->assertSame(0, $this->countLikeQuery($db, $tableName, $columnName, ['test%']));
753
        $this->assertSame(3, $this->countLikeQuery($db, $tableName, $columnName, ['%']));
754
755
        /* Multiple condition tests */
756
        $this->assertSame(2, $this->countLikeQuery($db, $tableName, $columnName, [
757
            'test0',
758
            'test\1',
759
        ]));
760
        $this->assertSame(3, $this->countLikeQuery($db, $tableName, $columnName, [
761
            'test0',
762
            'test\1',
763
            'test\2',
764
        ]));
765
        $this->assertSame(3, $this->countLikeQuery($db, $tableName, $columnName, [
766
            'foo',
767
            '%ba',
768
        ]));
769
    }
770
771
    /**
772
     * {@see https://github.com/yiisoft/yii2/issues/15355}
773
     */
774
    public function testExpressionInFrom(): void
775
    {
776
        $db = $this->getConnection();
777
778
        $query = (new Query($db))
779
            ->from(new Expression('(SELECT id, name, email, address, status FROM customer) c'))
780
            ->where(['status' => 2]);
781
782
        $result = $query->one();
783
784
        $this->assertEquals('user3', $result['name']);
785
    }
786
787
    public function testQueryCache()
788
    {
789
        $db = $this->getConnection();
790
791
        $db->setEnableQueryCache(true);
792
        $db->setQueryCache($this->cache);
793
794
        $query = (new Query($db))
795
            ->select(['name'])
796
            ->from('customer');
797
798
        $update = $db->createCommand('UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id');
799
800
        $this->assertEquals('user1', $query->where(['id' => 1])->scalar(), 'Asserting initial value');
801
802
        /* No cache */
803
        $update->bindValues([':id' => 1, ':name' => 'user11'])->execute();
804
805
        $this->assertEquals(
806
            'user11',
807
            $query->where(['id' => 1])->scalar(),
808
            'Query reflects DB changes when caching is disabled'
809
        );
810
811
        /* Connection cache */
812
        $db->cache(function (Connection $db) use ($query, $update) {
813
            $this->assertEquals(
814
                'user2',
815
                $query->where(['id' => 2])->scalar(),
816
                'Asserting initial value for user #2'
817
            );
818
819
            $update->bindValues([':id' => 2, ':name' => 'user22'])->execute();
820
821
            $this->assertEquals(
822
                'user2',
823
                $query->where(['id' => 2])->scalar(),
824
                'Query does NOT reflect DB changes when wrapped in connection caching'
825
            );
826
827
            $db->noCache(function () use ($query) {
828
                $this->assertEquals(
829
                    'user22',
830
                    $query->where(['id' => 2])->scalar(),
831
                    'Query reflects DB changes when wrapped in connection caching and noCache simultaneously'
832
                );
833
            });
834
835
            $this->assertEquals(
836
                'user2',
837
                $query->where(['id' => 2])->scalar(),
838
                'Cache does not get changes after getting newer data from DB in noCache block.'
839
            );
840
        }, 10);
841
842
        $db->setEnableQueryCache(false);
843
844
        $db->cache(function () use ($query, $update) {
845
            $this->assertEquals(
846
                'user22',
847
                $query->where(['id' => 2])->scalar(),
848
                'When cache is disabled for the whole connection, Query inside cache block does not get cached'
849
            );
850
851
            $update->bindValues([':id' => 2, ':name' => 'user2'])->execute();
852
853
            $this->assertEquals('user2', $query->where(['id' => 2])->scalar());
854
        }, 10);
855
856
        $db->setEnableQueryCache(true);
857
        $query->cache();
858
859
        $this->assertEquals('user11', $query->where(['id' => 1])->scalar());
860
861
        $update->bindValues([':id' => 1, ':name' => 'user1'])->execute();
862
863
        $this->assertEquals(
864
            'user11',
865
            $query->where(['id' => 1])->scalar(),
866
            'When both Connection and Query have cache enabled, we get cached value'
867
        );
868
        $this->assertEquals(
869
            'user1',
870
            $query->noCache()->where(['id' => 1])->scalar(),
871
            'When Query has disabled cache, we get actual data'
872
        );
873
874
        $db->cache(function () use ($query, $update) {
0 ignored issues
show
Unused Code introduced by
The import $update is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
875
            $this->assertEquals('user1', $query->noCache()->where(['id' => 1])->scalar());
876
            $this->assertEquals('user11', $query->cache()->where(['id' => 1])->scalar());
877
        }, 10);
878
    }
879
}
880