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

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6 7
namespace Doctrine\Tests\DBAL\Cache;

use Doctrine\DBAL\Cache\QueryCacheProfile;
8
use Doctrine\DBAL\ParameterType;
9
use Doctrine\Tests\DbalTestCase;
10
use function parse_str;
11 12 13

class QueryCacheProfileTest extends DbalTestCase
{
14 15
    private const LIFETIME  = 3600;
    private const CACHE_KEY = 'user_specified_cache_key';
16

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

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

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

26
    /** @var int[] */
27 28
    private $types = [ParameterType::INTEGER];

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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