Failed Conditions
Push — 2.7 ( c036c0...266f0d )
by Jonathan
57:23 queued 50:07
created

ORM/Functional/SecondLevelCacheManyToOneTest.php (1 issue)

1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional;
4
5
use Doctrine\ORM\Cache\Region;
6
use Doctrine\Tests\Models\Cache\Action;
7
use Doctrine\Tests\Models\Cache\City;
8
use Doctrine\Tests\Models\Cache\ComplexAction;
9
use Doctrine\Tests\Models\Cache\Country;
10
use Doctrine\Tests\Models\Cache\State;
11
use Doctrine\Tests\Models\Cache\Token;
12
13
/**
14
 * @group DDC-2183
15
 */
16
class SecondLevelCacheManyToOneTest extends SecondLevelCacheAbstractTest
17
{
18
    public function testPutOnPersist()
19
    {
20
        $this->loadFixturesCountries();
21
        $this->loadFixturesStates();
22
        $this->_em->clear();
23
24
        $this->assertTrue($this->cache->containsEntity(Country::class, $this->states[0]->getCountry()->getId()));
25
        $this->assertTrue($this->cache->containsEntity(Country::class, $this->states[1]->getCountry()->getId()));
26
        $this->assertTrue($this->cache->containsEntity(State::class, $this->states[0]->getId()));
27
        $this->assertTrue($this->cache->containsEntity(State::class, $this->states[1]->getId()));
28
    }
29
30
    public function testPutAndLoadManyToOneRelation()
31
    {
32
        $this->loadFixturesCountries();
33
        $this->loadFixturesStates();
34
        $this->_em->clear();
35
36
        $this->cache->evictEntityRegion(State::class);
37
        $this->cache->evictEntityRegion(Country::class);
38
39
        $this->assertFalse($this->cache->containsEntity(State::class, $this->states[0]->getId()));
40
        $this->assertFalse($this->cache->containsEntity(State::class, $this->states[1]->getId()));
41
        $this->assertFalse($this->cache->containsEntity(Country::class, $this->states[0]->getCountry()->getId()));
42
        $this->assertFalse($this->cache->containsEntity(Country::class, $this->states[1]->getCountry()->getId()));
43
44
        $c1 = $this->_em->find(State::class, $this->states[0]->getId());
45
        $c2 = $this->_em->find(State::class, $this->states[1]->getId());
46
47
        //trigger lazy load
48
        $this->assertNotNull($c1->getCountry()->getName());
49
        $this->assertNotNull($c2->getCountry()->getName());
50
51
        $this->assertTrue($this->cache->containsEntity(Country::class, $this->states[0]->getCountry()->getId()));
52
        $this->assertTrue($this->cache->containsEntity(Country::class, $this->states[1]->getCountry()->getId()));
53
        $this->assertTrue($this->cache->containsEntity(State::class, $this->states[0]->getId()));
54
        $this->assertTrue($this->cache->containsEntity(State::class, $this->states[1]->getId()));
55
56
        $this->assertInstanceOf(State::class, $c1);
57
        $this->assertInstanceOf(State::class, $c2);
58
        $this->assertInstanceOf(Country::class, $c1->getCountry());
59
        $this->assertInstanceOf(Country::class, $c2->getCountry());
60
61
        $this->assertEquals($this->states[0]->getId(), $c1->getId());
62
        $this->assertEquals($this->states[0]->getName(), $c1->getName());
63
        $this->assertEquals($this->states[0]->getCountry()->getId(), $c1->getCountry()->getId());
64
        $this->assertEquals($this->states[0]->getCountry()->getName(), $c1->getCountry()->getName());
65
66
        $this->assertEquals($this->states[1]->getId(), $c2->getId());
67
        $this->assertEquals($this->states[1]->getName(), $c2->getName());
68
        $this->assertEquals($this->states[1]->getCountry()->getId(), $c2->getCountry()->getId());
69
        $this->assertEquals($this->states[1]->getCountry()->getName(), $c2->getCountry()->getName());
70
71
        $this->_em->clear();
72
73
        $queryCount = $this->getCurrentQueryCount();
74
75
        $c3 = $this->_em->find(State::class, $this->states[0]->getId());
76
        $c4 = $this->_em->find(State::class, $this->states[1]->getId());
77
78
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
79
80
        //trigger lazy load from cache
81
        $this->assertNotNull($c3->getCountry()->getName());
82
        $this->assertNotNull($c4->getCountry()->getName());
83
84
        $this->assertInstanceOf(State::class, $c3);
85
        $this->assertInstanceOf(State::class, $c4);
86
        $this->assertInstanceOf(Country::class, $c3->getCountry());
87
        $this->assertInstanceOf(Country::class, $c4->getCountry());
88
89
        $this->assertEquals($c1->getId(), $c3->getId());
90
        $this->assertEquals($c1->getName(), $c3->getName());
91
92
        $this->assertEquals($c2->getId(), $c4->getId());
93
        $this->assertEquals($c2->getName(), $c4->getName());
94
95
        $this->assertEquals($this->states[0]->getCountry()->getId(), $c3->getCountry()->getId());
96
        $this->assertEquals($this->states[0]->getCountry()->getName(), $c3->getCountry()->getName());
97
98
        $this->assertEquals($this->states[1]->getCountry()->getId(), $c4->getCountry()->getId());
99
        $this->assertEquals($this->states[1]->getCountry()->getName(), $c4->getCountry()->getName());
100
    }
101
102
    public function testInverseSidePutShouldEvictCollection()
103
    {
104
        $this->loadFixturesCountries();
105
        $this->loadFixturesStates();
106
107
        $this->_em->clear();
108
109
        $this->cache->evictEntityRegion(State::class);
110
        $this->cache->evictEntityRegion(Country::class);
111
112
        //evict collection on add
113
        $c3    = $this->_em->find(State::class, $this->states[0]->getId());
114
        $prev  = $c3->getCities();
115
        $count = $prev->count();
116
        $city  = new City("Buenos Aires", $c3);
117
118
        $c3->addCity($city);
119
120
        $this->_em->persist($city);
121
        $this->_em->persist($c3);
122
        $this->_em->flush();
123
        $this->_em->clear();
124
125
        $state      = $this->_em->find(State::class, $c3->getId());
126
        $queryCount = $this->getCurrentQueryCount();
127
128
        // Association was cleared from EM
129
        $this->assertNotEquals($prev, $state->getCities());
130
131
        // New association has one more item (cache was evicted)
132
        $this->assertEquals($count + 1, $state->getCities()->count());
133
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
134
    }
135
136
    public function testShouldNotReloadWhenAssociationIsMissing()
137
    {
138
        $this->loadFixturesCountries();
139
        $this->loadFixturesStates();
140
        $this->_em->clear();
141
142
        $stateId1 = $this->states[0]->getId();
143
        $stateId2 = $this->states[3]->getId();
144
145
        $countryId1 = $this->states[0]->getCountry()->getId();
146
        $countryId2 = $this->states[3]->getCountry()->getId();
147
148
        $this->assertTrue($this->cache->containsEntity(Country::class, $countryId1));
149
        $this->assertTrue($this->cache->containsEntity(Country::class, $countryId2));
150
        $this->assertTrue($this->cache->containsEntity(State::class, $stateId1));
151
        $this->assertTrue($this->cache->containsEntity(State::class, $stateId2));
152
153
        $this->cache->evictEntityRegion(Country::class);
154
155
        $this->assertFalse($this->cache->containsEntity(Country::class, $countryId1));
156
        $this->assertFalse($this->cache->containsEntity(Country::class, $countryId2));
157
158
        $this->_em->clear();
159
160
        $queryCount = $this->getCurrentQueryCount();
161
162
        $state1 = $this->_em->find(State::class, $stateId1);
163
        $state2 = $this->_em->find(State::class, $stateId2);
164
165
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
166
167
        $this->assertInstanceOf(State::class, $state1);
168
        $this->assertInstanceOf(State::class, $state2);
169
        $this->assertInstanceOf(Country::class, $state1->getCountry());
170
        $this->assertInstanceOf(Country::class, $state2->getCountry());
171
172
        $queryCount = $this->getCurrentQueryCount();
173
174
        $this->assertNotNull($state1->getCountry()->getName());
175
        $this->assertNotNull($state2->getCountry()->getName());
176
        $this->assertEquals($countryId1, $state1->getCountry()->getId());
177
        $this->assertEquals($countryId2, $state2->getCountry()->getId());
178
179
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount());
180
    }
181
182
    public function testPutAndLoadNonCacheableManyToOne()
183
    {
184
        $this->assertNull($this->cache->getEntityCacheRegion(Action::class));
185
        $this->assertInstanceOf(Region::class, $this->cache->getEntityCacheRegion(Token::class));
186
187
        $token  = new Token('token-hash');
188
        $action = new Action('exec');
189
        $action->addToken($token);
190
191
        $this->_em->persist($token);
192
193
        $this->_em->flush();
194
        $this->_em->clear();
195
196
        $this->assertTrue($this->cache->containsEntity(Token::class, $token->token));
197
        $this->assertFalse($this->cache->containsEntity(Token::class, $action->name));
198
199
        $queryCount = $this->getCurrentQueryCount();
200
        $entity = $this->_em->find(Token::class, $token->token);
201
202
        $this->assertInstanceOf(Token::class, $entity);
203
        $this->assertEquals('token-hash', $entity->token);
204
205
        $this->assertInstanceOf(Action::class, $entity->getAction());
206
        $this->assertEquals('exec', $entity->getAction()->name);
207
208
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
209
    }
210
211
    public function testPutAndLoadNonCacheableCompositeManyToOne()
212
    {
213
        $this->assertNull($this->cache->getEntityCacheRegion(Action::class));
214
        $this->assertNull($this->cache->getEntityCacheRegion(ComplexAction::class));
215
        $this->assertInstanceOf(Region::class, $this->cache->getEntityCacheRegion(Token::class));
216
217
        $token  = new Token('token-hash');
218
219
        $action1 = new Action('login');
220
        $action2 = new Action('logout');
221
        $action3 = new Action('rememberme');
222
223
        $complexAction = new ComplexAction($action1, $action3, 'login,rememberme');
224
225
        $complexAction->addToken($token);
226
227
        $token->action = $action2;
0 ignored issues
show
Documentation Bug introduced by
It seems like $action2 of type Doctrine\Tests\Models\Cache\Action is incompatible with the declared type array of property $action.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
228
229
        $this->_em->persist($token);
230
231
        $this->_em->flush();
232
        $this->_em->clear();
233
234
        $this->assertTrue($this->cache->containsEntity(Token::class, $token->token));
235
        $this->assertFalse($this->cache->containsEntity(Action::class, $action1->name));
236
        $this->assertFalse($this->cache->containsEntity(Action::class, $action2->name));
237
        $this->assertFalse($this->cache->containsEntity(Action::class, $action3->name));
238
239
        $queryCount = $this->getCurrentQueryCount();
240
        /**
241
         * @var $entity Token
242
         */
243
        $entity = $this->_em->find(Token::class, $token->token);
244
245
        $this->assertInstanceOf(Token::class, $entity);
246
        $this->assertEquals('token-hash', $entity->token);
247
248
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
249
250
        $this->assertInstanceOf(Action::class, $entity->getAction());
251
        $this->assertInstanceOf(ComplexAction::class, $entity->getComplexAction());
252
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
253
254
        $this->assertInstanceOf(Action::class, $entity->getComplexAction()->getAction1());
255
        $this->assertInstanceOf(Action::class, $entity->getComplexAction()->getAction2());
256
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
257
258
        $this->assertEquals('login', $entity->getComplexAction()->getAction1()->name);
259
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
260
        $this->assertEquals('rememberme', $entity->getComplexAction()->getAction2()->name);
261
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
262
    }
263
}
264