|
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\Query\Query; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractQueryCacheTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function testQueryCache() |
|
14
|
|
|
{ |
|
15
|
|
|
$db = $this->getConnection(); |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
$query = (new Query($db))->select(['name'])->from('customer'); |
|
18
|
|
|
$update = $db->createCommand('UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id'); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertEquals('user1', $query->where(['id' => 1])->scalar(), 'Asserting initial value'); |
|
21
|
|
|
|
|
22
|
|
|
/* No cache */ |
|
23
|
|
|
$update->bindValues([':id' => 1, ':name' => 'user11'])->execute(); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertEquals( |
|
26
|
|
|
'user11', |
|
27
|
|
|
$query->where(['id' => 1])->scalar(), |
|
28
|
|
|
'Query reflects DB changes when caching is disabled' |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
/* Connection cache */ |
|
32
|
|
|
$db->cache(function (ConnectionInterface $db) use ($query, $update) { |
|
33
|
|
|
$this->assertEquals( |
|
34
|
|
|
'user2', |
|
35
|
|
|
$query->where(['id' => 2])->scalar(), |
|
36
|
|
|
'Asserting initial value for user #2' |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
$update->bindValues([':id' => 2, ':name' => 'user22'])->execute(); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertEquals( |
|
42
|
|
|
'user2', |
|
43
|
|
|
$query->where(['id' => 2])->scalar(), |
|
44
|
|
|
'Query does NOT reflect DB changes when wrapped in connection caching' |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$db->noCache(function () use ($query) { |
|
48
|
|
|
$this->assertEquals( |
|
49
|
|
|
'user22', |
|
50
|
|
|
$query->where(['id' => 2])->scalar(), |
|
51
|
|
|
'Query reflects DB changes when wrapped in connection caching and noCache simultaneously' |
|
52
|
|
|
); |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals( |
|
56
|
|
|
'user2', |
|
57
|
|
|
$query->where(['id' => 2])->scalar(), |
|
58
|
|
|
'Cache does not get changes after getting newer data from DB in noCache block.' |
|
59
|
|
|
); |
|
60
|
|
|
}, 10); |
|
61
|
|
|
|
|
62
|
|
|
$db->queryCacheEnable(false); |
|
63
|
|
|
|
|
64
|
|
|
$db->cache(function () use ($query, $update) { |
|
65
|
|
|
$this->assertEquals( |
|
66
|
|
|
'user22', |
|
67
|
|
|
$query->where(['id' => 2])->scalar(), |
|
68
|
|
|
'When cache is disabled for the whole connection, Query inside cache block does not get cached' |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$update->bindValues([':id' => 2, ':name' => 'user2'])->execute(); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertEquals('user2', $query->where(['id' => 2])->scalar()); |
|
74
|
|
|
}, 10); |
|
75
|
|
|
|
|
76
|
|
|
$db->queryCacheEnable(true); |
|
77
|
|
|
$query->cache(); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertEquals('user11', $query->where(['id' => 1])->scalar()); |
|
80
|
|
|
|
|
81
|
|
|
$update->bindValues([':id' => 1, ':name' => 'user1'])->execute(); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals( |
|
84
|
|
|
'user11', |
|
85
|
|
|
$query->where(['id' => 1])->scalar(), |
|
86
|
|
|
'When both Connection and Query have cache enabled, we get cached value' |
|
87
|
|
|
); |
|
88
|
|
|
$this->assertEquals( |
|
89
|
|
|
'user1', |
|
90
|
|
|
$query->noCache()->where(['id' => 1])->scalar(), |
|
91
|
|
|
'When Query has disabled cache, we get actual data' |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$db->cache(function () use ($query) { |
|
95
|
|
|
$this->assertEquals('user1', $query->noCache()->where(['id' => 1])->scalar()); |
|
96
|
|
|
$this->assertEquals('user11', $query->cache()->where(['id' => 1])->scalar()); |
|
97
|
|
|
}, 10); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
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.