QueryCacheProfileTest.php 3.94 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL\Cache;

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

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

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

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

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

Sergei Morozov's avatar
Sergei Morozov committed
24
    /** @var string[] */
25 26
    private $types = [ParameterType::INTEGER];

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

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

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

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

    public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven()
    {
        $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
56

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

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

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

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

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

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

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

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

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

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

107 108 109 110 111 112 113 114 115 116 117 118
        $params = [];
        parse_str($queryString, $params);

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

        foreach ($this->connectionParams as $param) {
            self::assertNotContains($param, $params['connectionParams']);
        }
    }

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

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

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

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