Skip to content

Commit 316ba5f

Browse files
authored
Restore functional cache tests (#8981)
1 parent a08b630 commit 316ba5f

File tree

6 files changed

+172
-218
lines changed

6 files changed

+172
-218
lines changed

tests/Doctrine/Tests/ORM/Cache/AbstractRegionTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\Tests\ORM\Cache;
66

7+
use Doctrine\Common\Cache\Cache;
78
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
89
use Doctrine\ORM\Cache\Region;
910
use Doctrine\Tests\Mocks\CacheEntryMock;
@@ -19,7 +20,7 @@ abstract class AbstractRegionTest extends OrmFunctionalTestCase
1920
/** @var Region */
2021
protected $region;
2122

22-
/** @var ArrayCache */
23+
/** @var Cache */
2324
protected $cache;
2425

2526
protected function setUp(): void

tests/Doctrine/Tests/ORM/Cache/DefaultRegionTest.php

+69-78
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
use BadMethodCallException;
88
use Doctrine\Common\Cache\Cache;
9-
use Doctrine\Common\Cache\CacheProvider;
109
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
1110
use Doctrine\ORM\Cache\CollectionCacheEntry;
1211
use Doctrine\ORM\Cache\Region;
1312
use Doctrine\ORM\Cache\Region\DefaultRegion;
1413
use Doctrine\Tests\Mocks\CacheEntryMock;
1514
use Doctrine\Tests\Mocks\CacheKeyMock;
15+
use Psr\Cache\CacheItemInterface;
16+
use Psr\Cache\CacheItemPoolInterface;
1617
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1718

1819
use function assert;
19-
use function class_exists;
2020

2121
/**
2222
* @group DDC-2183
@@ -36,15 +36,11 @@ public function testGetters(): void
3636

3737
public function testSharedRegion(): void
3838
{
39-
if (! class_exists(ArrayCache::class)) {
40-
$this->markTestSkipped('Test only applies with doctrine/cache 1.x');
41-
}
42-
4339
$cache = new SharedArrayCache();
4440
$key = new CacheKeyMock('key');
4541
$entry = new CacheEntryMock(['value' => 'foo']);
46-
$region1 = new DefaultRegion('region1', $cache->createChild());
47-
$region2 = new DefaultRegion('region2', $cache->createChild());
42+
$region1 = new DefaultRegion('region1', DoctrineProvider::wrap($cache->createChild()));
43+
$region2 = new DefaultRegion('region2', DoctrineProvider::wrap($cache->createChild()));
4844

4945
$this->assertFalse($region1->contains($key));
5046
$this->assertFalse($region2->contains($key));
@@ -135,76 +131,71 @@ public function corruptedDataDoesNotLeakIntoApplicationWhenGettingMultipleEntrie
135131
}
136132
}
137133

138-
if (class_exists(ArrayCache::class)) {
139-
/**
140-
* Cache provider that offers child cache items (sharing the same array)
141-
*
142-
* Declared as a different class for readability purposes and kept in this file
143-
* to keep its monstrosity contained.
144-
*
145-
* @internal
146-
*/
147-
final class SharedArrayCache extends ArrayCache
134+
/**
135+
* Cache provider that offers child cache items (sharing the same array)
136+
*
137+
* Declared as a different class for readability purposes and kept in this file
138+
* to keep its monstrosity contained.
139+
*
140+
* @internal
141+
*/
142+
final class SharedArrayCache extends ArrayAdapter
143+
{
144+
public function createChild(): CacheItemPoolInterface
148145
{
149-
public function createChild(): Cache
150-
{
151-
return new class ($this) extends CacheProvider {
152-
/** @var ArrayCache */
153-
private $parent;
154-
155-
public function __construct(ArrayCache $parent)
156-
{
157-
$this->parent = $parent;
158-
}
159-
160-
/**
161-
* {@inheritDoc}
162-
*/
163-
protected function doFetch($id)
164-
{
165-
return $this->parent->doFetch($id);
166-
}
167-
168-
/**
169-
* {@inheritDoc}
170-
*/
171-
protected function doContains($id)
172-
{
173-
return $this->parent->doContains($id);
174-
}
175-
176-
/**
177-
* {@inheritDoc}
178-
*/
179-
protected function doSave($id, $data, $lifeTime = 0)
180-
{
181-
return $this->parent->doSave($id, $data, $lifeTime);
182-
}
183-
184-
/**
185-
* {@inheritDoc}
186-
*/
187-
protected function doDelete($id)
188-
{
189-
return $this->parent->doDelete($id);
190-
}
191-
192-
/**
193-
* {@inheritDoc}
194-
*/
195-
protected function doFlush()
196-
{
197-
return $this->parent->doFlush();
198-
}
199-
200-
/**
201-
* {@inheritDoc}
202-
*/
203-
protected function doGetStats()
204-
{
205-
return $this->parent->doGetStats();
206-
}
207-
};
208-
}
146+
return new class ($this) implements CacheItemPoolInterface {
147+
/** @var CacheItemPoolInterface */
148+
private $parent;
149+
150+
public function __construct(CacheItemPoolInterface $parent)
151+
{
152+
$this->parent = $parent;
153+
}
154+
155+
public function getItem($key): CacheItemInterface
156+
{
157+
return $this->parent->getItem($key);
158+
}
159+
160+
public function getItems(array $keys = []): iterable
161+
{
162+
return $this->parent->getItems($keys);
163+
}
164+
165+
public function hasItem($key): bool
166+
{
167+
return $this->parent->hasItem($key);
168+
}
169+
170+
public function clear(): bool
171+
{
172+
return $this->parent->clear();
173+
}
174+
175+
public function deleteItem($key): bool
176+
{
177+
return $this->parent->deleteItem($key);
178+
}
179+
180+
public function deleteItems(array $keys): bool
181+
{
182+
return $this->parent->deleteItems($keys);
183+
}
184+
185+
public function save(CacheItemInterface $item): bool
186+
{
187+
return $this->parent->save($item);
188+
}
189+
190+
public function saveDeferred(CacheItemInterface $item): bool
191+
{
192+
return $this->parent->saveDeferred($item);
193+
}
194+
195+
public function commit(): bool
196+
{
197+
return $this->parent->commit();
198+
}
199+
};
209200
}
210201
}

tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php

+33-48
Original file line numberDiff line numberDiff line change
@@ -4,113 +4,98 @@
44

55
namespace Doctrine\Tests\ORM\Functional;
66

7-
use Doctrine\Common\Cache\ArrayCache;
87
use Doctrine\Common\Cache\Cache;
98
use Doctrine\Common\Cache\CacheProvider;
9+
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
1010
use Doctrine\ORM\Query;
1111
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
1212
use Doctrine\ORM\Query\ParserResult;
1313
use Doctrine\Tests\OrmFunctionalTestCase;
14-
use ReflectionProperty;
14+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1515

16-
use function class_exists;
16+
use function assert;
1717
use function count;
1818

19-
/**
20-
* QueryCacheTest
21-
*/
2219
class QueryCacheTest extends OrmFunctionalTestCase
2320
{
24-
/** @var ReflectionProperty */
25-
private $cacheDataReflection;
26-
2721
protected function setUp(): void
2822
{
29-
if (! class_exists(ArrayCache::class)) {
30-
$this->markTestSkipped('Test only applies with doctrine/cache 1.x');
31-
}
32-
33-
$this->cacheDataReflection = new ReflectionProperty(ArrayCache::class, 'data');
34-
$this->cacheDataReflection->setAccessible(true);
35-
3623
$this->useModelSet('cms');
3724

3825
parent::setUp();
3926
}
4027

41-
private function getCacheSize(ArrayCache $cache): int
42-
{
43-
return count($this->cacheDataReflection->getValue($cache));
44-
}
45-
46-
public function testQueryCacheDependsOnHints(): Query
28+
public function testQueryCacheDependsOnHints(): array
4729
{
4830
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
4931

50-
$cache = new ArrayCache();
51-
$query->setQueryCacheDriver($cache);
32+
$cache = new ArrayAdapter();
33+
$query->setQueryCacheDriver(DoctrineProvider::wrap($cache));
5234

5335
$query->getResult();
54-
$this->assertEquals(1, $this->getCacheSize($cache));
36+
self::assertCount(2, $cache->getValues());
5537

5638
$query->setHint('foo', 'bar');
5739

5840
$query->getResult();
59-
$this->assertEquals(2, $this->getCacheSize($cache));
41+
self::assertCount(3, $cache->getValues());
6042

61-
return $query;
43+
return [$query, $cache];
6244
}
6345

6446
/**
65-
* @param <type> $query
66-
*
6747
* @depends testQueryCacheDependsOnHints
6848
*/
69-
public function testQueryCacheDependsOnFirstResult($query): void
49+
public function testQueryCacheDependsOnFirstResult(array $previous): void
7050
{
71-
$cache = $query->getQueryCacheDriver();
72-
$cacheCount = $this->getCacheSize($cache);
51+
[$query, $cache] = $previous;
52+
assert($query instanceof Query);
53+
assert($cache instanceof ArrayAdapter);
54+
55+
$cacheCount = count($cache->getValues());
7356

7457
$query->setFirstResult(10);
7558
$query->setMaxResults(9999);
7659

7760
$query->getResult();
78-
$this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
61+
self::assertCount($cacheCount + 1, $cache->getValues());
7962
}
8063

8164
/**
82-
* @param <type> $query
83-
*
8465
* @depends testQueryCacheDependsOnHints
8566
*/
86-
public function testQueryCacheDependsOnMaxResults($query): void
67+
public function testQueryCacheDependsOnMaxResults(array $previous): void
8768
{
88-
$cache = $query->getQueryCacheDriver();
89-
$cacheCount = $this->getCacheSize($cache);
69+
[$query, $cache] = $previous;
70+
assert($query instanceof Query);
71+
assert($cache instanceof ArrayAdapter);
72+
73+
$cacheCount = count($cache->getValues());
9074

9175
$query->setMaxResults(10);
9276

9377
$query->getResult();
94-
$this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
78+
self::assertCount($cacheCount + 1, $cache->getValues());
9579
}
9680

9781
/**
98-
* @param <type> $query
99-
*
10082
* @depends testQueryCacheDependsOnHints
10183
*/
102-
public function testQueryCacheDependsOnHydrationMode($query): void
84+
public function testQueryCacheDependsOnHydrationMode(array $previous): void
10385
{
104-
$cache = $query->getQueryCacheDriver();
105-
$cacheCount = $this->getCacheSize($cache);
86+
[$query, $cache] = $previous;
87+
assert($query instanceof Query);
88+
assert($cache instanceof ArrayAdapter);
89+
90+
$cacheCount = count($cache->getValues());
10691

10792
$query->getArrayResult();
108-
$this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
93+
self::assertCount($cacheCount + 1, $cache->getValues());
10994
}
11095

11196
public function testQueryCacheNoHitSaveParserResult(): void
11297
{
113-
$this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
98+
$this->_em->getConfiguration()->setQueryCacheImpl($this->createMock(Cache::class));
11499

115100
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
116101

@@ -128,7 +113,7 @@ public function testQueryCacheNoHitSaveParserResult(): void
128113

129114
public function testQueryCacheHitDoesNotSaveParserResult(): void
130115
{
131-
$this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
116+
$this->_em->getConfiguration()->setQueryCacheImpl($this->createMock(Cache::class));
132117

133118
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
134119

@@ -167,6 +152,6 @@ public function testQueryCacheHitDoesNotSaveParserResult(): void
167152

168153
$query->setQueryCacheDriver($cache);
169154

170-
$users = $query->getResult();
155+
$query->getResult();
171156
}
172157
}

0 commit comments

Comments
 (0)