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
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Cache;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\ParameterType;
use PHPUnit\Framework\TestCase;
use function parse_str;
class QueryCacheProfileTest extends TestCase
{
private const LIFETIME = 3600;
private const CACHE_KEY = 'user_specified_cache_key';
private QueryCacheProfile $queryCacheProfile;
private string $query = 'SELECT * FROM foo WHERE bar = ?';
/** @var list<mixed> */
private array $params = [666];
/** @var list<ParameterType::INTEGER> */
private array $types = [ParameterType::INTEGER];
/** @var array<string, mixed> */
private array $connectionParams = [
'dbname' => 'database_name',
'user' => 'database_user',
'password' => 'database_password',
'host' => 'database_host',
'driver' => 'database_driver',
];
protected function setUp(): void
{
$this->queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY);
}
public function testShouldUseTheGivenCacheKeyIfPresent(): void
{
[$cacheKey] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
$this->connectionParams,
);
self::assertEquals(self::CACHE_KEY, $cacheKey, 'The returned cache key should match the given one');
}
public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven(): void
{
$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
[$cacheKey] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
$this->connectionParams,
);
self::assertNotEquals(
self::CACHE_KEY,
$cacheKey,
'The returned cache key should be generated automatically',
);
self::assertNotEmpty($cacheKey, 'The generated cache key should not be empty');
}
public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections(): void
{
$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
[$firstCacheKey] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
$this->connectionParams,
);
$this->connectionParams['host'] = 'a_different_host';
[$secondCacheKey] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
$this->connectionParams,
);
self::assertNotEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be different');
}
public function testConnectionParamsShouldBeHashed(): void
{
$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
[, $queryString] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
$this->connectionParams,
);
$params = [];
parse_str($queryString, $params);
self::assertArrayHasKey('connectionParams', $params);
foreach ($this->connectionParams as $param) {
self::assertIsString($params['connectionParams']);
self::assertStringNotContainsString($param, $params['connectionParams']);
}
}
public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges(): void
{
$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
[$firstCacheKey] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
$this->connectionParams,
);
[$secondCacheKey] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
$this->connectionParams,
);
self::assertEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be the same');
}
public function testShouldGenerateDifferentPasswordInTheParams(): void
{
[, $firstRealCacheKey] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
[
'user' => 'database_user',
'password' => 'first-password',
],
);
[, $secondRealCacheKey] = $this->queryCacheProfile->generateCacheKeys(
$this->query,
$this->params,
$this->types,
[
'user' => 'database_user',
'password' => 'second-password',
],
);
self::assertEquals(
$firstRealCacheKey,
$secondRealCacheKey,
'Cache keys for different password should be the same',
);
}
}
|