File: SecondLevelCacheFunctionalTestCase.php

package info (click to toggle)
doctrine 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,552 kB
  • sloc: php: 108,302; xml: 1,340; makefile: 35; sh: 14
file content (299 lines) | stat: -rw-r--r-- 9,244 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Cache;
use Doctrine\Tests\Models\Cache\Address;
use Doctrine\Tests\Models\Cache\Attraction;
use Doctrine\Tests\Models\Cache\AttractionContactInfo;
use Doctrine\Tests\Models\Cache\AttractionInfo;
use Doctrine\Tests\Models\Cache\AttractionLocationInfo;
use Doctrine\Tests\Models\Cache\Bar;
use Doctrine\Tests\Models\Cache\Beach;
use Doctrine\Tests\Models\Cache\City;
use Doctrine\Tests\Models\Cache\Country;
use Doctrine\Tests\Models\Cache\Person;
use Doctrine\Tests\Models\Cache\Restaurant;
use Doctrine\Tests\Models\Cache\State;
use Doctrine\Tests\Models\Cache\Travel;
use Doctrine\Tests\Models\Cache\Traveler;
use Doctrine\Tests\Models\Cache\TravelerProfile;
use Doctrine\Tests\Models\Cache\TravelerProfileInfo;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

#[Group('DDC-2183')]
abstract class SecondLevelCacheFunctionalTestCase extends OrmFunctionalTestCase
{
    /** @phpstan-var list<Person> */
    protected array $people = [];

    /** @phpstan-var list<Address> */
    protected array $addresses = [];

    /** @phpstan-var list<Country> */
    protected array $countries = [];

    /** @phpstan-var list<State> */
    protected array $states = [];

    /** @phpstan-var list<City> */
    protected array $cities = [];

    /** @phpstan-var list<Travel> */
    protected array $travels = [];

    /** @phpstan-var list<Traveler> */
    protected array $travelers = [];

    /** @phpstan-var list<Attraction> */
    protected array $attractions = [];

    /** @phpstan-var list<AttractionInfo> */
    protected array $attractionsInfo = [];

    /** @phpstan-var list<TravelerProfile> */
    protected array $travelersWithProfile = [];

    protected Cache $cache;

    protected function setUp(): void
    {
        $this->enableSecondLevelCache();

        $this->useModelSet('cache');

        parent::setUp();

        $this->cache = $this->_em->getCache();
    }

    protected function loadFixturesCountries(): void
    {
        $brazil  = new Country('Brazil');
        $germany = new Country('Germany');

        $this->countries[] = $brazil;
        $this->countries[] = $germany;

        $this->_em->persist($brazil);
        $this->_em->persist($germany);
        $this->_em->flush();
    }

    protected function loadFixturesStates(): void
    {
        $saopaulo = new State('São Paulo', $this->countries[0]);
        $rio      = new State('Rio de janeiro', $this->countries[0]);
        $berlin   = new State('Berlin', $this->countries[1]);
        $bavaria  = new State('Bavaria', $this->countries[1]);

        $this->states[] = $saopaulo;
        $this->states[] = $rio;
        $this->states[] = $bavaria;
        $this->states[] = $berlin;

        $this->_em->persist($saopaulo);
        $this->_em->persist($rio);
        $this->_em->persist($bavaria);
        $this->_em->persist($berlin);

        $this->_em->flush();
    }

    protected function loadFixturesCities(): void
    {
        $saopaulo = new City('São Paulo', $this->states[0]);
        $rio      = new City('Rio de janeiro', $this->states[0]);
        $berlin   = new City('Berlin', $this->states[1]);
        $munich   = new City('Munich', $this->states[1]);

        $this->states[0]->addCity($saopaulo);
        $this->states[0]->addCity($rio);
        $this->states[1]->addCity($berlin);
        $this->states[1]->addCity($berlin);

        $this->cities[] = $saopaulo;
        $this->cities[] = $rio;
        $this->cities[] = $munich;
        $this->cities[] = $berlin;

        $this->_em->persist($saopaulo);
        $this->_em->persist($rio);
        $this->_em->persist($munich);
        $this->_em->persist($berlin);

        $this->_em->flush();
    }

    protected function loadFixturesTraveler(): void
    {
        $t1 = new Traveler('Fabio Silva');
        $t2 = new Traveler('Doctrine Bot');

        $this->_em->persist($t1);
        $this->_em->persist($t2);

        $this->travelers[] = $t1;
        $this->travelers[] = $t2;

        $this->_em->flush();
    }

    protected function loadFixturesTravelersWithProfile(): void
    {
        $t1 = new Traveler('Test traveler 1');
        $t2 = new Traveler('Test traveler 2');
        $p1 = new TravelerProfile('First Traveler Profile');
        $p2 = new TravelerProfile('Second Traveler Profile');

        $t1->setProfile($p1);
        $t2->setProfile($p2);

        $this->_em->persist($p1);
        $this->_em->persist($p2);
        $this->_em->persist($t1);
        $this->_em->persist($t2);

        $this->travelersWithProfile[] = $t1;
        $this->travelersWithProfile[] = $t2;

        $this->_em->flush();
    }

    protected function loadFixturesTravelersProfileInfo(): void
    {
        $p1 = $this->travelersWithProfile[0]->getProfile();
        $p2 = $this->travelersWithProfile[1]->getProfile();
        $i1 = new TravelerProfileInfo($p1, 'First Profile Info ...');
        $i2 = new TravelerProfileInfo($p2, 'Second  Profile Info ...');

        $p1->setInfo($i1);
        $p2->setInfo($i2);

        $this->_em->persist($i1);
        $this->_em->persist($i2);
        $this->_em->persist($p1);
        $this->_em->persist($p2);

        $this->_em->flush();
    }

    protected function loadFixturesTravels(): void
    {
        $t1 = new Travel($this->travelers[0]);
        $t2 = new Travel($this->travelers[1]);
        $t3 = new Travel($this->travelers[1]);

        $t1->addVisitedCity($this->cities[0]);
        $t1->addVisitedCity($this->cities[1]);
        $t1->addVisitedCity($this->cities[2]);

        $t2->addVisitedCity($this->cities[1]);
        $t2->addVisitedCity($this->cities[3]);

        $this->_em->persist($t1);
        $this->_em->persist($t2);
        $this->_em->persist($t3);

        $this->travels[] = $t1;
        $this->travels[] = $t2;
        $this->travels[] = $t3;

        $this->_em->flush();
    }

    protected function loadFixturesAttractions(): void
    {
        $this->attractions[] = new Bar('Boteco São Bento', $this->cities[0]);
        $this->attractions[] = new Bar('Prainha Paulista', $this->cities[0]);
        $this->attractions[] = new Beach('Copacabana', $this->cities[1]);
        $this->attractions[] = new Beach('Ipanema', $this->cities[1]);
        $this->attractions[] = new Bar('Schneider Weisse', $this->cities[2]);
        $this->attractions[] = new Restaurant('Reinstoff', $this->cities[3]);
        $this->attractions[] = new Restaurant('Fischers Fritz', $this->cities[3]);

        $this->cities[0]->addAttraction($this->attractions[0]);
        $this->cities[0]->addAttraction($this->attractions[1]);
        $this->cities[1]->addAttraction($this->attractions[2]);
        $this->cities[1]->addAttraction($this->attractions[3]);
        $this->cities[2]->addAttraction($this->attractions[4]);
        $this->cities[3]->addAttraction($this->attractions[5]);
        $this->cities[3]->addAttraction($this->attractions[6]);

        foreach ($this->attractions as $attraction) {
            $this->_em->persist($attraction);
        }

        $this->_em->flush();
    }

    protected function loadFixturesAttractionsInfo(): void
    {
        $this->attractionsInfo[] = new AttractionContactInfo('0000-0000', $this->attractions[0]);
        $this->attractionsInfo[] = new AttractionContactInfo('1111-1111', $this->attractions[1]);
        $this->attractionsInfo[] = new AttractionLocationInfo('Some St 1', $this->attractions[2]);
        $this->attractionsInfo[] = new AttractionLocationInfo('Some St 2', $this->attractions[3]);

        foreach ($this->attractionsInfo as $info) {
            $this->_em->persist($info);
        }

        $this->_em->flush();
    }

    protected function loadFixturesPersonWithAddress(): void
    {
        $person1  = new Person('Guilherme Blanco');
        $address1 = new Address('Canada');

        $person1->address = $address1;
        $address1->person = $person1;

        $person2  = new Person('Marco Pivetta');
        $address2 = new Address('Germany');

        $person2->address = $address2;
        $address2->person = $person2;

        $this->people[] = $person1;
        $this->people[] = $person2;

        $this->addresses[] = $address1;
        $this->addresses[] = $address2;

        foreach ($this->people as $person) {
            $this->_em->persist($person);
        }

        foreach ($this->addresses as $address) {
            $this->_em->persist($address);
        }

        $this->_em->flush();
    }

    protected function getEntityRegion(string $className): string
    {
        return $this->cache->getEntityCacheRegion($className)->getName();
    }

    protected function getCollectionRegion(string $className, string $association): string
    {
        return $this->cache->getCollectionCacheRegion($className, $association)->getName();
    }

    protected function getDefaultQueryRegionName(): string
    {
        return $this->cache->getQueryCache()->getRegion()->getName();
    }

    protected function evictRegions(): void
    {
        $this->cache->evictQueryRegions();
        $this->cache->evictEntityRegions();
        $this->cache->evictCollectionRegions();
    }
}