|
8 | 8 | use Doctrine\DBAL\Cache\QueryCacheProfile;
|
9 | 9 | use Doctrine\DBAL\Connection;
|
10 | 10 | use Doctrine\DBAL\Driver;
|
| 11 | +use PHPUnit\Framework\Attributes\DataProvider; |
11 | 12 | use PHPUnit\Framework\TestCase;
|
| 13 | +use Psr\Cache\CacheItemPoolInterface; |
12 | 14 | use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
13 | 15 |
|
14 | 16 | class CachedQueryTest extends TestCase
|
15 | 17 | {
|
16 |
| - public function testCachedQuery(): void |
| 18 | + #[DataProvider('providePsrCacheImplementations')] |
| 19 | + public function testCachedQuery(callable $psrCacheProvider): void |
17 | 20 | {
|
18 |
| - $cache = new ArrayAdapter(); |
| 21 | + $cache = $psrCacheProvider(); |
19 | 22 |
|
20 | 23 | $connection = $this->createConnection(1, ['foo'], [['bar']]);
|
21 | 24 | $qcp = new QueryCacheProfile(0, __FUNCTION__, $cache);
|
22 | 25 |
|
23 |
| - self::assertSame([['foo' => 'bar']], $connection->executeCacheQuery('SELECT 1', [], [], $qcp) |
| 26 | + $firstResult = $connection->executeCacheQuery('SELECT 1', [], [], $qcp); |
| 27 | + self::assertSame([['foo' => 'bar']], $firstResult |
| 28 | + ->fetchAllAssociative()); |
| 29 | + $firstResult->free(); |
| 30 | + $secondResult = $connection->executeCacheQuery('SELECT 1', [], [], $qcp); |
| 31 | + self::assertSame([['foo' => 'bar']], $secondResult |
24 | 32 | ->fetchAllAssociative());
|
| 33 | + $secondResult->free(); |
25 | 34 | self::assertSame([['foo' => 'bar']], $connection->executeCacheQuery('SELECT 1', [], [], $qcp)
|
26 | 35 | ->fetchAllAssociative());
|
27 | 36 |
|
28 | 37 | self::assertCount(1, $cache->getItem(__FUNCTION__)->get());
|
29 | 38 | }
|
30 | 39 |
|
31 |
| - public function testCachedQueryWithChangedImplementationIsExecutedTwice(): void |
| 40 | + #[DataProvider('providePsrCacheImplementations')] |
| 41 | + public function testCachedQueryWithChangedImplementationIsExecutedTwice(callable $psrCacheProvider): void |
32 | 42 | {
|
33 | 43 | $connection = $this->createConnection(2, ['baz'], [['qux']]);
|
34 | 44 |
|
35 | 45 | self::assertSame([['baz' => 'qux']], $connection->executeCacheQuery(
|
36 | 46 | 'SELECT 1',
|
37 | 47 | [],
|
38 | 48 | [],
|
39 |
| - new QueryCacheProfile(0, __FUNCTION__, new ArrayAdapter()), |
| 49 | + new QueryCacheProfile(0, __FUNCTION__, $psrCacheProvider()), |
40 | 50 | )->fetchAllAssociative());
|
41 | 51 |
|
42 | 52 | self::assertSame([['baz' => 'qux']], $connection->executeCacheQuery(
|
43 | 53 | 'SELECT 1',
|
44 | 54 | [],
|
45 | 55 | [],
|
46 |
| - new QueryCacheProfile(0, __FUNCTION__, new ArrayAdapter()), |
| 56 | + new QueryCacheProfile(0, __FUNCTION__, $psrCacheProvider()), |
47 | 57 | )->fetchAllAssociative());
|
48 | 58 | }
|
49 | 59 |
|
50 |
| - public function testOldCacheFormat(): void |
| 60 | + #[DataProvider('providePsrCacheImplementations')] |
| 61 | + public function testOldCacheFormat(callable $psrCacheProvider): void |
51 | 62 | {
|
52 | 63 | $connection = $this->createConnection(1, ['foo'], [['bar']]);
|
53 |
| - $cache = new ArrayAdapter(); |
| 64 | + $cache = $psrCacheProvider(); |
54 | 65 | $qcp = new QueryCacheProfile(0, __FUNCTION__, $cache);
|
55 | 66 |
|
56 | 67 | [$cacheKey, $realKey] = $qcp->generateCacheKeys('SELECT 1', [], [], []);
|
@@ -83,4 +94,13 @@ private function createConnection(int $expectedQueryCount, array $columnNames, a
|
83 | 94 |
|
84 | 95 | return new Connection([], $driver);
|
85 | 96 | }
|
| 97 | + |
| 98 | + /** @return array<non-empty-string, list<callable():CacheItemPoolInterface>> */ |
| 99 | + public static function providePsrCacheImplementations(): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'serialized' => [static fn () => new ArrayAdapter(0, true)], |
| 103 | + 'by-reference' => [static fn () => new ArrayAdapter(0, false)], |
| 104 | + ]; |
| 105 | + } |
86 | 106 | }
|
0 commit comments