Failed Conditions
Pull Request — develop (#6873)
by
unknown
112:44 queued 47:41
created

testGetShouldIgnoreMissingEntityQueryCacheEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Cache;
6
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\ORM\Cache\DefaultQueryCache;
10
use Doctrine\ORM\Cache\EntityCacheEntry;
11
use Doctrine\ORM\Cache\EntityCacheKey;
12
use Doctrine\ORM\Cache\QueryCache;
13
use Doctrine\ORM\Cache\QueryCacheKey;
14
use Doctrine\ORM\Cache\QueryCacheEntry;
15
use Doctrine\ORM\Cache;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\ORM\Mapping\CacheMetadata;
18
use Doctrine\ORM\Query\ResultSetMappingBuilder;
19
use Doctrine\Tests\Mocks\TimestampRegionMock;
20
use Doctrine\Tests\Mocks\CacheRegionMock;
21
use Doctrine\Tests\Models\Cache\City;
22
use Doctrine\Tests\Models\Cache\Country;
23
use Doctrine\Tests\Models\Cache\State;
24
use Doctrine\Tests\Models\Cache\Restaurant;
25
use Doctrine\Tests\Models\Generic\BooleanModel;
26
use Doctrine\Tests\OrmTestCase;
27
28
/**
29
 * @group DDC-2183
30
 */
31
class DefaultQueryCacheTest extends OrmTestCase
32
{
33
    /**
34
     * @var \Doctrine\ORM\Cache\DefaultQueryCache
35
     */
36
    private $queryCache;
37
38
    /**
39
     * @var \Doctrine\ORM\EntityManagerInterface
40
     */
41
    private $em;
42
43
    /**
44
     * @var \Doctrine\Tests\Mocks\CacheRegionMock
45
     */
46
    private $region;
47
48
    /**
49
     * @var \Doctrine\Tests\ORM\Cache\CacheFactoryDefaultQueryCacheTest
50
     */
51
    private $cacheFactory;
52
53
    protected function setUp()
54
    {
55
        parent::setUp();
56
57
        $this->enableSecondLevelCache();
58
59
        $this->em           = $this->getTestEntityManager();
60
        $this->region       = new CacheRegionMock();
61
        $this->queryCache   = new DefaultQueryCache($this->em, $this->region);
62
        $this->cacheFactory = new CacheFactoryDefaultQueryCacheTest($this->queryCache, $this->region);
63
64
        $this->em->getConfiguration()
65
            ->getSecondLevelCacheConfiguration()
66
            ->setCacheFactory($this->cacheFactory);
67
    }
68
69
    public function testImplementQueryCache()
70
    {
71
        self::assertInstanceOf(QueryCache::class, $this->queryCache);
72
    }
73
74
    public function testGetRegion()
75
    {
76
        self::assertSame($this->region, $this->queryCache->getRegion());
77
    }
78
79
    public function testClearShouldEvictRegion()
80
    {
81
        $this->queryCache->clear();
82
83
        self::assertArrayHasKey('evictAll', $this->region->calls);
84
        self::assertCount(1, $this->region->calls['evictAll']);
85
    }
86
87
    public function testPutBasicQueryResult()
88
    {
89
        $result = [];
90
        $uow    = $this->em->getUnitOfWork();
91
        $key    = new QueryCacheKey('query.key1', 0);
92
        $rsm    = new ResultSetMappingBuilder($this->em);
93
94
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
95
96 View Code Duplication
        for ($i = 0; $i < 4; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            $name   = "Country $i";
98
            $entity = new Country($name);
99
100
            $entity->setId($i);
101
102
            $result[] = $entity;
103
104
            $uow->registerManaged($entity, ['id' => $entity->getId()], ['name' => $entity->getName()]);
105
        }
106
107
        self::assertTrue($this->queryCache->put($key, $rsm, $result));
108
        self::assertArrayHasKey('put', $this->region->calls);
109
        self::assertCount(5, $this->region->calls['put']);
110
111
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][0]['key']);
112
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][1]['key']);
113
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][2]['key']);
114
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][3]['key']);
115
        self::assertInstanceOf(QueryCacheKey::class, $this->region->calls['put'][4]['key']);
116
117
        self::assertInstanceOf(EntityCacheEntry::class, $this->region->calls['put'][0]['entry']);
118
        self::assertInstanceOf(EntityCacheEntry::class, $this->region->calls['put'][1]['entry']);
119
        self::assertInstanceOf(EntityCacheEntry::class, $this->region->calls['put'][2]['entry']);
120
        self::assertInstanceOf(EntityCacheEntry::class, $this->region->calls['put'][3]['entry']);
121
        self::assertInstanceOf(QueryCacheEntry::class, $this->region->calls['put'][4]['entry']);
122
    }
123
124
    public function testPutToOneAssociationQueryResult()
125
    {
126
        $result = [];
127
        $uow    = $this->em->getUnitOfWork();
128
        $key    = new QueryCacheKey('query.key1', 0);
129
        $rsm    = new ResultSetMappingBuilder($this->em);
130
131
        $rsm->addRootEntityFromClassMetadata(City::class, 'c');
132
        $rsm->addJoinedEntityFromClassMetadata(State::class, 's', 'c', 'state', ['id'=>'state_id', 'name'=>'state_name']);
133
134
        for ($i = 0; $i < 4; $i++) {
135
            $state = new State("State $i");
136
            $city  = new City("City $i", $state);
137
138
            $city->setId($i);
139
            $state->setId($i * 2);
140
141
            $result[] = $city;
142
143
            $uow->registerManaged($state, ['id' => $state->getId()], ['name' => $city->getName()]);
144
            $uow->registerManaged($city, ['id' => $city->getId()], ['name' => $city->getName(), 'state' => $state]);
145
        }
146
147
        self::assertTrue($this->queryCache->put($key, $rsm, $result));
148
        self::assertArrayHasKey('put', $this->region->calls);
149
        self::assertCount(9, $this->region->calls['put']);
150
151
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][0]['key']);
152
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][1]['key']);
153
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][2]['key']);
154
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][3]['key']);
155
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][4]['key']);
156
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][5]['key']);
157
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][6]['key']);
158
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][7]['key']);
159
        self::assertInstanceOf(QueryCacheKey::class, $this->region->calls['put'][8]['key']);
160
    }
161
162
    public function testPutToOneAssociation2LevelsQueryResult()
163
    {
164
        $result = [];
165
        $uow    = $this->em->getUnitOfWork();
166
        $key    = new QueryCacheKey('query.key1', 0);
167
        $rsm    = new ResultSetMappingBuilder($this->em);
168
169
        $rsm->addRootEntityFromClassMetadata(City::class, 'c');
170
        $rsm->addJoinedEntityFromClassMetadata(State::class, 's', 'c', 'state', ['id'=>'state_id', 'name'=>'state_name']);
171
        $rsm->addJoinedEntityFromClassMetadata(Country::class, 'co', 's', 'country', ['id'=>'country_id', 'name'=>'country_name']);
172
173
        for ($i = 0; $i < 4; $i++) {
174
            $country  = new Country("Country $i");
175
            $state    = new State("State $i", $country);
176
            $city     = new City("City $i", $state);
177
178
            $city->setId($i);
179
            $state->setId($i * 2);
180
            $country->setId($i * 3);
181
182
            $result[] = $city;
183
184
            $uow->registerManaged($country, ['id' => $country->getId()], ['name' => $country->getName()]);
185
            $uow->registerManaged($state, ['id' => $state->getId()], ['name' => $state->getName(), 'country' => $country]);
186
            $uow->registerManaged($city, ['id' => $city->getId()], ['name' => $city->getName(), 'state' => $state]);
187
        }
188
189
        self::assertTrue($this->queryCache->put($key, $rsm, $result));
190
        self::assertArrayHasKey('put', $this->region->calls);
191
        self::assertCount(13, $this->region->calls['put']);
192
193
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][0]['key']);
194
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][1]['key']);
195
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][2]['key']);
196
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][3]['key']);
197
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][4]['key']);
198
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][5]['key']);
199
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][6]['key']);
200
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][7]['key']);
201
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][8]['key']);
202
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][9]['key']);
203
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][10]['key']);
204
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][11]['key']);
205
        self::assertInstanceOf(QueryCacheKey::class, $this->region->calls['put'][12]['key']);
206
    }
207
208
    public function testPutToOneAssociationNullQueryResult()
209
    {
210
        $result = [];
211
        $uow    = $this->em->getUnitOfWork();
212
        $key    = new QueryCacheKey('query.key1', 0);
213
        $rsm    = new ResultSetMappingBuilder($this->em);
214
215
        $rsm->addRootEntityFromClassMetadata(City::class, 'c');
216
        $rsm->addJoinedEntityFromClassMetadata(State::class, 's', 'c', 'state', ['id'=>'state_id', 'name'=>'state_name']
217
        );
218
219
        for ($i = 0; $i < 4; $i++) {
220
            $city = new City("City $i", null);
221
222
            $city->setId($i);
223
224
            $result[] = $city;
225
226
            $uow->registerManaged($city, ['id' => $city->getId()], ['name' => $city->getName(), 'state' => null]);
227
        }
228
229
        self::assertTrue($this->queryCache->put($key, $rsm, $result));
230
        self::assertArrayHasKey('put', $this->region->calls);
231
        self::assertCount(5, $this->region->calls['put']);
232
233
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][0]['key']);
234
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][1]['key']);
235
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][2]['key']);
236
        self::assertInstanceOf(EntityCacheKey::class, $this->region->calls['put'][3]['key']);
237
        self::assertInstanceOf(QueryCacheKey::class, $this->region->calls['put'][4]['key']);
238
    }
239
240
    public function testPutToManyAssociationQueryResult()
241
    {
242
        $result = [];
243
        $uow    = $this->em->getUnitOfWork();
244
        $key    = new QueryCacheKey('query.key1', 0);
245
        $rsm    = new ResultSetMappingBuilder($this->em);
246
247
        $rsm->addRootEntityFromClassMetadata(State::class, 's');
248
        $rsm->addJoinedEntityFromClassMetadata(City::class, 'c', 's', 'cities', ['id'=>'c_id', 'name'=>'c_name']);
249
250
        for ($i = 0; $i < 4; $i++) {
251
            $state    = new State("State $i");
252
            $city1    = new City("City 1", $state);
253
            $city2    = new City("City 2", $state);
254
255
            $state->setId($i);
256
            $city1->setId($i + 11);
257
            $city2->setId($i + 22);
258
259
            $result[] = $state;
260
261
            $state->addCity($city1);
262
            $state->addCity($city2);
263
264
            $uow->registerManaged($city1, ['id' => $city1->getId()], ['name' => $city1->getName(), 'state' => $state]);
265
            $uow->registerManaged($city2, ['id' => $city2->getId()], ['name' => $city2->getName(), 'state' => $state]);
266
            $uow->registerManaged($state, ['id' => $state->getId()], ['name' => $state->getName(), 'cities' => $state->getCities()]);
267
        }
268
269
        self::assertTrue($this->queryCache->put($key, $rsm, $result));
270
        self::assertArrayHasKey('put', $this->region->calls);
271
        self::assertCount(13, $this->region->calls['put']);
272
    }
273
274 View Code Duplication
    public function testGetBasicQueryResult()
275
    {
276
        $rsm   = new ResultSetMappingBuilder($this->em);
277
        $key   = new QueryCacheKey('query.key1', 0);
278
        $entry = new QueryCacheEntry(
279
            [
280
                ['identifier' => ['id' => 1]],
281
                ['identifier' => ['id' => 2]]
282
            ]
283
        );
284
285
        $data = [
286
            ['id'=>1, 'name' => 'Foo'],
287
            ['id'=>2, 'name' => 'Bar']
288
        ];
289
290
        $this->region->addReturn('get', $entry);
291
292
        $this->region->addReturn(
293
            'getMultiple',
294
            [
295
                new EntityCacheEntry(Country::class, $data[0]),
296
                new EntityCacheEntry(Country::class, $data[1])
297
            ]
298
        );
299
300
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
301
302
        $result = $this->queryCache->get($key, $rsm);
303
304
        self::assertCount(2, $result);
305
        self::assertInstanceOf(Country::class, $result[0]);
306
        self::assertInstanceOf(Country::class, $result[1]);
307
        self::assertEquals(1, $result[0]->getId());
308
        self::assertEquals(2, $result[1]->getId());
309
        self::assertEquals('Foo', $result[0]->getName());
310
        self::assertEquals('Bar', $result[1]->getName());
311
    }
312
313 View Code Duplication
    public function testGetWithAssociation()
314
    {
315
        $rsm   = new ResultSetMappingBuilder($this->em);
316
        $key   = new QueryCacheKey('query.key1', 0);
317
        $entry = new QueryCacheEntry(
318
            [
319
                ['identifier' => ['id' => 1]],
320
                ['identifier' => ['id' => 2]]
321
            ]
322
        );
323
324
        $data = [
325
            ['id'=>1, 'name' => 'Foo'],
326
            ['id'=>2, 'name' => 'Bar']
327
        ];
328
329
        $this->region->addReturn('get', $entry);
330
331
        $this->region->addReturn(
332
            'getMultiple',
333
            [
334
                new EntityCacheEntry(Country::class, $data[0]),
335
                new EntityCacheEntry(Country::class, $data[1])
336
            ]
337
        );
338
339
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
340
341
        $result = $this->queryCache->get($key, $rsm);
342
343
        self::assertCount(2, $result);
344
        self::assertInstanceOf(Country::class, $result[0]);
345
        self::assertInstanceOf(Country::class, $result[1]);
346
        self::assertEquals(1, $result[0]->getId());
347
        self::assertEquals(2, $result[1]->getId());
348
        self::assertEquals('Foo', $result[0]->getName());
349
        self::assertEquals('Bar', $result[1]->getName());
350
    }
351
352
    public function testCancelPutResultIfEntityPutFails()
353
    {
354
        $result = [];
355
        $uow    = $this->em->getUnitOfWork();
356
        $key    = new QueryCacheKey('query.key1', 0);
357
        $rsm    = new ResultSetMappingBuilder($this->em);
358
359
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
360
361 View Code Duplication
        for ($i = 0; $i < 4; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
362
            $name   = "Country $i";
363
            $entity = new Country($name);
364
365
            $entity->setId($i);
366
367
            $result[] = $entity;
368
369
            $uow->registerManaged($entity, ['id' => $entity->getId()], ['name' => $entity->getName()]);
370
        }
371
372
        $this->region->addReturn('put', false);
373
374
        self::assertFalse($this->queryCache->put($key, $rsm, $result));
375
        self::assertArrayHasKey('put', $this->region->calls);
376
        self::assertCount(1, $this->region->calls['put']);
377
    }
378
379
    public function testCancelPutResultIfAssociationEntityPutFails()
380
    {
381
        $result = [];
382
        $uow    = $this->em->getUnitOfWork();
383
        $key    = new QueryCacheKey('query.key1', 0);
384
        $rsm    = new ResultSetMappingBuilder($this->em);
385
386
        $rsm->addRootEntityFromClassMetadata(City::class, 'c');
387
        $rsm->addJoinedEntityFromClassMetadata(State::class, 's', 'c', 'state', ['id'=>'state_id', 'name'=>'state_name']);
388
389
        $state = new State("State 1");
390
        $city  = new City("City 2", $state);
391
392
        $state->setId(1);
393
        $city->setId(11);
394
395
        $result[] = $city;
396
397
        $uow->registerManaged($state, ['id' => $state->getId()], ['name' => $city->getName()]);
398
        $uow->registerManaged($city, ['id' => $city->getId()], ['name' => $city->getName(), 'state' => $state]);
399
400
        $this->region->addReturn('put', true);  // put root entity
401
        $this->region->addReturn('put', false); // association fails
402
403
        self::assertFalse($this->queryCache->put($key, $rsm, $result));
404
    }
405
406
    public function testCancelPutToManyAssociationQueryResult()
407
    {
408
        $result = [];
409
        $uow    = $this->em->getUnitOfWork();
410
        $key    = new QueryCacheKey('query.key1', 0);
411
        $rsm    = new ResultSetMappingBuilder($this->em);
412
413
        $rsm->addRootEntityFromClassMetadata(State::class, 's');
414
        $rsm->addJoinedEntityFromClassMetadata(City::class, 'c', 's', 'cities', ['id'=>'c_id', 'name'=>'c_name']);
415
416
        $state = new State("State");
417
        $city1 = new City("City 1", $state);
418
        $city2 = new City("City 2", $state);
419
420
        $state->setId(1);
421
        $city1->setId(11);
422
        $city2->setId(22);
423
424
        $result[] = $state;
425
426
        $state->addCity($city1);
427
        $state->addCity($city2);
428
429
        $uow->registerManaged($city1, ['id' => $city1->getId()], ['name' => $city1->getName(), 'state' => $state]);
430
        $uow->registerManaged($city2, ['id' => $city2->getId()], ['name' => $city2->getName(), 'state' => $state]);
431
        $uow->registerManaged($state, ['id' => $state->getId()], ['name' => $state->getName(), 'cities' => $state->getCities()]);
432
433
        $this->region->addReturn('put', true);  // put root entity
434
        $this->region->addReturn('put', false); // collection association fails
435
436
        self::assertFalse($this->queryCache->put($key, $rsm, $result));
437
        self::assertArrayHasKey('put', $this->region->calls);
438
        self::assertCount(2, $this->region->calls['put']);
439
    }
440
441 View Code Duplication
    public function testIgnoreCacheNonGetMode()
442
    {
443
        $rsm   = new ResultSetMappingBuilder($this->em);
444
        $key   = new QueryCacheKey('query.key1', 0, Cache::MODE_PUT);
445
        $entry = new QueryCacheEntry(
446
            [
447
                ['identifier' => ['id' => 1]],
448
                ['identifier' => ['id' => 2]]
449
            ]
450
        );
451
452
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
453
454
        $this->region->addReturn('get', $entry);
455
456
        self::assertNull($this->queryCache->get($key, $rsm));
457
    }
458
459
    public function testIgnoreCacheNonPutMode()
460
    {
461
        $result = [];
462
        $uow    = $this->em->getUnitOfWork();
463
        $key    = new QueryCacheKey('query.key1', 0, Cache::MODE_GET);
464
        $rsm    = new ResultSetMappingBuilder($this->em);
465
466
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
467
468 View Code Duplication
        for ($i = 0; $i < 4; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
469
            $name   = "Country $i";
470
            $entity = new Country($name);
471
472
            $entity->setId($i);
473
474
            $result[] = $entity;
475
476
            $uow->registerManaged($entity, ['id' => $entity->getId()], ['name' => $entity->getName()]);
477
        }
478
479
        self::assertFalse($this->queryCache->put($key, $rsm, $result));
480
    }
481
482
    public function testGetShouldIgnoreOldQueryCacheEntryResult()
483
    {
484
        $rsm   = new ResultSetMappingBuilder($this->em);
485
        $key   = new QueryCacheKey('query.key1', 50);
486
        $entry = new QueryCacheEntry(
487
            [
488
                ['identifier' => ['id' => 1]],
489
                ['identifier' => ['id' => 2]]
490
            ]
491
        );
492
493
        $data = [
494
            ['id'=>1, 'name' => 'Foo'],
495
            ['id'=>2, 'name' => 'Bar']
496
        ];
497
498
        $entry->time = microtime(true) - 100;
499
500
        $this->region->addReturn('get', $entry);
501
502
        $this->region->addReturn(
503
            'getMultiple',
504
            [
505
                new EntityCacheEntry(Country::class, $data[0]),
506
                new EntityCacheEntry(Country::class, $data[1])
507
            ]
508
        );
509
510
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
511
512
        self::assertNull($this->queryCache->get($key, $rsm));
513
    }
514
515
    public function testGetShouldIgnoreNonQueryCacheEntryResult()
516
    {
517
        $rsm   = new ResultSetMappingBuilder($this->em);
518
        $key   = new QueryCacheKey('query.key1', 0);
519
        $entry = new \ArrayObject(
520
            [
521
                ['identifier' => ['id' => 1]],
522
                ['identifier' => ['id' => 2]]
523
            ]
524
        );
525
526
        $data = [
527
            ['id'=>1, 'name' => 'Foo'],
528
            ['id'=>2, 'name' => 'Bar']
529
        ];
530
531
        $this->region->addReturn('get', $entry);
532
533
        $this->region->addReturn(
534
            'getMultiple',
535
            [
536
                new EntityCacheEntry(Country::class, $data[0]),
537
                new EntityCacheEntry(Country::class, $data[1])
538
            ]
539
        );
540
541
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
542
543
        self::assertNull($this->queryCache->get($key, $rsm));
544
    }
545
546 View Code Duplication
    public function testGetShouldIgnoreMissingEntityQueryCacheEntry()
547
    {
548
        $rsm   = new ResultSetMappingBuilder($this->em);
549
        $key   = new QueryCacheKey('query.key1', 0);
550
        $entry = new QueryCacheEntry(
551
            [
552
                ['identifier' => ['id' => 1]],
553
                ['identifier' => ['id' => 2]]
554
            ]
555
        );
556
557
        $this->region->addReturn('get', $entry);
558
        $this->region->addReturn('getMultiple', [null]);
559
560
        $rsm->addRootEntityFromClassMetadata(Country::class, 'c');
561
562
        self::assertNull($this->queryCache->get($key, $rsm));
563
    }
564
565
    public function testGetAssociationValue()
566
    {
567
        $reflection = new \ReflectionMethod($this->queryCache, 'getAssociationValue');
568
        $rsm        = new ResultSetMappingBuilder($this->em);
569
        $key        = new QueryCacheKey('query.key1', 0);
0 ignored issues
show
Unused Code introduced by
The assignment to $key is dead and can be removed.
Loading history...
570
571
        $reflection->setAccessible(true);
572
573
        $germany  = new Country("Germany");
574
        $bavaria  = new State("Bavaria", $germany);
575
        $wurzburg = new City("Würzburg", $bavaria);
576
        $munich   = new City("Munich", $bavaria);
577
578
        $bavaria->addCity($munich);
579
        $bavaria->addCity($wurzburg);
580
581
        $munich->addAttraction(new Restaurant('Reinstoff', $munich));
582
        $munich->addAttraction(new Restaurant('Schneider Weisse', $munich));
583
        $wurzburg->addAttraction(new Restaurant('Fischers Fritz', $wurzburg));
584
585
        $rsm->addRootEntityFromClassMetadata(State::class, 's');
586
        $rsm->addJoinedEntityFromClassMetadata(City::class, 'c', 's', 'cities', [
587
            'id'   => 'c_id',
588
            'name' => 'c_name'
589
        ]
590
        );
591
        $rsm->addJoinedEntityFromClassMetadata(Restaurant::class, 'a', 'c', 'attractions', [
592
            'id'   => 'a_id',
593
            'name' => 'a_name'
594
        ]
595
        );
596
597
        $cities      = $reflection->invoke($this->queryCache, $rsm, 'c', $bavaria);
598
        $attractions = $reflection->invoke($this->queryCache, $rsm, 'a', $bavaria);
599
600
        self::assertCount(2, $cities);
601
        self::assertCount(2,  $attractions);
602
603
        self::assertInstanceOf(Collection::class, $cities);
604
        self::assertInstanceOf(Collection::class, $attractions[0]);
605
        self::assertInstanceOf(Collection::class, $attractions[1]);
606
607
        self::assertCount(2, $attractions[0]);
608
        self::assertCount(1, $attractions[1]);
609
    }
610
611
    /**
612
     * @expectedException Doctrine\ORM\Cache\CacheException
613
     * @expectedExceptionMessage Second level cache does not support scalar results.
614
     */
615
    public function testScalarResultException()
616
    {
617
        $result   = [];
618
        $key      = new QueryCacheKey('query.key1', 0);
619
        $rsm      = new ResultSetMappingBuilder($this->em);
620
621
        $rsm->addScalarResult('id', 'u', Type::getType('integer'));
622
623
        $this->queryCache->put($key, $rsm, $result);
624
    }
625
626
    /**
627
     * @expectedException Doctrine\ORM\Cache\CacheException
628
     * @expectedExceptionMessage Second level cache does not support multiple root entities.
629
     */
630
    public function testSupportMultipleRootEntitiesException()
631
    {
632
        $result   = [];
633
        $key      = new QueryCacheKey('query.key1', 0);
634
        $rsm      = new ResultSetMappingBuilder($this->em);
635
636
        $rsm->addEntityResult(City::class, 'e1');
637
        $rsm->addEntityResult(State::class, 'e2');
638
639
        $this->queryCache->put($key, $rsm, $result);
640
    }
641
642
    /**
643
     * @expectedException Doctrine\ORM\Cache\CacheException
644
     * @expectedExceptionMessage Entity "Doctrine\Tests\Models\Generic\BooleanModel" not configured as part of the second-level cache.
645
     */
646
    public function testNotCacheableEntityException()
647
    {
648
        $result    = [];
649
        $key       = new QueryCacheKey('query.key1', 0);
650
        $rsm       = new ResultSetMappingBuilder($this->em);
651
        $rsm->addRootEntityFromClassMetadata(BooleanModel::class, 'c');
652
653
        for ($i = 0; $i < 4; $i++) {
654
            $entity  = new BooleanModel();
655
            $boolean = ($i % 2 === 0);
656
657
            $entity->id             = $i;
658
            $entity->booleanField   = $boolean;
659
            $result[]               = $entity;
660
661
            $this->em->getUnitOfWork()->registerManaged($entity, ['id' => $i], ['booleanField' => $boolean]);
662
        }
663
664
        self::assertFalse($this->queryCache->put($key, $rsm, $result));
665
    }
666
667
}
668
669
class CacheFactoryDefaultQueryCacheTest extends Cache\DefaultCacheFactory
670
{
671
    private $queryCache;
672
    private $region;
673
674
    public function __construct(DefaultQueryCache $queryCache, CacheRegionMock $region)
675
    {
676
        $this->queryCache = $queryCache;
677
        $this->region     = $region;
678
    }
679
680
    public function buildQueryCache(EntityManagerInterface $em, $regionName = null)
681
    {
682
        return $this->queryCache;
683
    }
684
685
    public function getRegion(CacheMetadata $cache)
686
    {
687
        return $this->region;
688
    }
689
690
    public function getTimestampRegion()
691
    {
692
        return new TimestampRegionMock();
693
    }
694
}
695