Passed
Pull Request — master (#380)
by Wilmer
13:12
created

CommonQueryCacheTest::testQueryCacheWithQuery()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 86
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 55
nc 1
nop 0
dl 0
loc 86
rs 8.9818
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Common;
6
7
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
8
use Yiisoft\Db\Tests\AbstractQueryCacheTest;
9
use Yiisoft\Db\Tests\Support\TestTrait;
10
11
/**
12
 * @group mssql
13
 * @group mysql
14
 * @group pgsql
15
 * @group oracle
16
 * @group sqlite
17
 */
18
abstract class CommonQueryCacheTest extends AbstractQueryCacheTest
19
{
20
    use TestTrait;
21
22
    public function testCommand(): void
23
    {
24
        $db = $this->getConnectionWithData();
25
26
        $db->queryCacheEnable(true);
27
        $command = $db->createCommand(
28
            <<<SQL
29
            SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id
30
            SQL,
31
        );
32
33
        $this->assertSame('user1', $command->bindValue(':id', 1)->queryScalar());
34
35
        $update = $db->createCommand(
36
            <<<SQL
37
            UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id
38
            SQL,
39
        );
40
        $update->bindValues([':id' => 1, ':name' => 'user11'])->execute();
41
42
        $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar());
43
44
        $db->cache(function (ConnectionPDOInterface $db) use ($command, $update) {
45
            $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar());
46
47
            $update->bindValues([':id' => 2, ':name' => 'user22'])->execute();
48
49
            $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar());
50
51
            $db->noCache(function () use ($command) {
52
                $this->assertEquals('user22', $command->bindValue(':id', 2)->queryScalar());
53
            });
54
55
            $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar());
56
        }, 10);
57
58
        $db->queryCacheEnable(false);
59
60
        $db->cache(function () use ($command, $update) {
61
            $this->assertSame('user22', $command->bindValue(':id', 2)->queryScalar());
62
63
            $update->bindValues([':id' => 2, ':name' => 'user2'])->execute();
64
65
            $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar());
66
        }, 10);
67
68
        $db->queryCacheEnable(true);
69
        $command = $db->createCommand(
70
            <<<SQL
71
            SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id
72
            SQL,
73
        )->cache();
74
75
        $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar());
76
77
        $update->bindValues([':id' => 1, ':name' => 'user1'])->execute();
78
79
        $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar());
80
        $this->assertSame('user1', $command->noCache()->bindValue(':id', 1)->queryScalar());
81
82
        $command = $db->createCommand(
83
            <<<SQL
84
            SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id
85
            SQL,
86
        );
87
88
        $db->cache(function () use ($command) {
89
            $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar());
90
            $this->assertSame('user1', $command->noCache()->bindValue(':id', 1)->queryScalar());
91
        }, 10);
92
    }
93
94
    public function testQueryCacheWithQuery()
95
    {
96
        $db = $this->getConnectionWithData();
97
98
        $db->queryCacheEnable(true);
99
        $query = $this->getQuery($db)->select(['name'])->from('customer');
100
        $update = $db->createCommand(
101
            <<<SQL
102
            UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id
103
            SQL,
104
        );
105
106
        $this->assertSame('user1', $query->where(['id' => 1])->scalar(), 'Asserting initial value');
107
108
        /* No cache */
109
        $update->bindValues([':id' => 1, ':name' => 'user11'])->execute();
110
111
        $this->assertSame(
112
            'user11',
113
            $query->where(['id' => 1])->scalar(),
114
            'Query reflects DB changes when caching is disabled'
115
        );
116
117
        /* Connection cache */
118
        $db->cache(function (ConnectionPDOInterface $db) use ($query, $update) {
119
            $this->assertSame('user2', $query->where(['id' => 2])->scalar(), 'Asserting initial value for user #2');
120
121
            $update->bindValues([':id' => 2, ':name' => 'user22'])->execute();
122
123
            $this->assertSame(
124
                'user2',
125
                $query->where(['id' => 2])->scalar(),
126
                'Query does NOT reflect DB changes when wrapped in connection caching'
127
            );
128
129
            $db->noCache(function () use ($query) {
130
                $this->assertSame(
131
                    'user22',
132
                    $query->where(['id' => 2])->scalar(),
133
                    'Query reflects DB changes when wrapped in connection caching and noCache simultaneously'
134
                );
135
            });
136
137
            $this->assertSame(
138
                'user2',
139
                $query->where(['id' => 2])->scalar(),
140
                'Cache does not get changes after getting newer data from DB in noCache block.'
141
            );
142
        }, 10);
143
144
        $db->queryCacheEnable(false);
145
146
        $db->cache(function () use ($query, $update) {
147
            $this->assertSame(
148
                'user22',
149
                $query->where(['id' => 2])->scalar(),
150
                'When cache is disabled for the whole connection, Query inside cache block does not get cached'
151
            );
152
153
            $update->bindValues([':id' => 2, ':name' => 'user2'])->execute();
154
155
            $this->assertSame('user2', $query->where(['id' => 2])->scalar());
156
        }, 10);
157
158
        $db->queryCacheEnable(true);
159
        $query->cache();
160
161
        $this->assertSame('user11', $query->where(['id' => 1])->scalar());
162
163
        $update->bindValues([':id' => 1, ':name' => 'user1'])->execute();
164
165
        $this->assertSame(
166
            'user11',
167
            $query->where(['id' => 1])->scalar(),
168
            'When both Connection and Query have cache enabled, we get cached value'
169
        );
170
        $this->assertSame(
171
            'user1',
172
            $query->noCache()->where(['id' => 1])->scalar(),
173
            'When Query has disabled cache, we get actual data'
174
        );
175
176
        $db->cache(function () use ($query) {
177
            $this->assertSame('user1', $query->noCache()->where(['id' => 1])->scalar());
178
            $this->assertSame('user11', $query->cache()->where(['id' => 1])->scalar());
179
        }, 10);
180
    }
181
}
182