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

namespace Doctrine\Tests\DBAL\Cache;

use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\Tests\DbalTestCase;
7
use PDO;
8 9 10 11 12 13 14 15 16 17 18 19 20 21

class QueryCacheProfileTest extends DbalTestCase
{
    const LIFETIME = 3600;
    const CACHE_KEY = 'user_specified_cache_key';

    /** @var QueryCacheProfile */
    private $queryCacheProfile;

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

22
    public function testShouldUseTheGivenCacheKeyIfPresent()
23 24
    {
        $query  = 'SELECT * FROM foo WHERE bar = ?';
25 26
        $params = [666];
        $types  = [PDO::PARAM_INT];
27 28

        $connectionParams = array(
29 30
            'dbname'   => 'database_name',
            'user'     => 'database_user',
31
            'password' => 'database_password',
32 33
            'host'     => 'database_host',
            'driver'   => 'database_driver'
34 35
        );

36
        list($cacheKey) = $this->queryCacheProfile->generateCacheKeys(
37 38 39 40 41 42
            $query,
            $params,
            $types,
            $connectionParams
        );

43
        $this->assertEquals(self::CACHE_KEY, $cacheKey, 'The returned cache key should match the given one');
44 45 46 47 48
    }

    public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven()
    {
        $query  = 'SELECT * FROM foo WHERE bar = ?';
49 50
        $params = [666];
        $types  = [PDO::PARAM_INT];
51 52

        $connectionParams = array(
53 54
            'dbname'   => 'database_name',
            'user'     => 'database_user',
55
            'password' => 'database_password',
56 57
            'host'     => 'database_host',
            'driver'   => 'database_driver'
58 59 60
        );

        $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);
61

62
        list($cacheKey) = $this->queryCacheProfile->generateCacheKeys(
63 64 65 66 67
            $query,
            $params,
            $types,
            $connectionParams
        );
68

Antonio Vilar's avatar
Antonio Vilar committed
69 70
        $this->assertNotEquals(
            self::CACHE_KEY,
71
            $cacheKey,
Antonio Vilar's avatar
Antonio Vilar committed
72 73 74
            'The returned cache key should be generated automatically'
        );

75
        $this->assertNotEmpty($cacheKey, 'The generated cache key should not be empty');
76 77
    }

78
    public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections()
79 80
    {
        $query  = 'SELECT * FROM foo WHERE bar = ?';
81 82
        $params = [666];
        $types  = [PDO::PARAM_INT];
83 84

        $connectionParams = array(
85 86
            'dbname'   => 'database_name',
            'user'     => 'database_user',
87
            'password' => 'database_password',
88 89
            'host'     => 'database_host',
            'driver'   => 'database_driver'
90 91
        );

92 93
        $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);

94
        list($firstCacheKey) = $this->queryCacheProfile->generateCacheKeys(
95 96 97 98 99 100 101 102
            $query,
            $params,
            $types,
            $connectionParams
        );

        $connectionParams['host'] = 'a_different_host';

103
        list($secondCacheKey) = $this->queryCacheProfile->generateCacheKeys(
104 105 106 107 108 109 110 111 112 113 114 115
            $query,
            $params,
            $types,
            $connectionParams
        );

        $this->assertNotEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be different');
    }

    public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges()
    {
        $query  = 'SELECT * FROM foo WHERE bar = ?';
116 117
        $params = [666];
        $types  = [PDO::PARAM_INT];
118 119

        $connectionParams = array(
120 121
            'dbname'   => 'database_name',
            'user'     => 'database_user',
122
            'password' => 'database_password',
123 124
            'host'     => 'database_host',
            'driver'   => 'database_driver'
125
        );
126 127 128

        $this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);

129
        list($firstCacheKey) = $this->queryCacheProfile->generateCacheKeys(
130 131 132 133 134 135
            $query,
            $params,
            $types,
            $connectionParams
        );

136
        list($secondCacheKey) = $this->queryCacheProfile->generateCacheKeys(
137 138 139 140 141 142 143
            $query,
            $params,
            $types,
            $connectionParams
        );

        $this->assertEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be the same');
144 145
    }
}