QueryCacheProfileTest.php 3.98 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Cache;
4 5

use Doctrine\DBAL\Cache\QueryCacheProfile;
6
use Doctrine\DBAL\ParameterType;
7
use PHPUnit\Framework\TestCase;
8

9
use function parse_str;
10

11
class QueryCacheProfileTest extends TestCase
12
{
13 14
    private const LIFETIME  = 3600;
    private const CACHE_KEY = 'user_specified_cache_key';
15

Sergei Morozov's avatar
Sergei Morozov committed
16
    /** @var QueryCacheProfile */
17 18
    private $queryCacheProfile;

Sergei Morozov's avatar
Sergei Morozov committed
19
    /** @var string */
20 21
    private $query = 'SELECT * FROM foo WHERE bar = ?';

Sergei Morozov's avatar
Sergei Morozov committed
22
    /** @var int[] */
23 24
    private $params = [666];

Grégoire Paris's avatar
Grégoire Paris committed
25
    /** @var int[] */
26 27
    private $types = [ParameterType::INTEGER];

Sergei Morozov's avatar
Sergei Morozov committed
28
    /** @var string[] */
29
    private $connectionParams = [
Sergei Morozov's avatar
Sergei Morozov committed
30 31 32 33 34 35
        'dbname'   => 'database_name',
        'user'     => 'database_user',
        'password' => 'database_password',
        'host'     => 'database_host',
        'driver'   => 'database_driver',
    ];
36

37
    protected function setUp(): void
38 39 40 41
    {
        $this->queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY);
    }

42
    public function testShouldUseTheGivenCacheKeyIfPresent(): void
43
    {
Sergei Morozov's avatar
Sergei Morozov committed
44
        [$cacheKey] = $this->queryCacheProfile->generateCacheKeys(
45 46 47 48
            $this->query,
            $this->params,
            $this->types,
            $this->connectionParams
49 50
        );

51
        self::assertEquals(self::CACHE_KEY, $cacheKey, 'The returned cache key should match the given one');
52 53
    }

54
    public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven(): void
55 56
    {
        $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
57

Sergei Morozov's avatar
Sergei Morozov committed
58
        [$cacheKey] = $this->queryCacheProfile->generateCacheKeys(
59 60 61 62
            $this->query,
            $this->params,
            $this->types,
            $this->connectionParams
63
        );
64

65
        self::assertNotEquals(
Antonio Vilar's avatar
Antonio Vilar committed
66
            self::CACHE_KEY,
67
            $cacheKey,
Antonio Vilar's avatar
Antonio Vilar committed
68 69 70
            'The returned cache key should be generated automatically'
        );

71
        self::assertNotEmpty($cacheKey, 'The generated cache key should not be empty');
72 73
    }

74
    public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections(): void
75
    {
76 77
        $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);

Sergei Morozov's avatar
Sergei Morozov committed
78
        [$firstCacheKey] = $this->queryCacheProfile->generateCacheKeys(
79 80 81 82
            $this->query,
            $this->params,
            $this->types,
            $this->connectionParams
83 84
        );

85
        $this->connectionParams['host'] = 'a_different_host';
86

Sergei Morozov's avatar
Sergei Morozov committed
87
        [$secondCacheKey] = $this->queryCacheProfile->generateCacheKeys(
88 89 90 91
            $this->query,
            $this->params,
            $this->types,
            $this->connectionParams
92 93
        );

94
        self::assertNotEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be different');
95 96
    }

97
    public function testConnectionParamsShouldBeHashed(): void
98
    {
99
        $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
100

Sergei Morozov's avatar
Sergei Morozov committed
101
        [$cacheKey, $queryString] = $this->queryCacheProfile->generateCacheKeys(
102 103 104 105
            $this->query,
            $this->params,
            $this->types,
            $this->connectionParams
106
        );
107

108 109 110 111 112 113
        $params = [];
        parse_str($queryString, $params);

        self::assertArrayHasKey('connectionParams', $params);

        foreach ($this->connectionParams as $param) {
114
            self::assertStringNotContainsString($param, $params['connectionParams']);
115 116 117
        }
    }

118
    public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges(): void
119
    {
120 121
        $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);

Sergei Morozov's avatar
Sergei Morozov committed
122
        [$firstCacheKey] = $this->queryCacheProfile->generateCacheKeys(
123 124 125 126
            $this->query,
            $this->params,
            $this->types,
            $this->connectionParams
127 128
        );

Sergei Morozov's avatar
Sergei Morozov committed
129
        [$secondCacheKey] = $this->queryCacheProfile->generateCacheKeys(
130 131 132 133
            $this->query,
            $this->params,
            $this->types,
            $this->connectionParams
134 135
        );

136
        self::assertEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be the same');
137 138
    }
}