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