1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Cache; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile; |
6
|
|
|
use Doctrine\DBAL\ParameterType; |
7
|
|
|
use Doctrine\Tests\DbalTestCase; |
8
|
|
|
use function parse_str; |
9
|
|
|
|
10
|
|
|
class QueryCacheProfileTest extends DbalTestCase |
11
|
|
|
{ |
12
|
|
|
private const LIFETIME = 3600; |
13
|
|
|
private const CACHE_KEY = 'user_specified_cache_key'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var QueryCacheProfile |
17
|
|
|
*/ |
18
|
|
|
private $queryCacheProfile; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $query = 'SELECT * FROM foo WHERE bar = ?'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int[] |
27
|
|
|
*/ |
28
|
|
|
private $params = [666]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
private $types = [ParameterType::INTEGER]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string[] |
37
|
|
|
*/ |
38
|
|
|
private $connectionParams = [ |
39
|
|
|
'dbname' => 'database_name', |
40
|
|
|
'user' => 'database_user', |
41
|
|
|
'password' => 'database_password', |
42
|
|
|
'host' => 'database_host', |
43
|
|
|
'driver' => 'database_driver', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
protected function setUp() |
47
|
|
|
{ |
48
|
|
|
$this->queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testShouldUseTheGivenCacheKeyIfPresent() |
52
|
|
|
{ |
53
|
|
|
list($cacheKey) = $this->queryCacheProfile->generateCacheKeys( |
54
|
|
|
$this->query, |
55
|
|
|
$this->params, |
56
|
|
|
$this->types, |
57
|
|
|
$this->connectionParams |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
self::assertEquals(self::CACHE_KEY, $cacheKey, 'The returned cache key should match the given one'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven() |
64
|
|
|
{ |
65
|
|
|
$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); |
66
|
|
|
|
67
|
|
|
list($cacheKey) = $this->queryCacheProfile->generateCacheKeys( |
68
|
|
|
$this->query, |
69
|
|
|
$this->params, |
70
|
|
|
$this->types, |
71
|
|
|
$this->connectionParams |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
self::assertNotEquals( |
75
|
|
|
self::CACHE_KEY, |
76
|
|
|
$cacheKey, |
77
|
|
|
'The returned cache key should be generated automatically' |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
self::assertNotEmpty($cacheKey, 'The generated cache key should not be empty'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections() |
84
|
|
|
{ |
85
|
|
|
$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); |
86
|
|
|
|
87
|
|
|
list($firstCacheKey) = $this->queryCacheProfile->generateCacheKeys( |
88
|
|
|
$this->query, |
89
|
|
|
$this->params, |
90
|
|
|
$this->types, |
91
|
|
|
$this->connectionParams |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->connectionParams['host'] = 'a_different_host'; |
95
|
|
|
|
96
|
|
|
list($secondCacheKey) = $this->queryCacheProfile->generateCacheKeys( |
97
|
|
|
$this->query, |
98
|
|
|
$this->params, |
99
|
|
|
$this->types, |
100
|
|
|
$this->connectionParams |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
self::assertNotEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be different'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testConnectionParamsShouldBeHashed() |
107
|
|
|
{ |
108
|
|
|
$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); |
109
|
|
|
|
110
|
|
|
list($cacheKey, $queryString) = $this->queryCacheProfile->generateCacheKeys( |
111
|
|
|
$this->query, |
112
|
|
|
$this->params, |
113
|
|
|
$this->types, |
114
|
|
|
$this->connectionParams |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$params = []; |
118
|
|
|
parse_str($queryString, $params); |
119
|
|
|
|
120
|
|
|
self::assertArrayHasKey('connectionParams', $params); |
121
|
|
|
|
122
|
|
|
foreach ($this->connectionParams as $param) { |
123
|
|
|
self::assertNotContains($param, $params['connectionParams']); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges() |
128
|
|
|
{ |
129
|
|
|
$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null); |
130
|
|
|
|
131
|
|
|
list($firstCacheKey) = $this->queryCacheProfile->generateCacheKeys( |
132
|
|
|
$this->query, |
133
|
|
|
$this->params, |
134
|
|
|
$this->types, |
135
|
|
|
$this->connectionParams |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
list($secondCacheKey) = $this->queryCacheProfile->generateCacheKeys( |
139
|
|
|
$this->query, |
140
|
|
|
$this->params, |
141
|
|
|
$this->types, |
142
|
|
|
$this->connectionParams |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
self::assertEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be the same'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|