|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Tests\Mocks\TimestampRegionMock; |
|
6
|
|
|
use Doctrine\Tests\OrmTestCase; |
|
7
|
|
|
use Doctrine\Tests\Mocks\CacheRegionMock; |
|
8
|
|
|
use Doctrine\ORM\Cache\DefaultQueryCache; |
|
9
|
|
|
use Doctrine\ORM\Cache\QueryCacheKey; |
|
10
|
|
|
use Doctrine\ORM\Cache\QueryCacheEntry; |
|
11
|
|
|
use Doctrine\ORM\Query\ResultSetMappingBuilder; |
|
12
|
|
|
use Doctrine\Tests\Models\Cache\Country; |
|
13
|
|
|
use Doctrine\Tests\Models\Cache\City; |
|
14
|
|
|
use Doctrine\Tests\Models\Cache\State; |
|
15
|
|
|
use Doctrine\Tests\Models\Cache\Restaurant; |
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
17
|
|
|
use Doctrine\Tests\Models\Generic\BooleanModel; |
|
18
|
|
|
use Doctrine\ORM\Cache\EntityCacheEntry; |
|
19
|
|
|
use Doctrine\ORM\Cache; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @group DDC-2183 |
|
23
|
|
|
*/ |
|
24
|
|
|
class DefaultQueryCacheTest extends OrmTestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Doctrine\ORM\Cache\DefaultQueryCache |
|
28
|
|
|
*/ |
|
29
|
|
|
private $queryCache; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Doctrine\ORM\EntityManager |
|
33
|
|
|
*/ |
|
34
|
|
|
private $em; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \Doctrine\Tests\Mocks\CacheRegionMock |
|
38
|
|
|
*/ |
|
39
|
|
|
private $region; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Doctrine\Tests\ORM\Cache\CacheFactoryDefaultQueryCacheTest |
|
43
|
|
|
*/ |
|
44
|
|
|
private $cacheFactory; |
|
45
|
|
|
|
|
46
|
|
|
protected function setUp() |
|
47
|
|
|
{ |
|
48
|
|
|
parent::setUp(); |
|
49
|
|
|
|
|
50
|
|
|
$this->enableSecondLevelCache(); |
|
51
|
|
|
|
|
52
|
|
|
$this->em = $this->_getTestEntityManager(); |
|
53
|
|
|
$this->region = new CacheRegionMock(); |
|
54
|
|
|
$this->queryCache = new DefaultQueryCache($this->em, $this->region); |
|
55
|
|
|
$this->cacheFactory = new CacheFactoryDefaultQueryCacheTest($this->queryCache, $this->region); |
|
56
|
|
|
|
|
57
|
|
|
$this->em->getConfiguration() |
|
58
|
|
|
->getSecondLevelCacheConfiguration() |
|
59
|
|
|
->setCacheFactory($this->cacheFactory); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testImplementQueryCache() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\QueryCache', $this->queryCache); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testGetRegion() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->assertSame($this->region, $this->queryCache->getRegion()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testClearShouldEvictRegion() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->queryCache->clear(); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertArrayHasKey('evictAll', $this->region->calls); |
|
77
|
|
|
$this->assertCount(1, $this->region->calls['evictAll']); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testPutBasicQueryResult() |
|
81
|
|
|
{ |
|
82
|
|
|
$result = array(); |
|
83
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
84
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
85
|
|
|
$metadata = $this->em->getClassMetadata(Country::CLASSNAME); |
|
86
|
|
|
|
|
87
|
|
|
$rsm->addRootEntityFromClassMetadata(Country::CLASSNAME, 'c'); |
|
88
|
|
|
|
|
89
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
90
|
|
|
$name = "Country $i"; |
|
91
|
|
|
$entity = new Country($name); |
|
92
|
|
|
$result[] = $entity; |
|
93
|
|
|
|
|
94
|
|
|
$metadata->setFieldValue($entity, 'id', $i); |
|
95
|
|
|
$this->em->getUnitOfWork()->registerManaged($entity, array('id' => $i), array('name' => $name)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->assertTrue($this->queryCache->put($key, $rsm, $result)); |
|
99
|
|
|
$this->assertArrayHasKey('put', $this->region->calls); |
|
100
|
|
|
$this->assertCount(5, $this->region->calls['put']); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][0]['key']); |
|
103
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][1]['key']); |
|
104
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][2]['key']); |
|
105
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][3]['key']); |
|
106
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\QueryCacheKey', $this->region->calls['put'][4]['key']); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheEntry', $this->region->calls['put'][0]['entry']); |
|
109
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheEntry', $this->region->calls['put'][1]['entry']); |
|
110
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheEntry', $this->region->calls['put'][2]['entry']); |
|
111
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheEntry', $this->region->calls['put'][3]['entry']); |
|
112
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\QueryCacheEntry', $this->region->calls['put'][4]['entry']); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testPutToOneAssociationQueryResult() |
|
116
|
|
|
{ |
|
117
|
|
|
$result = array(); |
|
118
|
|
|
$uow = $this->em->getUnitOfWork(); |
|
119
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
120
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
121
|
|
|
$cityClass = $this->em->getClassMetadata(City::CLASSNAME); |
|
122
|
|
|
$stateClass = $this->em->getClassMetadata(State::CLASSNAME); |
|
123
|
|
|
|
|
124
|
|
|
$rsm->addRootEntityFromClassMetadata(City::CLASSNAME, 'c'); |
|
125
|
|
|
$rsm->addJoinedEntityFromClassMetadata(State::CLASSNAME, 's', 'c', 'state', array('id'=>'state_id', 'name'=>'state_name')); |
|
126
|
|
|
|
|
127
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
128
|
|
|
$state = new State("State $i"); |
|
129
|
|
|
$city = new City("City $i", $state); |
|
130
|
|
|
$result[] = $city; |
|
131
|
|
|
|
|
132
|
|
|
$cityClass->setFieldValue($city, 'id', $i); |
|
133
|
|
|
$stateClass->setFieldValue($state, 'id', $i*2); |
|
134
|
|
|
|
|
135
|
|
|
$uow->registerManaged($state, array('id' => $state->getId()), array('name' => $city->getName())); |
|
136
|
|
|
$uow->registerManaged($city, array('id' => $city->getId()), array('name' => $city->getName(), 'state' => $state)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$this->assertTrue($this->queryCache->put($key, $rsm, $result)); |
|
140
|
|
|
$this->assertArrayHasKey('put', $this->region->calls); |
|
141
|
|
|
$this->assertCount(9, $this->region->calls['put']); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][0]['key']); |
|
144
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][1]['key']); |
|
145
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][2]['key']); |
|
146
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][3]['key']); |
|
147
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][4]['key']); |
|
148
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][5]['key']); |
|
149
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][6]['key']); |
|
150
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][7]['key']); |
|
151
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\QueryCacheKey', $this->region->calls['put'][8]['key']); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function testPutToOneAssociation2LevelsQueryResult() |
|
155
|
|
|
{ |
|
156
|
|
|
$result = array(); |
|
157
|
|
|
$uow = $this->em->getUnitOfWork(); |
|
158
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
159
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
160
|
|
|
$cityClass = $this->em->getClassMetadata(City::CLASSNAME); |
|
161
|
|
|
$stateClass = $this->em->getClassMetadata(State::CLASSNAME); |
|
162
|
|
|
$countryClass = $this->em->getClassMetadata(Country::CLASSNAME); |
|
163
|
|
|
|
|
164
|
|
|
$rsm->addRootEntityFromClassMetadata(City::CLASSNAME, 'c'); |
|
165
|
|
|
$rsm->addJoinedEntityFromClassMetadata(State::CLASSNAME, 's', 'c', 'state', array('id'=>'state_id', 'name'=>'state_name')); |
|
166
|
|
|
$rsm->addJoinedEntityFromClassMetadata(Country::CLASSNAME, 'co', 's', 'country', array('id'=>'country_id', 'name'=>'country_name')); |
|
167
|
|
|
|
|
168
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
169
|
|
|
$country = new Country("Country $i"); |
|
170
|
|
|
$state = new State("State $i", $country); |
|
171
|
|
|
$city = new City("City $i", $state); |
|
172
|
|
|
|
|
173
|
|
|
$result[] = $city; |
|
174
|
|
|
|
|
175
|
|
|
$cityClass->setFieldValue($city, 'id', $i); |
|
176
|
|
|
$stateClass->setFieldValue($state, 'id', $i*2); |
|
177
|
|
|
$countryClass->setFieldValue($country, 'id', $i*3); |
|
178
|
|
|
|
|
179
|
|
|
$uow->registerManaged($country, array('id' => $country->getId()), array('name' => $country->getName())); |
|
180
|
|
|
$uow->registerManaged($state, array('id' => $state->getId()), array('name' => $state->getName(), 'country' => $country)); |
|
181
|
|
|
$uow->registerManaged($city, array('id' => $city->getId()), array('name' => $city->getName(), 'state' => $state)); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$this->assertTrue($this->queryCache->put($key, $rsm, $result)); |
|
185
|
|
|
$this->assertArrayHasKey('put', $this->region->calls); |
|
186
|
|
|
$this->assertCount(13, $this->region->calls['put']); |
|
187
|
|
|
|
|
188
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][0]['key']); |
|
189
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][1]['key']); |
|
190
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][2]['key']); |
|
191
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][3]['key']); |
|
192
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][4]['key']); |
|
193
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][5]['key']); |
|
194
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][6]['key']); |
|
195
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][7]['key']); |
|
196
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][8]['key']); |
|
197
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][9]['key']); |
|
198
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][10]['key']); |
|
199
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][11]['key']); |
|
200
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\QueryCacheKey', $this->region->calls['put'][12]['key']); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function testPutToOneAssociationNullQueryResult() |
|
204
|
|
|
{ |
|
205
|
|
|
$result = array(); |
|
206
|
|
|
$uow = $this->em->getUnitOfWork(); |
|
207
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
208
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
209
|
|
|
$cityClass = $this->em->getClassMetadata(City::CLASSNAME); |
|
210
|
|
|
|
|
211
|
|
|
$rsm->addRootEntityFromClassMetadata(City::CLASSNAME, 'c'); |
|
212
|
|
|
$rsm->addJoinedEntityFromClassMetadata(State::CLASSNAME, 's', 'c', 'state', array('id'=>'state_id', 'name'=>'state_name')); |
|
213
|
|
|
|
|
214
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
215
|
|
|
$city = new City("City $i", null); |
|
216
|
|
|
$result[] = $city; |
|
217
|
|
|
|
|
218
|
|
|
$cityClass->setFieldValue($city, 'id', $i); |
|
219
|
|
|
|
|
220
|
|
|
$uow->registerManaged($city, array('id' => $city->getId()), array('name' => $city->getName(), 'state' => null)); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$this->assertTrue($this->queryCache->put($key, $rsm, $result)); |
|
224
|
|
|
$this->assertArrayHasKey('put', $this->region->calls); |
|
225
|
|
|
$this->assertCount(5, $this->region->calls['put']); |
|
226
|
|
|
|
|
227
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][0]['key']); |
|
228
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][1]['key']); |
|
229
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][2]['key']); |
|
230
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\EntityCacheKey', $this->region->calls['put'][3]['key']); |
|
231
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Cache\QueryCacheKey', $this->region->calls['put'][4]['key']); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function testPutToManyAssociationQueryResult() |
|
235
|
|
|
{ |
|
236
|
|
|
$result = array(); |
|
237
|
|
|
$uow = $this->em->getUnitOfWork(); |
|
238
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
239
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
240
|
|
|
$cityClass = $this->em->getClassMetadata(City::CLASSNAME); |
|
241
|
|
|
$stateClass = $this->em->getClassMetadata(State::CLASSNAME); |
|
242
|
|
|
|
|
243
|
|
|
$rsm->addRootEntityFromClassMetadata(State::CLASSNAME, 's'); |
|
244
|
|
|
$rsm->addJoinedEntityFromClassMetadata(City::CLASSNAME, 'c', 's', 'cities', array('id'=>'c_id', 'name'=>'c_name')); |
|
245
|
|
|
|
|
246
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
247
|
|
|
$state = new State("State $i"); |
|
248
|
|
|
$city1 = new City("City 1", $state); |
|
249
|
|
|
$city2 = new City("City 2", $state); |
|
250
|
|
|
$result[] = $state; |
|
251
|
|
|
|
|
252
|
|
|
$cityClass->setFieldValue($city1, 'id', $i + 11); |
|
253
|
|
|
$cityClass->setFieldValue($city2, 'id', $i + 22); |
|
254
|
|
|
$stateClass->setFieldValue($state, 'id', $i); |
|
255
|
|
|
|
|
256
|
|
|
$state->addCity($city1); |
|
257
|
|
|
$state->addCity($city2); |
|
258
|
|
|
|
|
259
|
|
|
$uow->registerManaged($city1, array('id' => $city1->getId()), array('name' => $city1->getName(), 'state' => $state)); |
|
260
|
|
|
$uow->registerManaged($city2, array('id' => $city2->getId()), array('name' => $city2->getName(), 'state' => $state)); |
|
261
|
|
|
$uow->registerManaged($state, array('id' => $state->getId()), array('name' => $state->getName(), 'cities' => $state->getCities())); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$this->assertTrue($this->queryCache->put($key, $rsm, $result)); |
|
265
|
|
|
$this->assertArrayHasKey('put', $this->region->calls); |
|
266
|
|
|
$this->assertCount(13, $this->region->calls['put']); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function testGetBasicQueryResult() |
|
270
|
|
|
{ |
|
271
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
272
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
273
|
|
|
$entry = new QueryCacheEntry(array( |
|
274
|
|
|
array('identifier' => array('id' => 1)), |
|
275
|
|
|
array('identifier' => array('id' => 2)) |
|
276
|
|
|
)); |
|
277
|
|
|
|
|
278
|
|
|
$data = array( |
|
279
|
|
|
array('id'=>1, 'name' => 'Foo'), |
|
280
|
|
|
array('id'=>2, 'name' => 'Bar') |
|
281
|
|
|
); |
|
282
|
|
|
|
|
283
|
|
|
$this->region->addReturn('get', $entry); |
|
284
|
|
|
$this->region->addReturn('get', new EntityCacheEntry(Country::CLASSNAME, $data[0])); |
|
285
|
|
|
$this->region->addReturn('get', new EntityCacheEntry(Country::CLASSNAME, $data[1])); |
|
286
|
|
|
|
|
287
|
|
|
$rsm->addRootEntityFromClassMetadata(Country::CLASSNAME, 'c'); |
|
288
|
|
|
|
|
289
|
|
|
$result = $this->queryCache->get($key, $rsm); |
|
290
|
|
|
|
|
291
|
|
|
$this->assertCount(2, $result); |
|
292
|
|
|
$this->assertInstanceOf(Country::CLASSNAME, $result[0]); |
|
293
|
|
|
$this->assertInstanceOf(Country::CLASSNAME, $result[1]); |
|
294
|
|
|
$this->assertEquals(1, $result[0]->getId()); |
|
295
|
|
|
$this->assertEquals(2, $result[1]->getId()); |
|
296
|
|
|
$this->assertEquals('Foo', $result[0]->getName()); |
|
297
|
|
|
$this->assertEquals('Bar', $result[1]->getName()); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
public function testCancelPutResultIfEntityPutFails() |
|
301
|
|
|
{ |
|
302
|
|
|
$result = array(); |
|
303
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
304
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
305
|
|
|
$metadata = $this->em->getClassMetadata(Country::CLASSNAME); |
|
306
|
|
|
|
|
307
|
|
|
$rsm->addRootEntityFromClassMetadata(Country::CLASSNAME, 'c'); |
|
308
|
|
|
|
|
309
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
310
|
|
|
$name = "Country $i"; |
|
311
|
|
|
$entity = new Country($name); |
|
312
|
|
|
$result[] = $entity; |
|
313
|
|
|
|
|
314
|
|
|
$metadata->setFieldValue($entity, 'id', $i); |
|
315
|
|
|
$this->em->getUnitOfWork()->registerManaged($entity, array('id' => $i), array('name' => $name)); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
$this->region->addReturn('put', false); |
|
319
|
|
|
|
|
320
|
|
|
$this->assertFalse($this->queryCache->put($key, $rsm, $result)); |
|
321
|
|
|
$this->assertArrayHasKey('put', $this->region->calls); |
|
322
|
|
|
$this->assertCount(1, $this->region->calls['put']); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
public function testCancelPutResultIfAssociationEntityPutFails() |
|
326
|
|
|
{ |
|
327
|
|
|
$result = array(); |
|
328
|
|
|
$uow = $this->em->getUnitOfWork(); |
|
329
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
330
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
331
|
|
|
$cityClass = $this->em->getClassMetadata(City::CLASSNAME); |
|
332
|
|
|
$stateClass = $this->em->getClassMetadata(State::CLASSNAME); |
|
333
|
|
|
|
|
334
|
|
|
$rsm->addRootEntityFromClassMetadata(City::CLASSNAME, 'c'); |
|
335
|
|
|
$rsm->addJoinedEntityFromClassMetadata(State::CLASSNAME, 's', 'c', 'state', array('id'=>'state_id', 'name'=>'state_name')); |
|
336
|
|
|
|
|
337
|
|
|
$state = new State("State 1"); |
|
338
|
|
|
$city = new City("City 2", $state); |
|
339
|
|
|
$result[] = $city; |
|
340
|
|
|
|
|
341
|
|
|
$cityClass->setFieldValue($city, 'id', 1); |
|
342
|
|
|
$stateClass->setFieldValue($state, 'id', 11); |
|
343
|
|
|
|
|
344
|
|
|
$uow->registerManaged($state, array('id' => $state->getId()), array('name' => $city->getName())); |
|
345
|
|
|
$uow->registerManaged($city, array('id' => $city->getId()), array('name' => $city->getName(), 'state' => $state)); |
|
346
|
|
|
|
|
347
|
|
|
$this->region->addReturn('put', true); // put root entity |
|
348
|
|
|
$this->region->addReturn('put', false); // association fails |
|
349
|
|
|
|
|
350
|
|
|
$this->assertFalse($this->queryCache->put($key, $rsm, $result)); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
public function testCancelPutToManyAssociationQueryResult() |
|
354
|
|
|
{ |
|
355
|
|
|
$result = array(); |
|
356
|
|
|
$uow = $this->em->getUnitOfWork(); |
|
357
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
358
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
359
|
|
|
$cityClass = $this->em->getClassMetadata(City::CLASSNAME); |
|
360
|
|
|
$stateClass = $this->em->getClassMetadata(State::CLASSNAME); |
|
361
|
|
|
|
|
362
|
|
|
$rsm->addRootEntityFromClassMetadata(State::CLASSNAME, 's'); |
|
363
|
|
|
$rsm->addJoinedEntityFromClassMetadata(City::CLASSNAME, 'c', 's', 'cities', array('id'=>'c_id', 'name'=>'c_name')); |
|
364
|
|
|
|
|
365
|
|
|
$state = new State("State"); |
|
366
|
|
|
$city1 = new City("City 1", $state); |
|
367
|
|
|
$city2 = new City("City 2", $state); |
|
368
|
|
|
$result[] = $state; |
|
369
|
|
|
|
|
370
|
|
|
$stateClass->setFieldValue($state, 'id', 1); |
|
371
|
|
|
$cityClass->setFieldValue($city1, 'id', 11); |
|
372
|
|
|
$cityClass->setFieldValue($city2, 'id', 22); |
|
373
|
|
|
|
|
374
|
|
|
$state->addCity($city1); |
|
375
|
|
|
$state->addCity($city2); |
|
376
|
|
|
|
|
377
|
|
|
$uow->registerManaged($city1, array('id' => $city1->getId()), array('name' => $city1->getName(), 'state' => $state)); |
|
378
|
|
|
$uow->registerManaged($city2, array('id' => $city2->getId()), array('name' => $city2->getName(), 'state' => $state)); |
|
379
|
|
|
$uow->registerManaged($state, array('id' => $state->getId()), array('name' => $state->getName(), 'cities' => $state->getCities())); |
|
380
|
|
|
|
|
381
|
|
|
$this->region->addReturn('put', true); // put root entity |
|
382
|
|
|
$this->region->addReturn('put', false); // collection association fails |
|
383
|
|
|
|
|
384
|
|
|
$this->assertFalse($this->queryCache->put($key, $rsm, $result)); |
|
385
|
|
|
$this->assertArrayHasKey('put', $this->region->calls); |
|
386
|
|
|
$this->assertCount(2, $this->region->calls['put']); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
public function testIgnoreCacheNonGetMode() |
|
390
|
|
|
{ |
|
391
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
392
|
|
|
$key = new QueryCacheKey('query.key1', 0, Cache::MODE_PUT); |
|
393
|
|
|
$entry = new QueryCacheEntry(array( |
|
394
|
|
|
array('identifier' => array('id' => 1)), |
|
395
|
|
|
array('identifier' => array('id' => 2)) |
|
396
|
|
|
)); |
|
397
|
|
|
|
|
398
|
|
|
$rsm->addRootEntityFromClassMetadata(Country::CLASSNAME, 'c'); |
|
399
|
|
|
|
|
400
|
|
|
$this->region->addReturn('get', $entry); |
|
401
|
|
|
|
|
402
|
|
|
$this->assertNull($this->queryCache->get($key, $rsm)); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
public function testIgnoreCacheNonPutMode() |
|
406
|
|
|
{ |
|
407
|
|
|
$result = array(); |
|
408
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
409
|
|
|
$metadata = $this->em->getClassMetadata(Country::CLASSNAME); |
|
410
|
|
|
$key = new QueryCacheKey('query.key1', 0, Cache::MODE_GET); |
|
411
|
|
|
|
|
412
|
|
|
$rsm->addRootEntityFromClassMetadata(Country::CLASSNAME, 'c'); |
|
413
|
|
|
|
|
414
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
415
|
|
|
$name = "Country $i"; |
|
416
|
|
|
$entity = new Country($name); |
|
417
|
|
|
$result[] = $entity; |
|
418
|
|
|
|
|
419
|
|
|
$metadata->setFieldValue($entity, 'id', $i); |
|
420
|
|
|
$this->em->getUnitOfWork()->registerManaged($entity, array('id' => $i), array('name' => $name)); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
$this->assertFalse($this->queryCache->put($key, $rsm, $result)); |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
public function testGetShouldIgnoreOldQueryCacheEntryResult() |
|
427
|
|
|
{ |
|
428
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
429
|
|
|
$key = new QueryCacheKey('query.key1', 50); |
|
430
|
|
|
$entry = new QueryCacheEntry(array( |
|
431
|
|
|
array('identifier' => array('id' => 1)), |
|
432
|
|
|
array('identifier' => array('id' => 2)) |
|
433
|
|
|
)); |
|
434
|
|
|
$entities = array( |
|
435
|
|
|
array('id'=>1, 'name' => 'Foo'), |
|
436
|
|
|
array('id'=>2, 'name' => 'Bar') |
|
437
|
|
|
); |
|
438
|
|
|
|
|
439
|
|
|
$entry->time = time() - 100; |
|
440
|
|
|
|
|
441
|
|
|
$this->region->addReturn('get', $entry); |
|
442
|
|
|
$this->region->addReturn('get', new EntityCacheEntry(Country::CLASSNAME, $entities[0])); |
|
443
|
|
|
$this->region->addReturn('get', new EntityCacheEntry(Country::CLASSNAME, $entities[1])); |
|
444
|
|
|
|
|
445
|
|
|
$rsm->addRootEntityFromClassMetadata(Country::CLASSNAME, 'c'); |
|
446
|
|
|
|
|
447
|
|
|
$this->assertNull($this->queryCache->get($key, $rsm)); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
public function testGetShouldIgnoreNonQueryCacheEntryResult() |
|
451
|
|
|
{ |
|
452
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
453
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
454
|
|
|
$entry = new \ArrayObject(array( |
|
455
|
|
|
array('identifier' => array('id' => 1)), |
|
456
|
|
|
array('identifier' => array('id' => 2)) |
|
457
|
|
|
)); |
|
458
|
|
|
|
|
459
|
|
|
$data = array( |
|
460
|
|
|
array('id'=>1, 'name' => 'Foo'), |
|
461
|
|
|
array('id'=>2, 'name' => 'Bar') |
|
462
|
|
|
); |
|
463
|
|
|
|
|
464
|
|
|
$this->region->addReturn('get', $entry); |
|
465
|
|
|
$this->region->addReturn('get', new EntityCacheEntry(Country::CLASSNAME, $data[0])); |
|
466
|
|
|
$this->region->addReturn('get', new EntityCacheEntry(Country::CLASSNAME, $data[1])); |
|
467
|
|
|
|
|
468
|
|
|
$rsm->addRootEntityFromClassMetadata(Country::CLASSNAME, 'c'); |
|
469
|
|
|
|
|
470
|
|
|
$this->assertNull($this->queryCache->get($key, $rsm)); |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
public function testGetShouldIgnoreMissingEntityQueryCacheEntry() |
|
474
|
|
|
{ |
|
475
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
476
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
477
|
|
|
$entry = new QueryCacheEntry(array( |
|
478
|
|
|
array('identifier' => array('id' => 1)), |
|
479
|
|
|
array('identifier' => array('id' => 2)) |
|
480
|
|
|
)); |
|
481
|
|
|
|
|
482
|
|
|
$this->region->addReturn('get', $entry); |
|
483
|
|
|
$this->region->addReturn('get', null); |
|
484
|
|
|
|
|
485
|
|
|
$rsm->addRootEntityFromClassMetadata(Country::CLASSNAME, 'c'); |
|
486
|
|
|
|
|
487
|
|
|
$this->assertNull($this->queryCache->get($key, $rsm)); |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
public function testGetAssociationValue() |
|
491
|
|
|
{ |
|
492
|
|
|
$reflection = new \ReflectionMethod($this->queryCache, 'getAssociationValue'); |
|
493
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
494
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
|
|
|
|
|
495
|
|
|
|
|
496
|
|
|
$reflection->setAccessible(true); |
|
497
|
|
|
|
|
498
|
|
|
$germany = new Country("Germany"); |
|
499
|
|
|
$bavaria = new State("Bavaria", $germany); |
|
500
|
|
|
$wurzburg = new City("Würzburg", $bavaria); |
|
501
|
|
|
$munich = new City("Munich", $bavaria); |
|
502
|
|
|
|
|
503
|
|
|
$bavaria->addCity($munich); |
|
504
|
|
|
$bavaria->addCity($wurzburg); |
|
505
|
|
|
|
|
506
|
|
|
$munich->addAttraction(new Restaurant('Reinstoff', $munich)); |
|
507
|
|
|
$munich->addAttraction(new Restaurant('Schneider Weisse', $munich)); |
|
508
|
|
|
$wurzburg->addAttraction(new Restaurant('Fischers Fritz', $wurzburg)); |
|
509
|
|
|
|
|
510
|
|
|
$rsm->addRootEntityFromClassMetadata(State::CLASSNAME, 's'); |
|
511
|
|
|
$rsm->addJoinedEntityFromClassMetadata(City::CLASSNAME, 'c', 's', 'cities', array( |
|
512
|
|
|
'id' => 'c_id', |
|
513
|
|
|
'name' => 'c_name' |
|
514
|
|
|
)); |
|
515
|
|
|
$rsm->addJoinedEntityFromClassMetadata(Restaurant::CLASSNAME, 'a', 'c', 'attractions', array( |
|
516
|
|
|
'id' => 'a_id', |
|
517
|
|
|
'name' => 'a_name' |
|
518
|
|
|
)); |
|
519
|
|
|
|
|
520
|
|
|
$cities = $reflection->invoke($this->queryCache, $rsm, 'c', $bavaria); |
|
521
|
|
|
$attractions = $reflection->invoke($this->queryCache, $rsm, 'a', $bavaria); |
|
522
|
|
|
|
|
523
|
|
|
$this->assertCount(2, $cities); |
|
524
|
|
|
$this->assertCount(2, $attractions); |
|
525
|
|
|
|
|
526
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $cities); |
|
527
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $attractions[0]); |
|
528
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $attractions[1]); |
|
529
|
|
|
|
|
530
|
|
|
$this->assertCount(2, $attractions[0]); |
|
531
|
|
|
$this->assertCount(1, $attractions[1]); |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
|
|
/** |
|
535
|
|
|
* @expectedException Doctrine\ORM\Cache\CacheException |
|
536
|
|
|
* @expectedExceptionMessage Second level cache does not support scalar results. |
|
537
|
|
|
*/ |
|
538
|
|
|
public function testScalarResultException() |
|
539
|
|
|
{ |
|
540
|
|
|
$result = array(); |
|
541
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
542
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
543
|
|
|
|
|
544
|
|
|
$rsm->addScalarResult('id', 'u', 'integer'); |
|
545
|
|
|
|
|
546
|
|
|
$this->queryCache->put($key, $rsm, $result); |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
/** |
|
550
|
|
|
* @expectedException Doctrine\ORM\Cache\CacheException |
|
551
|
|
|
* @expectedExceptionMessage Second level cache does not support multiple root entities. |
|
552
|
|
|
*/ |
|
553
|
|
|
public function testSupportMultipleRootEntitiesException() |
|
554
|
|
|
{ |
|
555
|
|
|
$result = array(); |
|
556
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
557
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
558
|
|
|
|
|
559
|
|
|
$rsm->addEntityResult('Doctrine\Tests\Models\Cache\City', 'e1'); |
|
560
|
|
|
$rsm->addEntityResult('Doctrine\Tests\Models\Cache\State', 'e2'); |
|
561
|
|
|
|
|
562
|
|
|
$this->queryCache->put($key, $rsm, $result); |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
/** |
|
566
|
|
|
* @expectedException Doctrine\ORM\Cache\CacheException |
|
567
|
|
|
* @expectedExceptionMessage Entity "Doctrine\Tests\Models\Generic\BooleanModel" not configured as part of the second-level cache. |
|
568
|
|
|
*/ |
|
569
|
|
|
public function testNotCacheableEntityException() |
|
570
|
|
|
{ |
|
571
|
|
|
$result = array(); |
|
572
|
|
|
$key = new QueryCacheKey('query.key1', 0); |
|
573
|
|
|
$rsm = new ResultSetMappingBuilder($this->em); |
|
574
|
|
|
$className = 'Doctrine\Tests\Models\Generic\BooleanModel'; |
|
575
|
|
|
|
|
576
|
|
|
$rsm->addRootEntityFromClassMetadata($className, 'c'); |
|
577
|
|
|
|
|
578
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
579
|
|
|
$entity = new BooleanModel(); |
|
580
|
|
|
$boolean = ($i % 2 === 0); |
|
581
|
|
|
|
|
582
|
|
|
$entity->id = $i; |
|
583
|
|
|
$entity->booleanField = $boolean; |
|
584
|
|
|
$result[] = $entity; |
|
585
|
|
|
|
|
586
|
|
|
$this->em->getUnitOfWork()->registerManaged($entity, array('id' => $i), array('booleanField' => $boolean)); |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
$this->assertFalse($this->queryCache->put($key, $rsm, $result)); |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
class CacheFactoryDefaultQueryCacheTest extends Cache\DefaultCacheFactory |
|
595
|
|
|
{ |
|
596
|
|
|
private $queryCache; |
|
597
|
|
|
private $region; |
|
598
|
|
|
|
|
599
|
|
|
public function __construct(DefaultQueryCache $queryCache, CacheRegionMock $region) |
|
600
|
|
|
{ |
|
601
|
|
|
$this->queryCache = $queryCache; |
|
602
|
|
|
$this->region = $region; |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
public function buildQueryCache(EntityManagerInterface $em, $regionName = null) |
|
606
|
|
|
{ |
|
607
|
|
|
return $this->queryCache; |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
public function getRegion(array $cache) |
|
611
|
|
|
{ |
|
612
|
|
|
return $this->region; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
public function getTimestampRegion() |
|
616
|
|
|
{ |
|
617
|
|
|
return new TimestampRegionMock(); |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.