1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\Tests\Models\Cache\Attraction; |
6
|
|
|
use Doctrine\Tests\Models\Cache\Restaurant; |
7
|
|
|
use Doctrine\Tests\Models\Cache\Beach; |
8
|
|
|
use Doctrine\Tests\Models\Cache\City; |
9
|
|
|
use Doctrine\Tests\Models\Cache\Bar; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @group DDC-2183 |
13
|
|
|
*/ |
14
|
|
|
class SecondLevelCacheSingleTableInheritanceTest extends SecondLevelCacheAbstractTest |
15
|
|
|
{ |
16
|
|
|
public function testUseSameRegion() |
17
|
|
|
{ |
18
|
|
|
$attractionRegion = $this->cache->getEntityCacheRegion(Attraction::CLASSNAME); |
19
|
|
|
$restaurantRegion = $this->cache->getEntityCacheRegion(Restaurant::CLASSNAME); |
20
|
|
|
$beachRegion = $this->cache->getEntityCacheRegion(Beach::CLASSNAME); |
21
|
|
|
$barRegion = $this->cache->getEntityCacheRegion(Bar::CLASSNAME); |
22
|
|
|
|
23
|
|
|
$this->assertEquals($attractionRegion->getName(), $restaurantRegion->getName()); |
24
|
|
|
$this->assertEquals($attractionRegion->getName(), $beachRegion->getName()); |
25
|
|
|
$this->assertEquals($attractionRegion->getName(), $barRegion->getName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testPutOnPersistSingleTableInheritance() |
29
|
|
|
{ |
30
|
|
|
$this->loadFixturesCountries(); |
31
|
|
|
$this->loadFixturesStates(); |
32
|
|
|
$this->loadFixturesCities(); |
33
|
|
|
$this->loadFixturesAttractions(); |
34
|
|
|
|
35
|
|
|
$this->_em->clear(); |
36
|
|
|
|
37
|
|
|
$this->assertTrue($this->cache->containsEntity(Bar::CLASSNAME, $this->attractions[0]->getId())); |
38
|
|
|
$this->assertTrue($this->cache->containsEntity(Bar::CLASSNAME, $this->attractions[1]->getId())); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCountaisRootClass() |
42
|
|
|
{ |
43
|
|
|
$this->loadFixturesCountries(); |
44
|
|
|
$this->loadFixturesStates(); |
45
|
|
|
$this->loadFixturesCities(); |
46
|
|
|
$this->loadFixturesAttractions(); |
47
|
|
|
|
48
|
|
|
$this->_em->clear(); |
49
|
|
|
|
50
|
|
|
foreach ($this->attractions as $attraction) { |
51
|
|
|
$this->assertTrue($this->cache->containsEntity(Attraction::CLASSNAME, $attraction->getId())); |
52
|
|
|
$this->assertTrue($this->cache->containsEntity(get_class($attraction), $attraction->getId())); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testPutAndLoadEntities() |
57
|
|
|
{ |
58
|
|
|
$this->loadFixturesCountries(); |
59
|
|
|
$this->loadFixturesStates(); |
60
|
|
|
$this->loadFixturesCities(); |
61
|
|
|
$this->loadFixturesAttractions(); |
62
|
|
|
|
63
|
|
|
$this->_em->clear(); |
64
|
|
|
|
65
|
|
|
$this->cache->evictEntityRegion(Attraction::CLASSNAME); |
66
|
|
|
|
67
|
|
|
$entityId1 = $this->attractions[0]->getId(); |
68
|
|
|
$entityId2 = $this->attractions[1]->getId(); |
69
|
|
|
|
70
|
|
|
$this->assertFalse($this->cache->containsEntity(Attraction::CLASSNAME, $entityId1)); |
71
|
|
|
$this->assertFalse($this->cache->containsEntity(Attraction::CLASSNAME, $entityId2)); |
72
|
|
|
$this->assertFalse($this->cache->containsEntity(Bar::CLASSNAME, $entityId1)); |
73
|
|
|
$this->assertFalse($this->cache->containsEntity(Bar::CLASSNAME, $entityId2)); |
74
|
|
|
|
75
|
|
|
$entity1 = $this->_em->find(Attraction::CLASSNAME, $entityId1); |
76
|
|
|
$entity2 = $this->_em->find(Attraction::CLASSNAME, $entityId2); |
77
|
|
|
|
78
|
|
|
$this->assertTrue($this->cache->containsEntity(Attraction::CLASSNAME, $entityId1)); |
79
|
|
|
$this->assertTrue($this->cache->containsEntity(Attraction::CLASSNAME, $entityId2)); |
80
|
|
|
$this->assertTrue($this->cache->containsEntity(Bar::CLASSNAME, $entityId1)); |
81
|
|
|
$this->assertTrue($this->cache->containsEntity(Bar::CLASSNAME, $entityId2)); |
82
|
|
|
|
83
|
|
|
$this->assertInstanceOf(Attraction::CLASSNAME, $entity1); |
84
|
|
|
$this->assertInstanceOf(Attraction::CLASSNAME, $entity2); |
85
|
|
|
$this->assertInstanceOf(Bar::CLASSNAME, $entity1); |
86
|
|
|
$this->assertInstanceOf(Bar::CLASSNAME, $entity2); |
87
|
|
|
|
88
|
|
|
$this->assertEquals($this->attractions[0]->getId(), $entity1->getId()); |
89
|
|
|
$this->assertEquals($this->attractions[0]->getName(), $entity1->getName()); |
90
|
|
|
|
91
|
|
|
$this->assertEquals($this->attractions[1]->getId(), $entity2->getId()); |
92
|
|
|
$this->assertEquals($this->attractions[1]->getName(), $entity2->getName()); |
93
|
|
|
|
94
|
|
|
$this->_em->clear(); |
95
|
|
|
|
96
|
|
|
$queryCount = $this->getCurrentQueryCount(); |
97
|
|
|
|
98
|
|
|
$entity3 = $this->_em->find(Attraction::CLASSNAME, $entityId1); |
99
|
|
|
$entity4 = $this->_em->find(Attraction::CLASSNAME, $entityId2); |
100
|
|
|
|
101
|
|
|
$this->assertEquals($queryCount, $this->getCurrentQueryCount()); |
102
|
|
|
|
103
|
|
|
$this->assertInstanceOf(Attraction::CLASSNAME, $entity3); |
104
|
|
|
$this->assertInstanceOf(Attraction::CLASSNAME, $entity4); |
105
|
|
|
$this->assertInstanceOf(Bar::CLASSNAME, $entity3); |
106
|
|
|
$this->assertInstanceOf(Bar::CLASSNAME, $entity4); |
107
|
|
|
|
108
|
|
|
$this->assertNotSame($entity1, $entity3); |
109
|
|
|
$this->assertEquals($entity1->getId(), $entity3->getId()); |
110
|
|
|
$this->assertEquals($entity1->getName(), $entity3->getName()); |
111
|
|
|
|
112
|
|
|
$this->assertNotSame($entity2, $entity4); |
113
|
|
|
$this->assertEquals($entity2->getId(), $entity4->getId()); |
114
|
|
|
$this->assertEquals($entity2->getName(), $entity4->getName()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testQueryCacheFindAll() |
118
|
|
|
{ |
119
|
|
|
$this->loadFixturesCountries(); |
120
|
|
|
$this->loadFixturesStates(); |
121
|
|
|
$this->loadFixturesCities(); |
122
|
|
|
$this->loadFixturesAttractions(); |
123
|
|
|
|
124
|
|
|
$this->_em->clear(); |
125
|
|
|
|
126
|
|
|
$queryCount = $this->getCurrentQueryCount(); |
127
|
|
|
$dql = 'SELECT a FROM Doctrine\Tests\Models\Cache\Attraction a'; |
128
|
|
|
$result1 = $this->_em->createQuery($dql) |
129
|
|
|
->setCacheable(true) |
130
|
|
|
->getResult(); |
131
|
|
|
|
132
|
|
|
$this->assertCount(count($this->attractions), $result1); |
133
|
|
|
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount()); |
134
|
|
|
|
135
|
|
|
$this->_em->clear(); |
136
|
|
|
|
137
|
|
|
$result2 = $this->_em->createQuery($dql) |
138
|
|
|
->setCacheable(true) |
139
|
|
|
->getResult(); |
140
|
|
|
|
141
|
|
|
$this->assertCount(count($this->attractions), $result2); |
142
|
|
|
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount()); |
143
|
|
|
|
144
|
|
|
foreach ($result2 as $entity) { |
145
|
|
|
$this->assertInstanceOf(Attraction::CLASSNAME, $entity); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testShouldNotPutOneToManyRelationOnPersist() |
150
|
|
|
{ |
151
|
|
|
$this->loadFixturesCountries(); |
152
|
|
|
$this->loadFixturesStates(); |
153
|
|
|
$this->loadFixturesCities(); |
154
|
|
|
$this->loadFixturesAttractions(); |
155
|
|
|
|
156
|
|
|
$this->_em->clear(); |
157
|
|
|
|
158
|
|
|
foreach ($this->cities as $city) { |
159
|
|
|
$this->assertTrue($this->cache->containsEntity(City::CLASSNAME, $city->getId())); |
160
|
|
|
$this->assertFalse($this->cache->containsCollection(City::CLASSNAME, 'attractions', $city->getId())); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
foreach ($this->attractions as $attraction) { |
164
|
|
|
$this->assertTrue($this->cache->containsEntity(Attraction::CLASSNAME, $attraction->getId())); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testOneToManyRelationSingleTable() |
169
|
|
|
{ |
170
|
|
|
$this->loadFixturesCountries(); |
171
|
|
|
$this->loadFixturesStates(); |
172
|
|
|
$this->loadFixturesCities(); |
173
|
|
|
$this->loadFixturesAttractions(); |
174
|
|
|
|
175
|
|
|
$this->cache->evictEntityRegion(City::CLASSNAME); |
176
|
|
|
$this->cache->evictEntityRegion(Attraction::CLASSNAME); |
177
|
|
|
$this->cache->evictCollectionRegion(City::CLASSNAME, 'attractions'); |
178
|
|
|
|
179
|
|
|
$this->_em->clear(); |
180
|
|
|
|
181
|
|
|
$entity = $this->_em->find(City::CLASSNAME, $this->cities[0]->getId()); |
182
|
|
|
|
183
|
|
|
$this->assertInstanceOf(City::CLASSNAME, $entity); |
184
|
|
|
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $entity->getAttractions()); |
185
|
|
|
$this->assertCount(2, $entity->getAttractions()); |
186
|
|
|
|
187
|
|
|
$ownerId = $this->cities[0]->getId(); |
188
|
|
|
$queryCount = $this->getCurrentQueryCount(); |
189
|
|
|
|
190
|
|
|
$this->assertTrue($this->cache->containsEntity(City::CLASSNAME, $ownerId)); |
191
|
|
|
$this->assertTrue($this->cache->containsCollection(City::CLASSNAME, 'attractions', $ownerId)); |
192
|
|
|
|
193
|
|
|
$this->assertInstanceOf(Bar::CLASSNAME, $entity->getAttractions()->get(0)); |
194
|
|
|
$this->assertInstanceOf(Bar::CLASSNAME, $entity->getAttractions()->get(1)); |
195
|
|
|
$this->assertEquals($this->attractions[0]->getName(), $entity->getAttractions()->get(0)->getName()); |
196
|
|
|
$this->assertEquals($this->attractions[1]->getName(), $entity->getAttractions()->get(1)->getName()); |
197
|
|
|
|
198
|
|
|
$this->_em->clear(); |
199
|
|
|
|
200
|
|
|
$entity = $this->_em->find(City::CLASSNAME, $ownerId); |
201
|
|
|
|
202
|
|
|
$this->assertInstanceOf(City::CLASSNAME, $entity); |
203
|
|
|
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $entity->getAttractions()); |
204
|
|
|
$this->assertCount(2, $entity->getAttractions()); |
205
|
|
|
|
206
|
|
|
$this->assertEquals($queryCount, $this->getCurrentQueryCount()); |
207
|
|
|
|
208
|
|
|
$this->assertInstanceOf(Bar::CLASSNAME, $entity->getAttractions()->get(0)); |
209
|
|
|
$this->assertInstanceOf(Bar::CLASSNAME, $entity->getAttractions()->get(1)); |
210
|
|
|
$this->assertEquals($this->attractions[0]->getName(), $entity->getAttractions()->get(0)->getName()); |
211
|
|
|
$this->assertEquals($this->attractions[1]->getName(), $entity->getAttractions()->get(1)->getName()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testQueryCacheShouldBeEvictedOnTimestampUpdate() |
215
|
|
|
{ |
216
|
|
|
$this->loadFixturesCountries(); |
217
|
|
|
$this->loadFixturesStates(); |
218
|
|
|
$this->loadFixturesCities(); |
219
|
|
|
$this->loadFixturesAttractions(); |
220
|
|
|
$this->_em->clear(); |
221
|
|
|
|
222
|
|
|
$queryCount = $this->getCurrentQueryCount(); |
223
|
|
|
$dql = 'SELECT attraction FROM Doctrine\Tests\Models\Cache\Attraction attraction'; |
224
|
|
|
|
225
|
|
|
$result1 = $this->_em->createQuery($dql) |
226
|
|
|
->setCacheable(true) |
227
|
|
|
->getResult(); |
228
|
|
|
|
229
|
|
|
$this->assertCount(count($this->attractions), $result1); |
230
|
|
|
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount()); |
231
|
|
|
|
232
|
|
|
$contact = new Beach( |
233
|
|
|
'Botafogo', |
234
|
|
|
$this->_em->find(City::class, $this->cities[1]->getId()) |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
$this->_em->persist($contact); |
238
|
|
|
$this->_em->flush(); |
239
|
|
|
$this->_em->clear(); |
240
|
|
|
|
241
|
|
|
$queryCount = $this->getCurrentQueryCount(); |
242
|
|
|
|
243
|
|
|
$result2 = $this->_em->createQuery($dql) |
244
|
|
|
->setCacheable(true) |
245
|
|
|
->getResult(); |
246
|
|
|
|
247
|
|
|
$this->assertCount(count($this->attractions) + 1, $result2); |
248
|
|
|
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount()); |
249
|
|
|
|
250
|
|
|
foreach ($result2 as $entity) { |
251
|
|
|
$this->assertInstanceOf(Attraction::CLASSNAME, $entity); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|