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
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Connection;
use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
class CachedQueryTest extends TestCase
{
#[DataProvider('providePsrCacheImplementations')]
public function testCachedQuery(callable $psrCacheProvider): void
{
$cache = $psrCacheProvider();
$connection = $this->createConnection(1, ['foo'], [['bar']]);
$qcp = new QueryCacheProfile(0, __FUNCTION__, $cache);
$firstResult = $connection->executeCacheQuery('SELECT 1', [], [], $qcp);
self::assertSame([['foo' => 'bar']], $firstResult
->fetchAllAssociative());
$firstResult->free();
$secondResult = $connection->executeCacheQuery('SELECT 1', [], [], $qcp);
self::assertSame([['foo' => 'bar']], $secondResult
->fetchAllAssociative());
$secondResult->free();
self::assertSame([['foo' => 'bar']], $connection->executeCacheQuery('SELECT 1', [], [], $qcp)
->fetchAllAssociative());
self::assertCount(1, $cache->getItem(__FUNCTION__)->get());
}
#[DataProvider('providePsrCacheImplementations')]
public function testCachedQueryWithChangedImplementationIsExecutedTwice(callable $psrCacheProvider): void
{
$connection = $this->createConnection(2, ['baz'], [['qux']]);
self::assertSame([['baz' => 'qux']], $connection->executeCacheQuery(
'SELECT 1',
[],
[],
new QueryCacheProfile(0, __FUNCTION__, $psrCacheProvider()),
)->fetchAllAssociative());
self::assertSame([['baz' => 'qux']], $connection->executeCacheQuery(
'SELECT 1',
[],
[],
new QueryCacheProfile(0, __FUNCTION__, $psrCacheProvider()),
)->fetchAllAssociative());
}
#[DataProvider('providePsrCacheImplementations')]
public function testOldCacheFormat(callable $psrCacheProvider): void
{
$connection = $this->createConnection(1, ['foo'], [['bar']]);
$cache = $psrCacheProvider();
$qcp = new QueryCacheProfile(0, __FUNCTION__, $cache);
[$cacheKey, $realKey] = $qcp->generateCacheKeys('SELECT 1', [], [], []);
$cache->save(
$cache->getItem($cacheKey)->set([$realKey => [['foo' => 'bar']]]),
);
self::assertSame([['foo' => 'bar']], $connection->executeCacheQuery('SELECT 1', [], [], $qcp)
->fetchAllAssociative());
self::assertSame([['foo' => 'bar']], $connection->executeCacheQuery('SELECT 1', [], [], $qcp)
->fetchAllAssociative());
self::assertCount(1, $cache->getItem(__FUNCTION__)->get());
}
/**
* @param list<string> $columnNames
* @param list<list<mixed>> $rows
*/
private function createConnection(int $expectedQueryCount, array $columnNames, array $rows): Connection
{
$connection = $this->createMock(Driver\Connection::class);
$connection->expects(self::exactly($expectedQueryCount))
->method('query')
->willReturnCallback(static fn (): ArrayResult => new ArrayResult($columnNames, $rows));
$driver = $this->createMock(Driver::class);
$driver->method('connect')
->willReturn($connection);
return new Connection([], $driver);
}
/** @return array<non-empty-string, list<callable():CacheItemPoolInterface>> */
public static function providePsrCacheImplementations(): array
{
return [
'serialized' => [static fn () => new ArrayAdapter(0, true)],
'by-reference' => [static fn () => new ArrayAdapter(0, false)],
];
}
}
|