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