1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query; |
6
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
7
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
8
|
|
|
use Doctrine\Tests\Models\CMS\CmsArticle; |
9
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
10
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
11
|
|
|
use function count; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ResultCacheTest |
15
|
|
|
* |
16
|
|
|
* @author robo |
17
|
|
|
*/ |
18
|
|
|
class ResultCacheTest extends OrmFunctionalTestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \ReflectionProperty |
22
|
|
|
*/ |
23
|
|
|
private $cacheDataReflection; |
24
|
|
|
|
25
|
|
|
protected function setUp() { |
26
|
|
|
$this->cacheDataReflection = new \ReflectionProperty(ArrayCache::class, "data"); |
27
|
|
|
$this->cacheDataReflection->setAccessible(true); |
28
|
|
|
$this->useModelSet('cms'); |
29
|
|
|
parent::setUp(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ArrayCache $cache |
34
|
|
|
* @return integer |
35
|
|
|
*/ |
36
|
|
|
private function getCacheSize(ArrayCache $cache) |
37
|
|
|
{ |
38
|
|
|
return sizeof($this->cacheDataReflection->getValue($cache)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testResultCache() |
42
|
|
|
{ |
43
|
|
|
$user = new CmsUser; |
44
|
|
|
|
45
|
|
|
$user->name = 'Roman'; |
46
|
|
|
$user->username = 'romanb'; |
47
|
|
|
$user->status = 'dev'; |
48
|
|
|
|
49
|
|
|
$this->_em->persist($user); |
50
|
|
|
$this->_em->flush(); |
51
|
|
|
|
52
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); |
53
|
|
|
|
54
|
|
|
$cache = new ArrayCache(); |
55
|
|
|
|
56
|
|
|
$query->setResultCacheDriver($cache)->setResultCacheId('my_cache_id'); |
57
|
|
|
|
58
|
|
|
$this->assertFalse($cache->contains('my_cache_id')); |
59
|
|
|
|
60
|
|
|
$users = $query->getResult(); |
61
|
|
|
|
62
|
|
|
$this->assertTrue($cache->contains('my_cache_id')); |
63
|
|
|
$this->assertEquals(1, count($users)); |
64
|
|
|
$this->assertEquals('Roman', $users[0]->name); |
65
|
|
|
|
66
|
|
|
$this->_em->clear(); |
67
|
|
|
|
68
|
|
|
$query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); |
69
|
|
|
$query2->setResultCacheDriver($cache)->setResultCacheId('my_cache_id'); |
70
|
|
|
|
71
|
|
|
$users = $query2->getResult(); |
72
|
|
|
|
73
|
|
|
$this->assertTrue($cache->contains('my_cache_id')); |
74
|
|
|
$this->assertEquals(1, count($users)); |
75
|
|
|
$this->assertEquals('Roman', $users[0]->name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testSetResultCacheId() |
79
|
|
|
{ |
80
|
|
|
$cache = new ArrayCache; |
81
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); |
82
|
|
|
|
83
|
|
|
$query->setResultCacheDriver($cache); |
84
|
|
|
$query->setResultCacheId('testing_result_cache_id'); |
85
|
|
|
|
86
|
|
|
$this->assertFalse($cache->contains('testing_result_cache_id')); |
87
|
|
|
|
88
|
|
|
$users = $query->getResult(); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$this->assertTrue($cache->contains('testing_result_cache_id')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testUseResultCacheTrue() |
94
|
|
|
{ |
95
|
|
|
$cache = new ArrayCache(); |
96
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); |
97
|
|
|
|
98
|
|
|
$query->useResultCache(true); |
|
|
|
|
99
|
|
|
$query->setResultCacheDriver($cache); |
100
|
|
|
$query->setResultCacheId('testing_result_cache_id'); |
101
|
|
|
$users = $query->getResult(); |
|
|
|
|
102
|
|
|
|
103
|
|
|
$this->assertTrue($cache->contains('testing_result_cache_id')); |
104
|
|
|
|
105
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testUseResultCacheFalse() |
109
|
|
|
{ |
110
|
|
|
$cache = new ArrayCache(); |
111
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); |
112
|
|
|
|
113
|
|
|
$query->setResultCacheDriver($cache); |
114
|
|
|
$query->setResultCacheId('testing_result_cache_id'); |
115
|
|
|
$query->useResultCache(false); |
|
|
|
|
116
|
|
|
$query->getResult(); |
117
|
|
|
|
118
|
|
|
$this->assertFalse($cache->contains('testing_result_cache_id')); |
119
|
|
|
|
120
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @group DDC-1026 |
126
|
|
|
*/ |
127
|
|
|
public function testUseResultCacheParams() |
128
|
|
|
{ |
129
|
|
|
$cache = new ArrayCache(); |
130
|
|
|
$sqlCount = count($this->_sqlLoggerStack->queries); |
131
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1'); |
132
|
|
|
|
133
|
|
|
$query->setResultCacheDriver($cache); |
134
|
|
|
$query->useResultCache(true); |
|
|
|
|
135
|
|
|
|
136
|
|
|
// these queries should result in cache miss: |
137
|
|
|
$query->setParameter(1, 1); |
138
|
|
|
$query->getResult(); |
139
|
|
|
$query->setParameter(1, 2); |
140
|
|
|
$query->getResult(); |
141
|
|
|
|
142
|
|
|
$this->assertCount( |
143
|
|
|
$sqlCount + 2, |
144
|
|
|
$this->_sqlLoggerStack->queries, |
145
|
|
|
'Two non-cached queries.' |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
// these two queries should actually be cached, as they repeat previous ones: |
149
|
|
|
$query->setParameter(1, 1); |
150
|
|
|
$query->getResult(); |
151
|
|
|
$query->setParameter(1, 2); |
152
|
|
|
$query->getResult(); |
153
|
|
|
|
154
|
|
|
$this->assertCount( |
155
|
|
|
$sqlCount + 2, |
156
|
|
|
$this->_sqlLoggerStack->queries, |
157
|
|
|
'The next two sql queries should have been cached, but were not.' |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testEnableResultCache() : void |
162
|
|
|
{ |
163
|
|
|
$cache = new ArrayCache(); |
164
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); |
165
|
|
|
|
166
|
|
|
$query->enableResultCache(); |
167
|
|
|
$query->setResultCacheDriver($cache); |
168
|
|
|
$query->setResultCacheId('testing_result_cache_id'); |
169
|
|
|
$query->getResult(); |
170
|
|
|
|
171
|
|
|
$this->assertTrue($cache->contains('testing_result_cache_id')); |
172
|
|
|
|
173
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @group DDC-1026 |
178
|
|
|
*/ |
179
|
|
|
public function testEnableResultCacheParams() : void |
180
|
|
|
{ |
181
|
|
|
$cache = new ArrayCache(); |
182
|
|
|
$sqlCount = count($this->_sqlLoggerStack->queries); |
183
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1'); |
184
|
|
|
|
185
|
|
|
$query->setResultCacheDriver($cache); |
186
|
|
|
$query->enableResultCache(); |
187
|
|
|
|
188
|
|
|
// these queries should result in cache miss: |
189
|
|
|
$query->setParameter(1, 1); |
190
|
|
|
$query->getResult(); |
191
|
|
|
$query->setParameter(1, 2); |
192
|
|
|
$query->getResult(); |
193
|
|
|
|
194
|
|
|
$this->assertCount( |
195
|
|
|
$sqlCount + 2, |
196
|
|
|
$this->_sqlLoggerStack->queries, |
197
|
|
|
'Two non-cached queries.' |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
// these two queries should actually be cached, as they repeat previous ones: |
201
|
|
|
$query->setParameter(1, 1); |
202
|
|
|
$query->getResult(); |
203
|
|
|
$query->setParameter(1, 2); |
204
|
|
|
$query->getResult(); |
205
|
|
|
|
206
|
|
|
$this->assertCount( |
207
|
|
|
$sqlCount + 2, |
208
|
|
|
$this->_sqlLoggerStack->queries, |
209
|
|
|
'The next two sql queries should have been cached, but were not.' |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testDisableResultCache() : void |
214
|
|
|
{ |
215
|
|
|
$cache = new ArrayCache(); |
216
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); |
217
|
|
|
|
218
|
|
|
$query->setResultCacheDriver($cache); |
219
|
|
|
$query->setResultCacheId('testing_result_cache_id'); |
220
|
|
|
$query->disableResultCache(); |
221
|
|
|
$query->getResult(); |
222
|
|
|
|
223
|
|
|
$this->assertFalse($cache->contains('testing_result_cache_id')); |
224
|
|
|
|
225
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testNativeQueryResultCaching() |
229
|
|
|
{ |
230
|
|
|
$cache = new ArrayCache(); |
231
|
|
|
$rsm = new ResultSetMapping(); |
232
|
|
|
|
233
|
|
|
$rsm->addScalarResult('id', 'u', 'integer'); |
234
|
|
|
|
235
|
|
|
$query = $this->_em->createNativeQuery('select u.id FROM cms_users u WHERE u.id = ?', $rsm); |
236
|
|
|
|
237
|
|
|
$query->setParameter(1, 10); |
238
|
|
|
$query->setResultCacheDriver($cache)->enableResultCache(); |
239
|
|
|
|
240
|
|
|
$this->assertEquals(0, $this->getCacheSize($cache)); |
241
|
|
|
|
242
|
|
|
$query->getResult(); |
243
|
|
|
|
244
|
|
|
$this->assertEquals(1, $this->getCacheSize($cache)); |
245
|
|
|
|
246
|
|
|
return $query; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param string $query |
251
|
|
|
* @depends testNativeQueryResultCaching |
252
|
|
|
*/ |
253
|
|
|
public function testResultCacheNotDependsOnQueryHints($query) |
254
|
|
|
{ |
255
|
|
|
$cache = $query->getResultCacheDriver(); |
256
|
|
|
$cacheCount = $this->getCacheSize($cache); |
257
|
|
|
|
258
|
|
|
$query->setHint('foo', 'bar'); |
259
|
|
|
$query->getResult(); |
260
|
|
|
|
261
|
|
|
$this->assertEquals($cacheCount, $this->getCacheSize($cache)); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param <type> $query |
|
|
|
|
266
|
|
|
* @depends testNativeQueryResultCaching |
267
|
|
|
*/ |
268
|
|
|
public function testResultCacheDependsOnParameters($query) |
269
|
|
|
{ |
270
|
|
|
$cache = $query->getResultCacheDriver(); |
271
|
|
|
$cacheCount = $this->getCacheSize($cache); |
272
|
|
|
|
273
|
|
|
$query->setParameter(1, 50); |
274
|
|
|
$query->getResult(); |
275
|
|
|
|
276
|
|
|
$this->assertEquals($cacheCount + 1, $this->getCacheSize($cache)); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param <type> $query |
|
|
|
|
281
|
|
|
* @depends testNativeQueryResultCaching |
282
|
|
|
*/ |
283
|
|
|
public function testResultCacheNotDependsOnHydrationMode($query) |
284
|
|
|
{ |
285
|
|
|
$cache = $query->getResultCacheDriver(); |
286
|
|
|
$cacheCount = $this->getCacheSize($cache); |
287
|
|
|
|
288
|
|
|
$this->assertNotEquals(Query::HYDRATE_ARRAY, $query->getHydrationMode()); |
289
|
|
|
$query->getArrayResult(); |
290
|
|
|
|
291
|
|
|
$this->assertEquals($cacheCount, $this->getCacheSize($cache)); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @group DDC-909 |
296
|
|
|
*/ |
297
|
|
|
public function testResultCacheWithObjectParameter() |
298
|
|
|
{ |
299
|
|
|
$user1 = new CmsUser; |
300
|
|
|
$user1->name = 'Roman'; |
301
|
|
|
$user1->username = 'romanb'; |
302
|
|
|
$user1->status = 'dev'; |
303
|
|
|
|
304
|
|
|
$user2 = new CmsUser; |
305
|
|
|
$user2->name = 'Benjamin'; |
306
|
|
|
$user2->username = 'beberlei'; |
307
|
|
|
$user2->status = 'dev'; |
308
|
|
|
|
309
|
|
|
$article = new CmsArticle(); |
310
|
|
|
$article->text = "foo"; |
311
|
|
|
$article->topic = "baz"; |
312
|
|
|
$article->user = $user1; |
313
|
|
|
|
314
|
|
|
$this->_em->persist($article); |
315
|
|
|
$this->_em->persist($user1); |
316
|
|
|
$this->_em->persist($user2); |
317
|
|
|
$this->_em->flush(); |
318
|
|
|
|
319
|
|
|
$query = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1'); |
320
|
|
|
$query->setParameter(1, $user1); |
321
|
|
|
|
322
|
|
|
$cache = new ArrayCache(); |
323
|
|
|
|
324
|
|
|
$query->setResultCacheDriver($cache)->enableResultCache(); |
325
|
|
|
|
326
|
|
|
$articles = $query->getResult(); |
327
|
|
|
|
328
|
|
|
$this->assertEquals(1, count($articles)); |
329
|
|
|
$this->assertEquals('baz', $articles[0]->topic); |
330
|
|
|
|
331
|
|
|
$this->_em->clear(); |
332
|
|
|
|
333
|
|
|
$query2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1'); |
334
|
|
|
$query2->setParameter(1, $user1); |
335
|
|
|
|
336
|
|
|
$query2->setResultCacheDriver($cache)->enableResultCache(); |
337
|
|
|
|
338
|
|
|
$articles = $query2->getResult(); |
339
|
|
|
|
340
|
|
|
$this->assertEquals(1, count($articles)); |
341
|
|
|
$this->assertEquals('baz', $articles[0]->topic); |
342
|
|
|
|
343
|
|
|
$query3 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1'); |
344
|
|
|
$query3->setParameter(1, $user2); |
345
|
|
|
|
346
|
|
|
$query3->setResultCacheDriver($cache)->enableResultCache(); |
347
|
|
|
|
348
|
|
|
$articles = $query3->getResult(); |
349
|
|
|
|
350
|
|
|
$this->assertEquals(0, count($articles)); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|